/**************************************************
实验名称:I2C总线实验一
实验目的:利用定时器产生0~99秒表在数码管前两位显示
每过一秒写入到AT24C02里面,关闭实验板后
重新打开实验板,秒表从AT24C02读入数据继
续上一次的计时。
实验备注:
**************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
sbit SDA = P2^0;
sbit SCL = P2^1;
sbit DUAN = P2^6;
sbit WEI = P2^7;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
uchar num, sec;
void delay_1us();
void delay_1ms(uchar);
void display(uchar);
void at24_init();
void at24_start();
void at24_stop();
void at24_respons();
void at24_writebyte(uchar);
uchar at24_readbyte(uchar);
void at24_writeadd(uchar,uchar);
uchar at24_readadd(uchar);
void main()
{
at24_init();
sec = at24_readadd(5);
if(sec > 100)
sec = 0;
TMOD = 0x01;
TH0 = (65536-46080)/256;
TL0 = (65536-46080)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(num == 20)
{
num = 0;
sec++;
if(sec == 100)
{
sec = 0;
}
at24_writeadd(5,sec);
}
display(sec);
}
}
void delay_1us()
{
//uchar i, j;
//for(i = 0; i < 1; i++)
//for(j = 0; j < 1; j++);
;;
}
void delay_1ms(uchar x)
{
uchar i, j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
void display(uchar x)
{
DUAN = 1;
P0 = duan_table[x/10];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(5);
DUAN = 1;
P0 = duan_table[x%10];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(5);
}
void at24_init()
{
SDA = 1;
delay_1us();
SCL = 1;
delay_1us();
}
void at24_start()
{
SDA = 1;
delay_1us();
SCL = 1;
delay_1us();
SDA = 0;
delay_1us();
}
void at24_stop()
{
SDA = 0; //必须先把SDA拉低
delay_1us();
SCL = 1;
delay_1us();
SDA = 1;
delay_1us();
}
void at24_respons()
{
uchar i;
//i = 0;
SCL = 1;
delay_1us();
while((SDA == 1) && (i<250))
i++;
SCL = 0;
delay_1us();
}
void at24_writebyte(uchar dat)
{
uchar i,temp;
i = 0;
temp = dat;
for(i = 0; i < 8; i++)
{
temp = temp<<1;
SCL = 0;
delay_1us();
SDA = CY;
delay_1us();
SCL = 1;
delay_1us();
}
SCL = 0;
delay_1us();
SDA = 1;
delay_1us();
}
uchar at24_readbyte()
{
uchar i, temp;
//i = 0;
//temp = 0;
SCL = 0;
delay_1us();
SDA = 1;
delay_1us();
for(i = 0; i < 8; i++)
{
SCL = 1;
delay_1us();
temp = ((temp<<1) | SDA);
SCL = 0;
delay_1us();
}
return temp;
}
void at24_writeadd(uchar address,uchar dat)
{
EA = 0;
at24_start();
at24_writebyte(0xa0);
at24_respons();
at24_writebyte(address);
at24_respons();
at24_writebyte(dat);
at24_respons();
at24_stop();
EA = 1;
delay_1ms(5);
}
uchar at24_readadd(uchar address)
{
uchar temp;
at24_start();
at24_writebyte(0xa0);
at24_respons();
at24_writebyte(address);
at24_respons();
at24_start();
at24_writebyte(0xa1);
at24_respons();
temp = at24_readbyte();
at24_stop();
delay_1ms(1);
return temp;
}
void Timer0() interrupt 1
{
TH0 = (65536-46080)/256;
TL0 = (65536-46080)%256;
num++;
}
/**************************************************
实验名称:I2C总线实验二
实验目的:从键盘上读取两位数值,并储存在AT24C02里,
再从AT24C02中读取出来显示在数码管前两位。
1,数码管显示00的时候,按下键盘输入要储存
的两位数字。
2.数码管显示已经储存的数字,并等待键盘的
第二次输入。
实验备注:
**************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
sbit SDA = P2^0;
sbit SCL = P2^1;
sbit DUAN = P2^6;
sbit WEI = P2^7;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
uchar code heng_table[]=
{0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
uchar num_led, num_i2c, flag_key, ge, shi;
void delay_4us();
void delay_1ms(uchar);
void display(uchar);
uchar keyscan();
uchar keynum();
void i2c_init();
void i2c_respons();
void i2c_start();
void i2c_stop();
void i2c_writebyte(uchar);
uchar i2c_readbyte();
void i2c_writeadd(uchar,uchar);
uchar i2c_readadd(uchar);
void main()
{
flag_key = 0;
num_led = 0;
num_i2c = 0;
i2c_init();
i2c_writeadd(3, num_i2c);
while(1)
{
display(num_led);
num_i2c = keynum();
if(flag_key == 3)
{
i2c_writeadd(3, num_i2c);
flag_key = 0;
}
num_led = i2c_readadd(3);
}
}
void delay_4us()
{;;}
void delay_1ms(uchar x)
{
uchar i, j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
void display(uchar num)
{
DUAN = 1;
P0 = duan_table[num/10];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(2);
DUAN = 1;
P0 = duan_table[num%10];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(2);
}
uchar keyscan()
{
uchar key, temp;
key = 44;
P3 = heng_table[0];
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
delay_1ms(5);
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
temp = P3;
switch (temp)
{
case 0xee:
key = 1;
break;
case 0xde:
key = 2;
break;
case 0xbe:
key = 3;
break;
case 0x7e:
key = 4;
break;
}
while(temp != 0xf0)
{
temp = P3;
temp = temp & 0xf0;
}
}
}
P3 = heng_table[1];
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
delay_1ms(5);
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
temp = P3;
switch (temp)
{
case 0xed:
key = 5;
break;
case 0xdd:
key = 6;
break;
case 0xbd:
key = 7;
break;
case 0x7d:
key = 8;
break;
}
while(temp != 0xf0)
{
temp = P3;
temp = temp & 0xf0;
}
}
}
P3 = heng_table[2];
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
delay_1ms(5);
temp = P3;
temp = temp & 0xf0;
if(temp != 0xf0)
{
temp = P3;
switch (temp)
{
case 0xeb:
key = 9;
break;
case 0xdb:
key = 0;
break;
case 0xbb:
key = 11;
break;
case 0x7b:
key = 12;
break;
}
while(temp != 0xf0)
{
temp = P3;
temp = temp & 0xf0;
}
}
}
return key;
}
uchar keynum()
{
uchar temp;
temp = 0;
if(flag_key == 0)
{
shi = keyscan();
if(shi != 44)
{
flag_key = 1;
}
}
if(flag_key == 1)
{
ge = keyscan();
if(ge != 44)
{
flag_key = 2;
}
}
if(flag_key == 2)
{
temp = shi*10 + ge;
flag_key = 3;
}
return temp;
}
void i2c_init()
{
SCL = 1;
delay_4us();
SDA = 1;
delay_4us();
}
void i2c_respons()
{
uchar i;
i = 0;
SCL = 1;
delay_4us();
while((SDA ==1) && (i < 250))i++;
SCL = 0;
delay_4us();
}
void i2c_start()
{
SDA = 1;
delay_4us();
SCL = 1;
delay_4us();
SDA = 0;
delay_4us();
}
void i2c_stop()
{
SDA = 0;
delay_4us();
SCL = 1;
delay_4us();
SDA = 1;
delay_4us();
}
void i2c_writebyte(uchar x)
{
uchar i, temp;
temp = x;
for(i = 0; i < 8; i++)
{
temp = temp << 1;
SCL = 0;
delay_4us();
SDA = CY;
delay_4us();
SCL = 1;
delay_4us();
}
SCL = 0;
delay_4us();
SCL = 1;
delay_4us();
}
uchar i2c_readbyte()
{
uchar i ,temp;
SCL = 0;
delay_4us();
SDA = 1;
delay_4us();
for(i = 0; i < 8; i++)
{
SCL = 1;
delay_4us();
temp = ((temp << 1) | SDA);
SCL = 0;
}
return temp;
}
void i2c_writeadd(uchar address, uchar dat)
{
i2c_start();
i2c_writebyte(0xa0);
i2c_respons();
i2c_writebyte(address);
i2c_respons();
i2c_writebyte(dat);
i2c_respons();
i2c_stop();
}
uchar i2c_readadd(uchar address)
{
uchar temp;
i2c_start();
i2c_writebyte(0xa0);
i2c_respons();
i2c_writebyte(address);
i2c_respons();
i2c_start();
i2c_writebyte(0xa1);
i2c_respons();
temp = i2c_readbyte();
i2c_stop();
return temp;
}