Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views8 pages

Embedded C Lab Merged

Uploaded by

Baasith Basheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Embedded C Lab Merged

Uploaded by

Baasith Basheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab – 5 void main(void)

EMBEDDED C BASICS {
(Blinking of LED) while(1) //Infinite LOOP
{
Basics: LED = 0; // LED ON
 Embedded System = Hardware + Software Delay();
 8 Bit “CISC” Architecture LED = 1; // LED OFF
 CISC(Complex Instruction Set Computer) Architecture Delay();
}
Complete task by using less number of assembly }
lines.
It consists of,
 Memories void Delay(void) {
 Serial communcation int i, j;
 Interrupts for (i = 0; i < 10; i++) {
 Input/output ports for (j = 0; j < 10000; j++) {
 Timers }
}
Board Number : AT89C51eD2 }
Clock freq : 11.0592 Mhz
Baud Rate: 9600 EMBEDDED C BASICS

Embedded C Additional Keywords.


 Sbit QUESTION 1:
 Bit (Size - 1 Bit)
 SFR (Size - 1 Byte) Write an 8051 C program to display 00 to FF using Port1
 Volatile
 Macros define CODE:
#include<reg52.h>
void delay_ms(unsigned int j);
Sbit (size -1 bit):
It is used for decalring the single PIN of the microcontroller void main(void){
in the SFR Register. unsigned char z;
Syntax to use the SBit, P1=0x00;
sbit Any_Name = Px^y; for(z=0;z<255;z++){
P1=z;
Example : delay_ms(1000);
sbit a=P0^1; // declares the respective pin with a variable }
}
CODE :
void delay_ms(unsigned int j){
#include<reg52.h> unsigned int i;
//21BEC1457 for(;j>0;j--)
{
sbit LED = P1^0; for(i=250;i>0;i--){
}
void Delay(void); //Function prototype declaration
for(i=250;i>0;i--){ void delay_ms(unsigned int j);
} void main(void)
} {
} unsigned int z;
for (z=0;z<50000;z++)
QUESTION 2: {
P1 = 0xAA;
Write a 8051 program to toggle PORT1 delay_ms(1000);
P1 = 0x55;
delay_ms(1000);
CODE: }
#include<reg52.h> }
void delay_ms(unsigned int j); void delay_ms(unsigned int j)
{
void main(void){ unsigned int i;
unsigned char z; for(;j>0;j--)
P1=0x00; {
z=11; for(i=250;i>0;i--)
while(z>0){ {
P1=0xAA; }
delay_ms(500); for(i=250;i>0;i--)
P1=0x55; {
delay_ms(500); }
} }
} }

void delay_ms(unsigned int j){ QUESTION 4:


unsigned int i;
for(;j>0;j--) Write a 8051 program to toggle one bit of PORT1 50000 times
{
for(i=250;i>0;i--){
} CODE:
for(i=250;i>0;i--){
} #include<reg51.h>
} void delay_ms(unsigned int j);
} sbit toggle = P1^0;
void main(void){
unsigned int z;
QUESTION 3: for (z=0; z<=50000; z++){
toggle = 0;
Write a 8051 program to toggle PORT1 50000 times delay_ms(1000);
toggle = 1;
CODE: delay_ms(1000);
}
//pgm to toggle all bits 50000 times }
#include <reg51.h> void delay_ms(unsigned int j){
unsigned char i; (Ex: Air Bags in Cars, Here Timers acts as an Counter to Initialize
for(;j>0;j--) the interrupt for 18ms in view of accident.)
{
for(i=250;i>0;i--)
{ MODE 1:
}  16 Bit timer
for(i=250;i>0;i--)  Coomon mode
{
 TLx is incremented form 0 to 255
}
}  Then resets to 0 and Causing THx to be incremented to 1
}
MODE 2:
EMBEDDED C PROGRAMMING
8051 – TIMERS/COUNTERS  8 bit Auto Reload Mode
 Tlx is the Timer itself
What is a Timer ?  Thus, TLx starts counting up
 Timers are being set in Microcontrollers to 
initialize a interrupt in the scenario/Code.
TCON: (Bit Addressable)
 Specific Registers are used for Timer
 It is a 16 Bit Register
 TMOD (Timer Mode Register)
(Gate =0 is being set for software control, Gate=1 for
Hardware Control)

4 Modes : (M1 and M0)


00 – Mode 0
INITIALIZING A TIMER :
01 – Mode 1 – 16 Bit
 Initialize TMOD SFR
10 -- Mode 2 – 8 bit
11 -- Mode 3 – 8 Bit  GATE0 and C/T0 are both 0 since we want the Timer to be
 TCON – (Timer Control Register) independent of the external pins.
 THx(Timer High Register)  TMOD = 0x01;
 TLx(Timer Low Register  TR0 = 1; (TR0 bit can be set by)
In order to generate a delay of 1ms, the calculations using above #include <reg51.h>
steps are as follows: void T0M1Delay();
N = 1ms/1.085ps = 922. sbit myBit = P1^5;
M = 65536 – 922 = 64614 (decimal). void main(){
64614 in Hexadeciaml = FC66h. while(1){
Load TH with 0xFC and TL with 0x66. myBit =1;
T0M1Delay();
11.0592  Crystal Frequency ; 12  No. of cycles myBit=0;
= 11.0592/12 = 921.5Khz T0M1Delay();
Timer Period = (1/f)  1/921.5  1.085 Micro Seconds
}
}
void T0M1Delay(){
TMOD = 0x01;
TH0 = 0x4B;
TL0 = 0xFD;
1) Write a 8051 C Program to toggle Port P1 Continously with some TR0 = 1;
delay. Use Timer 0 (16 Bit Mode) for Delay. while(TF0 == 1);
TF0 = 0;
Code: TR0 = 0;
//21BEC1457 }
#include <reg51.h>
void timer_delay(); 3) Write a 8051 C Program to toggle ort P2 Continously with 500ms
void main(){ delay. Use Timer 1 (16 Bit Mode) for Delay.
P1=0xAA; Code:
timer_delay(); #include<reg51.h>
P1=0x55; //21BEC1457
timer_delay(); void TMR_delay(){
} TMOD=0x10;
void timer_delay(){ TH1=0x00;
TMOD = 0x01; TL1=0x00;
TH0 = 0xFE; TR1=1;
TL0 = 0xBE; while(TF1==1);
TR0 = 1; TF1=0;
while(TF0 == 1); TR1=0;
TF0 = 0; }
TR0 = 0; unsigned char i;
} void main(){
while(1){
2) Write a 8051 C program to toggle port only bit P1.5 Continously P2=0xAA;
every 50ms. for(i=0;i<7;i++){
N = 50ms/1.085us = 460082. TMR_delay();
M = 65536 – 460082= 19454 (decimal). }
19453 in Hexadeciaml = 4BFD. P2=0X55;
Load TH with 0x4B and TL with 0xFD. for(i=0;i<7;i++){
Code: TMR_delay();
//21BEC1457
} T1 = 1;
} TMOD = 0x60;
} TH1 = 0;
4) while(1){
Write an 8051 C program to create a frequency of 2500 Hz on pin do{
P2.7. Use Timer 1, mode 2 to create the delay. TR1 = 1;
CODE: P1 = TL1;
#include<reg51.h> }
void TMR_Delay(void); while (TF1==0);
sbit mybit = P2^7; TR1 = 0;
void main(void){ TF1=0;
while(1){ }
mybit = ~mybit; }
TMR_Delay(); 2) Assume that a 1 Hz external clock is being fed into pin
} T0(P3.4). Write a C program for Counter 0 in mode 1 to count up and
} display the state of the TL0 and TH0 registers on P2 and P1
void TMR_Delay(void){ respectively.
TMOD = 0x20;
TH1 = -184; Code:
TR1 = 1; #include<reg51.h>
while(TF1 == 0); //21BEC1457
TR1 = 0; void main(){
TF1 = 0; T0=1;
} TMOD=0x05;
TL0=0;
PROBLEM STATEMENTS: TH0=0;
A. Assume that a 1 Hz external clock is being fed into pin T1(P3.5). while(1){
Write a C program for Counter 1 in mode 2 to count and display the do{
state of the TL1 count on P1. Start the count at 0H. TR0=1;
B. Assume that a 1 Hz external clock is being fed into pin T0(P3.4). P2=TL0;
Write a C program for Counter 0 in mode 1 to count up and display P1=TH0;
the state of the TL0 and TH0 registers on P2 and P1 respectively.
}
C. Write an 8051 C program to create a delay of 250ms in P1. Find the
while(TF0==0);
delay values.
TR0=0;
TF0=0;
}
1) Assume that a 1 Hz external clock is being fed into pin }
T1(P3.5). Write a C program for Counter 1 in mode 2 to count 3) Write an 8051 C program to create a delay of 250ms in P1. Find
and display the state of the TL1 count on P1. Start the count the delay values.
Code:
at 0H.
#include<reg51.h>
Code: //21BEC1457
//21BEC1457 void delay(unsigned int val);
#include <reg51.h> void main(){
void main(){ while(1){
P1 = 0x00;
delay(250); COUNTER : TMOD REGISTER
P1 = 0xFF;
delay(250);
}
}
void delay(unsigned int val){
unsigned int i,j;
for(i = 0;i<val;i++){
for(j=0;j<500;j++){
}
}
}

