Embedded C Programming Tutorial with Keil Language Embedded C is the most popular programming language in the software field for developing electronic gadgets. Each processor is associated with embedded software. Embedded C Programming plays a major role in performing specific functions by the processor. In our day-to-day life, we frequently use many electronic devices such as washing machines, mobile phones, digital camera and so on will work based on microcontrollers that are programmed by embedded C. Embedded System Programming The C code written is more reliable, portable, and scalable; and in fact, much easier to understand. The first and foremost tool is the embedded software that decides the operation of an embedded system. Embedded C programming language is most frequently used for programming the microcontrollers. Embedded C Programming Tutorial (8051) For writing the program the embedded designers must have sufficient knowledge on the hardware of particular processors or controllers as the embedded C programming is a full hardware related programming technique. Programming Tutorial Earlier, many embedded applications were developed by using assembly level programming. However, they did not provide portability to overcome this problem with the advent of various high-level languages like C, COBOL, and Pascal. However, it was the C language that got extensive acceptance for embedded systems application development, and it continues to do so. Embedded System The embedded system is defined as the combination of embedded C programming software and hardware part majorly consist of microcontrollers and it is intended to perform the specific task. These types of embedded systems are being used in our daily life such as washing machines and video recorders, refrigerators and so on. The embedded system was first introduced by the 8051 microcontrollers. Embedded System Introduction to the 8051 Microcontroller The 8051 microcontroller is a basic microcontroller, it is first introduced by the ‘Intel Corporation’ since 1970. It is developed by the 8086 processor architecture. The 8051 is a family of the microcontroller, which has been developed by different manufacturers such as Philips, Atmel, dalls, and so on. The 8051 microcontrollers has been used in lots of embedded products from small children’s toys to large automotive systems. 8051 Microcontroller The 8051 microcontroller is the 8-bit ‘CISC’ architecture. It consists of memories, serial communication, interrupts, input/output ports and timer/counters, built into a single integrated chip, which is programmed to control the peripheral devices which are interfaced with it. The program is stored in the RAM of the microcontroller but before writing the program, we must aware of the RAM organization of the microcontroller. Embedded System Programming: Basics Declaration Every function is a collection of statements that perform a specific task and the collection of one or more functions is called a programming language. Every language consists of some basic elements and grammatical rules. The C language programming is designed to function with the character set, variables, data types, constants, keywords, expressions and so on are used to write a C program. All these considered under header file or library file and it is represented as #include<studio.h> Embedded C Programming Development The extension of the C language is called an Embedded C programming language. As compared with above, the embedded programming in C language has some additional features such as data types and keywords and header file or library file is represented as #include<microcontroller name. h> Embedded C Additional Keywords sbit bit SFR volatile macros define The “sbit” is used for declaring the single PIN of the microcontroller. For example, LED is connected to the P0.1 pin, it is not recommended to send the value to the port pin directly, first, we have to declare the pin with another variable then after we can use anywhere in the program. Syntax: sbit a=P0^1; //declares the respective pin with a variable// a=0x01; //send the value to the port pin// The “bit” is used for checking the status of the variable. Syntax: bit c; //declares the bit variable// c=a; //a value is assigned to the c variable // if(c==1) //check the condition true or false// { ….. …… } The “SFR” keyword is used to access the SFR registers by another name. The SFR register defined as a special function register, it contains all peripherally related registers by indicating the address. The SFR register is declared by the SFR keyword. The SFR keyword must be in capital letters. Syntax: SFR port=0x00; //0x00 is a port0 address it is declared by port variable// Port=0x01; //then send the value to the port0// delay(); port=0x00; delay(); The “volatile” keyword is the most important in embedded system development. The variable that declares with the volatile keyword value could not be changed unexpectedly. It can be used in memory-mapped peripheral registers, global variables modified by the ISRs. Without using the volatile keyword for transmitting and receiving the data, code error or an optimization error will take place. Syntax: volatile int k; The macro is a name it is used to declare the block of statements as a pre-processor directive. Whenever the name is used, it is replaced by the contents of the macro. The macros represent the #define. The whole port pins are defined by the macros. Syntax: #define dat Po; //the whole port is declared by a variable// dat=0x01; //data send to the port0// Basic Embedded C Programs The microcontroller programming will differ for each type of operating system. Even though there are many operating systems are existing such as Linux, Windows, RTOS and so on. However, RTOS has several advantages for embedded system development. This article discusses basic embedded C programming to develop embedded C programming using an 8051 microcontroller. Embedded C Programming Steps LED blinking using with 8051 microcontroller Number Displaying on 7-segment display using 8051 microcontroller Timer/Counter calculations and program using 8051 microcontroller Serial Communication calculations and program using 8051 microcontroller Interrupt Programs using 8051 microcontroller Keypad Programming using 8051 microcontroller LCD Programming with 8051 microcontroller LED Blinking using 8051 Microcontroller The LED is a semiconductor device which is used in many applications, mostly for indication purpose. It is finding a huge range of applications as indicators during the test to check the validity of results at different stages. They are very cheap and easily available in a variety of shapes, colors, and sizes. The LEDs are used to design message display boards and traffic control signal lights etc. Here the LEDs are interfaced with the PORT0 of the 8051 microcontrollers. LED Blinking using 8051 Microcontroller 1. 01010101 10101010 #include<reg51.h> //header file// void main() //the program execution stat point// { unsigned int i; //data type// while(1) //for continuous loop// { P0=0x55; //send the hexa value to the port0// for(i=0;i<30000;i++) //normal delay// P0=0x3AA; //send the hexa value to the port0// for(i=0;i<30000;i++) //normal delay// } } 2. 00000001 00000010 00000100 . . 10000000 #include<reg51.h> void main() { unsignedint i; unsigned char j,b; while(1) { P0=0x01; b=P0; for(j-0;j<3000;j++); for(j=0;j<8;j++) { b=b<<1; P0=b; for(j-0;j<3000;j++); } } } 3. 00001111 11110000 #include<reg51.h> void main() { unsignedint i; while(1) { P0=0x0F; for(j-0;j<3000;j++); P0=0xF0; for(j-0;j<3000;j++); } } 4. 00000001 00000011 00000111 . . 11111111 #include<reg51.h> void main() { unsignedint i; unsigned char j,b; while(1) { P0=0x01; b=P0; for(j-0;j<3000;j++); for(j=0;j<8;j++) { b=b<<1; b=b|0x01; P0=b; for(j-0;j<3000;j++); } } } Displaying Numbers on 7-Segment Display using 8051 Microcontroller The 7-segment displays is the basic electronic displays, which are used in many systems to display the numeric information. It consists of eight LEDs which are connected in sequential manner so as to display digits from 0 to 9, when proper combinations of LEDs are switched on. They can display only one digit at a time. Displaying Numbers on 7-Segment Display using 8051 Microcontroller 1. WAP to display the numbers form ‘0 to F’ on four 7segment displays ? #include<reg51.h> sbit a= P3^0; sbit b= P3^1; sbit c= P3^2; sbit d= P3^3; void main() { unsignedchar n[10]={0×40,0xF9,0×24,0×30,0×19,0×12,0×02,0xF8,0xE00,0×10}; unsigned inti,j; a=b=c=d=1; while(1) { for(i=0;i<10;i++) { P2=n[i]; for(j=0;j<60000;j++); } } } 2. WAP to display the numbers from ’00 to 10’ on 7segment displays ? #include<reg51.h> sbit a= P3^0; sbit b= P3^1; void display1(); void display2(); void delay(); void main() { unsignedchar n[10]={0×40,0xF9,0×24,0×30,0×19,0×12,0×02,0xF8,0xE00,0×10}; unsigned inti,j; ds1=ds2=0; while(1) { for(i=0,i<20;i++) display1(); display2(); } } void display1() { a=1; b=0; P2=s[ds1]; delay(); a=1; b=0; P2=s[ds1]; delay(); } void display2() { ds1++; if(ds1>=10) { ds1=0; ds2++; if(ds2>=10) { ds1=ds2=0; } } } void delay() { unsignedint k; for(k=0;k<30000;k++); } Timer/Counter Calculations and Program using 8051 Microcontroller The delay is the one of the important factors in the application software development. However, the normal delay will not give the precious result to overcome this problem for implementing the timer delay. The timers and counters are hardware components of the microcontroller, which is used in many applications to provide the precious time delay with count pulses.The both tasks are implemented by the software technique. Timer Delay WAP to generate the 500us time delay using T1M2(timer1 and mode2) ? #include<reg51.h> void main() { unsigned char i; TMOD=0x20; //set the timer mode// for(i=0i<2;i++) //double the time daly// { TL1=0x19; //set the time delay// TH1=0x00; TR1=1; //timer oN// While(TF1==0); //check the flag bit// TF1=0; } TR1=0; //timer off// } Normal Loop Delay void delay() { unsignedint k; for(k=0;k<30000;k++); } Serial Communication Calculations and Program using 8051 Microcontroller Serial communication is commonly used for transmitting and receiving the signal. The 8051 microcontroller have consist UART serial communication the signals transmitted and received by the Rx and Tx pins. The UART takes bytes of data and sends the individual bits in a sequential manner. The registers are a way to collect and store the data in the memory. UART is a half-duplex protocol. Half-duplex means transferring and receiving the data, but not at the same time. Serial Communication Calculations and Program using 8051 Microcontroller 1. WAP to transmit the character ‘S’ to the serial window use 9600 as the baud rate? 28800 is the maximum baud rate of the 8051 microcontroller 28800/9600= 3 That baud rate ‘3’ is stored in the timers #include<reg51.h> void main() { SCON=0x50; //start the serial communication// TNOD=0x20; //selected the timer mode// TH1=3; // load the baud rate// TR1=1; //Timer ON// SBUF=’S’; //store the character in the register// while(TI==0); //check the interrupt register// TI=0; TR1=0; //OFF the timer// while(1); //continuous loop// } 2. WAP to receive the data from the hyperterminal and send that data to the PORT 0 of the Microcontroller using 9600 baud? 28800 is the maximum baud rate of the 8051 microcontroller 28800/9600= 3 That baud rate ‘3’ is stored in the timers #include<reg51.h> void main() { SCON=0x50; //start the serial communication// TMOD=0x20; //selected the timer mode// TH1=3; // load the baud rate// TR1=1; //Timer ON// PORT0=SBUF; //send the data from SBUF to port0// while(RI==0); //check the interrupt register// RI=0; TR1=0; //OFF the timer// while(1); //stop the program when character is received// } Interrupt Programs using 8051 Microcontroller The interrupt is a signal that forcing to stop the current program and execute the other program immediately. The 8051 microcontroller provide 6 interrupt, which are internal and external interrupt sources. When the interrupt occurs the microcontroller pause the current task and attend to the interrupt by executing the ISR then microcontroller returns back to the recent task. WAP to perform left shift operation when timer 0 interrupts occurs then perform the interrupt operation for the P0 in the main function? #include<reg51.h> unsigned char b; void timer0() interrupt 2 //selected timer0 interrupt// { b=0x10; P1=b<<2; } void main() { unsigned char a,i; IE=0x82 //enable the timer0 interrupt// TMOD=0x01; TLo=0xFC; //interrupt timer// TH1=0xFB; TR0=1; a=0x00; while(1) { for(i=0;i<255;i++) { a++; Po=a; } } } Keypad Programming using 8051 Microcontroller The matrix keypad is an analog switching device, which is used in many embedded applications to allow the user to perform the necessary tasks. A matrix keypad consists of an arrangement of switches in matrix format in rows and columns. The rows and columns are connected to the microcontroller such that the row of switches are connected to one pin and switches in each column are connected to another pin, then perform the operations. Keypad Programming using 8051 Microcontroller 1. WAP to toggle the LED by pressing the switch #include<reg51.h> sbit a=P3^0; sbit b=P3^1; sbit c=P3^2; sbit d=P3^3; void delay(); void main() { while(1) { a=0; b=1; c=1; d=1; delay(); a=1; b=0; c=1; d=1; void delay() { unsigned char i; TMOD=0x20; //set the timer mode// for(i=0i<2;i++) //double the time daly// { TL1=0x19; //set the time delay// TH1=0x00; TR1=1; //timer oN// While(TF1==0); //check the flag bit// TF1=0; } TR1=0; //timer off// } 2. WAP to Switch ON the LED by pressing the key ‘1’ on the keypad? #include<reg51.h> sbit r1=P2^0; sbit c1=P3^0; sbit LED=P0^1; void main() { r1=0; if(c1==0) { LED=0xff; } } 3. WAP to display the number 0,1,2,3,4,5 on the seven segment by pressing the respective key on the keypad? #include<reg51.h> sbit r1=P2^0; sbit c1=P3^0; sbit r2=P2^0; sbit c2=P3^0; sbit a=P0^1; void main() { r1=0; a=1; if(c1==0) { a=0xFC; } If(c2==0) { a=0x60; } if(c3==0) { a=0xDA; } If(c4==0) { a=0xF2; } } LCD Programming with 8051 Microcontroller The LCD display is an electronic device, which is frequently used in many applications for displaying the information in a text or image format. The LCD is a display that can easily show characters on its screen. The LCD display have consists 8-data lines and 3-control lines which are used to interface to the microcontroller. LCD Programming with 8051 Microcontroller WAP to display the “EDGEFX KITS” on LED display ? #include<reg51.h> #define kam P0 voidlcd_initi(); voidlcd_dat(unsigned char ); voidlcd_cmd(unsigned char ); void delay(); void display(unsigned char *s, unsigned char r) sbitrs=P2^0; sbitrw=P2^1; sbit en=P2^2; void main() { lcd_initi(); lcd_cmd(0x80); delay(100); lcd_cmd(0xc0); display(“edgefx kits”,11); while(1); } void display(unsigned char *s, unsigned char r) { unsignedint w; for(w=0;w<r;w++) { lcd_data(s[w]); } } voidlcd_initi() { lcd_cmd(0×01); delay(100); lcd_cmd(0×38); delay(100); lcd_cmd(0×06); delay(100); lcd_cmd(0x0c); delay(100); } voidlcd_dat(unsigned char dat) { kam = dat; rs=1; rw=0; en=1; delay(100); en=0; } } voidlcd_cmd(unsigned char cmd) { kam=cmd; rs=0; rw=0; en=1; delay(100); en=0; } void delay( unsigned int n) { unsignedint a; for(a=0;a<n;a++); } Hope this article gives a basic information about embedded system programming using 8051 microcontroller with a few example programs. For detailed embedded C programming tutorial please post your comments and queries in the comment section below. Share This Post: Facebook Twitter Google+ LinkedIn Pinterest Post navigation ‹ Previous Application of Power Electronics in Automotive ApplicationsNext › Microcontroller based Electrical Projects Related Content Kogge Stone Adder : Circuit, Working, Advantages, Disadvantages & Its Applications Brent Kung Adder : Circuit, Working, Advantages, Disadvantages & Its Applications Inverting Summing Amplifier : Circuit, Working, Derivation, Transfer Function & Its Applications Active Band Pass Filter : Circuit, Types, Frequency Response, Q Factor, Advantages & Its Applications Comments are closed.