?? hygs_v4.0_test.c
字號:
#include <AT89X52.h>
#include <math.h>
#include <intrins.h>
#define CLK P1_4
#define DIN P1_3
#define SCL P1_1
#define SDA P1_0
unsigned char XSCS=0,XSCS1=0;
unsigned char xsdh;
bit temp0,temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp10;
bit temp0_0,temp0_1,temp0_2,temp0_3,temp0_4,temp0_5,temp0_6,temp0_7,temp0_10;
bit temp1_0,temp1_1,temp1_2,temp1_3,temp1_4,temp1_5,temp1_6,temp1_7,temp1_10;
//bit temp_aj1,temp_aj2;
//bit temp1_aj1,temp1_aj2;
//bit temp2_aj1,temp2_aj2;
void display(void);
void DELAY(unsigned int dltime);
void timer0(void);
void i2c_write(unsigned char,unsigned char);
void i2c_send8bit(unsigned char);
unsigned char i2c_read(unsigned char);
unsigned char i2c_receive8bit(void);
void i2c_start(void);
void i2c_stop(void);
bit i2c_ack(void);
void jidianqi(void);
void display(void)//將DISP1改為dispaly
{
unsigned char i=0;
P3_6=0;//顯示用八上升沿D觸發器的時鐘輸入端
if (P3_5==0) //如果有鍵按下,則使所有LED點亮,這里是按鍵2
xsdh=0xff;//dispout,xsdh是如何跟硬件聯系的?它們是間接聯系的,
else if (XSCS==0)//實質是通過后面的while語句控制LED的移位
xsdh=0x01;
else if (XSCS==1)
xsdh=0x02;
else if (XSCS==2)
xsdh=0x04;
else if (XSCS==3)
xsdh=0x08;
else if (XSCS==4)
xsdh=0x10;
else if (XSCS==5)
xsdh=0x20;
else if (XSCS==6)
xsdh=0x40;
else if (XSCS==7)
xsdh=0x80;
else
xsdh=0;
i=8;
while(i>0)
{
CLK=0;
DELAY(3);
DELAY(20);
if((xsdh&0x01)==0)
DIN=0;
else
DIN=1;
CLK=1;
DELAY(3);
DELAY(20);
i--;
xsdh=xsdh>>=1;//右移是干嗎?右移其實就是實現了外部LED的移位顯示
}
P3_6=1;
}
void DELAY(unsigned int dltime)
{
unsigned int m;
for(m=0;m<dltime;m++)
{
;
}
}
void timer0(void) interrupt 1 //中斷源為定時器0
{
TL0=0X58;//定時器的定時時間為50ms
TH0=0X9e;
if (XSCS1<20)
XSCS1++;
else XSCS1=0;
if (XSCS1==3)
{
if (XSCS<16)
XSCS++;
else XSCS=0;
}
if (P3_4==0) //如果有鍵按下,則使所有LED點亮,這里是按鍵1*******
{ //為什么對按鍵2的寫法和按鍵1的寫法不一樣?因為按鍵1是通過單片機
SBUF=0xff; //內部專用的串行口進行移位控制的
while (TI==0)
;
}
else if (XSCS==8)
{
SBUF=0x1;
while (TI==0)
;
}
else if (XSCS==9)
{
SBUF=0x2;
while (TI==0)
;
}
else if (XSCS==10)
{
SBUF=0x4;
while (TI==0)
;
}
else if (XSCS==11)
{
SBUF=0x8;
while (TI==0)
;
}
else if (XSCS==12)
{
SBUF=0x10;
while (TI==0)
;
}
else if (XSCS==13)
{
SBUF=0x20;
while (TI==0)
;
}
else if (XSCS==14)
{
SBUF=0x40;
while (TI==0)
;
}
else if (XSCS==15)
{
SBUF=0x80;
while (TI==0)
;
}
else SBUF=0;
}
void i2c_write(unsigned char Address,unsigned char Data)
{
do
{
i2c_start();//發送開始信號
i2c_send8bit(0xA0); //送8位數據,A0表示寫
} while(i2c_ack()); //當有發送接收確認信號時
i2c_send8bit(Address); // Address=0x00
i2c_ack();
i2c_send8bit(Data);
i2c_ack();
i2c_stop();
return;
}
unsigned char i2c_read(unsigned char Address)
{
unsigned char c;
do
{
i2c_start();
i2c_send8bit(0xA0);
}while(i2c_ack()); //=1,表示無確認,再次發送
i2c_send8bit(Address);
i2c_ack();
do
{
i2c_start();
i2c_send8bit(0xA1);
}while(i2c_ack());
c=i2c_receive8bit();
i2c_ack();
i2c_stop();
return(c);
}
//發送開始信號
void i2c_start(void)
{
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0; //SCL下降信號比SDA來的晚一些
return;
}
//發送結束信號
void i2c_stop(void)
{
SDA = 0;
SCL = 1;
SDA = 1;
return;
}
//發送接收確認信號
bit i2c_ack(void)
{
bit ack;
SDA = 1;
SCL = 1;
if (SDA==1)
ack = 1;
else
ack = 0;
SCL = 0;
return (ack);//返回ack值為1說明已確認
}
//送八位數據
void i2c_send8bit(unsigned char b)
{
unsigned char a;
for(a=0;a<8;a++)
{
if ((b << a ) & 0x80)
SDA = 1;
else
SDA = 0;
SCL = 1;
SCL = 0;
}
return;
}
//接收八位數據
unsigned char i2c_receive8bit(void)
{
unsigned char a;
unsigned char b=0;
for(a=0;a<8;a++)
{
SCL = 1;
b=b<<1;
if (SDA==1)
b=b|0x01; //按位或
SCL = 0;
}
return (b);
}
void jidianqi(void)
{
temp0=P2_0;
temp1=P2_1;
temp2=P2_2;
temp3=P2_3;
temp4=P2_4;
temp5=P2_5;
temp6=P2_6;
temp7=P2_7;
temp10=P3_2;
DELAY(200);
temp0_0=P2_0;
temp0_1=P2_1;
temp0_2=P2_2;
temp0_3=P2_3;
temp0_4=P2_4;
temp0_5=P2_5;
temp0_6=P2_6;
temp0_7=P2_7;
temp0_10=P3_2;
DELAY(2000);
temp1_0=P2_0;
temp1_1=P2_1;
temp1_2=P2_2;
temp1_3=P2_3;
temp1_4=P2_4;
temp1_5=P2_5;
temp1_6=P2_6;
temp1_7=P2_7;
temp1_10=P3_2;
DELAY(2000);
P3_7=0;
DELAY(200);
if ((temp0==temp0_0)&&(temp0==temp1_0)
&&(temp1==temp0_1)&&(temp1==temp1_1)
&&(temp2==temp0_2)&&(temp2==temp1_2)
&&(temp3==temp0_3)&&(temp3==temp1_3)
&&(temp4==temp0_4)&&(temp4==temp1_4)
&&(temp5==temp0_5)&&(temp5==temp1_5)
&&(temp6==temp0_6)&&(temp6==temp1_6)
&&(temp7==temp0_7)&&(temp7==temp1_7)
)
{
P0_4=temp0;
P0_6=temp1;
P0_5=temp2;
P0_3=temp3;
P0_2=temp4;
P0_7=temp5;
P0_1=temp6;
P0_0=temp7;
if (temp10==0x00)
{
P0=0x00;
P3_7=0;
DELAY(2000);
P3_7=1;
display();
}
}
P3_7=1;
display();
}
//$$$$$$$$$$$$$$$$主程序$$$$$$$$$$$$$$$
void main(void)
{
char lunliu;
TMOD=0x01;//選擇定時器T0方式1
TL0=0xAF;//延時0.1秒
TH0=0x3C;
EA=1;
ET0=1;
TR0=1;
P0=0x00;
SCON=0x00;
while (1)
{
jidianqi();
//如果按鍵1和按鍵2一起按下,就對EEPROM進行寫和讀的操作,
//并通過8個繼電器的輪流閉開一次體現
if ((P3_4==0x00)&&(P3_5==0x00))//EEPROM的寫入不能太頻繁,當有按鍵按下時才寫入數據
{
unsigned char i;
for (i=1;i>0;i--)
{
i2c_write(0x00,0x66);
_nop_();
lunliu=i2c_read(0x00);
}
if (lunliu==0x66)
{
P0=0x00;
DELAY(1000);
P3_7=0;
DELAY(1000);
P3_7=1;
display();
DELAY(38000);
P0=0xff;
DELAY(1000);
P3_7=0;
DELAY(1000);
P3_7=1;
display();
DELAY(38000);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -