MICROPROCESSORS AND MICROCONTROLLERS Semester VI
Ch 3 . 8051 Programming in C • Data types in 8051 C • programming for time delay • I/O programming in 8051 C, • Logic operations in 8051 C, • Control statements and loops in embedded C, • Functions and Arrays in embedded C, • Data conversion programs in 8051 C, • Accessing code ROM space using 8051 C, • Data serialization using 8051 C.
Why 8051 Programming in C ? • It is easier and less time consuming to write in C than Assembly. • C is easier to modify and update. • You can use code available in function libraries. • C code is portable to other microcontroller with little or no modification.
• The C language is developed for creating system application that direct interacts to the computer hardware devices. • embedded C is used in microprocessor or microcontrollers applications. C Language Embedded C Independent of hardware architecture Dependent on hardware architecture Used for Desktop application, OS and PC Used for limited resources like RAM, ROM and peripheral on embedded controller
Data types in 8051 C • unsigned char ( 1byte , data range 0 to 255) • signed char ( 1 byte , data range -128 to +127 ) • unsigned int ( 2 byte , data range 0 to 65535 ) • signed int ( 2 byte, data range -32768 to +32767 ) • sbit (single bit) ( ex. SBIT MYBIT = P1^0 ) 1 bit size • bit and sf ( ex. SFR P1 = 0x81 )
Unsigned char • As 8051 is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type takes a value in the range of 0 – 255 (00H – FFH) and the most widely used data type for 8051. • The unsigned char data type can be used in situations such as setting a counter value and ASCII characters, instead of signed char. • It is important to specify the keyword unsigned infront of the char else compiler will use the signed char as the default. As 8051 has a limited number of registers and data RAM locations, using the int in place of char can lead to a larger size hex file. This is not a significant issue in high level language.
How to install and run program in 8051 C ? • https://www.youtube.com/watch?v=MG595VN4r70&ab_channel=Ed uvance • SEE ABOVE VIDEO TO INSTALL KEIL SOFTWARE. • https://www.youtube.com/watch?v=fjimCEfIFys&ab_channel=NirajKa pase • See above video to run 8051 C programm using KEIL SOFTWARE.
Write an 8051 C program to send values 00H -0AAH to port P1 #include <reg51.h> // contains all the port registers and internal // RAM of 8051 declared void main ( ) { unsigned char z; // z is allotted a space of 1 byte for (z=0; z<=170;z++) { P1=z; } // values 00H-0AAH sent to port 1 } Note: On executing the program the values 00H to 0AAH will be displayed on port 1
Programing for time delay in 8051 C • There are two ways to create a time delay in 8051 C: 1) Using a simple for loop 2) Using the 8051 timers In either case, when we write a time delay we must use the oscilloscope to measure the duration of our time delay. Next, we use the for loop to create time delays.
• In creating a time delay using a for loop, we must be mindful of three factors that can affect the accuracy of the delay. 1)The 8051 design, the number of machine cycles and the number of clock periods per machine cycle vary among different versions of the 8051/52 microcontroller. 2) The crystal frequency connected to the X1 – X2 input pins. The duration of the clock period for the machine cycle is a function of this crystal frequency 3) Compiler choice. The third factor that affects the time delay is the compiler used to compile the C program.
• In the case of C programs, it is the C compiler that converts the C statements and functions to Assembly language instructions. As a result, different compilers produce different code. • In other words, if we compile a given 8051 C programs with different compilers, each compiler produces different hex code.
Link for Timer programing video • https://www.youtube.com/watch?v=WncD0AnRDls&ab_channel=Elec troTechCC • 16x2 LCD Embedded C program for 8051 with Keil and Proteus simula tion

8051 Programing in C.pptx this is ppt on 8051 programming

  • 1.
  • 2.
    Ch 3 .8051 Programming in C • Data types in 8051 C • programming for time delay • I/O programming in 8051 C, • Logic operations in 8051 C, • Control statements and loops in embedded C, • Functions and Arrays in embedded C, • Data conversion programs in 8051 C, • Accessing code ROM space using 8051 C, • Data serialization using 8051 C.
  • 3.
    Why 8051 Programmingin C ? • It is easier and less time consuming to write in C than Assembly. • C is easier to modify and update. • You can use code available in function libraries. • C code is portable to other microcontroller with little or no modification.
  • 4.
    • The Clanguage is developed for creating system application that direct interacts to the computer hardware devices. • embedded C is used in microprocessor or microcontrollers applications. C Language Embedded C Independent of hardware architecture Dependent on hardware architecture Used for Desktop application, OS and PC Used for limited resources like RAM, ROM and peripheral on embedded controller
  • 5.
    Data types in8051 C • unsigned char ( 1byte , data range 0 to 255) • signed char ( 1 byte , data range -128 to +127 ) • unsigned int ( 2 byte , data range 0 to 65535 ) • signed int ( 2 byte, data range -32768 to +32767 ) • sbit (single bit) ( ex. SBIT MYBIT = P1^0 ) 1 bit size • bit and sf ( ex. SFR P1 = 0x81 )
  • 6.
    Unsigned char • As8051 is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type takes a value in the range of 0 – 255 (00H – FFH) and the most widely used data type for 8051. • The unsigned char data type can be used in situations such as setting a counter value and ASCII characters, instead of signed char. • It is important to specify the keyword unsigned infront of the char else compiler will use the signed char as the default. As 8051 has a limited number of registers and data RAM locations, using the int in place of char can lead to a larger size hex file. This is not a significant issue in high level language.
  • 7.
    How to installand run program in 8051 C ? • https://www.youtube.com/watch?v=MG595VN4r70&ab_channel=Ed uvance • SEE ABOVE VIDEO TO INSTALL KEIL SOFTWARE. • https://www.youtube.com/watch?v=fjimCEfIFys&ab_channel=NirajKa pase • See above video to run 8051 C programm using KEIL SOFTWARE.
  • 8.
    Write an 8051C program to send values 00H -0AAH to port P1 #include <reg51.h> // contains all the port registers and internal // RAM of 8051 declared void main ( ) { unsigned char z; // z is allotted a space of 1 byte for (z=0; z<=170;z++) { P1=z; } // values 00H-0AAH sent to port 1 } Note: On executing the program the values 00H to 0AAH will be displayed on port 1
  • 9.
    Programing for timedelay in 8051 C • There are two ways to create a time delay in 8051 C: 1) Using a simple for loop 2) Using the 8051 timers In either case, when we write a time delay we must use the oscilloscope to measure the duration of our time delay. Next, we use the for loop to create time delays.
  • 10.
    • In creatinga time delay using a for loop, we must be mindful of three factors that can affect the accuracy of the delay. 1)The 8051 design, the number of machine cycles and the number of clock periods per machine cycle vary among different versions of the 8051/52 microcontroller. 2) The crystal frequency connected to the X1 – X2 input pins. The duration of the clock period for the machine cycle is a function of this crystal frequency 3) Compiler choice. The third factor that affects the time delay is the compiler used to compile the C program.
  • 11.
    • In thecase of C programs, it is the C compiler that converts the C statements and functions to Assembly language instructions. As a result, different compilers produce different code. • In other words, if we compile a given 8051 C programs with different compilers, each compiler produces different hex code.
  • 16.
    Link for Timerprograming video • https://www.youtube.com/watch?v=WncD0AnRDls&ab_channel=Elec troTechCC • 16x2 LCD Embedded C program for 8051 with Keil and Proteus simula tion