lamp.c
上传用户:dlknorr
上传日期:2019-12-31
资源大小:27k
文件大小:5k
- #include "reg51.h"
- #include "stdio.h"
- #include "string.h"
- #define SER0_READ_BUF_SIZE 10
- unsigned char SER0_READ_BUF[SER0_READ_BUF_SIZE];
- int SER0_HEAD_PTR;
- int SER0_END_PTR;
- int SER0_BUF_LEN;
- unsigned char ser_buf[5];
- sbit watchdog=P3^3;
- sbit SCL=P3^5;
- sbit SDA=P3^4;
- void communication(); //processing received data
- void init();
- void senddata();
- int SerRead(unsigned char *s,int maxlength,unsigned int timeout);
- void Ser0Putc(char SER0_SEND);
- void Ser0Write(char *SER0_W_PTR,SER0_W_LEN);
- int Ser0Getc();
- //延时1.0058毫秒
- void delay(unsigned int i)
- {
- unsigned int j;
- for(j=0;j<=i;j++);
- }
- void writex(unsigned char k)
- {
- unsigned char k1,temp;
- temp=k;
- for(k1=0;k1<=7;k1++)
- {
- SCL=0;delay(1);
- temp=temp<<1;
- SDA=CY;
- delay(1);
- SCL=1;
- delay(1);
- }
- }
- void begin(void)
- {
- SCL=0;
- delay(2000);
- SDA=1; delay(1);
- SCL=1; delay(1);
- SDA=0; delay(1);
- }
- void over(void)
- {
- SCL=0; delay(1);
- SDA=0; delay(1);
- SCL=1; delay(1);
- SDA=1; delay(1);
- }
- void ack(void)
- {
- SCL=0;
- delay(1);
- SDA=1;
- SCL=1;
- delay(1);
- }
- void writebyte(unsigned char address,unsigned char dat)
- {
- unsigned char i;
- for(i=0;i<2;i++)
- {
- begin();
- writex(0xa0);
- ack();
- writex(address);
- ack();
- writex(dat);
- ack();
- over();
- }
- }
- unsigned char readbyte(unsigned char address)
- { unsigned char k;
- unsigned char nn,tmp;
-
- begin();
- writex(0xa0);
- ack();
- writex(address);
- ack();
- begin();
- writex(0xa1);
- ack();
- for(k=0;k<=7;k++)
- {
- SCL=0;
- delay(1);
- if(SDA==1) nn=1;
- else nn=0;
- tmp=tmp<<1;
- tmp=tmp+nn;
- SCL=1;
- delay(1);
- }
- SDA=1;
- delay(1);
- SCL=1;
- delay(1);
- SCL=0;
- over();
-
- return(tmp);
- }
- void delayms(int MS)
- {
- int i,j;
- for(i=1;i<=MS;i++) for(j=0;j<=113;j++) {watchdog=~watchdog;}
- }
- void init()
- {
- P0=readbyte(0x10);
- P1=readbyte(0x11);
- P2=readbyte(0x12);
-
- IE=0X90; //allow to serials and counter and *************int0 interrupt
- SCON=0X50; //mode1
- TMOD=0X21; //16bit counter 021
- TH1=0Xfd; //set the baudrate to 9600 E8
- TR1=1;
- }
- main()
- {
- init();
-
- Ser0Write("Start",5);
- while(1)
- {
- communication();
- delayms(20);
- }
- }
- void communication()
- {
- int rcv,checksum;
- rcv=SerRead(ser_buf,5,20);
- if(rcv==5)
- {
- checksum=ser_buf[0]+ser_buf[1]+ser_buf[2]+ser_buf[3];
- if(checksum=ser_buf[4])
- {
- if(ser_buf[0]=='W')
- {
- P0=ser_buf[1];
- P1=ser_buf[2];
- P2=ser_buf[3];
- //save it to 24C01
- writebyte(0x10,ser_buf[1]);
- writebyte(0x11,ser_buf[2]);
- writebyte(0x12,ser_buf[3]);
- Ser0Write(ser_buf,5);
- }
- else if(ser_buf[0]=='R')
- {
- ser_buf[0]='R';
- ser_buf[1]=P0;
- ser_buf[2]=P1;
- ser_buf[3]=P2;
- Ser0Write(ser_buf,5);
- }
- }
- }
- }
- int SerRead(unsigned char *s,int maxlength,unsigned int timeout)
- {
- int receive;
- unsigned int i,receive_length,j;
- receive_length=0;
- for(i=0;i<maxlength;i++)
- {
- for(j=0;j<timeout;j++)
- {
- receive=Ser0Getc();
- if(receive!=-1) break;
- delayms(1);
- }
- if(j>=timeout)
- {
- return -1;
- }
- s[receive_length++]=receive;
- }
- return maxlength;
- }
- int Ser0Getc()
- {
- int result;
- if(SER0_BUF_LEN==0) result=-1;
- else
- {
- result=SER0_READ_BUF[SER0_END_PTR++];
- SER0_BUF_LEN--;
- if(SER0_END_PTR>=SER0_READ_BUF_SIZE) SER0_END_PTR=0;
- }
- return result;
- }
- void Ser0Write(char *SER0_W_PTR,SER0_W_LEN)
- {
- int i;
- for(i=0;i<SER0_W_LEN;i++)
- {
- SBUF=SER0_W_PTR[i];
- while(TI==0);
- TI=0;
- }
- }
- //------------------------------------------------------------------------
- // UART0中断服务子程序
- //------------------------------------------------------------------------
- void UART0_int (void) interrupt 4 using 3
- {
- EA=0;
- if (TI==1)
- {
- // TI0=0; //清除发送中断标志
- }
-
- if (RI==1)
- {
- RI=0; //清除接收中断标志
- SER0_READ_BUF[SER0_HEAD_PTR++] = SBUF;
- SER0_BUF_LEN++;
- if(SER0_HEAD_PTR>=SER0_READ_BUF_SIZE) SER0_HEAD_PTR=0;
- if(SER0_BUF_LEN>=SER0_READ_BUF_SIZE) SER0_BUF_LEN=0;
- }
- EA=1;
- }