Mô phỏng trên Proteus:
mô phỏng trên Proteus
Sơ đồ nguyên lý:
Sơ đồ nguyên lý
[success title="Mã nguồn" icon="check-circle"]
#include "16f877a.h"
#include "def_877a.h"
#device *=16 ADC=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "lcd_lib_4bit.c"
//=======================================================================
void send(int8 a);
void HIENTHI_LCD(int8 t);
void HIENTHI_LCD_float(int8 t);
int8 count=0;
int8 i,value,vl[10];
//=======================================================================
#INT_RDA //ngat khi nhan du lieu.
Receive_isr()
{
char c;
c=getc();
switch(c)
{
case '0': //xoa man hinh LCD
{
LCD_putcmd(0x01);
}
break;
//o day co the su dung ma thap phan! (ngoai tru cac ky tu dieu khien)
case '-': //lui con tro hien thi LCD 1 don vi.
{
LCD_putcmd(0x10);
}
break;
case '1': //truyen len may tinh: gia tri do duoc.
{
Set_ADC_channel(0); //kenh 0 chan so2
delay_us(10);
for(i=0;i<10;i++)
{
vl[i]=read_adc();
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/20.48;
send(value);
}
break;
case '2': //truyen len may tinh: gia tri do duoc.
{
Set_ADC_channel(1); //kenh 1 chan so3
delay_us(10);
if(read_adc()==0)
{
for(i=0;i<10;i++)
{
vl[i]=read_adc();
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
}
else
{
value=read_adc()+6;
for(i=0;i<10;i++)
{
vl[i]=read_adc()+6;
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
}
send(value);
}
break;
case '9': //thoat khoi ham ngat, cho ADC làm viec.
{
count=0; //ket thuc viec truyen ky tu len LCD
LCD_putcmd(0x01);
}
break;
default : //truyen ky tu xuong LCD ^^.
{
count++;
if(count==1) LCD_putcmd(0x01);
if(count==16) LCD_putcmd(0xC0);
if(count==32)
{
LCD_putcmd(0x01);
count=0;
}
LCD_putchar(c);
}
}
}
//=======================================================================
void main()
{
//trisD=0x0C; //D0,D1 LA CONG VAO, D2-D7 LA CONG RA.
enable_interrupts(int_rda); //cho phep ngat noi tiep nhan.
enable_interrupts(GLOBAL);
LCD_init(); //Khoi tao LCD.
LCD_putcmd(0xC0);
Printf(LCD_putchar,"Khoi tao...");
delay_ms(250);
setup_adc_ports(AN0_AN1_AN3); //Khoi tao che do cho bo ADC.
setup_adc(ADC_CLOCK_INTERNAL);
delay_us(10);
while(1)
{
//do nhiet do
{
Set_ADC_channel(0); //kenh 0 chan so2
delay_us(10);
value=read_adc();
value=value/2.048;
LCD_putcmd(0x80);
LCD_putchar("Nhiet do: ");
LCD_putcmd(0x89);
HIENTHI_LCD(value);
LCD_putcmd(0x8C);
LCD_putchar("oC");
delay_ms(0.1);
}
//do diep ap
{
Set_ADC_channel(1); //kenh 1 chan so3
delay_us(10);
if(read_adc()==0)
{
for(i=0;i<10;i++)
{
vl[i]=read_adc();
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
}
else
{
value=read_adc()+6;
for(i=0;i<10;i++)
{
vl[i]=read_adc()+6;
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
}
LCD_PutCmd(0xC0);
LCD_putchar("DIEN AP : ");
LCD_putcmd(0xC9);
//HIENTHI_LCD(value);
HIENTHI_LCD_float(value);
LCD_putcmd(0xCD);
LCD_putchar("V ");
}
}
}
//=======================================================================
void HIENTHI_LCD(int8 t)
{
unsigned char v;
if(t<10)
LCD_putchar(t+48);
else
if(t<100)
{
LCD_putchar(t/10+48);
LCD_putchar(t%10+48);
}
else
{
v=t/10;
LCD_putchar(v/10+48);
LCD_putchar(v%10+48);
LCD_putchar(t%10+48);
}
}
void send(int8 a)
{
if(a<10)
{
putc(a+48);
}
if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
putc(c+48);
putc(d+48);
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
putc(t+48);
putc(c+48);
putc(d+48);
}
}
void HIENTHI_LCD_float(int8 t)
{
int8 v;
if(t<10)
{
LCD_putchar(48);
LCD_putchar(46);
LCD_putchar(t+48);
LCD_putchar(32);
}
else
if(t<100)
{
LCD_putchar(t/10+48);
LCD_putchar(46);
LCD_putchar(t%10+48);
LCD_putchar(32);
}
else
{
v=t/10;
LCD_putchar(v/10+48);
LCD_putchar(v%10+48);
LCD_putchar(46);
LCD_putchar(t%10+48);
}
}
/*
void send_float(float a)
{
if(a<10)
{
putc(a+48);
}
if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
putc(c+48);
putc(d+48);
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
putc(t+48);
putc(c+48);
putc(d+48);
}
}*/
//=======================================================================[/success]
Đăng nhận xét