Bài này Chip lập trình chuyển đổi ADC đúng rồi,nhưng có chỗ hiển thị LCD chưa được, bạn nào biết thì đóng góp Chip nhé..^^
CODE C
#include <REGX51.H>
#include<math.h>
#include <stdio.h>
#include <intrins.h>
#include <string.h>
/********************/
#define ADC_CS P3_3
#define ADC_RD P3_4
#define ADC_WR P3_5
#define ADC_INTR P3_6
#define ADC_DATA P1
/*sbit CS = P3^3;
sbit RD = P3^4;
sbit WR = P3^5;
sbit INTR = P3^6; */
/**************/
#define LCDdata P2
sbit BF = 0xA7;
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
unsigned char ADC_val,voltage;
// Khai bao cac bien toan cuc
unsigned char code string1[] = "Chipkool.net";
unsigned char code string2[] = "Dien ap = ";
unsigned char voltage,digit1,digit2;
/***************************/
void delay(unsigned char ms)
{
unsigned char i,j;
for(i=0;i<ms;i++)
for(j=0;j<125;j++){}
}
void delayshort(void)
{
unsigned char a;
for(a=0;a<10;a++);
}
/*********************************/
void wait(void)
{
RS=0;//chon thanh ghi lenh
RW=1;//doc du lieu tu lcd
LCDdata=0xff;
while(BF)
{
EN=0;
EN=1;
}
}
/************************************/
void LCDcontrol(unsigned char x)
{
RS=0; //chon thanh ghi lenh
RW=0;//ghi lenh len LCD
LCDdata=x;
EN=1;
EN=0;
wait();
}
/**************************************/
void LCDinit(void)
{
LCDcontrol(0x38);//ma tran 5x7
LCDcontrol(0x0e);//bat man hinh bat con tro
LCDcontrol(0x01);//xoa man hinh
}
/*************************************/
void LCDwrite(unsigned char c)
{
RS=1;//chon thanh ghi du lieu
RW=0;//ghi du lieu ra
LCDdata=c;
EN=1;
EN=0;
wait();
}
/****************************************/
void LCDwrites(unsigned char*s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++)
{
LCDwrite(*(s+count));
}
}
/*****************************************/
void LCDclear(void)
{
LCDcontrol(0x01);
wait();
}
/*****************************/
void Convert()
{
// ra lenh chuyen doi
ADC_RD =1;
ADC_CS =0;
delayshort();
ADC_WR =0;
delayshort();
ADC_WR =1;
delayshort();
ADC_CS =1;
// cho chuyen doi xong
while(ADC_INTR==1);//chuyen xong 1 bit thi ngat ADC
ADC_DATA = 0xff;//chuyen port thanh cong vao de chuan bi doc du lieu
ADC_CS =0;
delayshort();
ADC_RD =0;
delayshort();
ADC_CS =1;
// Doc du lieu vao
voltage = ADC_DATA;
ADC_RD = 1;
delayshort();
ADC_CS = 1;
}
/*******************************************/
void view(unsigned char var)
{
unsigned char val;
if(val<10){
LCDwrite(val+48);
}else if(val<100){
LCDwrite(val/10+48);
LCDwrite(val%10+48);
}else{
var=val/10;
LCDwrite(var/10+48);
LCDwrite(var%10+48);
LCDwrite(val%10+48);
}
}
/************************************************/
void main(void)
{
LCDinit();
LCDcontrol(0x83);
LCDwrites("ADC0804");
while(1){
LCDcontrol(0xC0);
Convert();
//view(ADC_DATA);
view(voltage);
}
}
(Empty)
Nguồn: Blog Điện Tử | Tin Học - Chia sẻ kiến thức - Kết nối đam mê điện tử
Đăng nhận xét