module 3_programming in C_presenatation
module 3_programming in C_presenatation
We should stick with the unsigned char
unless the data needs to be represented as
signed numbers.
addresses
Set counter values of more than 256
#include <reg51.h>
#include <reg51.h>
P1=0x55;//P1=55H
• reg51. h is necessary.
2.Use the sfr data type and declare by yourself
Sfr P1 = 0x90;
P1=0x55;//P1=55H
• reg51. h is not necessary
Translation between C and Assembly
•A loop in Assembly
MOV R2,#255
ABC:MOV P1,R2
DJNZ R2,ABC
•A for-loop in C
#include <reg51.h>
void main( )
{
unsigned char i;
for(i=0;i<=255;i++)
P1 =i ;
}
2.Write an 8051 C program to toggle all the bits of
P1 only once. 400TIMES
#include <reg51.h>
void main(void)
{
Unsigned int i;
For( i=0;i<=399;i++)
{
p1=0x55;
p1=0xAA;
}}
1-0
0-1
#include <reg51.h>
void main(void)
{
While (1) // for({;;}
{
p1=0x55;
p1=0xAA;
}
}
4. Write an 8051 C program to send values of –4 to
+4 to port P1.
//Singed numbers
#include <reg51.h>
void main(void)
{
char
mynum[]={+1,-
1,+2,-2,+3,-
3,+4,-4};
unsigned char i;
for (i=0;i<=8;i++)
P1=mynum[i
];
5.Write an 8051 C program to send values of –4
to +4 to port P1.
//Singed numbers
#include
<reg51.h> void
main(void)
{ signed char i;
for (i=-4;i<=4;i++)
P1=i;
}
6 .Write an 8051 C program to send hex values for
ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D
to port P1.
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”;
unsigned char i;
for (i=0;i<=10;i++)
P1=mynum[i];
}
Note:
1. Pay careful
7. Write an 8051 C program to toggle bit D0 of the
port P1 50,000 times.
#include <reg51.h>
void main(void)
{
unsigned int i;
for
(i=0;i<=50000;i+
+)
{
P1=0x55;
P1=0xAA;
}
}
Write an 8051 C program to toggle bit D0 of the
port P1 (P1.0) 50,000 times.
Conti…
Time delay in C
There are two ways to create a time delay in 8051 C
Using the 8051 timer
registers in assembly
be mindful of three factors that can affect the accuracy of the delay
The 8051 design
– The number of machine cycle
– The number of clock periods per machine cycle
The crystal frequency connected to the X1 – X2 input pins
Compiler choice
– C compiler converts the C statements and functions to Assembly language
instructions
– Different compilers produce different code
Write an 8051 C program to toggle bits of P1
continuously forever with some delay.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever Void Delay(unsigned int time)
{
{ unsigned int i,j;
p1=0x55; for (i=0;i<time;i++)
MSDelay(250); for (j=0;j<1275;j++);
}
p1=0xAA;
Delay(250);
}
}
Byte Size I/O
This slide is intentionally left
blank
LEDs are connected to bits P1 and P2. Write an 8051 C
program that shows the count from 0 to FFH (0000 0000 to
1111 1111 in binary) on the LEDs.
#include <reg51.h>
#define LED P2;
void main(void)
{
P1=00; //clear P1
LED=0; //clear
P2
while(1)
{
P1++; //increment P1
LED++; //increment
P2
}
}
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
unsigned char mybyte; void MSDelay(unsigned int itime)
P1=0xFF; //make P1 input port {
while (1) unsigned int i,j;
{ for (i=0;i<itime;i++)
mybyte=P1; //get a byte from P1 for (j=0;j<1275;j++);
MSDelay(500); }
P2=mybyte; //send it to P2
}
}
Write an 8051 C program to get a byte of data form P0. If it
is less than 100, send it to P1; otherwise, send it to P2.
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P0=0xFF; //make P0 input port void MSDelay(unsigned int itime)
while (1) {
{ unsigned int i,j;
mybyte=P0; //get a byte from P0 for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
if (mybyte<100)
}
P1=mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}
I/O PROGRAMMING Bit-addressable I/O
Note:
Ports P0 – P3 are bit-addressable and we use sbit data type
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor=P1^1;
sbit Buzzer=P1^7;
void main(void)
void MSDelay(unsigned int itime)
{
{
Dsensor=1;
//make P1.1 an
unsigned int i,j;
input for (i=0;i<itime;i++)
while (1) for (j=0;j<1275;j++);
{ }
while
(Dsensor==1)//w
hile it opens
{
Buzzer=0;
MSDelay(200);
Buzzer=1;
MSDelay(200);
Write an 8051 C program to toggle all the bits of
P0, P1, and P2 continuously with a 250 ms delay.
Use the sfr keyword to declare the port addresses.
sfr P0=0x80;
sfr
P1=0x90;
sfr P2=0xA0;
void MSDelay(unsigned int);
void main(void)
void MSDelay(unsigned int itime)
{ {
while (1) unsigned int i,j;
{ for (i=0;i<itime;i++)
P0=0x55; for (j=0;j<1275;j++);
P1=0x55; }
P2=0x55;
MSDelay
(250);
P0=0xAA;
P1=0xAA;
The data pins of an LCD are connected to P1. The
information is latched into the LCD whenever its Enable
pin goes from high to low. Write an 8051 C program to
send “ECED-SVNIT” to this LCD.
#include <reg51.h>
#define LCDData P1 //LCDData declaration
sbit En=P2^0; //the enable pin
void main(void)
{
unsigned char message[] =“ECED-SVNIT”;
unsigned char z;
for (z=0;z<10;z++) //send 10 characters
{
LCDData=message[z];
En=1; //a high-
En=0; //-to-low pulse
to latch data
}
}
Write an 8051 C program to turn bit P1.5 on and
off 50,000 times.
#include <reg51.h>
sbit MYBIT=0x95;
void main(void)
{
unsigned int z;
for
(z=0;z<50000;z+
+)
{ MYBIT
=1;
MYBIT=0;
}
}
Note
I/O PROGRAMMING: Using bit
Data Type for Bit-addressable
RAM
Write an 8051 C program to get the status of bit P1.0, save it, and send
it to P2.7 continuously.
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit; //use bit to declare bit- addressable memory
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=membit; //send it to P2.7
}
}
We use bit data type to access data in a bit-addressable section of the data
RAM space 20 – 2FH
Operators in C
Logical operators
AND (&&), OR (||), and NOT (!)
Bit-wise operators
AND (&), OR (|), EX-OR (^), Inverter
(~),
Shift Right (>>), and Shift Left (<<)
Write an 8051 C program to toggle all the bits of P0 and
P2 continuously with a 250 ms delay. Using the inverting
and Ex-OR operators, respectively.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=P2^0xFF;
MSDelay(250);
}
}
Write an 8051 C program to get bit P1.0 and send it to
P2.7 after inverting it.
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit;
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=~membit; //invert it and send it to
P2.7
}
}
Write an 8051 C program to read the P1.0
and P1.1 bits and issue an ASCII character to
P0 according to the following table.
P1.1 P1.0
0 0 send ‘0’ to P0
0 1 send ‘1’ to P0
1 0 send ‘2’ to P0
1 1 send ‘3’ to P0
#include <reg51.h>
void main(void)
{
unsignbed char z;
z=P1;
z=z&0x3;
switch
(z)
{
Case
0:
P0=‘0
’;
break;
Case 1: P0=‘1’;
break;
Case 2:
P0=‘2’;
break;
DATA CONVERSION: Packed BCD to ASCII
Conversion
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char mybyte=0x29;
x=mybyte&0x0F;
P1=x|0x30;
y=mybyte&0xF0;
y=y>>4; P2=y|
0x30;
}
DATA CONVERSION: ASCII to Packed BCD
Conversion
#include <reg51.h>
void main(void)
{
unsigned char mydata[
]={0x25,0x62,0x3F,0x52,0xE8};
unsigned char chksum=0;
unsigned char x;
for (x=0;x<5;x++)
chksum=chksum + mydata[x];
if (chksum==0)
P0=‘G’;
else
P0=‘B’;
DATA CONVERSION: Binary (hex) to Decimal and ASCII
Conversion
beyond
Array elements:- addresses right after
variables
Array elements need contiguous RAM
locations and that limits the size of the array
due to the fact that we have only 128 bytes of
RAM for everything
Stack:- addresses right after array elements
ACCESSING CODE ROM
To make the C compiler use the code space instead of the
RAM space, we need to put the keyword code in front of the
variable declaration
Example:
#include <reg51.h>
void main(void)
{
code unsigned char mynum[ ]=“ABCDEF”;
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z];
}
Compare and contrast the following programs and
discuss the advantages and disadvantages of
each one.
(a)
(a) uses the RAM data space to store
array elements, therefore the size of the
array is limited
(b) uses a separate area of the code space for
data. This allows the size of the array to be as
long as you want if you have the on-chip
ROM.
However, the more code space you use for
data, the less space is left for your
program code
DATA SERIALIZATION
Serializing data is a way of sending a byte
of data one bit at a time through a single
pin of microcontroller. There are two ways:
Using the serial port
Transfer data one bit a time and control the
sequence of data and spaces in between
them (to be studied here)
Write a C program to send out the value 44H
serially one bit at a time via P1.0. The LSB should
go out first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCLSB=ACC^0;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=ACCLSB;
ACC=ACC>>1;
}
}
Write a C program to send out the value 44H
serially one bit at a time via P1.0. The MSB should
go out first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
void main(void)
{
unsigned char sendbyte=0x44;
unsigned char x;
ACC = sendbyte;
for (x=0;x<8;x+
+)
{
P1b0=ACCMSB;
ACC=ACC<<1;
}
}
Write a C program to bring in a byte of data
serially one bit at a time via P1.0. The LSB should
come in first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
bit membit;
void main(void)
{
unsigned
char x;
for
(x=0;x<8;x+
+)
{
membit=P1b0;
ACC=ACC>>1;
ACCMSB=me
mbit;
Write a C program to bring in a byte of data
serially one bit at a time via P1.0. The MSB should
come in first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCLSB=ACC^0;
bit membit;
void main(void)
{
unsigned
char x;
for
(x=0;x<8;x+
+)
{
membit=P1b0;
ACC=ACC<<1;
ACCLSB=mem
bit;