iicled.c
来自「VB环境下的串口通讯设计」· C语言 代码 · 共 516 行
C
516 行
/************************************************/
#include <REG52.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define int8 unsigned char
#define int16 unsigned int
#define int32 unsigned long
sbit SCLK=P3^5;
sbit IO=P3^6;
sbit RESET=P3^7;
sbit I2C_SDA= P1^5;
sbit I2C_SCK= P1^4;
sbit I2C_WP= P1^3;
//============================
sbit LED1=P3^4;
sbit LED2=P1^6;
sbit GRE=P1^7;
sbit s1=P0^3;
sbit s2=P2^3;
sbit SCK=P2^4;
sbit RCK=P2^5;
sbit nG=P2^6;
sbit SER=P2^7;
uchar xdata tab[16][6];
uchar count;
uchar l;
uint m;
bdata uchar temp;//位于位寻址区的temp变量,
sbit bit0=temp^0;//发送数据或命令时用到,通过右移操作将数据或命令放到IO线上
sbit bit7=temp^7;//接收数据时用到,接收到的数据始终存入最高位,然后右移
void McuInit(void);
void Timer_Int();
void tab_time(uchar hh,uchar hl,uchar mh,uchar ml);
uchar read_time(uchar com);
//===============================================
/* 列选择*/
void lay(int8 n)
{
int8 i;
i=n;
s1=0;
s2=0;
if(n<8)
{
//_nop_();
P0=(P0&0xf8)|(~i);
}
else
{ i=i-7;
i=i&0x07;
i=i|0xf0; //保护nG位
P2=(P2&0xf8)|(~i);
}
}
/*延时*/
void delay(int8 N)
{
int8 i,j,k;
for(i=0;i<100;i++)
for(j=0;j<10;j++)
for(k=0;k<N;k++);
}
/*发送字节,移位发送*/
void WriteByte(int8 val)
{
int8 i,m;
m=~val;
for (i = 8; i > 0; i --)
{
SCK = 0;//拉低74HC595时钟
SER = m&0x01;//发送74HC595一位串行数据
SCK = 1;//拉高74HC595时钟
m=m>>1;
_nop_();//延时
}
SER = 1;//释放数据总线
}
/*发送字*/
void display(int8 n )
{
int8 i;
for(i=0;i<6;i++)
WriteByte(tab[n][i]);
RCK=0; //保存并行数据
_nop_();
RCK=1;
}
void delays(int8 n)
{ n=n;
m=m+0x1;
LED1=m&0x800;
GRE=~LED1;
LED2=m&0x1000;
}
void XianShiX(void)
{
/**/display(0);//行0
lay(14); //列
delays(1); //短延时
/**/display(1);//行1
lay(13);
delays(1);
/**/display(2);//行2
lay(12);
delays(1);
/**/display(3);//行3
lay(11);
delays(1);
/**/display(4);//行4
lay(10);
delays(1);
/**/display(5);//行5
lay(9);
delays(1);
/**/display(6);//行6
lay(8);
delays(1);
///**/Timer_Int();
/**/display(7);//行7
lay(15);
delays(1);
/**/display(8);//行8
lay(7);
delays(1);
/**/display(9);//行9
lay(6);
delays(1);
/**/display(10);//行10
lay(5);
delays(1);
/**/display(11);//行11
lay(4);
delays(1);
/**/display(12); //行12
lay(0);
delays(1);
/**/display(13); //行13
lay(1);
delays(1);
/**/display(14); //行14
lay(2);
delays(1);
/**/display(15); //行15
lay(3);
delays(1);
}
//=============================
//延时10US
//内部调用函数
void Delay_10_uS(void)
{
char i=2;
while(i--);
}
//开始位
//内部调用函数
bit I2C_Start(void)
{
Delay_10_uS();
I2C_SDA =1;
Delay_10_uS();
I2C_SCK =1;
Delay_10_uS();
if ( I2C_SDA == 0) return 0;
if ( I2C_SCK == 0) return 0;
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 0;
Delay_10_uS();
return 1;
}
//停止位
//内部调用函数
void I2C_Stop(void)
{
Delay_10_uS();
I2C_SDA = 0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
}
//读数据后拉高应答信号
//内部调用函数
void I2C_Nack(void)
{
Delay_10_uS();
I2C_SDA=1;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
Delay_10_uS();
}
//写单字节数据
//内部调用函数
bit I2C_Send_Byte( uchar d)
{
uchar i = 8;
bit bit_ack;
while( i-- )
{
Delay_10_uS();
if ( d &0x80 ) I2C_SDA =1;
else I2C_SDA =0;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
I2C_SCK = 0;
d = d << 1;
}
Delay_10_uS();
I2C_SDA = 1;
Delay_10_uS();
I2C_SCK = 1;
Delay_10_uS();
bit_ack = I2C_SDA;
I2C_SCK =0;
Delay_10_uS();
return bit_ack;
}
//接收单字节数据
//内部调用函数
uchar I2C_Receive_Byte(void)
{
uchar i = 8, d;
Delay_10_uS();
I2C_SDA = 1;
while ( i--)
{
d = d << 1;
Delay_10_uS();
I2C_SCK =1;
if ( I2C_SDA ) d++;
Delay_10_uS();
I2C_SCK =0;
}
return d;
}
//读多字节数据
//供外部调用函数
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
s1=0;s2=0;nG=1;
while(count--)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256
*2 );*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );//写命令设备地址为10100000最后一位为0"写标志"
I2C_Send_Byte( AT24C64_address/256 );//写存储器页地址
I2C_Send_Byte( AT24C64_address % 256 );//写存储器字节地址
I2C_Start();
/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
I2C_Send_Byte( 0xa1 );//写命令设备地址为10100001最后一位为1"读标志"
*(uchar*)mcu_address = I2C_Receive_Byte();//读单字节数据到MCU指针指向的地址
I2C_Nack();//回应答信号
I2C_Stop();
((uchar*)mcu_address)++;
AT24C64_address++;
}
s1=1;s2=1;nG=0;
}
//=====================================
void main()
{
int16 len;
uchar hh,hl,mh,ml;
McuInit();
GRE=1;
AT24C64_R(tab,0x00,96);
TR0=0;
//TR1=1;
EA=0;
while(1)
{
nG=0;
//======================================
len=0x400;
AT24C64_R(tab,0x00,96);//欢迎您
while(len>0){XianShiX();len--;}
//======================================
len=0x400;
AT24C64_R(tab,0x60,96);//欢迎光临
while(len>0){XianShiX();len--;}
//======================================
len=0x400;
AT24C64_R(tab,0xc0,96); //出入平安
while(len>0){XianShiX();len--;}
//======================================
hl=read_time(0x85);
hh=hl>>4;
hl=hl&0x0f;
ml=read_time(0x83);
mh=ml>>4;
ml=ml&0x0f;
tab_time(hh,hl,mh,ml); //查表并设置时间
len=0x400;
while(len>0){XianShiX();len--;}
//======================================
len=0x400;
AT24C64_R(tab,0x120,96);//温世坚
while(len>0){XianShiX();len--;}
AT24C64_R(tab,0x00,96);
}
}
void McuInit(void)
{
s1=0;s2=0;nG=1;
EA=0;
LED1=0;
LED2=0;
GRE=0;
delay(250);
LED1=1;
LED2=0;
GRE=0;
delay(100);
LED1=0;
LED2=1;
GRE=0;
delay(90);
LED1=0;
LED2=0;
GRE=1;
delay(60);
LED1=1;
LED2=0;
GRE=1;
delay(40);
LED1=1;
LED2=1;
GRE=0;
//========================
delay(20);
LED1=0;
LED2=1;
GRE=0;
delay(20);
LED1=1;
LED2=0;
GRE=0;
delay(20);
LED1=0;
LED2=0;
GRE=1;
delay(20);
LED1=1;
LED2=1;
GRE=0;
delay(20);
LED1=1;
LED2=0;
GRE=1;
delay(20);
LED1=0;
LED2=1;
GRE=1;
delay(20);
LED1=1;
LED2=1;
GRE=1;
delay(20);
LED1=0;
LED2=0;
GRE=0;
ET0 = 0; //允许定时器0中断
TMOD = 0X01; //定时器工作方式选择
TL0 = 0x00;
TH0 = 0x00; //定时器赋予初值
TR0 = 0; //启动定时器
TR1=0;
EX0=0;
EX1=0;
EA =0 ; //开总中断
nG=0;
}
void tab_time(uchar hh,uchar hl,uchar mh,uchar ml)
{
uchar code timer[10][16]=
{//数字0-9的点阵表
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"0",0*/
{0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00},/*"1",1*/
{0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00},/*"2",2*/
{0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00},/*"3",3*/
{0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00},/*"4",4*/
{0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00},/*"5",5*/
{0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"6",6*/
{0x00,0x00,0x00,0x7E,0x44,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00},/*"7",7*/
{0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00},/*"8",8*/
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00}/*"9",9*/
};
uchar xdata ttt[6][16]=
{ //00:00 时间显示形式
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"0",0*/
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"0",1*/
{0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00},/*":",2*/
{0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00},/*":",3*/
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"0",4*/
{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00}/*"0",5*/
};
uchar i,j;
for(i=0;i<16;i++)
{
ttt[0][i]=timer[hh][i];
ttt[1][i]=timer[hl][i];
ttt[4][i]=timer[mh][i];
ttt[5][i]=timer[ml][i];
}
for(i=0;i<6;i++)
for(j=0;j<16;j++)
tab[j][i]=ttt[i][j];
}
//====================================================================================================
//--------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------
名称:read_time
参数:指令码com
功能:读取年、月、日、星期、时、分、秒的函数
--------------------------------------------------------------------------------*/
uchar read_time(uchar com){
uchar i;
temp=com;
SCLK=0;
RESET=0;
RESET=1;
for(i=0;i<8;i++){
IO=bit0;
//_nop_();_nop_();
SCLK=1;
//_nop_();
SCLK=0;
temp>>=1;
}
temp=0;
IO=1;//置1再读
bit7=IO;
for(i=0;i<7;i++){
temp>>=1;
SCLK=1;
SCLK=0;
bit7=IO;
}
RESET=0;
SCLK=1;
return temp;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?