关于1602显示ds18b20温度程序的改错问题。
时间:10-02
整理:3721RD
点击:
一堆类似的错误,求大神调教~
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2; //温度传感器信号线
sbit rs=P1^0;
sbit rw=P1^1;
sbit lcden=P2^5;
sbit dula=P2^6;
sbit wela=P2^7;
uint temp;
float f_temp;
void delay(uint z)//延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void dsreset(void) //18B20复位,初始化函数
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit tempreadbit(void) //读1位函数
{
uint i;
bit dat;
ds=0;i++; //i++ 起延时作用
ds=1;i++;i++;
dat=ds;
i=8;while(i>0)i--;
return (dat);
}
uchar tempread(void) //读1个字节
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tempwritebyte(uchar dat) //向18B20写一个字节数据
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //写 1
{
ds=0;
i++;i++;
ds=1;
i=8;while(i>0)i--;
}
else
{
ds=0; //写 0
i=8;while(i>0)i--;
ds=1;
i++;i++;
}
}
}
void tempchange(void) //DS18B20 开始获取温度并转换
{
dsreset();
delay(1);
tempwritebyte(0xcc); // 写跳过读ROM指令
tempwritebyte(0x44); // 写温度转换指令
}
uint get_temp() //读取寄存器中存储的温度数据
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread(); //读低8位
b=tempread(); //读高8位
temp=b;
temp<<=8; //两个字节组合为1个字
temp=temp|a;
f_temp=temp*0.0625; //温度在寄存器中为12位 分辨率位0.0625°
temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入
return temp; //temp是整型
}
void write_wendu(uchar add,uchar temp)
{
uchar bai,shi,ge;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
write_com(0x80+0x40+add);
write_date(0x30+bai);
write_date(0x30+shi);
write_date(0x30+ge);
}
void init()//初始化函数
rw=0;
dula=0;//关闭两锁存器锁存端,防止操作液晶时数码管会出乱码
wela=0;
lcden=0;
write_com(0x38);//初始化1602液晶
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);//设置显示初始坐标
void main()
{
int();
while(1)
{ int i;
tempchange();
for(i=10;i>0;i--)
{
write_wendu(get_temp());}
}
}
------------------------------------------------------------
Build target 'Target 1'
assembling STARTUP.A51...
compiling 222.c...
222.C(168): error C244: 'rw': can't initialize, bad type or class
222.C(168): error C132: 'rw': not in formal parameter list
222.C(169): error C244: 'dula': can't initialize, bad type or class
222.C(169): error C132: 'dula': not in formal parameter list
222.C(170): error C244: 'wela': can't initialize, bad type or class
222.C(170): error C132: 'wela': not in formal parameter list
222.C(171): error C244: 'lcden': can't initialize, bad type or class
222.C(171): error C132: 'lcden': not in formal parameter list
222.C(172): error C141: syntax error near '0x38'
222.C(172): error C132: 'write_com': not in formal parameter list
222.C(173): error C141: syntax error near '0x0c'
222.C(173): error C132: 'write_com': not in formal parameter list
222.C(174): error C141: syntax error near '0x06'
222.C(174): error C132: 'write_com': not in formal parameter list
222.C(175): error C141: syntax error near '0x01'
222.C(175): error C132: 'write_com': not in formal parameter list
222.C(176): error C141: syntax error near '0x80'
222.C(176): error C132: 'write_com': not in formal parameter list
222.C(184): error C132: 'main': not in formal parameter list
222.C(184): error C141: syntax error near '{'
222.C(187): error C132: 'i': not in formal parameter list
222.C(188): error C132: 'tempchange': not in formal parameter list
222.C(189): error C141: syntax error near 'for'
222.C(189): error C141: syntax error near '=', expected ')'
222.C(189): error C129: missing ';' before '>'
Target not created
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2; //温度传感器信号线
sbit rs=P1^0;
sbit rw=P1^1;
sbit lcden=P2^5;
sbit dula=P2^6;
sbit wela=P2^7;
uint temp;
float f_temp;
void delay(uint z)//延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void dsreset(void) //18B20复位,初始化函数
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit tempreadbit(void) //读1位函数
{
uint i;
bit dat;
ds=0;i++; //i++ 起延时作用
ds=1;i++;i++;
dat=ds;
i=8;while(i>0)i--;
return (dat);
}
uchar tempread(void) //读1个字节
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
void tempwritebyte(uchar dat) //向18B20写一个字节数据
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //写 1
{
ds=0;
i++;i++;
ds=1;
i=8;while(i>0)i--;
}
else
{
ds=0; //写 0
i=8;while(i>0)i--;
ds=1;
i++;i++;
}
}
}
void tempchange(void) //DS18B20 开始获取温度并转换
{
dsreset();
delay(1);
tempwritebyte(0xcc); // 写跳过读ROM指令
tempwritebyte(0x44); // 写温度转换指令
}
uint get_temp() //读取寄存器中存储的温度数据
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread(); //读低8位
b=tempread(); //读高8位
temp=b;
temp<<=8; //两个字节组合为1个字
temp=temp|a;
f_temp=temp*0.0625; //温度在寄存器中为12位 分辨率位0.0625°
temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入
return temp; //temp是整型
}
void write_wendu(uchar add,uchar temp)
{
uchar bai,shi,ge;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
write_com(0x80+0x40+add);
write_date(0x30+bai);
write_date(0x30+shi);
write_date(0x30+ge);
}
void init()//初始化函数
rw=0;
dula=0;//关闭两锁存器锁存端,防止操作液晶时数码管会出乱码
wela=0;
lcden=0;
write_com(0x38);//初始化1602液晶
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);//设置显示初始坐标
void main()
{
int();
while(1)
{ int i;
tempchange();
for(i=10;i>0;i--)
{
write_wendu(get_temp());}
}
}
------------------------------------------------------------
Build target 'Target 1'
assembling STARTUP.A51...
compiling 222.c...
222.C(168): error C244: 'rw': can't initialize, bad type or class
222.C(168): error C132: 'rw': not in formal parameter list
222.C(169): error C244: 'dula': can't initialize, bad type or class
222.C(169): error C132: 'dula': not in formal parameter list
222.C(170): error C244: 'wela': can't initialize, bad type or class
222.C(170): error C132: 'wela': not in formal parameter list
222.C(171): error C244: 'lcden': can't initialize, bad type or class
222.C(171): error C132: 'lcden': not in formal parameter list
222.C(172): error C141: syntax error near '0x38'
222.C(172): error C132: 'write_com': not in formal parameter list
222.C(173): error C141: syntax error near '0x0c'
222.C(173): error C132: 'write_com': not in formal parameter list
222.C(174): error C141: syntax error near '0x06'
222.C(174): error C132: 'write_com': not in formal parameter list
222.C(175): error C141: syntax error near '0x01'
222.C(175): error C132: 'write_com': not in formal parameter list
222.C(176): error C141: syntax error near '0x80'
222.C(176): error C132: 'write_com': not in formal parameter list
222.C(184): error C132: 'main': not in formal parameter list
222.C(184): error C141: syntax error near '{'
222.C(187): error C132: 'i': not in formal parameter list
222.C(188): error C132: 'tempchange': not in formal parameter list
222.C(189): error C141: syntax error near 'for'
222.C(189): error C141: syntax error near '=', expected ')'
222.C(189): error C129: missing ';' before '>'
Target not created
我想帮你调,可是这会儿在手机上。你把程序发到我的邮箱,我明天早上给你调,这里有的错误很容易改的,你先自己试试
邮箱是qjq2126@126.com
是不是应该在初始化函数下面加上大括号?