Chinaunix首页 | 论坛 | 博客
  • 博客访问: 830859
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2016-11-30 16:28:50

最近在读取SHT3x系列sensor的温度和湿度,用到的是IIC接口。
顺便写了一下STM32的IIC接口。

这次配置的是STM32内部的IIC接口。

注意:读的时候,怎么发送Ack, 和 NAck信号,参考stm的设计文档。

点击(此处)折叠或打开

  1. #include "Dev_SHT3X.h"
  2. #include "globalDef.h"
  3. #include <stdio.h>


  4. #define I2C1_OWN_ADDRESS7 0x0A
  5. #define I2C_Speed         40000
  6. #define SHT3X_ADDRESS     0x44

  7. /* read out command */
  8. #define CMD_READH_SHX     0x2c
  9. #define CMD_READL_SHX     0x06

  10. /**
  11. * SHX device: IIC1
  12. * PB6 - SCL
  13. * PB7 - SDA
  14. */
  15. static void IIC_gpioConfig(void)
  16. {
  17.     GPIO_InitTypeDef GPIO_InitStructure;

  18.     /* 使能与 I2C1 有关的时钟 */
  19.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  20.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

  21.     /* PB6-I2C1_SCL、PB7-I2C1_SDA*/
  22.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  23.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;     // 开漏输出
  25.     GPIO_Init(GPIOB, &GPIO_InitStructure);        
  26. }

  27. static void IIC_modeConfig(void)
  28. {
  29.     I2C_InitTypeDef I2C_InitStructure;

  30.     I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; /* I2C 配置 */
  31.     I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; /* 高电平数据稳定,低电平数据变化 SCL 时钟线的占空比 */
  32.     I2C_InitStructure.I2C_OwnAddress1 = I2C1_OWN_ADDRESS7; /* setting master address. */
  33.     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ; /* enable ack */
  34.     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* 7-bit addressing mode */
  35.     I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; /* communication speed <= 400k */

  36.     I2C_Init(I2C1, &I2C_InitStructure);
  37.     I2C_Cmd(I2C1, ENABLE);    
  38. }


  39. void SHT3X_InitDevice(void)
  40. {
  41.     IIC_gpioConfig();    
  42.     IIC_modeConfig();
  43. }


  44. void sht3x_readTempHumi(uint8_t* pBuffer, uint8_t NumByteToRead)
  45. {
  46.     while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); /* detect IIC busy */

  47.     I2C_GenerateSTART(I2C1, ENABLE); /* Send START condition */

  48.     /* Test on EV5 and clear it */
  49.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  50.     /* Send slave device address for write */
  51.     I2C_Send7bitAddress(I2C1, (SHT3X_ADDRESS << 1), I2C_Direction_Transmitter);

  52.     /* Test on EV6 and clear it */
  53.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  54.     /* Clear EV6 by setting again the PE bit */
  55.     I2C_Cmd(I2C1, ENABLE);

  56.     /* Send the device address to write to */
  57.     I2C_SendData(I2C1, CMD_READH_SHX);

  58.     /* Test on EV8 and clear it */
  59.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  60.     /* Send the device address to write to */
  61.     I2C_SendData(I2C1, CMD_READL_SHX);

  62.     /* Test on EV8 and clear it */
  63.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  64. //------------------------------------------------

  65.     /* Send STRAT condition a second time */
  66.     I2C_GenerateSTART(I2C1, ENABLE);

  67.     /* Test on EV5 and clear it */
  68.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  69.     /* Send shx address for read */
  70.     I2C_Send7bitAddress(I2C1, ((SHT3X_ADDRESS << 1) | 0x01), I2C_Direction_Receiver);

  71.     /* Test on EV6 and clear it */
  72.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  73.     /* While there is data to be read */
  74.     while(NumByteToRead)
  75.     {
  76.         if(NumByteToRead == 1)
  77.         {
  78.             /* Disable Acknowledgement */
  79.             I2C_AcknowledgeConfig(I2C1, DISABLE);

  80.             /* Send STOP Condition */
  81.             I2C_GenerateSTOP(I2C1, ENABLE);
  82.         }

  83.         /* Test on EV7 and clear it */
  84.         if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
  85.         {
  86.             /* Read a byte from the EEPROM */
  87.             *pBuffer = I2C_ReceiveData(I2C1);

  88.             /* Point to the next location where the byte read will be saved */
  89.             pBuffer++;

  90.             /* Decrement the read bytes counter */
  91.             NumByteToRead--;
  92.         }
  93.     }

  94.     /* Enable Acknowledgement to be ready for another reception */
  95.     I2C_AcknowledgeConfig(I2C1, ENABLE);
  96. }



  97. //-----------------------------------------------------------------------------
  98. static float SHT3X_CalcTemperature(uint16_t rawValue)
  99. {
  100.     // calculate temperature [°C]
  101.     // T = -45 + 175 * rawValue / (2^16-1)
  102.     return 175.0f * (float)rawValue / 65535.0f - 45.0f;
  103. }

  104. //-----------------------------------------------------------------------------
  105. static float SHT3X_CalcHumidity(uint16_t rawValue)
  106. {
  107.     // calculate relative humidity [%RH]
  108.     // RH = rawValue / (2^16-1) * 100
  109.     return 100.0f * (float)rawValue / 65535.0f;
  110. }

  111. void sht3x_testTask(void)
  112. {
  113.     uint8_t recBuffer[6];
  114.     uint16_t rawdata;
  115.     float fTemp, fHumi;

  116.     sht3x_readTempHumi(&recBuffer[0], 6);
  117.     rawdata = recBuffer[0];
  118.     rawdata = (rawdata << 8) + recBuffer[1];
  119.     fTemp = SHT3X_CalcTemperature(rawdata);

  120.     rawdata = recBuffer[3];
  121.     rawdata = (rawdata << 8) + recBuffer[4];
  122.     fHumi = SHT3X_CalcHumidity(rawdata);

  123.     enterCriticalSection();
  124.     printf("T: %0.2f, H: %0.1f\r\n", fTemp, fHumi);
  125.     exitCriticalSection();
  126. }


点击(此处)折叠或打开

  1. #ifndef _DEV_SHT3X_H
  2. #define _DEV_SHT3X_H

  3. #include "bsp_iic.h"
  4. #include "bsp_SysConfig.h"



  5. extern void SHT3X_InitDevice(void);
  6. extern void sht3x_testTask(void);


  7. #endif /* _DEV_SHT3X_H */





阅读(2929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~