LABORATORY MANUAL EMBEDDED C LABORATORY M. Tech I Year I Sem R13 DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGG. BALAJI INSTITUTE OF TECHNOLOGY & SCIENCE Laknepally, Narsampet, Warangal 1
M.TECH. (EMBEDDED SYSTEMS)-R13 Regulations JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD M. Tech I Year I Sem. Embedded Systems EMBEDDED C LABORATORY Note: Minimum of 10 experiments have to be conducted. The following programs have to be tested on 89C51 Development board/equivalent using Embedded C Language on Keil IDE or Equivalent. 1. Program to toggle all the bits of Port P1 continuously with 250 ms delay. 2. Program to toggle only the bit P1.5 continuously with some delay. Use Timer 0, mode 1 to create delay. 3. Program to interface a switch and a buzzer to two different pins of a Port such that the buzzer should sound as long as the switch is pressed. 4. Program to interface LCD data pins to port P1 and display a message on it. 5. Program to interface keypad. Whenever a key is pressed, it should be displayed on LCD. 6. Program to interface seven segment display unit. 7. Program to transmit a message from Microcontroller to PC serially using RS232. 8. Program to receive a message from PC serially using RS232. 9. Program to get analog input from Temperature sensor and display the temperature value on PC Monitor. 10. Program to interface Stepper Motor to rotate the motor in clockwise and anticlockwise directions 11. Program to Sort RTOS on to 89C51 development board. 12. Program to interface Elevator. 2
Experiment no: 1 PROGRAMMING PORTS Program to toggle all the bits of Port P1 continuously with 250 ms delay. 3
#include<reg51.h> void delay(unsigned int msec) //delay function int i,j; for(i=0;i<msec;i++) for(j=0;j<1275;j++); Void main() P1=0x00; While (1) P1=0xff; delay(250); P1=0x00; delay(250); 4
Experiment no: 2 DELAY USING TIMERS Program to toggle only the bit P1.5 continuously with some delay. Use Timer 0, mode 1 to create delay. 5
#include <REGX51.H> void delay(); void main() while(1) P1=0X10; delay(); P1=0x00; void delay() TMOD=0x01; TH1=0Xff; TL1=0Xff; TR1=1; while(tf1==0); TF1=0; TR1=0; 6
Experiment no: 3 SWITCH & BUZZER INTERFACING Program to interface a switch and a buzzer to two different pins of a Port such that the buzzer should sound as long as the switch is pressed. H/w set up: 1. Switch is connected to P1.1 2. Buzzer is connected to P1.2 7
#include <REGX51.H> sbit a=p1^1; sbit a=p1^2; void main() a=b=0; while(1) if(a==1) b=1; else b=0; 8
Experiment no: 4 LCD INTERFACING Program to interface LCD data pins to port P1 and display a message on it. 9
//Program to display String on LCD #include<regx51.h> sfr lcd_data_pin=0x90; // data port P1 sbit rs=p3^0; // Register select pin sbit rw=p3^1; // Read write pin sbit en=p3^6; // Enable pin void delay(unsigned int msec) //delay function int i,j; for(i=0;i<msec;i++) for(j=0;j<1275;j++); void lcd_command(unsigned char comm) // function to send command to LCD lcd_data_pin=comm; en=1; rs=0; rw=0; delay(1); en=0; void lcd_data(unsigned char disp) // function to send data on LCD lcd_data_pin=disp; en=1; rs=1; rw=0; 10
delay(1); en=0; lcd_dataa(unsigned char *disp) // function to send string to LCD int x; for(x=0;disp[x]!=0;x++) lcd_data(disp[x]); void lcd_ini() //Function to inisialize the LCD lcd_command(0x38); delay(5); lcd_command(0x0f); delay(5); lcd_command(0x80); delay(5); void main() lcd_ini(); lcd_dataa("welcome!"); 11
Experiment no: 5 4X3 KEYPAD INTERFACING Program to interface keypad. Whenever a key is pressed, it should be displayed on LCD. 12
// Program to interface controller with controller #include<reg51.h> #define port P1 #define dataport P2 // Dataport for lcd #define key P0 // Port for keypad #define sec 100 sbit rs = port^1; sbit rw = port^2; sbit en = port^3; sbit col1=key^4; sbit col2=key^5; sbit col3=key^6; sbit row1=key^0; sbit row2=key^1; sbit row3=key^2; sbit row4=key^3; void delay(unsigned int msec) //Time delay function int i,j ; for(i=0;i<msec;i++) for(j=0;j<1275;j++); 13
void lcd_cmd(unsigned char item) //Function to send command to LCD dataport = item; rs= 0; rw=0; en=1; delay(1); en=0; return; void lcd_data(unsigned char item) //Funtion to send data on LCD dataport = item; rs= 1; rw=0; en=1; delay(1); en=0; return; void lcd_data_string(unsigned char *str) // Function to send string on LCD 14
int i=0; while(str[i]!='\0') lcd_data(str[i]); i++; delay(10); return; void lcd(unsigned char str[10]) // Funtion to Initialize LCD lcd_cmd(0x38); lcd_cmd(0x0e); //delay(sec); lcd_cmd(0x01); //delay(sec); lcd_cmd(0x82); //delay(sec); lcd_data_string(str); void display(int a) //Display functon for LCD 15
switch(a) case 1:lcd("one 1"); break; case 2:lcd("two 2"); break; case 3:lcd("three 3"); break; case 4:lcd("four 4"); break; case 5:lcd("five 5"); break; case 6:lcd("six 6"); break; case 7:lcd("seven 7"); break; case 8:lcd("EIGHT 8"); break; case 9:lcd("NINE 9"); break; case 0:lcd("ZERO 0"); break; case 11:lcd("*"); break; case 12:lcd("#"); 16
break; void check_col1() //Function for checking column one row1=row2=row3=row4=1; row1=0; if(col1==0) display(1); row1=1; row2=0; if(col1==0) display(4); row2=1; row3=0; if(col1==0) display(7); row3=1; row4=0; if(col1==0) display(11); row4=1; 17
void check_col2() //Function for checking column two row1=row2=row3=row4=1; row1=0; if(col2==0) display(2); row1=1; row2=0; if(col2==0) display(5); row2=1; row3=0; if(col2==0) display(8); row3=1; row4=0; if(col2==0) display(0); row4=1; void check_col3() //Function for checking column three row1=row2=row3=row4=1; row1=0; 18
if(col3==0) display(3); row1=1; row2=0; if(col3==0) display(6); row2=1; row3=0; if(col3==0) display(9); row3=1; row4=0; if(col3==0) display(12); //For # row4=1; void main() col1=col2=col3=1; //Input Port while(1) row1=row2=row3=row4=0; if(col1==0) check_col1(); 19
else if(col2==0) check_col2(); else if(col3==0) check_col3(); 20
Experiment no: 6 7 SEGMENT DISPLAY INTERFACING Program to interface seven segment display unit. 21
// Program to interface single seven segment #include<reg51.h> delay_ms(int time) // Time delay function int i,j; for(i=0;i<time;i++) for(j=0;j<1275;j++); void main() char num[]=0x40,0xf9,0x24,0x30,0x19,0x12,0x02,0xf8,0x00,0x10;// Hex values corresponding to digits 0 to 9 int c; while(1) for(c=0;c<10;c++) P2=num[c]; delay_ms(200); 22
Experiment no: 7 SERIAL COMMUNICATION TRANSMITTER Program to transmit a message from Microcontroller to PC serially using RS232. 23
#include <REGX51.H> void main() SCON=0x40; TMOD=0x20; TH1=0xFD; TR1=1; SBUF='serial communication transmitter'; while(ti==0); TI=0; while(1); 24
Experiment no: 8 SERIAL COMMUNICATION RECEIVER Program to receive a message from PC serially using RS232. 25
#include <REGX51.H> void main() int i; char a[]="serial communication receiver"; SCON=0x40; TMOD=0x20; TH1=0xFD; TR1=1; for(i=0;a[i]!='\0';i++) SBUF=a[i]; while(ti==0); TI=0; while(1); 26
Experiment no: 9 TEMPERATURE SENSOR INTERFACING Program to get analog input from Temperature sensor and display the temperature value on PC Monitor. 27
//Program to check the working of ADC0804 using LED's on its output port. #include<reg51.h> #define input P0 //Input port to read the values of ADc #define output P2 // Output port, connected to LED's. sbit wr= P1^1; // Write pin. It is used to start the conversion. sbit rd= P1^0; // Read pin. It is used to extract the data from internal register to the output pins of ADC. sbit intr= P1^2; // Interrupt pin. This is used to indicate the end of conversion. It goes low when conversion is complete. void delay(unsigned int msec ) // The delay function provides delay in msec. int i,j ; for(i=0;i<msec;i++) for(j=0; j<1275; j++); void adc() // Function to read the values from ADC and display on the LED's. rd=1; wr=0; delay(1); wr=1; 28
while(intr==1); rd=0; output=input; delay(1); intr=1; void main() input=0xff; // Declare port 0 as input port. while(1) adc(); 29
Experiment no: 10 STEPPER MOTOR INTERFACING Program to interface Stepper Motor to rotate the motor in clockwise and anticlockwise directions 30
// Program to interface Stepper Motor with 8051 Microcontroller (AT89C51) /**** Wave Drive Stepping ****/ #include<regx51.h> sfr stepper=0xa0; void delay(unsigned int count) int i; for(i=0;i<count;i++); void main() while(1) stepper=0x01; delay(350); stepper=0x02; delay(350); stepper=0x04; delay(350); stepper=0x08; delay(350); 31
/*****************************/ /**** Half Drive Stepping ****/ #include<reg51.h> sfr stepper=0xa0; void delay(unsigned int count) int i; for(i=0;i<count;i++); void main() while(1) stepper=0x01; delay(300); stepper=0x03; delay(300); stepper=0x02; delay(300); stepper=0x06; delay(300); 32
stepper=0x04; delay(300); stepper=0x0c; delay(300); stepper=0x08; delay(300); stepper=0x09; delay(300); /*****************************/ 33
EXPERIMENT-11 /****************************************************************************/ /* */ /* RTX_Test.C: A RTX-51 Application */ /* */ /****************************************************************************/ #include <rtx51tny.h> // RTX-51 tiny functions & defines #include <reg52.h> #define LCD_DATA P1 to Port(Port1) //8051 Register //Define LCD_DATA Lines #define LED P0 // Define LED for PORT 0 /***********************Define LCD control pins******************************/ sbit RS = P3^5; //Register Select sbit RW = P3^6; //LCD Read/Write sbitlcd_e = P3^7; //LCD Enable code unsigned char msg[] = (" 8051 MCOS RTOS "); code unsigned char msg1[] = (" MULTITASKING "); //Display the message //void lcd_init(void); voidlcd_cmd(unsigned char); voidlcd_display(unsigned char); /***************************************************************************/ /* Task 0 : RTX-51 tiny Initializ task */ /***************************************************************************/ voidinit(void)_task_ 0 os_create_task (1); // start task 1 INIT_UART os_create_task (2); // start task 2 SEND_UART os_create_task (3); // start task 2 LED Blink os_create_task (4); // start task 2 LCD Display while (1) // endless loop os_wait (K_TMO, 5, 0); // wait for timeout: 5 ticks /**************************************************************************/ /* Task 1 : RTX-51 tiny starts initialize serial port with task 0 */ /**************************************************************************/ 34
voiduart (void) _task_ 1 SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload TH1 = 0xFD; // TH1: reload value for 9600 @ 11.0592MHz TR1 = 1; // TR1: timer 1 run TI = 1; // TI: set TI to send first char of UART /**************************************************************************/ /* Task 2 : RTX-51 tiny starts send UART data with task 0 */ /**************************************************************************/ voiduart_send(void) _task_ 2 while (1) os_wait (K_TMO, 10, 0); SBUF = 'A'; /**************************************************************************/ /* Task 3 : RTX-51 tiny starts LED Blink with task 0 */ /**************************************************************************/ void led(void) _task_ 3 while (1) LED = 0x55; os_wait (K_TMO, 30, 0); LED = 0xAA; os_wait (K_TMO, 30, 0); // endless loop /**************************************************************************/ /* Task 4 : RTX-51 tiny starts LCD Initializaion with task 0 */ /**************************************************************************/ voidlcd(void) _task_ 4 while (1) unsigned char i; // endless loop dot lcd_cmd(0x38); os_wait (K_TMO, 2, 0); lcd_cmd(0x0c); os_wait (K_TMO, 2, 0); lcd_cmd(0x06); //Display On, cursor off //Shift Cursor to right //2x16 Character 5x7 35
os_wait (K_TMO, 2, 0); lcd_cmd(0x01); os_wait (K_TMO, 2, 0); //------------------------------------------- // First Line Message Display //------------------------------------------- lcd_cmd(0x80); os_wait (K_TMO, 2, 0); i=0; while(msg[i]!='\0') lcd_display(msg[i]); i++; os_wait (K_TMO, 4, 0); //Clear display screen //First Line Initialization //------------------------------------------- // Second Line Message Display //------------------------------------------- lcd_cmd(0xc0); os_wait (K_TMO, 2, 0); i=0; while(msg1[i]!='\0') lcd_display(msg1[i]); i++; os_wait (K_TMO, 4, 0); //---------------------------------- // LCD command Function //---------------------------------- voidlcd_cmd(unsigned char cmnd) LCD_DATA = cmnd; RS = 0; RW = 0; lcd_e = 1; os_wait (K_TMO, 2, 0); lcd_e = 0; //---------------------------------- // LCD Data Function //---------------------------------- voidlcd_display(unsigned char dat) //Second Line Initialization 36
LCD_DATA = dat; RS = 1; RW = 0; lcd_e = 1; os_wait (K_TMO, 2, 0); lcd_e = 0; 37