找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4121|回复: 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, 下载次数: 133)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表