久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5055|回復: 0
打印 上一主題 下一主題
收起左側

STM32驅動LM75A源碼與資料,已測試可以直接運行

[復制鏈接]
跳轉到指定樓層
樓主
LM75A驅動,已測試可以直接運行

模擬I2C控制,LM75A
從上往下一次為
VCC--3.3V---紅色箭頭
SDA--單片機PB7口
SCL-PB6
OS--懸空
GND--地



模塊來自翔寶,引腳上拉電阻已上拉,具體電路自行百度,僅程序。

程序有兩個版本,一個是來自百度搜索移植,GPIO配置為開漏,一個移植原子的模擬I2C,gpio為推挽

LM75A引腳圖與資料下載:


單片機源程序如下:
  1. #include "stm32f10x.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. //#include "myiic.h"
  6. #include "i2c_ee.h"       
  7. u8 addr=0x90;
  8. u8 addr1=0x91;
  9. u8 buff[2]={0};
  10. float data;
  11. int main(void)
  12. {         
  13.        
  14.         delay_init();                     //延時函數初始化          
  15.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置中斷優先級分組為組2:2位搶占優先級,2位響應優先級
  16.         uart_init(115200);                 //串口初始化為115200
  17.         IIC_Init();
  18.        
  19.          
  20.          while(1){
  21.                  
  22.                  printf("%d \r\n",I2C_LM75_Read());
  23.                  data=0.125*(I2C_LM75_Read());
  24.                  printf("%.3f ℃\r\n",data);
  25.                  
  26.                  delay_ms(1000);
  27.                  
  28.          }         
  29.          
  30. }
