các bác sửa lỗi chương trình này hộ em với
#include <AT89X51.H>
#include <stdio.h>
#include<string.h>
#define MCP3204_CLK P1_7
#define MCP3204_DOUT P1_6
#define MCP3204_DIN P1_5
#define MCP3204_CS P3_5
#define RS P3_0
#define RW P3_1//RW=0 => ghi
#define EN P3_2//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
#define INTR P3_0
#define RD P3_1
#define WR P3_2
//===========================
unsigned char k;
int_0() interrupt 0 //ma chuong trinh su dung ngat ngoai 0
{ if(k>=125)
{ k=125; }
else {k=k+1;}
}
int_1() interrupt 2 //ma chuong trinh su dung ngat ngoai 1
{ if(k<=10)
{k=10; }
else {k=k-1;}
}
void init_intterupt()
{ EA=1; // Cho phep tat ca cac ngat
EX0=1; // ngat ngoai 0
EX1=1; // ngat ngoai 1
P3_2=1; // dua len cao de lam dau vao
P3_3=1; // dua len cao de lam dau vao
IT0=1; // ngat theo suon ngat 0
IT1=1; // ngat theo suon ngat 1
}
//============================
void LCDWriteCmd(unsigned char c) //CT con ghi du lieu len LCD
{
RS=0;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms();
}
void delay() //do tre la 25ms
{
TMOD=0x01; //ghi tu dieu khien vao thanh ghi tmode
TH0=0x9e; //chon timer0 che do 1
TL0=0x58;
TR0=1;
while(TF0==0)
{}
TF0=0;
}
void delay1()
{
TMOD=0x01;
TH0=0xee;
TL0=0xee;
TR0=1;
while(TF0==0)
{}
TF0=0;
}
//==============================
void LCDWriteData(unsigned char c) //CT con doc du lieu tu LCD
{
RS=1;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms();
}
//=============================
void LCDcursorxy(int x, int y)
{
if((x<1||x>2)&&(y<1||y>16))
{
x=1;
y=1;
}
if(x == 1)
LCDWriteCmd(0x7F+y);
else
LCDWriteCmd(0xBF+y);
}
//===============================
void LCD_init() // Khoi tao LCD
{
delay_us();
LCDWriteCmd(0x38);
LCDWriteCmd(0x0C);
// LCDWriteCmd(0x06);
LCDWriteCmd(0x01); // Xoa man hinh LCD
}
//================================
void LCD_clear()
{
LCDWriteCmd(0x01);
}
//===============================
void LCD_home()
{
LCDWriteCmd(0x80);
}
//=============================
void LCD_putstr(unsigned char *s)
{
while (*s)
{
LCDWriteData(*s);
s++;
}
}
//============================
void HienThi_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
{ LCDWriteData(t+48);
}else if(t<100){
LCDWriteData(t/10+48);
LCDWriteData(t%10+48);
}else{
v=t/10;
LCDWriteData(v/10+48);
LCDWriteData(v%10+48);
LCDWriteData(t%10+48);
}
}
//==========================================
void main (void)
{
void adc_init();
unsigned char gt=0; //gt la bie^n' cho gia tri 8bit ADC
LCD_init();
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
while(1)
{
WR=0; // Bat dau chuyen doi gia tri tu ADC
delay_ms(5); // Tao tre de cap nhat du lieu tu ADC
WR=1; //
while(!INTR);
RD=0; // Nhan du lieu da duoc chuyen doi
gt=P2;
LCDWriteCmd(0x01);
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
LCDWriteCmd(0xC0);
LCD_putstr("Nhiet do:");
HienThi_ADC(gt);
delay_ms(150);
}
}
void adc_init() {
output_high(MCP3204_CS);
}
void write_adc_byte(BYTE data_byte, BYTE number_of_bits) {
BYTE i;
delay_us(2);
for(i=0; i<number_of_bits; ++i) {
output_low(MCP3204_CLK);
if((data_byte & 1)==0)
output_low(MCP3204_DIN);
else
output_high(MCP3204_DIN);
data_byte=data_byte>>1;
delay_us(50);
output_high(MCP3204_CLK);
delay_us(50);
}
}
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(MCP3204_CLK);
delay_us(50);
shift_left(&data,1,input(MCP3204_DOUT));
output_high(MCP3204_CLK);
delay_us(50);
}
return(data);
}
long int read_analog_mcp(BYTE channel, BYTE mode) {
int l;
long int h;
BYTE ctrl_bits;
delay_us(200);
if(mode!=0)
mode=1;
output_low(MCP3204_CLK);
output_high(MCP3204_DIN);
output_low(MCP3204_CS);
if(channel==1) // Change so MSB of channel #
ctrl_bits=4; // is in LSB place
else if(channel==3)
ctrl_bits=6;
else if(channel==4)
ctrl_bits=1;
else if(channel==6)
ctrl_bits=3;
else
ctrl_bits=channel;
ctrl_bits=ctrl_bits<<1;
if(mode==1) // In single mode
ctrl_bits |= 1;
else // In differential mode
ctrl_bits &= 0xfe;
ctrl_bits=ctrl_bits<<1; // Shift so LSB is start bit
ctrl_bits |= 1;
write_adc_byte( ctrl_bits, 7); // Send the control bits
h=read_adc_byte(4);
l=read_adc_byte(8);
output_high(MCP3204_CS);
return((h<<8)|l);
}
long int read_analog( BYTE channel ) // Auto specifies single mode
{
long int temp;
if(channel<4)
temp=read_analog_mcp( channel, 1);
else
temp=0;
return temp;
}
void convert_to_volts( long int data, char volts[6]) {
BYTE i, d, div_h, div_l;
long int temp,div;
div=4095/5;
if(data>=4090)
data=4095;
for(i=0;i<=4;i++) {
temp=data/div;
volts[i]=(BYTE)temp+'0';
if(i==0) {
volts[1]='.';
i++;
}
temp=div*(BYTE)temp;
data=data-temp;
div=div/10;
}
volts[i]='\0';
}
#include <AT89X51.H>
#include <stdio.h>
#include<string.h>
#define MCP3204_CLK P1_7
#define MCP3204_DOUT P1_6
#define MCP3204_DIN P1_5
#define MCP3204_CS P3_5
#define RS P3_0
#define RW P3_1//RW=0 => ghi
#define EN P3_2//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
#define INTR P3_0
#define RD P3_1
#define WR P3_2
//===========================
unsigned char k;
int_0() interrupt 0 //ma chuong trinh su dung ngat ngoai 0
{ if(k>=125)
{ k=125; }
else {k=k+1;}
}
int_1() interrupt 2 //ma chuong trinh su dung ngat ngoai 1
{ if(k<=10)
{k=10; }
else {k=k-1;}
}
void init_intterupt()
{ EA=1; // Cho phep tat ca cac ngat
EX0=1; // ngat ngoai 0
EX1=1; // ngat ngoai 1
P3_2=1; // dua len cao de lam dau vao
P3_3=1; // dua len cao de lam dau vao
IT0=1; // ngat theo suon ngat 0
IT1=1; // ngat theo suon ngat 1
}
//============================
void LCDWriteCmd(unsigned char c) //CT con ghi du lieu len LCD
{
RS=0;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms();
}
void delay() //do tre la 25ms
{
TMOD=0x01; //ghi tu dieu khien vao thanh ghi tmode
TH0=0x9e; //chon timer0 che do 1
TL0=0x58;
TR0=1;
while(TF0==0)
{}
TF0=0;
}
void delay1()
{
TMOD=0x01;
TH0=0xee;
TL0=0xee;
TR0=1;
while(TF0==0)
{}
TF0=0;
}
//==============================
void LCDWriteData(unsigned char c) //CT con doc du lieu tu LCD
{
RS=1;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms();
}
//=============================
void LCDcursorxy(int x, int y)
{
if((x<1||x>2)&&(y<1||y>16))
{
x=1;
y=1;
}
if(x == 1)
LCDWriteCmd(0x7F+y);
else
LCDWriteCmd(0xBF+y);
}
//===============================
void LCD_init() // Khoi tao LCD
{
delay_us();
LCDWriteCmd(0x38);
LCDWriteCmd(0x0C);
// LCDWriteCmd(0x06);
LCDWriteCmd(0x01); // Xoa man hinh LCD
}
//================================
void LCD_clear()
{
LCDWriteCmd(0x01);
}
//===============================
void LCD_home()
{
LCDWriteCmd(0x80);
}
//=============================
void LCD_putstr(unsigned char *s)
{
while (*s)
{
LCDWriteData(*s);
s++;
}
}
//============================
void HienThi_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
{ LCDWriteData(t+48);
}else if(t<100){
LCDWriteData(t/10+48);
LCDWriteData(t%10+48);
}else{
v=t/10;
LCDWriteData(v/10+48);
LCDWriteData(v%10+48);
LCDWriteData(t%10+48);
}
}
//==========================================
void main (void)
{
void adc_init();
unsigned char gt=0; //gt la bie^n' cho gia tri 8bit ADC
LCD_init();
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
while(1)
{
WR=0; // Bat dau chuyen doi gia tri tu ADC
delay_ms(5); // Tao tre de cap nhat du lieu tu ADC
WR=1; //
while(!INTR);
RD=0; // Nhan du lieu da duoc chuyen doi
gt=P2;
LCDWriteCmd(0x01);
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
LCDWriteCmd(0xC0);
LCD_putstr("Nhiet do:");
HienThi_ADC(gt);
delay_ms(150);
}
}
void adc_init() {
output_high(MCP3204_CS);
}
void write_adc_byte(BYTE data_byte, BYTE number_of_bits) {
BYTE i;
delay_us(2);
for(i=0; i<number_of_bits; ++i) {
output_low(MCP3204_CLK);
if((data_byte & 1)==0)
output_low(MCP3204_DIN);
else
output_high(MCP3204_DIN);
data_byte=data_byte>>1;
delay_us(50);
output_high(MCP3204_CLK);
delay_us(50);
}
}
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(MCP3204_CLK);
delay_us(50);
shift_left(&data,1,input(MCP3204_DOUT));
output_high(MCP3204_CLK);
delay_us(50);
}
return(data);
}
long int read_analog_mcp(BYTE channel, BYTE mode) {
int l;
long int h;
BYTE ctrl_bits;
delay_us(200);
if(mode!=0)
mode=1;
output_low(MCP3204_CLK);
output_high(MCP3204_DIN);
output_low(MCP3204_CS);
if(channel==1) // Change so MSB of channel #
ctrl_bits=4; // is in LSB place
else if(channel==3)
ctrl_bits=6;
else if(channel==4)
ctrl_bits=1;
else if(channel==6)
ctrl_bits=3;
else
ctrl_bits=channel;
ctrl_bits=ctrl_bits<<1;
if(mode==1) // In single mode
ctrl_bits |= 1;
else // In differential mode
ctrl_bits &= 0xfe;
ctrl_bits=ctrl_bits<<1; // Shift so LSB is start bit
ctrl_bits |= 1;
write_adc_byte( ctrl_bits, 7); // Send the control bits
h=read_adc_byte(4);
l=read_adc_byte(8);
output_high(MCP3204_CS);
return((h<<8)|l);
}
long int read_analog( BYTE channel ) // Auto specifies single mode
{
long int temp;
if(channel<4)
temp=read_analog_mcp( channel, 1);
else
temp=0;
return temp;
}
void convert_to_volts( long int data, char volts[6]) {
BYTE i, d, div_h, div_l;
long int temp,div;
div=4095/5;
if(data>=4090)
data=4095;
for(i=0;i<=4;i++) {
temp=data/div;
volts[i]=(BYTE)temp+'0';
if(i==0) {
volts[1]='.';
i++;
}
temp=div*(BYTE)temp;
data=data-temp;
div=div/10;
}
volts[i]='\0';
}