Top Banner
15
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 8051 programming in c
Page 2: 8051 programming in c

Outline Types of programming languages

Machine language

Assembly language

High level language

Programming 8051 in C

Creating hex file using Keil Compiler

Burning hex file into microcontroller

ISP

Universal Super-pro programmer

Page 3: 8051 programming in c

Why program the 8051 in C?

It is easier and less time consuming 2 write in C than assembly

C is easier 2 modify & update

C code is portable

Page 4: 8051 programming in c

Toggle all the bits of P1 continuously

#include<reg51.h>

void main (void)

{

for(; ;) // repeat forever

{

P1=0x55; // 0x indicates data is in hex

P1=0xAA;

}

}

Page 5: 8051 programming in c

C data types for the 8051

Since one of the goals of 8051 C programmers is to create smaller hex files, it is worthwhile to re-examine C data types for 8051 C

This will help programmers 2 create smaller hex files

Page 6: 8051 programming in c

Unsigned char

8-bit data type-takes value in the range of 0-255 (00-FFH)

Most widely data types for the 8051

Signed char is default

Page 7: 8051 programming in c

Write an 8051 C program to send values 00-FF to P1

#include<reg51.h>

Void main (void)

{

unsigned char z;

for(z=0;z<=255;z++)

P1=z;

}

Page 8: 8051 programming in c

Signed char

8-bit data type dat uses d MBS-D7 to represent – or + value

Values from -128 to +127

Default is signed char

Page 9: 8051 programming in c

Unsigned int

16-bit data type

Values in the range 0-65535 (0000-FFFF H)

Used to set counter values of >256

Takes 2 bytes of memory

Page 10: 8051 programming in c

Sbit data type

Used for a single bit

Format:

sbit name= bit

For ex:

sbit LED =P1^0

Page 11: 8051 programming in c

Write an 8051 C program to toggle bit D0 of port P1 50,000 times

#include<reg51.h>sbit MYBIT=P1^0; //sbit is declared out of main

//programVoid main (void)

{unsigned int z;for(z=0;z<50000;z++){

MYBIT=o;MYBIT=1;

}}

Page 12: 8051 programming in c

Time delay using C

There are two ways to create a time delay in 8051 C:

1. Using a simple for loop

2. Using the 8051 timers

Page 13: 8051 programming in c

Three factors that can affect the accuracy of delay

Compiler choice

Crystal frequency

The 8051 design

Original 8051 design used 12 clock periods per m/c cycle

DS5000- 4 clock periods per m/c cycle

DS89C420- 1 clock periods per m/c cycle

Page 14: 8051 programming in c

Toggle all the bits of P1 continuously with some delay

#include<reg51.h>void main (void){

unsigned int x;for(; ;) // repeat forever{

P1=0x55; // 0x indicates data is in hexfor (x=0;x<40,000;x++); //delay size unknownP1=0xAA;for (x=0;x<40,000;x++);

}}

Page 15: 8051 programming in c

Get a byte of data from P0. If it is less than 100, send it to P1; otherwise send it to P2

#include<reg51.h>void main (void){

unsigned int mybyte;P0=0xFF;for(; ;) // repeat forever{

mybyte = P0; // 0x indicates data is in hexif(mybyte<100)

P1=mybyte; //send it to P1 if less than 100else

P2=mybyte; //send it to P2 if more than 100}

}