復制代碼
  1. #ifndef __I2C_EE_C
  2. #define __I2C_EE_C
  3. #endif

  4. #include "i2c_ee.h"
  5. /********************************************************************************
  6. * 使用I2C1讀寫AT24C256,LM75
  7. *******************************************************************************/

  8. //u16 test=0x00e7;

  9. void I2C_delay(void)
  10. {
  11.         u8 i=50;        //這里可以優化速度,經測試最低到5還能寫入
  12.         while(i--);
  13. }

  14. u16 I2C_Start(void)
  15. {
  16.         SDA_H;
  17.         SCL_H;
  18.         I2C_delay();
  19.         if(!SDA_read)return(I2C_TM_ERR);        //SDA線為低電平則總線忙,退出
  20.         SDA_L;
  21.         I2C_delay();
  22.         if(SDA_read) return(I2C_TM_ERR);        //SDA線為高電平則總線出錯,退出
  23.         SCL_L;
  24.         I2C_delay();
  25.         return (I2C_TM_OK);
  26. }

  27. void I2C_Stop(void)
  28. {
  29.         SCL_L;
  30.         I2C_delay();
  31.         SDA_L;
  32.         I2C_delay();
  33.         SCL_H;
  34.         I2C_delay();
  35.         SDA_H;
  36.         I2C_delay();
  37.         SCL_L;
  38.         I2C_delay();
  39. }

  40. void I2C_Ack(void)
  41. {
  42.         SCL_L;
  43.         I2C_delay();
  44.         SDA_L;
  45.         I2C_delay();
  46.         SCL_H;
  47.         I2C_delay();
  48.         SCL_L;
  49.         I2C_delay();
  50. }

  51. void I2C_NoAck(void)
  52. {       
  53.         SCL_L;
  54.         I2C_delay();
  55.         SDA_H;
  56.         I2C_delay();
  57.         SCL_H;
  58.         I2C_delay();
  59.         SCL_L;
  60.         I2C_delay();
  61. }

  62. u16 I2C_WaitAck(void)        //返回為:=1有ACK,=0無ACK
  63. {
  64.         SCL_L;
  65.         I2C_delay();
  66.         SDA_H;
  67.         I2C_delay();
  68.         SCL_H;
  69.         I2C_delay();
  70.         if(SDA_read)
  71.         {
  72.                 SCL_L;
  73.                 return(I2C_TM_ERR);
  74.         }
  75.         SCL_L;
  76.         return(I2C_TM_OK);
  77. }

  78. void I2C_SendByte(u8 SendByte)        //數據從高位到低位//
  79. {
  80.         u8 i=8;
  81.         while(i--)
  82.         {
  83.                 SCL_L;
  84.                 I2C_delay();
  85.                 if(SendByte&0x80)SDA_H;
  86.                 else SDA_L;
  87.                 SendByte<<=1;
  88.                 I2C_delay();
  89.                 SCL_H;
  90.                 I2C_delay();
  91.         }
  92.         SCL_L;
  93. }

  94. u8 I2C_ReceiveByte(void)        //數據從高位到低位//
  95. {
  96.         u8 i=8;
  97.         u8 ReceiveByte=0;

  98.         SDA_H;
  99.         while(i--)
  100.         {
  101.                 ReceiveByte<<=1;
  102.                 SCL_L;
  103.                 I2C_delay();
  104.                 SCL_H;
  105.                 I2C_delay();
  106.                 if(SDA_read)
  107.                 {
  108.                         ReceiveByte|=0x01;
  109.                 }
  110.         }
  111.         SCL_L;
  112.         return ReceiveByte;
  113. }

  114. /*******************************************************************************
  115. * Function Name  : I2C_EE_ByteWrite
  116. * Description    : Writes one byte to the I2C EEPROM.
  117. * Input          : - pBuffer : pointer to the buffer  containing the data to be
  118. *                    written to the EEPROM.
  119. *                  - WriteAddr : EEPROM's internal address to write to.
  120. * Output         : None
  121. * Return         : None
  122. *******************************************************************************/
  123. u16 I2C_EE_ByteWrite(u8* pBuffer, u16 WriteAddr)
  124. {
  125.         if(I2C_Start()==I2C_TM_ERR)return(I2C_TM_ERR);
  126.         I2C_SendByte(EEPROM_ADDRESS);                                        //設置器件地址
  127.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  128.         I2C_SendByte((WriteAddr>>8) & 0x00FF);                        //設置高起始地址
  129.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  130.         I2C_SendByte((u8)(WriteAddr & 0x00FF));                        //設置低起始地址
  131.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  132.         I2C_SendByte(*pBuffer);
  133.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  134.         I2C_Stop();
  135.         //注意:因為這里要等待EEPROM寫完,可以采用查詢或延時方式(10ms)
  136.         if(I2C_EE_WaitEepromStandbyState()==I2C_TM_ERR) return(I2C_TM_ERR);
  137.         return (I2C_TM_OK);
  138. }

  139. /*******************************************************************************
  140. * Function Name  : I2C_EE_PageWrite
  141. * Description    : Writes more than one byte to the EEPROM with a single WRITE
  142. *                  cycle. The number of byte can't exceed the EEPROM page size.
  143. * Input          : - pBuffer : pointer to the buffer containing the data to be
  144. *                    written to the EEPROM.
  145. *                  - WriteAddr : EEPROM's internal address to write to.
  146. *                  - NumByteToWrite : number of bytes to write to the EEPROM.
  147. * Output         : None
  148. * Return         : None
  149. *******************************************************************************/
  150. u16 I2C_EE_PageWrite(u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite)
  151. {
  152.         if(I2C_Start()==I2C_TM_ERR)return I2C_TM_ERR;
  153.         I2C_SendByte(EEPROM_ADDRESS);                                        //設置器件地址
  154.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  155.         I2C_SendByte((WriteAddr>>8) & 0x00FF);                        //設置高起始地址
  156.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  157.         I2C_SendByte((u8)(WriteAddr & 0x00FF));                        //設置低起始地址
  158.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}

  159.         while(NumByteToWrite--)
  160.         {
  161.                 I2C_SendByte(* pBuffer);
  162.                 if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  163.                 pBuffer++;
  164.         }
  165.         I2C_Stop();
  166.         //注意:因為這里要等待EEPROM寫完,可以采用查詢或延時方式(10ms)
  167.         //Systick_Delay_1ms(10);
  168.         if(I2C_EE_WaitEepromStandbyState()==I2C_TM_ERR) return(I2C_TM_ERR);
  169.         return(I2C_TM_OK);
  170. }

  171. /*******************************************************************************
  172. * Function Name  : I2C_EE_BufferWrite
  173. * Description    : Writes buffer of data to the I2C EEPROM.
  174. * Input          : - pBuffer : pointer to the buffer  containing the data to be
  175. *                    written to the EEPROM.
  176. *                  - WriteAddr : EEPROM's internal address to write to.
  177. *                  - NumByteToWrite : number of bytes to write to the EEPROM.
  178. * Output         : None
  179. * Return         : None
  180. *******************************************************************************/
  181. u16 I2C_EE_BufferWrite(u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite)
  182. {
  183.         u16 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;

  184.         Addr = WriteAddr % I2C_PageSize;
  185.         count = I2C_PageSize - Addr;
  186.         NumOfPage =  NumByteToWrite / I2C_PageSize;
  187.         NumOfSingle = NumByteToWrite % I2C_PageSize;

  188.         /* If WriteAddr is I2C_PageSize aligned  */
  189.         if(Addr == 0)
  190.         {
  191.                 /* If NumByteToWrite < I2C_PageSize */
  192.                 if(NumOfPage == 0)
  193.                 {
  194.                         if(I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle)==I2C_TM_ERR) return(I2C_TM_ERR);
  195.                 }
  196.                 /* If NumByteToWrite > I2C_PageSize */
  197.                 else
  198.                 {
  199.                         while(NumOfPage--)
  200.                         {
  201.                                 if(I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize)==I2C_TM_ERR) return(I2C_TM_ERR);
  202.                                 WriteAddr +=  I2C_PageSize;
  203.                                 pBuffer += I2C_PageSize;
  204.                         }
  205.        
  206.                         if(NumOfSingle!=0)
  207.                         {
  208.                                 if(I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle)==I2C_TM_ERR) return(I2C_TM_ERR);
  209.                         }
  210.                 }
  211.         }
  212.         /* If WriteAddr is not I2C_PageSize aligned  */
  213.         else
  214.         {
  215.                 /* If NumByteToWrite < I2C_PageSize */
  216.                 if(NumOfPage== 0)
  217.                 {
  218.                         if(I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle)==I2C_TM_ERR) return(I2C_TM_ERR);
  219.                 }
  220.                 /* If NumByteToWrite > I2C_PageSize */
  221.                 else
  222.                 {
  223.                         NumByteToWrite -= count;
  224.                         NumOfPage =  NumByteToWrite / I2C_PageSize;
  225.                         NumOfSingle = NumByteToWrite % I2C_PageSize;       

  226.                         if(count != 0)
  227.                         {
  228.                                 if(I2C_EE_PageWrite(pBuffer, WriteAddr, count)==I2C_TM_ERR) return(I2C_TM_ERR);
  229.                                 WriteAddr += count;
  230.                                 pBuffer += count;
  231.                         }

  232.                         while(NumOfPage--)
  233.                         {
  234.                                 if(I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize)==I2C_TM_ERR) return(I2C_TM_ERR);
  235.                                 WriteAddr +=  I2C_PageSize;
  236.                                 pBuffer += I2C_PageSize;
  237.                         }
  238.                         if(NumOfSingle != 0)
  239.                         {
  240.                                 if(I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle)==I2C_TM_ERR) return(I2C_TM_ERR);
  241.                         }
  242.                 }
  243.         }
  244.         return(I2C_TM_OK);
  245. }

  246. /*******************************************************************************
  247. * Function Name  : I2C_EE_BufferRead
  248. * Description    : Reads a block of data from the EEPROM.
  249. * Input          : - pBuffer : pointer to the buffer that receives the data read
  250. *                    from the EEPROM.
  251. *                  - ReadAddr : EEPROM's internal address to read from.
  252. *                  - NumByteToRead : number of bytes to read from the EEPROM.
  253. * Output         : None
  254. * Return         : None
  255. *******************************************************************************/
  256. u16 I2C_EE_BufferRead(u8* pBuffer, u16 ReadAddr, u16 NumByteToRead)
  257. {
  258.         if(I2C_Start()==I2C_TM_ERR)return(I2C_TM_ERR);
  259.         I2C_SendByte(EEPROM_ADDRESS);                                        //設置器件地址
  260.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  261.         I2C_SendByte((ReadAddr>>8) & 0x00FF);                        //設置高起始地址
  262.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  263.         I2C_SendByte((u8)(ReadAddr & 0x00FF));                   //設置低起始地址
  264.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  265.         if(I2C_Start()==I2C_TM_ERR)return(I2C_TM_ERR);
  266.         I2C_SendByte(EEPROM_ADDRESS | 0x0001);
  267.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  268.         while(NumByteToRead)
  269.         {
  270.                 *pBuffer = I2C_ReceiveByte();
  271.                 if(NumByteToRead == 1)I2C_NoAck();
  272.                 else I2C_Ack();
  273.                 pBuffer++;
  274.                 NumByteToRead--;
  275.         }
  276.         I2C_Stop();

  277.         return(I2C_TM_OK);
  278. }

  279. /*******************************************************************************
  280. * Function Name  : I2C_EE_WaitEepromStandbyState
  281. * Description    : Wait for EEPROM Standby state
  282. * Input          : None
  283. * Output         : None
  284. * Return         : None
  285. *******************************************************************************/
  286. u16 I2C_EE_WaitEepromStandbyState(void)
  287. {
  288.         u32 tmout=I2C_TIMEOUT;

  289.         do
  290.         {
  291.                 if(I2C_Start()==I2C_TM_ERR)return I2C_TM_ERR;
  292.                 I2C_SendByte(EEPROM_ADDRESS);                                        //設置器件地址
  293.                 if(I2C_WaitAck()==I2C_TM_OK){I2C_Stop(); return(I2C_TM_OK);}
  294.                 tmout--;
  295.         }while(tmout>0);

  296.         //停止位
  297.         I2C_Stop();
  298.         return(I2C_TM_ERR);
  299. }

  300. /*******************************************************************************
  301. * Function Name  : I2C_LM75_Read
  302. * Description    : Reads Temperature data from the LM75.
  303. * Input          : None
  304. * Output         : 0xFFFF -- Error
  305. *                                        0x0000 ~ 0x01FF -- Valid data
  306. *                                        Temp        Binary                                         Hex
  307. *                                        +125        0111 1101 0xxx xxxx         0FAh
  308. *                                        +25                0001 1001 0xxx xxxx         032h
  309. *                                        +0.5        0000 0000 1xxx xxxx         001h
  310. *                                        0                0000 0000 0xxx xxxx                000h
  311. *                                        -0.5        1111 1111 1xxx xxxx         1FFh
  312. *                                        -25                1110 0111 0xxx xxxx         1CEh
  313. *                                        -55                1100 1001 0xxx xxxx         192h
  314. * Return         : None
  315. *******************************************************************************/
  316. u16 I2C_LM75_Read(void)
  317. {
  318.         vu16 TempH,TempL;

  319.         if(I2C_Start()==I2C_TM_ERR)return(I2C_TM_ERR);
  320.         I2C_SendByte(LM75_ADDRESS);                                                //設置器件地址
  321.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}
  322.         I2C_SendByte(0x00);                                                                //設置溫度寄存器地址
  323.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}

  324.         if(I2C_Start()==I2C_TM_ERR)return(I2C_TM_ERR);

  325.         I2C_SendByte(LM75_ADDRESS | 0x01);                        //設置器件地址
  326.         if(I2C_WaitAck()==I2C_TM_ERR){I2C_Stop(); return(I2C_TM_ERR);}

  327.         TempH = I2C_ReceiveByte();
  328.         I2C_Ack();
  329.         TempL = I2C_ReceiveByte();
  330.         I2C_NoAck();
  331.         I2C_Stop();

  332. //        if(TempH==0xFF && TempL==0xFF) return I2C_TM_ERR;
  333. //        TempH = (TempH<<8) | (TempL&0x80);
  334. //        TempH = test;
  335.         return(TempH);
  336. }
