📄 tian.c
字号:
#include <reg51.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit addo=P1^2;//数据线
sbit adcs=P1^0;//片选线
sbit adclk=P1^1;//时钟线
sbit I2C_SCK=P1^5;//时钟线
sbit I2C_SDA=P1^6;//数据线
//键盘键码对应表
uchar code number[]={0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88};
//uchar code number1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar j,str[10],str1[10];
/*********************************************************
tcl8031 子程序
**********************************************************/
void Delay(uchar x) //延迟
{
uchar i;
for(i=0;i<x;i++)
{
}
}
void Adclk() //下降沿
{
adclk=1;
Delay(2);
adclk=0;
Delay(2);
}
uchar ReadAd(void) //取数
{
uchar i;
uchar str;
adcs=0;
Adclk();
str=0;
// while(1)
// {
// if(addo==0)
// break;
// }
for(i=0;i<8;i++)
{
Adclk();
str<<=1;
str=str|addo;
}
adcs=1;
return str;
}
/************************************************************
24c64子程序
**************************************************************/
bit I2C_Start(void);
void I2C_Stop(void);
void I2C_Ack(void);
void I2C_Nack(void);
bit I2C_Send_Byte( uchar);
uchar I2C_Receive_Byte(void);
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count);
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count);
void Delay_10_uS(void) //延迟
{
char i=10;
while(i--);
}
void Delay_N_mS( uint n_milisecond) /* 延迟 n delay */
{
uchar i;
while(n_milisecond--)
{
i=37;
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_Ack(void)
{
Delay_10_uS();
I2C_SDA=0;
Delay_10_uS();
I2C_SCK=1;
Delay_10_uS();
I2C_SCK=0;
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_W(void *mcu_address,uint AT24C64_address,uint count)
{
// DOG_WDI=!DOG_WDI;
// DOGTIME=0;
while(count--)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
I2C_Send_Byte( AT24C64_address/256 );
I2C_Send_Byte( AT24C64_address %256 );
I2C_Send_Byte( *(uchar*)mcu_address );
I2C_Stop();
Delay_N_mS(10); /* waiting for write cycle to be completed */
((uchar*)mcu_address)++;
AT24C64_address++;
}
}
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
// DOG_WDI=!DOG_WDI;
// DOGTIME=0;
while(count--)
{
I2C_Start();
/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/ /* 24C16 USE */
I2C_Send_Byte( 0xa0 );
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 );
*(uchar*)mcu_address = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
((uchar*)mcu_address)++;
AT24C64_address++;
}
}
/*uchar str[20]={10,9,8,7,6,5,4,3,2,0x1};
char str2[20];
main()
{
// uchar i;
AT24C64_W(str,0,10);
while(1)
AT24C64_R(str2,0,10);
P1=0;
}*/
/************************************************
键盘子程序
***********************************************/
void dlms(void); // delay time
uchar kbscan(void);
void dlms(void)
{
uchar i;
for(i=200;i>0;i--){}
}
uchar ckbscan(void) //select the key
{
uchar sccode,recode,i,a;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{ dlms();
if((P1&0xf0)!=0xf0)
{ sccode=0xfe;
while((sccode&0x10)!=0)
{P1=sccode;
if((P1&0xf0)!=0xf0)
{ a=P1;
i=a&0xf0;
recode=i|0x0f;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return(0);
}
/**************************************
功能函数
*******************************************/
deroutput() //直接输出
{uchar i,j;
while(1)
{
P1=0x00;
for(i=0;i<10;i++)
str[i]= ReadAd();
AT24C64_W(str,0x00,10);
AT24C64_R(str1,0x00,10);
for(i=0;i<10;i++)
P0=str1[i];
j=ckbscan();
if(j==0x18) break;
}
}
/*storage() //存储子函数
{
uchar i;
for(j=0x00;;)
{
for(i=0;i<10;i++)
str[i]= ReadAd();
AT24C64_W(str,j,10);
j+=0x0a;
if(ckbscan()==0x18) break;
}
}
show() //延迟转换
{uchar i;
i=0x00;
while(1)
{
AT24C64_R(str1,i,10);
for(i=0;i<10;i++)
P0=str1[i];
i+=0xa0;
if(i>(j-0x01))
{ while(1)
{
if(ckbscan()==0x18)break;
}
break;
}
}
}
*/
//主程序
main()
{ uchar i;
IE=0x00;
SP=0x60;
P3=0x00;
while(1)
{i=ckbscan();
if(i==0x11) i=1;
if(i==0x14) i=2;
if(i==0x18) i=3;
if(i==0x12) i=4;
switch(i){
case 1: deroutput(); break; //开始取数
// case 2: storage(); break; //存储
case 3: break; //结束
//case 4: show() ;break; //过去显示
default: break;
}
if(i==3) break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -