serial dim atmel code
/*
* Project name:
DIMMER
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include <reg51.H>
sbit S1=P2^0;
sbit S2=P2^1;
sbit S3=P2^2;
sbit S4=P2^3;
sbit S5=P2^4;
sbit S6=P2^5;
void CONVERT_DISPLAY(unsigned int);
void transmit_byte(unsigned char );
void msdelay(int);
void main(){
unsigned char LOAD1=0,LOAD2=0,LOAD3=0;
TMOD=0X20;
TH1=0XFD; // serail intiz
SCON=0X50;
TR1=1;
P2=0XFF;
while(1)
{
if(S1==0)
{
if(LOAD1<100)
LOAD1++;
transmit_byte('A');
CONVERT_DISPLAY(LOAD1);
msdelay(500);
}
if(S2==0)
{
if(LOAD1>0)
LOAD1--;
transmit_byte('A');
CONVERT_DISPLAY(LOAD1);
msdelay(500);
}
if(S3==0)
{
if(LOAD2<100)
LOAD2++;
transmit_byte('B');
CONVERT_DISPLAY(LOAD2);
msdelay(500);
}
if(S4==0)
{
if(LOAD2>0)
LOAD2--;
transmit_byte('B');
CONVERT_DISPLAY(LOAD2);
msdelay(500);
}
if(S5==0)
{
if(LOAD3<100)
LOAD3++;
transmit_byte('C');
CONVERT_DISPLAY(LOAD3);
msdelay(500);
}
if(S6==0)
{
if(LOAD3>0)
LOAD3--;
transmit_byte('C');
CONVERT_DISPLAY(LOAD3);
msdelay(500);
}
}
}
void msdelay(int itime)
{ //function to generate time delay
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<300;j++);
}
void transmit_byte(unsigned char byte)
{
SBUF=byte;
while(!TI); //transimt the serial data
TI=0;
}
void CONVERT_DISPLAY(unsigned int d)
{
unsigned char dig1,dig2,dig3,dig[3];
unsigned char x;
unsigned char temp;
temp=d;
temp=temp/10;
dig1=d%10;
dig2=temp%10;
dig3=temp/10;
dig[0]=dig3;
dig[1]=dig2;
dig[2]=dig1;
for(x=0;x<3;x++)
{
temp=dig[x]|0x30;
transmit_byte(temp);
}
}