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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6456|回復: 1
收起左側

合泰單片機SHT20驅動程序+串口發送程序源碼

[復制鏈接]
ID:281642 發表于 2018-2-2 10:13 | 顯示全部樓層 |閱讀模式
實測有效!

單片機源程序如下:
  1. /*********************************************************************************************************//**
  2. * @file    I2C/7_bit_mode_master/main.c
  3. * @version $Rev:: 924          $
  4. * @date    $Date:: 07-22 #$
  5. * @brief   Main program.
  6. *************************************************************************************************************
  7. * @attention
  8. *
  9. * Firmware Disclaimer Information
  10. *
  11. * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
  12. *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
  13. *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
  14. *    other intellectual property laws.
  15. *
  16. * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
  17. *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
  18. *    other than HOLTEK and the customer.
  19. *
  20. * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
  21. *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
  22. *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
  23. *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
  24. *
  25. * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
  26. ************************************************************************************************************/

  27. /* Includes ------------------------------------------------------------------------------------------------*/
  28. #include "ht32.h"
  29. #include "ht32_board.h"
  30. #include "ht32_board_config.h"
  31. /** @addtogroup HT32_Series_Peripheral_Examples HT32 Peripheral Examples
  32.   * @{
  33.   */

  34. /** @addtogroup I2C_Examples I2C
  35.   * @{
  36.   */

  37. /** @addtogroup I2C_7_bit_mode_master
  38.   * @{
  39.   */


  40. /* Private types -------------------------------------------------------------------------------------------*/
  41. typedef enum {Fail = 0, Pass = !Fail} TestResult;
  42. typedef enum {Write = 0, Read = !Write} writeorread;
  43. typedef enum {ACK = 1, NACK = !ACK} NACKORACK;

  44. /* Private constants ---------------------------------------------------------------------------------------*/
  45. #define I2C_SLAVE_ADDRESS      0x80
  46. #define measureT               0xF3
  47. #define measureH               0xF5
  48. /* Private function prototypes -----------------------------------------------------------------------------*/
  49. TestResult CmpBuffer(u8* Buffer1, u8* Buffer2, u32 BufferLength);
  50. void Delay1us(unsigned long time);
  51. void SetSda(void);
  52. void ResetSda(void);
  53. void SetScl(void);
  54. void ResetScl(void);
  55. void I2CSTARTACTION(void);
  56. void I2CSTOP(void);
  57. void I2CWRITE(u8 txd);
  58. void SDA_OUT(void);
  59. void SDA_IN(void);
  60. void SCL_OUT(void);
  61. void SCL_IN(void);
  62. void I2CINIT(void);
  63. void I2CSTART(u8 slaveadr,u8 WOR);
  64. u8 I2C_WAITACK(void);
  65. FlagStatus Read_Sda(void);
  66. FlagStatus Read_Scl(void);
  67. void I2C_ACK(void);
  68. void I2C_NACK(void);
  69. u8 I2CREAD(u8 ack);
  70. float get_humidity(u8 slave_adr);
  71. float get_temperature(u8 slave_adr);
  72. void USART_Tx(const char* TxBuffer, u32 length);
  73. void Float2Char(float Value,char *array);
  74. /* Global functions ----------------------------------------------------------------------------------------*/
  75. /*********************************************************************************************************//**
  76.   * @brief  Main program.
  77.   * @retval None
  78.   ***********************************************************************************************************/
  79. int main(void)
  80. {USART_InitTypeDef USART_InitStructure;
  81.   CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
  82.   /* Initialize LED1 on HT32 board         */
  83.           CKCUClock.Bit.BFTM0      = 1;
  84.   HT32F_DVB_LEDInit(HT_LED1);
  85.   HT32F_DVB_LEDInit(HT_LED2);
  86.   /* Enable I2C0 ,I2C1 & AFIO APB clock                                                                     */
  87.   CKCUClock.Bit.PA= 1;
  88.   CKCUClock.Bit.AFIO        = 1;
  89.         CKCUClock.Bit.USART1      = 1;
  90.   CKCU_PeripClockConfig(CKCUClock, ENABLE);
  91.    AFIO_GPxConfig(GPIO_PA,AFIO_PIN_4, AFIO_FUN_USART_UART);//tx
  92.   AFIO_GPxConfig(GPIO_PA,AFIO_PIN_5, AFIO_FUN_USART_UART);//rx
  93.         USART_InitStructure.USART_BaudRate = 115200;
  94.   USART_InitStructure.USART_WordLength = USART_WORDLENGTH_8B;
  95.   USART_InitStructure.USART_StopBits = USART_STOPBITS_1;
  96.   USART_InitStructure.USART_Parity = USART_PARITY_NO;
  97.   USART_InitStructure.USART_Mode = USART_MODE_NORMAL;
  98.   USART_Init(HT_USART1, &USART_InitStructure);
  99.         USART_TxCmd(COM1_PORT, ENABLE);
  100.   USART_RxCmd(COM1_PORT, ENABLE);
  101.         /*--------------------------------------------------------------------------------------------------------*/
  102.   /* Master Transmitter                                                                                     */
  103.   /*--------------------------------------------------------------------------------------------------------*/
  104. while(1)
  105. {Delay1us(1000000);
  106. get_humidity(I2C_SLAVE_ADDRESS);
  107. get_temperature(I2C_SLAVE_ADDRESS);
  108. printf("濕度是 %0.1f%%\r\n", get_humidity(I2C_SLAVE_ADDRESS));
  109. printf("溫度是 %0.1f℃\r\n", get_temperature(I2C_SLAVE_ADDRESS));

  110.         HT32F_DVB_LEDOn(HT_LED2);
  111. }
  112. }
  113. /*********************************************************************************************************//**
  114.   * @brief  1us
  115.   *
  116.   ***********************************************************************************************************/
  117. void Delay1us(unsigned long time)
  118. {
  119.   BFTM_SetCompare(HT_BFTM0, 48*time);
  120.   BFTM_SetCounter(HT_BFTM0, 0);
  121.   BFTM_OneShotModeCmd(HT_BFTM0, ENABLE);
  122.   BFTM_EnaCmd(HT_BFTM0, ENABLE);
  123.         while (BFTM_GetFlagStatus(HT_BFTM0) != SET);
  124.     BFTM_ClearFlag(HT_BFTM0);
  125.         
  126. }

  127. #if (HT32_LIB_DEBUG == 1)
  128. /*********************************************************************************************************//**
  129.   * @brief  Report both the error name of the source file and the source line number.
  130.   * @param  filename: pointer to the source file name.
  131.   * @param  uline: error line source number.
  132.   * @retval None
  133.   ***********************************************************************************************************/
  134. void assert_error(u8* filename, u32 uline)
  135. {
  136.   /*
  137.      This function is called by IP library that the invalid parameters has been passed to the library API.
  138.      Debug message can be added here.
  139.      Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
  140.   */

  141.   while (1)
  142.   {
  143.   }
  144. }
  145. #endif
  146.   void I2CSTARTACTION(void)
  147.         {SDA_OUT();
  148.                 SetSda();
  149.         SetScl();
  150.         Delay1us(5);
  151.         ResetSda();
  152.         Delay1us(5);
  153.         ResetScl();
  154.         Delay1us(5);
  155.         }
  156.         void I2CSTOP(void)
  157.         { SDA_OUT();
  158.                 ResetSda();
  159.                 ResetScl();
  160.                 Delay1us(5);
  161.                 SetScl();
  162.           Delay1us(5);
  163.                 SetSda();
  164.                 Delay1us(5);
  165.         }
  166.         void SetSda(void)
  167.         {GPIO_SetOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
  168.          }
  169.         void ResetSda(void)
  170.         {GPIO_ClearOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
  171.         }
  172.         void SetScl(void)
  173.         {GPIO_SetOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
  174.          }
  175.         void ResetScl(void)
  176.         {GPIO_ClearOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
  177.         }
  178.         void I2CWRITE(u8 txd)
  179.         {u8 i;
  180.                 SDA_OUT();
  181.                 SCL_OUT();
  182.     for(i=0;i<8;i++)
  183.                 {if(txd&0x80)
  184.                         {SetSda();
  185.                         }
  186.                         else
  187.                         {ResetSda();
  188.                         }
  189.                         Delay1us(1);
  190.                         SetScl();
  191.                         Delay1us(5);
  192.                         ResetScl();
  193.                         txd<<=1;
  194.                         Delay1us(1);
  195.                         }
  196.                 }
  197. void I2CINIT(void)
  198. { /* Configure GPIO to I2C mode for Master  pa0=scl pa1=sda                                                                */
  199.   AFIO_GPxConfig(HTCFG_I2C_MASTER_SCL_GPIO_ID, HTCFG_I2C_MASTER_SCL_AFIO_PIN, AFIO_FUN_GPIO);
  200.   AFIO_GPxConfig(HTCFG_I2C_MASTER_SDA_GPIO_ID, HTCFG_I2C_MASTER_SDA_AFIO_PIN, AFIO_FUN_GPIO);
  201.   GPIO_OpenDrainConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, ENABLE);
  202.         GPIO_OpenDrainConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, ENABLE);
  203.         SDA_OUT();
  204.         SCL_OUT();
  205.         SetSda();
  206.         Delay1us(1);
  207.         SetScl();
  208.         Delay1us(1);
  209. }
  210. void SDA_OUT(void)
  211. {GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, DISABLE);
  212. GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, GPIO_DIR_OUT);
  213. }
  214. void SDA_IN(void)
  215. {GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, GPIO_DIR_IN);
  216.         GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, ENABLE);
  217. }
  218. void SCL_OUT(void)
  219. {GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, DISABLE);
  220. GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, GPIO_DIR_OUT);
  221. }
  222. void SCL_IN(void)
  223. {GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, GPIO_DIR_IN);
  224.         GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, ENABLE);
  225. }
  226. void I2CSTART(u8 slaveadr,u8 WOR)
  227. {
  228.         if(WOR==0)
  229.         { I2CSTARTACTION();
  230.                 I2CWRITE(slaveadr&0xFE);        
  231.         }
  232.         else
  233.         { I2CSTARTACTION();
  234.                 I2CWRITE(slaveadr|0x01);
  235.         }
  236. }
  237. u8 I2C_WAITACK(void)
  238. {u8 i;
  239.             SDA_OUT();
  240.             Delay1us(4);
  241.                         SetSda();
  242.                         Delay1us(1);
  243.                         SetScl();
  244.                         Delay1us(1);
  245.                         SDA_IN();
  246.                         i=100;
  247.                         do
  248.                         {if(!Read_Sda())break;
  249.                                 i--;
  250.                                 Delay1us(4);
  251.                         }while(i>0);
  252.                         if(i)
  253.                         {ResetScl();
  254.                                 return 1;
  255.                         }
  256.                         else
  257.                         {I2CSTOP();
  258.                                 return 0;
  259.                         }
  260. }
  261. void I2C_ACK(void)
  262. { ResetScl();
  263.         SDA_OUT();
  264.         ResetSda();
  265.         Delay1us(2);
  266.         SetScl();
  267.         Delay1us(2);
  268.         ResetScl();
  269.         Delay1us(15);
  270. }
  271. void I2C_NACK(void)
  272. { ResetScl();
  273.         SDA_OUT();
  274.         SetSda();
  275.         Delay1us(2);
  276.         SetScl();
  277.         Delay1us(2);
  278.         ResetScl();
  279.         Delay1us(15);
  280. }
  281. FlagStatus Read_Sda(void)
  282. {return GPIO_ReadInBit(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
  283. }
  284. FlagStatus Read_Scl(void)
  285. {return GPIO_ReadInBit(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
  286. }
  287. u8 I2CREAD(u8 ack)
  288. {u8 i,receive=0;
  289.         SDA_IN();
  290.   SCL_OUT();
  291.         for(i=0;i<8;i++)
  292.         {ResetScl();
  293.         Delay1us(4);
  294.                 SetScl();
  295.                 receive<<=1;
  296.                 if(Read_Sda())receive++;
  297.                 Delay1us(4);
  298.   }
  299.         if(ack)
  300.                 I2C_ACK();
  301.         else
  302.                 I2C_NACK();
  303.         return receive;
  304. }
  305. float get_temperature(u8 slave_adr)
  306. {u8 LSB,MSB;
  307.         float Temperature;
  308.         float temp;
  309.         I2CINIT();
  310.         Delay1us(50);
  311.         I2CSTART(slave_adr,Write);
  312.         while(!I2C_WAITACK());
  313. ……………………

  314. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
main.zip (3 KB, 下載次數: 50)
回復

使用道具 舉報

ID:66915 發表于 2019-11-22 22:39 | 顯示全部樓層
不錯不錯啊,給力
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲综合色婷婷 | 亚洲欧美成人影院 | 91一区二区 | 免费一级毛片 | 视频二区在线观看 | 久草网址 | 在线播放中文 | 久久精品中文字幕 | 国产精品综合一区二区 | 亚洲电影一区二区三区 | 午夜不卡一区二区 | 91日韩在线| 91麻豆精品国产91久久久更新资源速度超快 | 中文字幕久久精品 | 在线视频一区二区 | 欧美在线一区二区视频 | 91视频国产精品 | 7777久久 | 九九九久久国产免费 | 黄色一级毛片免费看 | 一区二区三区欧美 | 日韩一区二区三区视频在线观看 | 欧美久操网 | 国产精品久久久久久久久 | 人人爽日日躁夜夜躁尤物 | 午夜精品91 | 亚洲天堂网站 | 欧美在线色视频 | www亚洲精品 | 久久久精品一区二区三区 | 91传媒在线观看 | 一区二区三区在线免费观看 | 久久精品久久久 | 欧美成人综合 | 91日日| 亚洲www啪成人一区二区麻豆 | 国产高清在线精品 | 久久久久久高潮国产精品视 | 亚洲人在线 | 精彩视频一区二区三区 | 国产高清在线视频 |