8051 Development Board
*
* Project name:
8051 Development Board
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include <reg52.h>
#define adc_port P2 //ADC Port
sbit rd=P3^0; //Read signal P1.0
sbit wr=P3^1; //Write signal P1.1
sbit cs=P3^2; //Chip Select P1.2
sbit intr=P3^3; //INTR signal P1.3
#define LCD_PORT P1//used for data pins.
sbit rs=P3^7;
sbit rw=P3^6;
sbit en=P3^5;
sbit d7=P2^7;
void conv(); //Start of conversion function
void read();//Read ADC function
void cmd_wrt(unsigned char);
void data_wrt(unsigned char);
void busy();
void DELAY();
unsigned char adc_val;
void main() {
unsigned char cmd[]={0x38,0x0F,0x01,0X06,0X80},i,temp,x,y,z;
unsigned int d1=0,d2=0,d3=0,d4=0;
while (1) { //Forever loop
conv(); //Start conversion
read(); //Read ADC
P2 = adc_val; //Send the read value to P3
for(i=0;i<=4;i++)
{
temp=cmd[i];
cmd_wrt(temp);
DELAY();
}
temp=P2;
data_wrt(temp);
DELAY();
x=P2;
d1=x%100; //conversion hexadecimal value to decimal value
d2=x/10;
d3=d2%10;
d4=d2/10;
d4=((d4*16)*16);
d3= d3*16;
d1=d1+d3+d4;
y=0.0192*d1;
z=y*6;
data_wrt(z);
DELAY();
}
}
void conv() {
cs = 0; //Make CS low
wr = 0; //Make WR low
wr = 1; //Make WR high
cs = 1; //Make CS high
while (intr); //Wait for INTR to go low
}
void read() {
cs = 0; //Make CS low
rd = 0; //Make RD low
adc_val = adc_port; //Read ADC port
rd = 1; //Make RD high
cs = 1; //Make CS high
}
void busy()
{
d7=1;
rs=0;
rw=1;
while(d7!=0)
{
en=0;
DELAY();
en=1;
}
}
void cmd_wrt(unsigned char val)
{
busy();
LCD_PORT=val;
rs=0;
rw=0;
en=1;
DELAY();
en=0;
}
void data_wrt(unsigned char ch)
{
busy();
LCD_PORT=ch;
rs=1;
rw=0;
en=1;
DELAY();
en=0;
}
void DELAY()
{
unsigned int x=20000;
while(x--);
}