微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > IIC总线读写程序

IIC总线读写程序

时间:10-02 整理:3721RD 点击:
利用定时器产生一个0~99秒变化的秒表,并且显示在数码管上,没过一秒将这个变化的数写入板上AT24C02内部。当关闭实验板电源,并再次打开实验板电源时,单片机先从AT24C02中将原来写入的数读取出来,接着此数继续变化并显示在数码管上。

  1. #include <reg52.h>

  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. bit write = 0;
  5. sbit sda = P0^1;
  6. sbit scl = P0^0;
  7. sbit wp = P0^2;
  8. sbit dula = P3^4;
  9. sbit wela = P3^5;

  10. uchar sec, tcnt;

  11. uchar code table[] = {
  12. 0x3f, 0x06, 0x5b, 0x4f,
  13. 0x66, 0x6d, 0x7d, 0x07,
  14. 0x7f, 0x6f, 0x77, 0x7c,
  15. 0x39, 0x5e, 0x79, 0x71};

  16. void delay()
  17. {;;}

  18. void delay1ms(uint z)
  19. {
  20.     uint x, y;
  21.     for(x=z; x>0; x--)
  22.         for(y=110; y>0; y--);
  23. }

  24. void start()
  25. {
  26.     sda = 1;
  27.     delay();
  28.     scl = 1;
  29.     delay();
  30.     sda = 0;
  31.     delay();
  32. }

  33. void stop()
  34. {
  35.     sda = 0;
  36.     delay();
  37.     scl = 1;
  38.     delay();
  39.     sda = 1;
  40.     delay();
  41. }

  42. void response()
  43. {
  44.     uchar i;
  45.     scl = 1;
  46.     delay();
  47.     while((sda == 1) && (i<250)) i++;
  48.     scl = 0;
  49.     delay();
  50. }

  51. void init()
  52. {
  53.     sda = 1;
  54.     delay();
  55.     scl = 1;
  56.     delay();
  57.     wp = 0;
  58. }

  59. void write_byte(uchar date)
  60. {

  61.     uchar i, temp;
  62.     temp = date;
  63.     for(i=0; i<8; i++)
  64.     {   
  65.         temp = temp << 1;
  66.         scl = 0;
  67.         delay();
  68.         sda = CY;
  69.         delay();
  70.         scl = 1;
  71.         delay();
  72.     }
  73.     scl = 0;
  74.     delay();
  75.     sda = 1;
  76.     delay();
  77. }

  78. uchar read_byte()
  79. {
  80.     uchar i, k;
  81.     scl = 0;
  82.     delay();
  83.     sda = 1;
  84.     delay();
  85.     for(i=0; i<8; i++)
  86.     {
  87.         scl = 1;
  88.         delay();
  89.         k = (k<<1) | sda;
  90.         scl = 0;
  91.         delay();
  92.     }
  93.     return k;
  94. }

  95. void write_add(uchar address, uchar date)
  96. {
  97.     start();
  98.     write_byte(0xae);
  99.     response();
  100.     write_byte(address);
  101.     response();
  102.     write_byte(date);
  103.     response();
  104.     stop();
  105. }

  106. uchar read_add(uchar address)
  107. {
  108.     uchar date;
  109.     start();
  110.     write_byte(0xae);
  111.     response();
  112.     write_byte(address);
  113.     response();
  114.     start();
  115.     write_byte(0xaf);
  116.     response();
  117.     date = read_byte();
  118.     stop();
  119.     return date;
  120. }

  121. void display(uchar bai_c, uchar sh_c)
  122. {
  123. /*    dula = 1;
  124.     P1 = table[bai_c];
  125.     dula = 0;
  126.     P1 = 0xff;
  127.     wela = 1;
  128.     P1 = 0xfe;
  129.     wela = 0;
  130.     delay1ms(5);
  131.    
  132.     P1 = 0x00;
  133.     dula = 1;
  134.     dula = 0;
  135.    
  136.     dula = 1;
  137.     P1 = table[sh_c];
  138.     dula = 0;
  139.     P1 = 0xff;
  140.     wela = 1;
  141.     P1 = 0xfd;
  142.     wela = 0;
  143.     delay1ms(5);
  144.    
  145.     P1 = 0x00;
  146.     dula = 1;
  147.     dula = 0;*/
  148.    
  149.     P1 = 0xff;
  150.     wela = 1;
  151.     P1 = 0xfe;
  152.     wela = 0;
  153.     P1 = 0x00;
  154.     dula = 1;
  155.     P1 = table[bai_c];
  156.     dula = 0;
  157.     delay1ms(5);
  158.    
  159.     P1 = 0x00;
  160.     dula = 1;
  161.     dula = 0;
  162.    
  163.     P1 = 0xff;
  164.     wela = 1;
  165.     P1 =0xfd;
  166.     wela = 0;
  167.     P1 = 0x00;
  168.     dula = 1;
  169.     P1 = table[sh_c];
  170.     dula = 0;
  171.     delay1ms(5);
  172.    
  173.     P1 = 0x00;
  174.     dula = 1;
  175.     dula = 0;
  176. }

  177. void main()
  178. {
  179.     init();
  180.     sec = read_add(2);
  181.     if(sec>100) sec = 0;
  182.     TMOD = 0x01;
  183.     EA = 1;
  184.     ET0 = 1;
  185.     TH0 = 15536/256;
  186.     TL0 = 15536%256;
  187.     TR0 = 1;
  188.     while(1)
  189.     {
  190.         display(sec/10, sec%10);
  191.         if(write == 1)
  192.         {
  193.             write = 0;
  194.             write_add(2, sec);
  195.         }
  196.     }
  197. }

  198. void t0() interrupt 1
  199. {
  200.     TH0 = 15536/256;
  201.     TL0 = 15536%256;
  202.     tcnt++;
  203.     if(tcnt == 20)
  204.     {
  205.         tcnt = 0;
  206.         sec++;
  207.         write = 1;
  208.         if(sec == 100) sec = 0;
  209.     }
  210. }

复制代码



完整程序吗

你这也不全呀

软件模拟的,移植方便。

仿真一下

是完整的,但要根据具体开发板修改一下。抱歉一直没回复,好久没有上过论坛了。

是完整的,但要根据具体开发板修改一下。抱歉一直没回复,好久没有上过论坛了。

学习学习,谢谢分享

郭天祥的程序?

没错,就是他的程序

谢谢

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top