復制代碼


所有資料51hei提供下載:
LM75A溫度驅動.rar (2.03 MB, 下載次數: 136)



評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美日韩在线一区二区三区 | av免费网站在线观看 | 精品国产乱码久久久久久丨区2区 | 在线观看国产 | 亚洲美女av网站 | 91久久精品一区二区二区 | 国产成人自拍av | 蜜桃视频成人 | 一区二区三区视频在线 | 天堂久久网 | 97视频免费| 日韩在线观看视频一区 | 久久久av中文字幕 | 日韩欧美国产一区二区 | 精品国产乱码久久久久久图片 | 青青草视频网站 | 日韩精品在线播放 | 中文字幕在线网 | 久久精彩 | 欧美中文字幕在线观看 | 视频一二三区 | 国产成人免费网站 | 毛片免费视频 | aaa国产大片 | 亚洲一区二区视频在线观看 | 久久综合影院 | 免费成人高清在线视频 | 91激情电影| 91精品国产综合久久婷婷香蕉 | 婷婷久久久久 | 黄色片在线观看网址 | 日本视频一区二区三区 | 天天久久 | 免费观看日韩av | 日日操夜夜摸 | 国产精品99久久久久久www | av一二三四| 国产精品视频播放 | 中文字幕视频在线免费 | 在线观看日本网站 | 狠狠撸在线视频 |