本人初学者,关于main添加头文件有问题请教大神
时间:10-02
整理:3721RD
点击:
我想做掉电保护,向main中添加I2c.h,可是总是报错,估计是头文件出错,请指点,谢谢
以下是main函数:#include<reg52.h>
#include"I2C.h"
#define uint unsigned int
#define uchar unsigned char
uint shi,fen,miao,num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;
void delay10ms();
void timer(uint num,uint wei);
void keyscan();
void main()
{
miao=read_add(1);
fen=read_add(2);
shi=read_add(3);
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
for(;;)
{
keyscan();
timer(shi,2);
timer(fen,1);
timer(miao,0);
if(write==1)
{
wrie=0;
write_add(1,miao);
write_add(2,fen);
write_add(3,shi);
}
}
}
void keyscan()
{
if(key1==0)
{
delay10ms();
if(key1==0)
{
miao++;
while(~key1);
}
}
if(key2==0)
{
delay10ms();
if(key2==0)
{
fen++;
while(~key2);
}
}
if(key3==0)
{
delay10ms();
if(key3==0)
{
shi++;
while(~key3);
}
}
if(key4==0)
{
delay10ms();
if(key4==0)
{
TR0=~TR0;
while(~key4);
}
}
}
void timer(uint num,uint wei)
{
uint shi,ge;
if(num==60)
num=0;
shi=num/10;
ge=num%10;
P1=0x05;
P0=0x02;
P0=0x00;
P1=0x02;
P0=0x02;
P0=0x00;
if(wei==0)
{
P1=0x07;
P0=table[ge];
P0=0x00;
P1=0x06;
P0=table[shi];
P0=0x00;
}
if(wei==1)
{
P1=0x04;
P0=table[ge];
P0=0x00;
P1=0x03;
P0=table[shi];
P0=0x00;
}
if(wei==2)
{
P1=0x01;
P0=table[ge];
P0=0x00;
P1=0x00;
P0=table[shi];
P0=0x00;
}
}
void time()interrupt 1
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
write=1;
if(miao==60)
{
miao=0;
fen++;
if(shi==24)
{
shi=0;
}
}
}
}
void delay10ms()
{
uint i,j;
for(i=10;i>0;i--)
for(j=110;j>0;j--);
}
然后是I2c.c
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;
sbit sda=P2^0;
bit write=0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void delay()
{;;}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250)) i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
sda=date>>7;
date=date<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
然后建立头文件.h
#ifndef __I2c_h_
#define __I2c_h_
//typedef void delayms(uint z) ;
//void delay();
//void init();
//void start();
//void stop();
//void respons();
//void write_byte(uchar date);
//uchar read_byte();
void write_add(uchar address,uchar date);
uchar read_add(uchar address);
#endif
但是编译就报错,错误见下:
I2C.H(13): error C141: syntax error near 'address', expected ')'
I2C.H(14): error C129: missing ';' before 'read_add'
main.c - 2 Error(s), 0 Warning(s).
以下是main函数:#include<reg52.h>
#include"I2C.h"
#define uint unsigned int
#define uchar unsigned char
uint shi,fen,miao,num;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;
void delay10ms();
void timer(uint num,uint wei);
void keyscan();
void main()
{
miao=read_add(1);
fen=read_add(2);
shi=read_add(3);
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
for(;;)
{
keyscan();
timer(shi,2);
timer(fen,1);
timer(miao,0);
if(write==1)
{
wrie=0;
write_add(1,miao);
write_add(2,fen);
write_add(3,shi);
}
}
}
void keyscan()
{
if(key1==0)
{
delay10ms();
if(key1==0)
{
miao++;
while(~key1);
}
}
if(key2==0)
{
delay10ms();
if(key2==0)
{
fen++;
while(~key2);
}
}
if(key3==0)
{
delay10ms();
if(key3==0)
{
shi++;
while(~key3);
}
}
if(key4==0)
{
delay10ms();
if(key4==0)
{
TR0=~TR0;
while(~key4);
}
}
}
void timer(uint num,uint wei)
{
uint shi,ge;
if(num==60)
num=0;
shi=num/10;
ge=num%10;
P1=0x05;
P0=0x02;
P0=0x00;
P1=0x02;
P0=0x02;
P0=0x00;
if(wei==0)
{
P1=0x07;
P0=table[ge];
P0=0x00;
P1=0x06;
P0=table[shi];
P0=0x00;
}
if(wei==1)
{
P1=0x04;
P0=table[ge];
P0=0x00;
P1=0x03;
P0=table[shi];
P0=0x00;
}
if(wei==2)
{
P1=0x01;
P0=table[ge];
P0=0x00;
P1=0x00;
P0=table[shi];
P0=0x00;
}
}
void time()interrupt 1
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
write=1;
if(miao==60)
{
miao=0;
fen++;
if(shi==24)
{
shi=0;
}
}
}
}
void delay10ms()
{
uint i,j;
for(i=10;i>0;i--)
for(j=110;j>0;j--);
}
然后是I2c.c
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;
sbit sda=P2^0;
bit write=0;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void delay()
{;;}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250)) i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
sda=date>>7;
date=date<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
然后建立头文件.h
#ifndef __I2c_h_
#define __I2c_h_
//typedef void delayms(uint z) ;
//void delay();
//void init();
//void start();
//void stop();
//void respons();
//void write_byte(uchar date);
//uchar read_byte();
void write_add(uchar address,uchar date);
uchar read_add(uchar address);
#endif
但是编译就报错,错误见下:
I2C.H(13): error C141: syntax error near 'address', expected ')'
I2C.H(14): error C129: missing ';' before 'read_add'
main.c - 2 Error(s), 0 Warning(s).
你在主程序里面调用#include"I2C.c",而是在I2C里面调用#include"I2C.h" 就行了。
我们一般都是在主程序里调用.H, 然后在.H中调用.C
我就是这样写的呀,报错了,请指点
恩,我这样试了,可是常规的不都是添加.h文件吗,为什么这个是.c 呢,求解
而且我这样修正后出现了新的错误,如下:
MAIN.C(7): error C279: 'table': multiple initialization
这是我程序本身的问题吧,请问该如何修改