SCON (Serial Control Register):

SBUF (Serial Buffer Register):


 SBUF Register : For a byte of data to be transferred via the
TxD line, it must be placed in the SBUF.
 SBUF holds the byte of data when it is received by the MCS51’s
SM0: Serial port Mode specifier 1-bit RxD line.
SM1: Serial port Mode specifier 2-bit

Baud comparision for SMOD =0 and SMOD =1:


[NOTE: SM2,TB8,RB8 = 0]
If : TI = 1  Ready to tnx second 8 bit data
RI = 1  It will remove the start and stop bits.
REN = 1  Then only we can use serial Communication.
So we have to Make the Value as 50
SCON=0x50;
TR1=1;
while (1) {
SBUF='A'; //Place value in buffer
while (TI==0);
TI=0;
}
}

PCON (Power Control Register):


 Is an 8-bit SFR OUTPUT :
 Byte Addressable Register
Now setting TR1 to 0 To avoid Printing “A” Continously [ The Timer
is left ON on previous code]

2) Write an 8051 C Program to transfer the message “YES” serially


AT 9600 Baud, 8-bit data, 1 stop bit. Do this continuously.

CODE:
#include <reg51.h>
 SMOD : Double Baud rate Bit //21BEC457
void tnx(unsigned char);
 GF1: Genral Purpose Flag bit
