错误:
1.C(342): error C267: 'AT24C02Read': requires ANSI-style prototype
1.C(615): error C202: 'tempH_buf': undefined identifier
1.C(616): error C202: 'tempH_buf': undefined identifier
1.C(617): error C202: 'tempH_buf': undefined identifier
1.C(618): error C202: 'tempH_buf': undefined identifier
1.C(642): error C202: 'tempL_buf': undefined identifier
1.C(643): error C202: 'tempL_buf': undefined identifier
1.C(644): error C202: 'tempL_buf': undefined identifier
1.C(645): error C202: 'tempL_buf': undefined identifier
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
程序:
#include <reg52.h>
#include <stdio.h>
#include <main.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD1602_E =P2^5;
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^7;
sbit DSPORT=P2^4;
sbit beep=P2^3;
sbit Led_H=P2^2;
sbit Led_L=P2^1;
sbit SDA=P1^0;
sbit SCL=P1^1;
sbit Key_Swith1=P1^2;
sbit Key_Swith2=P1^3;
sbit Key_Swith3=P1^4 ;
sbit Machine_H=P1^5;
sbit Machine_L=P1^6;
char Set_temp_L=16,Set_temp_H=26; //设定温度上下限默认值
uint temp_val; //检测的实际温度
uchar mode; //温度模式
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//***************************************************************************************//
void LCD_write_com(uchar com)
{
LCD1602_E = 0; //使能
LCD1602_RS = 0; //选择发送命令
LCD1602_RW = 0; //选择写入
P0 = com; //放入命令
delay1(5); //等待数据稳定
LCD1602_E = 1; //写入时序
delay1(5); //保持时间
LCD1602_E = 0;
}
void LCD_write_data(uchar dat)
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //选择输入数据
LCD1602_RW = 0; //选择写入
P0 = dat; //写入数据
delay1(5);
LCD1602_E = 1; //写入时序
delay1(5); //保持时间
LCD1602_E = 0;
}
void LCD_Init()
{
LCD_write_com(0x38); //开显示
LCD_write_com(0x0c); //开显示不显示光标
LCD_write_com(0x06); //写一个指针加1
LCD_write_com(0x01); //清屏
LCD_write_com(0x80); //设置数据指针起点
}
///******************************************************************************///
void LCD_Clear()
{
LCD_write_com(0x01);
LCD_write_com(0x80);
}
//********************************************************//
void Delay10us()
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
/*******************************************************************************
* 函数名 : I2cStart()
* 函数功能 : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
* 输入 : 无
* 输出 : 无
* 备注 : 起始之后SDA和SCL都为0
*******************************************************************************/
void I2cStart()
{
SDA=1;
Delay10us();
SCL=1;
Delay10us();//建立时间是SDA保持时间>4.7us
SDA=0;
Delay10us();//保持时间是>4us
SCL=0;
Delay10us();
}
/*******************************************************************************
* 函数名 : I2cStop()
* 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
* 输入 : 无
* 输出 : 无
* 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲
*******************************************************************************/
void I2cStop()
{
SDA=0;
Delay10us();
SCL=1;
Delay10us();//建立时间大于4.7us
SDA=1;
Delay10us();
}
/*******************************************************************************
* 函数名 : I2cSendByte(unsigned char dat)
* 函数功能 : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定
* 输入 : num
* 输出 : 0或1。发送成功返回1,发送失败返回0
* 备注 : 发送完一个字节SCL=0,SDA=1
*******************************************************************************/
unsigned char I2cSendByte(unsigned char dat)
{
unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。
for(a=0;a<8;a++)//要发送8位,从最高位开始
{
SDA=dat>>7; //起始信号之后SCL=0,所以可以直接改变SDA信号
dat=dat<<1;
Delay10us();
SCL=1;
Delay10us();//建立时间>4.7us
SCL=0;
Delay10us();//时间大于4us
}
SDA=1;
Delay10us();
SCL=1;
while(SDA)//等待应答,也就是等待从设备把SDA拉低
{
b++;
if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
{
SCL=0;
Delay10us();
return 0;
}
}
SCL=0;
Delay10us();
return 1;
}
/*******************************************************************************
* 函数名 : I2cReadByte()
* 函数功能 : 使用I2c读取一个字节
* 输入 : 无
* 输出 : dat
* 备注 : 接收完一个字节SCL=0,SDA=1.
*******************************************************************************/
unsigned char I2cReadByte()
{
unsigned char a=0,dat=0;
SDA=1; //起始和发送一个字节之后SCL都是0
Delay10us();
for(a=0;a<8;a++)//接收8个字节
{
SCL=1;
Delay10us();
dat<<=1;
dat|=SDA;
Delay10us();
SCL=0;
Delay10us();
}
return dat;
}
/*******************************************************************************
* 函数名 : void At24c02Write(unsigned char addr,unsigned char dat)
* 函数功能 : 往24c02的一个地址写入一个数据
* 输入 : 无
* 输出 : 无
*******************************************************************************/
void AT24C02Write(unsigned char addr,unsigned char dat)
{
I2cStart();
I2cSendByte(0xa0);//发送写器件地址
I2cSendByte(addr);//发送要写入内存地址
I2cSendByte(dat); //发送数据
I2cStop();
}
/*******************************************************************************
* 函数名 : unsigned char At24c02Read(unsigned char addr)
* 函数功能 : 读取24c02的一个地址的一个数据
* 输入 : 无
* 输出 : 无
*******************************************************************************/
unsigned char At24C02Read(unsigned char addr)
{
unsigned char num;
I2cStart();
I2cSendByte(0xa0); //发送写器件地址
I2cSendByte(addr); //发送要读取的地址
I2cStart();
I2cSendByte(0xa1); //发送读器件地址
num=I2cReadByte(); //读取数据
I2cStop();
return num;
}
//*******************************************************//
//在任何位置显示字符串
void LCD_Dispstring(uchar x,uchar line,uchar *p)
{
char i=0;
if(line<1) //第一行显示
{
while(*p!='\0')
{
if(i<16-x)
{
LCD_write_com(0x80+i+x);
}
else
{
LCD_write_com(0x40+0x80+i+x-16);
}
LCD_write_data(*p);
p++;
i++;
}
}
else //第2行显示
{
while(*p!='\0')
{
if(i<16-x)
{
LCD_write_com(0x80+0x40+i+x);
}
else
{
LCD_write_com(0x80+i+x-16);
}
LCD_write_data(*p);
p++;
i++;
}
}
}
//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void Temp_DataPros()
{
short temp;
uchar temp_buf[5];
uchar tempH_buf[3];
uchar tempL_buf[3];
temp=DS18B20_ReadTemp();
temp_val=temp;
if(temp<0)
{
temp=-temp;
LCD_Dispstring(2+5,0,"-");
}
else
{
LCD_Dispstring(2+5,0," ");
}
temp_buf[0]=temp/100+0x30;
temp_buf[1]=temp%100/10+0x30;
temp_buf[2]='.';
temp_buf[3]=temp%100%10+0x30;
temp_buf[4]='\0';
LCD_Dispstring(2+6,0,temp_buf); //显示检测的温度xx.x
tempH_buf[0]=Set_temp_H/10+0x30;
tempH_buf[1]=Set_temp_H%10+0x30;
tempH_buf[2]='\0';
LCD_Dispstring(5,1,tempH_buf); //显示设定的温度上限值xx
tempL_buf[0]=Set_temp_L/10+0x30;
tempL_buf[1]=Set_temp_L%10+0x30;
tempL_buf[2]='\0';
LCD_Dispstring(14,1,tempL_buf); //显示设定的温度下限值xx
}
void Start_display()
{
if(AT24C02Read(255)!=18)
{
AT24C02Write(0,Set_temp_H);
AT24C02Write(2,Set_temp_L);
AT24C02Write(255,18);
}
else
{
Set_temp_H=AT24C02Read(0);
Set_temp_L=AT24C02Read(2);
}
LCD_Dispstring(2,0,"Temp: ℃");
LCD_Dispstring(0,1,"Max: ℃");
LCD_Dispstring(9,1,"Min: ℃");
}
void Delay1ms(unsigned int y)
{
unsigned int x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
/*******************************************************************************
* 函数名 : DS18b20_Init
* 函数功能 : 初始化
* 输入 : 无
* 输出 : 初始化成功返回1,失败返回0
*******************************************************************************/
unsigned char DS18B20_Init()
{
unsigned int i;
DSPORT=0; //将总线拉低480us~960us
i=70;
while(i--);//延时642us
DSPORT=1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i=0;
while(DSPORT) //等待DS18B20拉低总线
{
i++;
if(i>5000)//等待>5MS
return 0;//初始化失败
}
return 1;//初始化成功
}
/*******************************************************************************
* 函数名 : DS18b20_WriteByte
* 函数功能 : 向18B20写入一个字节
* 输入 : com
* 输出 : 无
*******************************************************************************/
void DS18B20_WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j++)
{
DSPORT=0; //每写入一位数据之前先把总线拉低1us
i++;
DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
i=6;
while(i--); //延时68us,持续时间最少60us
DSPORT=1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
dat>>=1;
}
}
/*******************************************************************************
* 函数名 : DD18b20_ReadByte
* 函数功能 : 读取一个字节
* 输入 : com
* 输出 : 无
*******************************************************************************/
unsigned char DS18B20_ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0;//先将总线拉低1us
i++;
DSPORT=1;//然后释放总线
i++;
i++;//延时6us等待数据稳定
bi=DSPORT; //读取数据,从最低位开始读取
/*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
byte=(byte>>1)|(bi<<7);
i=4; //读取完之后等待48us再接着读取下一个数
while(i--);
}
return byte;
}
/*******************************************************************************
* 函数名 : DS18b20_ChangTemp
* 函数功能 : 让18b20开始转换温度
* 输入 : com
* 输出 : 无
*******************************************************************************/
void DS18B20_ChangTemp()
{
DS18B20_Init();
Delay1ms(1);
DS18B20_WriteByte(0xcc); //跳过ROM操作命令
DS18B20_WriteByte(0x44); //温度转换命令
// Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
}
/*******************************************************************************
* 函数名 : DS18b20_ReadTempCom
* 函数功能 : 发送读取温度命令
* 输入 : com
* 输出 : 无
*******************************************************************************/
void DS18B20_ReadTempCom()
{
DS18B20_Init();
Delay1ms(1);
DS18B20_WriteByte(0xcc); //跳过ROM操作命令
DS18B20_WriteByte(0xbe); //发送读取温度命令
}
/*******************************************************************************
* 函数名 : Ds18b20ReadTemp
* 函数功能 : 读取温度
* 输入 : com
* 输出 : 无
*******************************************************************************/
short DS18B20_ReadTemp()
{
unsigned char temp=0;
unsigned char tmh,tml;
short tem;
DS18B20_ChangTemp(); //先写入转换命令
DS18B20_ReadTempCom(); //然后等待转换完后发送读取温度命令
tml=DS18B20_ReadByte(); //读取温度值共16位,先读低字节
tmh=DS18B20_ReadByte(); //再读高字节
if(tmh>7)
{
tmh=~tmh;
tml=~tml;
temp=0;//温度为负
}
else
{
temp=1;//温度为正
}
tem=tmh; //获得高八位
tem<<=8;
tem|=tml;//获得底八位
tem=(double)tem*0.625;//转换 放大10倍 精度0.1
if(temp)
return tem; //返回温度值
else
return -tem;
}
#define Key_mode 1 //模式选择
#define Key_add 2 //设置加
#define Key_minus 3 //设置减
//mode: 0-单次扫描 1-连续扫描
uchar Key_scan(uchar mode)
{
static uchar Key_Swith=1;
if(Key_Swith&&(Key_Swith1==0||Key_Swith2==0||Key_Swith3==0))
{
delay1(10);
Key_Swith=0;
if(Key_Swith1=0)
{
return Key_add;
}
else if(Key_Swith2=0)
{
return Key_minus;
}
else if(Key_Swith3=0);
{
return Key_mode;
}
}
else if(Key_Swith1==1&&Key_Swith2==1&&Key_Swith3==1)
{
Key_Swith=1;
}
if(mode)
{
Key_Swith=1;
}
return 0;
}
void Key_Pros()
{
uchar Key_Swith;
uchar Temp_buf[3];
Key_scan(0);
if(Key_Swith==Key_mode)
{
mode++;
LCD_Clear();
if(mode==1)
{
LCD_Dispstring(0,0,"SetH: ℃");
}
else if(mode==2)
{
LCD_Dispstring(0,1,"SetL: ℃");
}
else
{
mode=0;
LCD_Dispstring(2,0,"Temp: ℃");
LCD_Dispstring(0,1,"Max: ℃");
LCD_Dispstring(9,1,"Min: ℃");
}
}
if(mode==1) //设置温度上限
{
switch(Key_Swith)
{
case Key_add:
Set_temp_H++;
if(Set_temp_H>=36)Set_temp_H=36; //上限最高温度
break;
case Key_minus:
Set_temp_H--;
if(Set_temp_H<=16)Set_temp_H=16; //上限最低温度
break;
}
tempH_buf[0]=Set_temp_H/10+0x30;
tempH_buf[1]=Set_temp_H%10+0x30;
tempH_buf[2]='\0';
LCD_Dispstring(6,0,tempH_buf);
AT24C02Write(0,Set_temp_H);
}
else if(mode==2)
{
switch(Key_Swith)
{
case Key_add:
Set_temp_L++;
if(Set_temp_L>=36)Set_temp_L=36; //下限最高温度
break;
case Key_minus:
Set_temp_L--;
if(Set_temp_L<=-16)Set_temp_L=-16; //下限最低温度
break;
}
tempL_buf[0]=Set_temp_L/10+0x30;
tempL_buf[1]=Set_temp_L%10+0x30;
tempL_buf[2]='\0';
LCD_Dispstring(6,0,tempL_buf);
AT24C02Write(0,Set_temp_L);
}
}
void TempData_Compare()
{
if(temp_val>Set_temp_H*10) //实际温度高于上限值 报警 散热
{
Led_H=1;
Led_L=0;
Machine_H=1;
Machine_L=0;
sound();
}
else if(temp_val<Set_temp_L*10) //实际温度低于下限值 报警 加热
{
Led_L=0;
Led_L=1;
Machine_H=0;
Machine_L=1;
sound();
}
else //实际温度在下限值和上限值之间 取消报警 取消加热 取消散热
{
Machine_H=0;
Machine_L=0;
Led_H=0;
Led_L=0;
}
}void delay(uint a)
{
uint i,j;
for(i=a;i>0; i--)
for(j=110;j>0;j--);
}
void warn()
{
uchar a;
//*************************************//
beep=0; //蜂鸣器响
a=50;
while(a--)
{
delay(10);
}
//************************************//
beep=1; //蜂鸣器不响
a=50;
while(a--)
{
delay(10);
}
}
void main()
{
Machine_H=0;
Machine_L=0;
Led_H=0;
Led_L=0;
LCD_Init();
Start_display();
while(1)
{
if(mode==0)
Temp_DataPros();
Key_Pros();
TempData_Compare();
}
}
|