You are on page 1of 5

/*

Do anh sang dung LDR va ADC0808 hoac ADC0809

*/
#include <REGX51.H>
#define VREF 5 //VREF=5V
//Khai bao chan giao tiep ADC0808
#define ADC0808_DATA P2 //PORT
#define ADC0808_A P3_3 //PIN
#define ADC0808_B P3_2
#define ADC0808_C P3_1
#define ADC0808_ALE P3_0
#define ADC0808_START P3_5
#define ADC0808_EOC P3_6
#define ADC0808_OE P3_7
#define ADC0808_CLK P2_7
//Khai bao chan giao tiep LCD16x2 4bit
#define LCD_RS P0_1
#define LCD_RW P0_2
#define LCD_EN P0_3
#define LCD_D4 P0_4
#define LCD_D5 P0_5
#define LCD_D6 P0_6
#define LCD_D7 P0_7
unsigned char d=0x00;

unsigned int prev_value=0,new_value;


/*****************ADC0808*********************/
//Ham doc ADC0808 theo kenh
unsigned char ADC0808_Read(unsigned char channel){
unsigned char kq;
ADC0808_A = channel & 0x01;
ADC0808_B = channel & 0x02;
ADC0808_C = channel & 0x04;
ADC0808_ALE = 1;
ADC0808_START = 1;
ADC0808_ALE = 0;
ADC0808_START = 0;
while(ADC0808_EOC);
while(!ADC0808_EOC);
ADC0808_OE = 1;
kq = ADC0808_DATA;
ADC0808_OE = 0;
return kq;
}
/*****************Ham delay*********************/
void delay_us(unsigned int t){
unsigned int i;
for(i=0;i<t;i++);
}
void delay_ms(unsigned int t){
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<125;j++);
}
/**************Ctr giao tiep LCD 16x2 4bit**********************/
void Lcd_Send_Command(unsigned char cmd) //send command to LCD
{
LCD_RW =0;// Mode write, not read
LCD_RS =0;// reg Instruction, not Data
LCD_DATA =cmd;
LCD_EN = 0;
LCD_EN =1;
if(cmd<=0x02){
Delay_ms(3);
}
else{
Delay_ms(2);}
}
void Lcd_Init() //khoi tao LCD
{
Lcd_Send_Command(0x30);
Delay_ms(5);
Lcd_Send_Command(0x30);
Delay_ms(1);
Lcd_Send_Command(0x30);
Lcd_Send_Command(0x38); // 2 line, font 5x8
Lcd_Send_Command(0x01); // clear LCD
//Lcd_Send_Command(0x0C); //display and turn off pointer,
Lcd_Send_Command(0x0E);//you can also display and turn on pointer by 0x0E
Lcd_Send_Command(0x06);//auto shift pointer to right after printf a letter
}
void Lcd_Goto(unsigned char row, unsigned char col)//row 0,1 and col 0,1
{
unsigned char Cmd;
Cmd = (row == 0? 0x80:0xC0) + col; // col + 0x80 or 0x0C
Lcd_Send_Command(Cmd);
}
void Lcd_Send_Letter(char c)// display a character
{
LCD_RW =0;
LCD_RS =1; // reg of Data, not instruction
LCD_DATA =c;
LCD_EN = 0;
LCD_EN =1;
Delay_ms(1);
}
void Lcd_Send_String(char *str)//display a string
{
unsigned char i=0;
while(str[i]!= NULL) // NULL = 0
{
Lcd_Send_Letter(str[i]);
i++;
}
}
/**************Show Temp***********************
unsigned char Temp(unsigned char adc){//Tinh nhiet do tu adc8bit
return ((VREF*adc)/2.56f); //Tinh nhiet do
} */
void TempShow(unsigned char z){ //Chuyen doi hien thi
LCD_Puts("Cuong do sang: ");
LCD_PutChar((z/100)+48);//Tram
LCD_PutChar((z%100/10)+48);//Chuc
LCD_PutChar((z%10)+48);//Don vi
LCD_Puts("lit");
}
/******************Ctr ngat timer 0**************************/
void INT_Timer0()interrupt 1 {
//ctr phuc vu ngat tao xung clock cho ADC0808
ADC0808_CLK=~ADC0808_CLK; //Dao bit
}

unsigned char temp;


void hienthicuongdosang()

unsigned int tmp1,tmp2,t1,t,j;

unsigned char asci[3] ;

tmp1 = (d & 0x0F);

tmp2 = d & 0xF0;

tmp2 = tmp2>>4;

t = tmp1+tmp2*16;

new_value = t;

if(new_value != prev_value)

if(t>=100)

t1=t%10;

asci[2]=t1+0x30;

t=t/10;

t1=t%10;

asci[1]=t1+0x30;

t = t/10;

asci[0] = t+0x30;

else

t1=t%10;

asci[1]=t1+0x30;

t=t/10;

asci[0]=t+0x30;
asci[2] = 0x20;

LCD_SendCommand(0xC0);

for(j=0;j<=2;j++)

LCD_PutChar(asci[j]);

LCD_SendCommand(0x94);

if(d<50) LCD_Puts("-dim light ");

else if((d>=50) & (d<100))LCD_Puts("-midium light ");

else if((d>=100) & (d<200)) LCD_Puts("-good light ");

else if(d>=200) LCD_Puts("-full light ");

prev_value = new_value;

void khoitao()

P1=0xFF;

P2=0x00;

P0 = 0x00;

P3=0x08;

WR = 1;

RD = 1;

IE=0x84;

void main()

khoitao();

LCD_Init();

LCD_SendCommand(0x01);

LCD_Puts("light intensity=");
while(1)

WR=0;

WR=1;

while(d==0x00);

hienthicuongdosang();

delay_ms(10);

d = 0x00;

You might also like