lamp.c
上传用户:dlknorr
上传日期:2019-12-31
资源大小:27k
文件大小:5k
源码类别:

单片机开发

开发平台:

C/C++

  1. #include "reg51.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4. #define SER0_READ_BUF_SIZE 10
  5. unsigned char  SER0_READ_BUF[SER0_READ_BUF_SIZE];
  6. int SER0_HEAD_PTR;
  7. int SER0_END_PTR;
  8. int SER0_BUF_LEN;
  9. unsigned char ser_buf[5];
  10. sbit watchdog=P3^3;
  11. sbit SCL=P3^5;
  12. sbit SDA=P3^4;
  13. void communication();           //processing  received data
  14. void init();
  15. void senddata();
  16. int  SerRead(unsigned char *s,int maxlength,unsigned int timeout);
  17. void Ser0Putc(char SER0_SEND);
  18. void Ser0Write(char *SER0_W_PTR,SER0_W_LEN);
  19. int  Ser0Getc();
  20. //延时1.0058毫秒
  21. void delay(unsigned int i)
  22. {
  23. unsigned int j;
  24.   for(j=0;j<=i;j++);
  25. }
  26. void writex(unsigned char k)
  27.      {
  28. unsigned char k1,temp;
  29.        temp=k;
  30.        for(k1=0;k1<=7;k1++)
  31.          {
  32. SCL=0;delay(1);
  33.            temp=temp<<1;
  34. SDA=CY;
  35.            delay(1);
  36.            SCL=1; 
  37. delay(1);
  38.  } 
  39.      }
  40. void begin(void)
  41.      {
  42. SCL=0; 
  43. delay(2000);
  44.        SDA=1; delay(1);
  45.        SCL=1; delay(1);
  46.        SDA=0; delay(1);  
  47.      }  
  48. void over(void)
  49.      {
  50. SCL=0;  delay(1);      
  51.        SDA=0;  delay(1);
  52.        SCL=1;  delay(1);
  53.        SDA=1;  delay(1);
  54.      }
  55. void ack(void)     
  56.     {
  57. SCL=0;  
  58. delay(1);
  59.       SDA=1;  
  60.       SCL=1;  
  61. delay(1);
  62. }
  63. void writebyte(unsigned char address,unsigned char dat)
  64.      { 
  65. unsigned char i;
  66.         for(i=0;i<2;i++)
  67.           {
  68. begin();
  69.             writex(0xa0);
  70. ack();
  71.             writex(address);
  72. ack();
  73.             writex(dat);
  74. ack();
  75.             over();
  76.   }
  77.      }
  78. unsigned char readbyte(unsigned char  address)
  79.  { unsigned char  k;
  80.    unsigned char nn,tmp;
  81.       
  82.         begin();
  83.         writex(0xa0);
  84.   ack();
  85.         writex(address);
  86. ack();    
  87.         begin();
  88.         writex(0xa1);
  89.         ack();
  90.         for(k=0;k<=7;k++)
  91.         {  
  92. SCL=0; 
  93. delay(1);
  94.             if(SDA==1) nn=1;
  95.             else nn=0;
  96.             tmp=tmp<<1;
  97.             tmp=tmp+nn;
  98.             SCL=1;
  99. delay(1);
  100.          }
  101.         SDA=1; 
  102. delay(1);
  103.         SCL=1; 
  104. delay(1);
  105.         SCL=0;
  106.         over();
  107.        
  108.         return(tmp);
  109.   }
  110. void delayms(int MS)
  111. {
  112. int i,j;
  113. for(i=1;i<=MS;i++) for(j=0;j<=113;j++) {watchdog=~watchdog;}
  114. }
  115. void init()
  116. {
  117. P0=readbyte(0x10);
  118. P1=readbyte(0x11);
  119. P2=readbyte(0x12);
  120. IE=0X90; //allow to serials and counter and *************int0 interrupt
  121. SCON=0X50; //mode1 
  122. TMOD=0X21; //16bit counter   021
  123. TH1=0Xfd; //set the baudrate to 9600      E8
  124. TR1=1;
  125. }
  126. main()
  127. {
  128. init();
  129.     Ser0Write("Start",5);
  130. while(1)
  131.    {
  132.       communication();
  133.   delayms(20);
  134.    }
  135. }
  136. void communication()
  137. {
  138.    int rcv,checksum;
  139.    rcv=SerRead(ser_buf,5,20);
  140.    if(rcv==5)
  141.    {
  142.   checksum=ser_buf[0]+ser_buf[1]+ser_buf[2]+ser_buf[3];
  143.   if(checksum=ser_buf[4])
  144.   {
  145.         if(ser_buf[0]=='W') 
  146.     {
  147.           P0=ser_buf[1];
  148.       P1=ser_buf[2];
  149.       P2=ser_buf[3];
  150.   //save it to 24C01
  151.   writebyte(0x10,ser_buf[1]);
  152.   writebyte(0x11,ser_buf[2]);
  153.   writebyte(0x12,ser_buf[3]);
  154.           Ser0Write(ser_buf,5);
  155. }
  156.     else if(ser_buf[0]=='R') 
  157.     {
  158.      ser_buf[0]='R';
  159.      ser_buf[1]=P0;
  160.      ser_buf[2]=P1;
  161.      ser_buf[3]=P2;
  162.      Ser0Write(ser_buf,5);
  163.    }
  164.   }
  165.     }
  166. }
  167. int  SerRead(unsigned char *s,int maxlength,unsigned int timeout)
  168. {
  169.   int receive;
  170.   unsigned int i,receive_length,j;  
  171.   receive_length=0;
  172.   for(i=0;i<maxlength;i++)
  173.   {   
  174.     for(j=0;j<timeout;j++) 
  175. {
  176.   receive=Ser0Getc();
  177.   if(receive!=-1) break;
  178.   delayms(1);
  179. }    
  180. if(j>=timeout)
  181. {
  182.   return -1;
  183. }
  184.     s[receive_length++]=receive;
  185.   }
  186.   return maxlength;
  187. }
  188. int  Ser0Getc()
  189. {
  190. int result;
  191. if(SER0_BUF_LEN==0) result=-1;
  192. else
  193. {
  194.       result=SER0_READ_BUF[SER0_END_PTR++];
  195.   SER0_BUF_LEN--;
  196.   if(SER0_END_PTR>=SER0_READ_BUF_SIZE) SER0_END_PTR=0;
  197. }
  198. return result;
  199. }
  200. void Ser0Write(char *SER0_W_PTR,SER0_W_LEN)
  201. {
  202. int i;
  203. for(i=0;i<SER0_W_LEN;i++)
  204. {
  205.   SBUF=SER0_W_PTR[i];
  206.       while(TI==0);
  207.   TI=0;
  208. }
  209. }
  210. //------------------------------------------------------------------------
  211. // UART0中断服务子程序
  212. //------------------------------------------------------------------------
  213. void UART0_int (void) interrupt 4 using 3
  214. {
  215. EA=0;
  216.   if (TI==1)
  217.     { 
  218. // TI0=0;                            //清除发送中断标志
  219.     }
  220.   
  221.   if (RI==1)
  222.     {
  223.     RI=0;                               //清除接收中断标志
  224. SER0_READ_BUF[SER0_HEAD_PTR++] = SBUF;
  225. SER0_BUF_LEN++;
  226. if(SER0_HEAD_PTR>=SER0_READ_BUF_SIZE) SER0_HEAD_PTR=0;
  227. if(SER0_BUF_LEN>=SER0_READ_BUF_SIZE)  SER0_BUF_LEN=0;
  228. }
  229. EA=1;
  230. }