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
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
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;
}
}
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
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
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;
}
Signed char
 8-bit data type dat uses d MBS-D7 to represent – or +
value
 Values from -128 to +127
 Default is signed char
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
Sbit data type
Used for a single bit
Format:
sbit name= bit
For ex:
sbit LED =P1^0
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
//program
Void main (void)
{
unsigned int z;
for(z=0;z<50000;z++)
{
MYBIT=o;
MYBIT=1;
}
}
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
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
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 hex
for (x=0;x<40,000;x++); //delay size unknown
P1=0xAA;
for (x=0;x<40,000;x++);
}
}
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 hex
if(mybyte<100)
P1=mybyte; //send it to P1 if less than 100
else
P2=mybyte; //send it to P2 if more than 100
}
}

8051 programming in c

  • 2.
    Outline  Types ofprogramming 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
  • 3.
    Why program the8051 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
  • 4.
    Toggle all thebits of P1 continuously #include<reg51.h> void main (void) { for(; ;) // repeat forever { P1=0x55; // 0x indicates data is in hex P1=0xAA; } }
  • 5.
    C data typesfor 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
  • 6.
    Unsigned char  8-bitdata type-takes value in the range of 0-255 (00- FFH)  Most widely data types for the 8051  Signed char is default
  • 7.
    Write an 8051C 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; }
  • 8.
    Signed char  8-bitdata type dat uses d MBS-D7 to represent – or + value  Values from -128 to +127  Default is signed char
  • 9.
    Unsigned int  16-bitdata type  Values in the range 0-65535 (0000-FFFF H)  Used to set counter values of >256  Takes 2 bytes of memory
  • 10.
    Sbit data type Usedfor a single bit Format: sbit name= bit For ex: sbit LED =P1^0
  • 11.
    Write an 8051C program to toggle bit D0 of port P1 50,000 times #include<reg51.h> sbit MYBIT=P1^0; //sbit is declared out of main //program Void main (void) { unsigned int z; for(z=0;z<50000;z++) { MYBIT=o; MYBIT=1; } }
  • 12.
    Time delay usingC  There are two ways to create a time delay in 8051 C: 1. Using a simple for loop 2. Using the 8051 timers
  • 13.
    Three factors thatcan 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
  • 14.
    Toggle all thebits 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 hex for (x=0;x<40,000;x++); //delay size unknown P1=0xAA; for (x=0;x<40,000;x++); } }
  • 15.
    Get a byteof 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 hex if(mybyte<100) P1=mybyte; //send it to P1 if less than 100 else P2=mybyte; //send it to P2 if more than 100 } }