void main(void){
 GF0: Genral Purpose Flag bit TMOD=0x20; //use Timer 1, mode 2
 PD: Power down bit TH1=0xFD; //9600 baud rate
 IDL: Idle Mode Bit SCON=0x50;
TR1=1; //start timer
while (1) {
INITIAILIZE THE UART (configurations): tnx('Y');
 Configure Serial port Node 1 or 3 tnx('E');
 Configure Timer 1 to Time mode 2 ( 8 bit auto reload) tnx('S');
 Set TH1 to xxH to reflect the correct frequency for desired }
}
baud.
 Set PCON.7 (SMOD) to double the baud Rate void tnx(unsigned char x){
SBUF=x; //place value in buffer
while (TI==0); //wait until transmitted
TASK STATEMENTS:
TI=0;
1) Write a C Program for 8051 to transfer the letter “A” serially
}
at 4800 baud continuously.

CODE:
OUTPUTS:
#include <reg51.h>
//21BEC1457
Now setting TR1 to 0 To avoid Printing “A” Continously [ The Timer
void main(void){
is left ON on previous code]
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
3) Write an 8051 C Program to receive byte of data serially and else {
put them in P1. Set the Baud Rate AT 4800 Baud, 8-bit data, 1 PCON=PCON|0x80;
stop bit. for (x=0;x<10;x++) {
SBUF=m2[x];
CODE: while(TI==0);
#include <reg51.h> TI=0;
void main(void){ }
unsigned char mybyte; }
TMOD=0x20; //use Timer 1, mode 2 }
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1; 5) Write an 8051 program to transmit a string “test” with a baud
while (1) { rate 9600.
while (RI==0);
mybyte=SBUF; CODE:
P1=mybyte; #include <reg51.h>
RI=0; //21BEC1457
} void UART_Init()
} {
4) Write an 8051 C Program to send the two messages “Normal Speed” TMOD = 0x20;
and “High Speed” to the serial port. Assuming that SW is connected TH1 = 0xFD;
to pin P2.0, monitor its status and set the baud rate as follows: SCON = 0x50;
TR1 = 1; }
SW = 0, 28,800 baud rate void Transmit_data(char tx_data)
SW = 1, 56K baud rate {
Assume that XTAL = 11.0592 MHz for both cases. SBUF = tx_data;
while (TI==0);
CODE: TI = 0;
#include <reg51.h> }
//21BEC1457 void String(char *str)
sbit mp=P2^0; {
void main(void){ int i;
unsigned char x; for(i=0;str[i]!=0;i++)
unsigned char m1[]="NORMAL Speed"; {
unsigned char m2[]="HIGH Speed"; Transmit_data(str[i]);
TMOD=0x20; }
TH1=0xFF; }
SCON=0x50; void main()
TR1=1; {
if(mp==0) { UART_Init();
for (x=0;x<12;x++) { String("test");
SBUF=m1[x]; while(1);
while(TI==0); }
TI=0;
}
}

You might also like