Em làm đề tài đo nhiệt độ có cái code này dùng keil c dịch nó báo lỗi sửa mãi không được đây là code của em.các bác xem rồi sửa giùm em với
#include"imt_reg51.h"
#include"intrins.h"
#include"delay.h"
#include"port_init.h"
#include"lcd.h"
void main()
{
unsigned char value_T;
port_init();
LCD_ON_OFF = LCD_ON;
lcd_init();
while(1)
{
//TEMPERATURE SENSOR
lcd_gotoxy(0,0);
lcd_printf("T: C");
lcd_cmd_wr(0x84);
lcd_data_wr(0xdf);
ADC_CH0=0;
ADC_CH1=0;
ADC_CH2=0;
ADC_ALE=0;
ADC_SOC=0;
ADC_OE=0;
value_T=hex_dec(start_conv());
T=hex_dec(value_T+0x02);
lcd_cmd_wr(0x82);
lcd_print(T);
}
void port_init(void)
{
P0 = 0xFF; // Port0 initialized as input port
P1 = 0xFF; // Port1 initialized as input port
P2 = 0xFF; // Port2 initialized as input port
P3 = 0xFF; // Port3 initialized as input port
}
void lcd_init(void)
{
uchar i;
uchar lcd_initseq[4] =
{
MODE6, // Interface datalength=8, display lines=2, character font=5*7 dots.
DISP_ON_CU_OFF, // Display ON and Cursor OFF
CLR_DISP // Write space code to all DDRAM, set AC to 0 and set for Cursor Increment mode.
};
delay_msec(5);
for(i = 0;i < 4;i++)
{
lcd_cmd_wr(lcd_initseq[i]); // LCD command write module
}
}
void lcd_cmd_wr(uchar lcd_cmd)
{
wait_lcd(); // wait until lcd is busy
DATA_BUS = lcd_cmd; // place lcd command value on the data bus
LCD_RS = LCD_CMD_REG; // Select command register for storing command value
LCD_RW = LCD_WRITE; // select write operation mode to write comand value into command register
ADD_BUS = LCD_ADD; // enable lcd to read command value available on data bus and store into command register
delay_msec(1);
ADD_BUS = DISABLE; // disable all peripherl device
}
void lcd_data_wr(uchar lcd_data)
{
wait_lcd(); // wait until lcd is busy
DATA_BUS = lcd_data; // place data value on data bus which is to be written into lcd.
LCD_RS = LCD_DATA_REG; // select lcd data register to store data value
LCD_RW = LCD_WRITE; // select write operation mode for writing data value into lcd data register
ADD_BUS = LCD_ADD; // enable lcd to read data value available on the data bus
delay_msec(1);
ADD_BUS = DISABLE; //disable all peripheral devices
}
bit lcd_gotoxy(unsigned char x, unsigned char y)
{
unsigned char xy;
bit flag = 0;
y = (y == 0)?(0x80)y == 1)?(0XC0)0X00)); // Assign LCD column Address to y coordinate
x = ((x >= 0)||(x <= 15))?x:0x00; // Assign LCD Row Address to X coordinate
xy = x + y; // XY coordinate Address
if(xy)
{
lcd_cmd_wr(xy); // If Valid Address Move cursor to LCD xy coordinate
flag = 1;
}
return(flag);
}
void wait_lcd()
{
LCD_BF = 1; // set D7 data line as input line to read busy flag
DATA_BUS = INPUT_PORT;
LCD_RS = LCD_CMD_REG; //select lcd command register to read busy flag
LCD_RW = LCD_READ; // select read operation to read busy flag from lcd
ADD_BUS = LCD_ADD; // enable lcd to read busy flag
delay_msec(1);
while(LCD_BF) // check continuously until lcd is busy
{
}
ADD_BUS = DISABLE; // disble all peripheral device including lcd.
}
unsigned char start_conv()
{
unsigned char C;
P0=0xff;
P2=0xff;
ADC_ALE=1; //ADC_ALE//
delay_msec(1);
ADC_SOC=1; //ADC_SOC//
delay_msec(1);
ADC_ALE=0; //ADC_ALE//
delay_msec(1);
ADC_SOC=0; //ADC_SOC
delay_msec(1);
while(ADC_EOC==0);
ADC_OE=1; //ADC_OE
P0=0x22;
C=P2;
delay_msec(1);
ADC_OE=0;
P0=0xff; //ADC_OE
return(C);
}
unsigned char hex_dec(unsigned char D)
{
unsigned char LB,HB,temp,count,AX;
temp=D;
temp=_crol_((0xf0&temp),4);
for(HB=0x00,count=0x00;count<temp;count++)
{
HB=HB+0x16;
AX=AC;
if(AX==1)
{
HB=HB+0X06;
}
if((HB&0X0F)>0X09&&(HB&0XF0)>0X90)
{
HB=HB+0X66;
}
else if((HB&0X0F)>0X09)
{
HB=HB+0X06;
}
else if((HB&0XF0)>0X90)
{
HB=HB+0X60;
}
else
{
HB=HB;
}
}
LB=D&0x0F;
HB=HB+LB;
cảm ơn các bác
AX=AC;
if(AX==1)
{
HB=HB+0X06;
}
if((HB&0X0F)>0X09&&(HB&0XF0)>0X90)
{
HB=HB+0X66;
}
else if((HB&0X0F)>0X09)
{
HB=HB+0X06;
}
else if((HB&0XF0)>0X90)
{
HB=HB+0X60;
}
else
{
HB=HB;
}
return(HB);
}
#include"imt_reg51.h"
#include"intrins.h"
#include"delay.h"
#include"port_init.h"
#include"lcd.h"
void main()
{
unsigned char value_T;
port_init();
LCD_ON_OFF = LCD_ON;
lcd_init();
while(1)
{
//TEMPERATURE SENSOR
lcd_gotoxy(0,0);
lcd_printf("T: C");
lcd_cmd_wr(0x84);
lcd_data_wr(0xdf);
ADC_CH0=0;
ADC_CH1=0;
ADC_CH2=0;
ADC_ALE=0;
ADC_SOC=0;
ADC_OE=0;
value_T=hex_dec(start_conv());
T=hex_dec(value_T+0x02);
lcd_cmd_wr(0x82);
lcd_print(T);
}
void port_init(void)
{
P0 = 0xFF; // Port0 initialized as input port
P1 = 0xFF; // Port1 initialized as input port
P2 = 0xFF; // Port2 initialized as input port
P3 = 0xFF; // Port3 initialized as input port
}
void lcd_init(void)
{
uchar i;
uchar lcd_initseq[4] =
{
MODE6, // Interface datalength=8, display lines=2, character font=5*7 dots.
DISP_ON_CU_OFF, // Display ON and Cursor OFF
CLR_DISP // Write space code to all DDRAM, set AC to 0 and set for Cursor Increment mode.
};
delay_msec(5);
for(i = 0;i < 4;i++)
{
lcd_cmd_wr(lcd_initseq[i]); // LCD command write module
}
}
void lcd_cmd_wr(uchar lcd_cmd)
{
wait_lcd(); // wait until lcd is busy
DATA_BUS = lcd_cmd; // place lcd command value on the data bus
LCD_RS = LCD_CMD_REG; // Select command register for storing command value
LCD_RW = LCD_WRITE; // select write operation mode to write comand value into command register
ADD_BUS = LCD_ADD; // enable lcd to read command value available on data bus and store into command register
delay_msec(1);
ADD_BUS = DISABLE; // disable all peripherl device
}
void lcd_data_wr(uchar lcd_data)
{
wait_lcd(); // wait until lcd is busy
DATA_BUS = lcd_data; // place data value on data bus which is to be written into lcd.
LCD_RS = LCD_DATA_REG; // select lcd data register to store data value
LCD_RW = LCD_WRITE; // select write operation mode for writing data value into lcd data register
ADD_BUS = LCD_ADD; // enable lcd to read data value available on the data bus
delay_msec(1);
ADD_BUS = DISABLE; //disable all peripheral devices
}
bit lcd_gotoxy(unsigned char x, unsigned char y)
{
unsigned char xy;
bit flag = 0;
y = (y == 0)?(0x80)y == 1)?(0XC0)0X00)); // Assign LCD column Address to y coordinate
x = ((x >= 0)||(x <= 15))?x:0x00; // Assign LCD Row Address to X coordinate
xy = x + y; // XY coordinate Address
if(xy)
{
lcd_cmd_wr(xy); // If Valid Address Move cursor to LCD xy coordinate
flag = 1;
}
return(flag);
}
void wait_lcd()
{
LCD_BF = 1; // set D7 data line as input line to read busy flag
DATA_BUS = INPUT_PORT;
LCD_RS = LCD_CMD_REG; //select lcd command register to read busy flag
LCD_RW = LCD_READ; // select read operation to read busy flag from lcd
ADD_BUS = LCD_ADD; // enable lcd to read busy flag
delay_msec(1);
while(LCD_BF) // check continuously until lcd is busy
{
}
ADD_BUS = DISABLE; // disble all peripheral device including lcd.
}
unsigned char start_conv()
{
unsigned char C;
P0=0xff;
P2=0xff;
ADC_ALE=1; //ADC_ALE//
delay_msec(1);
ADC_SOC=1; //ADC_SOC//
delay_msec(1);
ADC_ALE=0; //ADC_ALE//
delay_msec(1);
ADC_SOC=0; //ADC_SOC
delay_msec(1);
while(ADC_EOC==0);
ADC_OE=1; //ADC_OE
P0=0x22;
C=P2;
delay_msec(1);
ADC_OE=0;
P0=0xff; //ADC_OE
return(C);
}
unsigned char hex_dec(unsigned char D)
{
unsigned char LB,HB,temp,count,AX;
temp=D;
temp=_crol_((0xf0&temp),4);
for(HB=0x00,count=0x00;count<temp;count++)
{
HB=HB+0x16;
AX=AC;
if(AX==1)
{
HB=HB+0X06;
}
if((HB&0X0F)>0X09&&(HB&0XF0)>0X90)
{
HB=HB+0X66;
}
else if((HB&0X0F)>0X09)
{
HB=HB+0X06;
}
else if((HB&0XF0)>0X90)
{
HB=HB+0X60;
}
else
{
HB=HB;
}
}
LB=D&0x0F;
HB=HB+LB;
cảm ơn các bác
AX=AC;
if(AX==1)
{
HB=HB+0X06;
}
if((HB&0X0F)>0X09&&(HB&0XF0)>0X90)
{
HB=HB+0X66;
}
else if((HB&0X0F)>0X09)
{
HB=HB+0X06;
}
else if((HB&0XF0)>0X90)
{
HB=HB+0X60;
}
else
{
HB=HB;
}
return(HB);
}
Comment