找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2194|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

想請(qǐng)教大神 這個(gè)程序是什么意思

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:188684 發(fā)表于 2017-6-8 19:02 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
想請(qǐng)教大神 這個(gè)程序是什么意思
新建 Microsoft Office Word 2007 文檔.docx (14.02 KB, 下載次數(shù): 4)
  1. #include <reg51.h>

  2. typedef unsigned char uchar;
  3. typedef unsigned int uint;

  4. #define write_c02 0xa0
  5. #define read_c02 0xa1

  6. sbit sda = P2^0;
  7. sbit scl = P2^1;

  8. void delay()
  9. {
  10. //delay:5us
  11. ;;
  12. }


  13. //i2c:init
  14. void i2c_init()
  15. {
  16. sda = 1;
  17. delay();
  18. scl = 1;
  19. delay();
  20. }


  21. //delayms:
  22. void delayms(uint xms)
  23. {
  24. uchar x, y;
  25. for(x = xms; x > 0; x--)
  26.    for(y = 110; y > 0; y--);
  27. }

  28. //start:
  29. void start() //啟動(dòng)i2c
  30. {
  31. sda = 1;
  32. scl = 1;
  33. delay();//延時(shí)必須大于4.7us,此約為五微秒
  34. sda = 0; //在scl為高電平時(shí),sda一個(gè)下降沿為啟動(dòng)信號(hào)
  35. delay();
  36. }

  37. //stop:
  38. void stop() //停止i2c
  39. {
  40. sda = 0;
  41. scl = 1;
  42. delay();
  43. sda = 1; //在scl為高電平時(shí),sdasda一個(gè)上升沿為停止信號(hào)
  44. delay();
  45. }

  46. //ack:
  47. void ack() //應(yīng)答信號(hào)0
  48. {
  49. uchar i = 0; //等待變量
  50. scl = 1;//在scl為高電平期間等待應(yīng)答
  51. delay();
  52. while((sda == 1) && i < 250)//若為應(yīng)答0即退出,從機(jī)向主機(jī)發(fā)送應(yīng)答信號(hào)
  53. i++;//等待一段時(shí)間
  54. scl = 0; //應(yīng)答之后將scl拉低
  55. delay();
  56. }

  57. //nack:
  58. void nack() //非應(yīng)答
  59. {
  60. scl = 1;//在scl為高電平期間,由主機(jī)向從機(jī)發(fā)送一個(gè)1,非應(yīng)答信號(hào)
  61. delay();
  62. sda = 1;
  63. scl = 0; //應(yīng)答之后將scl拉低
  64. delay();
  65. }

  66. //send byte:
  67. void send_byte(uchar date)//寫(xiě)一個(gè)字節(jié)
  68. {
  69. uchar i, temp;
  70. temp = date; //存入要寫(xiě)入的數(shù)據(jù),即要發(fā)送到sda上的數(shù)據(jù)
  71. for(i = 0; i < 8; i++)
  72. { //發(fā)送8位
  73.   temp <<= 1; //to CY 將數(shù)據(jù)的最高位移入到PSW中的CY位中
  74.   scl = 0;//只有在scl為低電平時(shí),才允許sda上的數(shù)據(jù)變化
  75.   delay();
  76.   sda = CY; //將CY里的數(shù)據(jù)發(fā)送到sda數(shù)據(jù)線上
  77.   delay(); //可以延時(shí)
  78.   scl = 1; //在scl為高電平時(shí),不允許sda上的數(shù)據(jù)變化,使數(shù)據(jù)穩(wěn)定
  79.   delay();
  80.   scl = 0;//允許sda數(shù)據(jù)線的數(shù)據(jù)變化,等待下一個(gè)數(shù)據(jù)的傳輸
  81.   delay();
  82. }
  83. //wait ack:發(fā)送完一個(gè)字節(jié)數(shù)據(jù)后要主機(jī)要等待從機(jī)的應(yīng)答,第九位
  84. scl = 0;//允許sda變化
  85. delay();
  86. sda = 1;//wait:ack,sda拉高等待應(yīng)答,當(dāng)sda=0時(shí),表示從機(jī)的應(yīng)答
  87. delay();
  88. }

  89. //read: byte
  90. uchar read_byte() //讀一個(gè)字節(jié)數(shù)據(jù)
  91. {
  92. uchar i, j, k;
  93. scl = 0; //讀之前先允許sda變化
  94. delay(); //等待數(shù)據(jù)
  95. for(i = 0; i < 8; i++)
  96. {
  97.   scl = 1; //不允許sda變化
  98.   delay(); //使sda數(shù)據(jù)穩(wěn)定后開(kāi)始讀數(shù)據(jù)
  99.   j = sda; //讀出sda上的數(shù)據(jù)
  100.   k = (k << 1)| j; //將數(shù)據(jù)通過(guò)|運(yùn)算存入k中
  101.   scl = 0;//允許sda變化等待下一位數(shù)據(jù)的到來(lái)
  102.   delay();
  103. }
  104. //delay();//可不用延時(shí)
  105. return k;//返回讀出的數(shù)據(jù)
  106. }

  107. //write:at24c02  在at24c02中的指定地址寫(xiě)入數(shù)據(jù)
  108. void write_at24c02(uchar address, uchar date)
  109. {
  110. start(); //啟動(dòng)i2c
  111. send_byte(write_c02);//寫(xiě)入期間地址和寫(xiě)操作
  112. ack(); //從機(jī)應(yīng)答0
  113. send_byte(address); //寫(xiě)入寫(xiě)數(shù)據(jù)的單元地址
  114. ack(); //ack0
  115. send_byte(date); //在指定地址中寫(xiě)入數(shù)據(jù)
  116. ack(); //從機(jī)應(yīng)答0
  117. stop();    //停止i2c
  118. }

  119. //read: at24c02   在at24c02的指定地址中讀出寫(xiě)入的數(shù)據(jù)
  120. uchar read_at24c02(address)
  121. {
  122. uchar dat;//用來(lái)存儲(chǔ)讀出的數(shù)據(jù)
  123. start(); //啟動(dòng)i2c
  124. send_byte(write_c02); //寫(xiě)入at24c02器件地址和寫(xiě)操作
  125. ack(); //從機(jī)應(yīng)答0
  126. send_byte(address); //寫(xiě)入要讀取AT24C02的數(shù)據(jù)的單元地址
  127. ack(); //從機(jī)應(yīng)答0
  128. start();  //再次啟動(dòng)i2c
  129. send_byte(read_c02); //寫(xiě)入AT24C02器件地址和讀操作
  130. ack();//從機(jī)應(yīng)答‘0’
  131. dat = read_byte();//讀出指定地址中的數(shù)據(jù)
  132. nack(); //主機(jī)發(fā)出非應(yīng)答‘1’
  133. stop();  //停止i2c
  134. return dat;    //返回讀出的數(shù)據(jù)
  135. }

  136. //main:
  137. void main()
  138. {
  139. uchar i;
  140. i2c_init();
  141. start();
  142. while(1)
  143. {
  144.   for(i = 0x00; i < 0xff; i++)
  145.   {
  146.    write_at24c02(10, i);
  147.    delayms(10);//需等待十毫秒
  148.    P1 = read_at24c02(10);//1010 1010
  149.    delayms(2000);
  150.   }
  151. }
  152. }
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:206560 發(fā)表于 2017-6-9 04:07 | 只看該作者
軟件模擬I2C通信,與AT24C02連接,讀寫(xiě)數(shù)據(jù)。
回復(fù)

使用道具 舉報(bào)

板凳
ID:164602 發(fā)表于 2017-6-9 08:20 | 只看該作者
這個(gè)程序,就是對(duì)24C02進(jìn)行讀寫(xiě),并顯示讀出的內(nèi)容。
前面的子函數(shù),都是IIC(讀用I方C,即I平方的意思)通訊的,如:初始化IIC,IIC開(kāi)始、停止,應(yīng)答,讀寫(xiě)等。
主函數(shù)內(nèi)容:
(1)先初始化IIC
(2)然后循環(huán)進(jìn)行對(duì)24C02位地址10的寫(xiě)和讀,讀出的值在P1口顯示(可能是數(shù)碼管)。
程序每次只寫(xiě)讀一位(不要被i變量從0x00到0xff騙了),因?yàn)?4C02是位存貯的。
回復(fù)

使用道具 舉報(bào)

地板
ID:7485 發(fā)表于 2017-6-9 10:04 | 只看該作者
你不知道是什么程序,怎么會(huì)用到它呢?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表