欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
合泰單片機SHT20驅(qū)動程序+串口發(fā)送程序源碼
[打印本頁]
作者:
moonine9
時間:
2018-2-2 10:13
標題:
合泰單片機SHT20驅(qū)動程序+串口發(fā)送程序源碼
實測有效!
單片機源程序如下:
/*********************************************************************************************************//**
* @file I2C/7_bit_mode_master/main.c
* @version $Rev:: 924 $
* @date $Date:: 07-22 #$
* @brief Main program.
*************************************************************************************************************
* @attention
*
* Firmware Disclaimer Information
*
* 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
* proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
* other intellectual property laws.
*
* 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
* other than HOLTEK and the customer.
*
* 3. The program technical documentation, including the code, is provided "as is" and for customer reference
* only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
* the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
* the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
*
* <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
************************************************************************************************************/
/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include "ht32_board.h"
#include "ht32_board_config.h"
/** @addtogroup HT32_Series_Peripheral_Examples HT32 Peripheral Examples
* @{
*/
/** @addtogroup I2C_Examples I2C
* @{
*/
/** @addtogroup I2C_7_bit_mode_master
* @{
*/
/* Private types -------------------------------------------------------------------------------------------*/
typedef enum {Fail = 0, Pass = !Fail} TestResult;
typedef enum {Write = 0, Read = !Write} writeorread;
typedef enum {ACK = 1, NACK = !ACK} NACKORACK;
/* Private constants ---------------------------------------------------------------------------------------*/
#define I2C_SLAVE_ADDRESS 0x80
#define measureT 0xF3
#define measureH 0xF5
/* Private function prototypes -----------------------------------------------------------------------------*/
TestResult CmpBuffer(u8* Buffer1, u8* Buffer2, u32 BufferLength);
void Delay1us(unsigned long time);
void SetSda(void);
void ResetSda(void);
void SetScl(void);
void ResetScl(void);
void I2CSTARTACTION(void);
void I2CSTOP(void);
void I2CWRITE(u8 txd);
void SDA_OUT(void);
void SDA_IN(void);
void SCL_OUT(void);
void SCL_IN(void);
void I2CINIT(void);
void I2CSTART(u8 slaveadr,u8 WOR);
u8 I2C_WAITACK(void);
FlagStatus Read_Sda(void);
FlagStatus Read_Scl(void);
void I2C_ACK(void);
void I2C_NACK(void);
u8 I2CREAD(u8 ack);
float get_humidity(u8 slave_adr);
float get_temperature(u8 slave_adr);
void USART_Tx(const char* TxBuffer, u32 length);
void Float2Char(float Value,char *array);
/* Global functions ----------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* @brief Main program.
* @retval None
***********************************************************************************************************/
int main(void)
{USART_InitTypeDef USART_InitStructure;
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
/* Initialize LED1 on HT32 board */
CKCUClock.Bit.BFTM0 = 1;
HT32F_DVB_LEDInit(HT_LED1);
HT32F_DVB_LEDInit(HT_LED2);
/* Enable I2C0 ,I2C1 & AFIO APB clock */
CKCUClock.Bit.PA= 1;
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.USART1 = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PA,AFIO_PIN_4, AFIO_FUN_USART_UART);//tx
AFIO_GPxConfig(GPIO_PA,AFIO_PIN_5, AFIO_FUN_USART_UART);//rx
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WORDLENGTH_8B;
USART_InitStructure.USART_StopBits = USART_STOPBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_NO;
USART_InitStructure.USART_Mode = USART_MODE_NORMAL;
USART_Init(HT_USART1, &USART_InitStructure);
USART_TxCmd(COM1_PORT, ENABLE);
USART_RxCmd(COM1_PORT, ENABLE);
/*--------------------------------------------------------------------------------------------------------*/
/* Master Transmitter */
/*--------------------------------------------------------------------------------------------------------*/
while(1)
{Delay1us(1000000);
get_humidity(I2C_SLAVE_ADDRESS);
get_temperature(I2C_SLAVE_ADDRESS);
printf("濕度是 %0.1f%%\r\n", get_humidity(I2C_SLAVE_ADDRESS));
printf("溫度是 %0.1f℃\r\n", get_temperature(I2C_SLAVE_ADDRESS));
HT32F_DVB_LEDOn(HT_LED2);
}
}
/*********************************************************************************************************//**
* @brief 1us
*
***********************************************************************************************************/
void Delay1us(unsigned long time)
{
BFTM_SetCompare(HT_BFTM0, 48*time);
BFTM_SetCounter(HT_BFTM0, 0);
BFTM_OneShotModeCmd(HT_BFTM0, ENABLE);
BFTM_EnaCmd(HT_BFTM0, ENABLE);
while (BFTM_GetFlagStatus(HT_BFTM0) != SET);
BFTM_ClearFlag(HT_BFTM0);
}
#if (HT32_LIB_DEBUG == 1)
/*********************************************************************************************************//**
* @brief Report both the error name of the source file and the source line number.
* @param filename: pointer to the source file name.
* @param uline: error line source number.
* @retval None
***********************************************************************************************************/
void assert_error(u8* filename, u32 uline)
{
/*
This function is called by IP library that the invalid parameters has been passed to the library API.
Debug message can be added here.
Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
*/
while (1)
{
}
}
#endif
void I2CSTARTACTION(void)
{SDA_OUT();
SetSda();
SetScl();
Delay1us(5);
ResetSda();
Delay1us(5);
ResetScl();
Delay1us(5);
}
void I2CSTOP(void)
{ SDA_OUT();
ResetSda();
ResetScl();
Delay1us(5);
SetScl();
Delay1us(5);
SetSda();
Delay1us(5);
}
void SetSda(void)
{GPIO_SetOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
}
void ResetSda(void)
{GPIO_ClearOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
}
void SetScl(void)
{GPIO_SetOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
}
void ResetScl(void)
{GPIO_ClearOutBits(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
}
void I2CWRITE(u8 txd)
{u8 i;
SDA_OUT();
SCL_OUT();
for(i=0;i<8;i++)
{if(txd&0x80)
{SetSda();
}
else
{ResetSda();
}
Delay1us(1);
SetScl();
Delay1us(5);
ResetScl();
txd<<=1;
Delay1us(1);
}
}
void I2CINIT(void)
{ /* Configure GPIO to I2C mode for Master pa0=scl pa1=sda */
AFIO_GPxConfig(HTCFG_I2C_MASTER_SCL_GPIO_ID, HTCFG_I2C_MASTER_SCL_AFIO_PIN, AFIO_FUN_GPIO);
AFIO_GPxConfig(HTCFG_I2C_MASTER_SDA_GPIO_ID, HTCFG_I2C_MASTER_SDA_AFIO_PIN, AFIO_FUN_GPIO);
GPIO_OpenDrainConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, ENABLE);
GPIO_OpenDrainConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, ENABLE);
SDA_OUT();
SCL_OUT();
SetSda();
Delay1us(1);
SetScl();
Delay1us(1);
}
void SDA_OUT(void)
{GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, DISABLE);
GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, GPIO_DIR_OUT);
}
void SDA_IN(void)
{GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, GPIO_DIR_IN);
GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN, ENABLE);
}
void SCL_OUT(void)
{GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, DISABLE);
GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, GPIO_DIR_OUT);
}
void SCL_IN(void)
{GPIO_DirectionConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, GPIO_DIR_IN);
GPIO_InputConfig(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN, ENABLE);
}
void I2CSTART(u8 slaveadr,u8 WOR)
{
if(WOR==0)
{ I2CSTARTACTION();
I2CWRITE(slaveadr&0xFE);
}
else
{ I2CSTARTACTION();
I2CWRITE(slaveadr|0x01);
}
}
u8 I2C_WAITACK(void)
{u8 i;
SDA_OUT();
Delay1us(4);
SetSda();
Delay1us(1);
SetScl();
Delay1us(1);
SDA_IN();
i=100;
do
{if(!Read_Sda())break;
i--;
Delay1us(4);
}while(i>0);
if(i)
{ResetScl();
return 1;
}
else
{I2CSTOP();
return 0;
}
}
void I2C_ACK(void)
{ ResetScl();
SDA_OUT();
ResetSda();
Delay1us(2);
SetScl();
Delay1us(2);
ResetScl();
Delay1us(15);
}
void I2C_NACK(void)
{ ResetScl();
SDA_OUT();
SetSda();
Delay1us(2);
SetScl();
Delay1us(2);
ResetScl();
Delay1us(15);
}
FlagStatus Read_Sda(void)
{return GPIO_ReadInBit(HT_GPIOA, HTCFG_I2C_MASTER_SDA_AFIO_PIN);
}
FlagStatus Read_Scl(void)
{return GPIO_ReadInBit(HT_GPIOA, HTCFG_I2C_MASTER_SCL_AFIO_PIN);
}
u8 I2CREAD(u8 ack)
{u8 i,receive=0;
SDA_IN();
SCL_OUT();
for(i=0;i<8;i++)
{ResetScl();
Delay1us(4);
SetScl();
receive<<=1;
if(Read_Sda())receive++;
Delay1us(4);
}
if(ack)
I2C_ACK();
else
I2C_NACK();
return receive;
}
float get_temperature(u8 slave_adr)
{u8 LSB,MSB;
float Temperature;
float temp;
I2CINIT();
Delay1us(50);
I2CSTART(slave_adr,Write);
while(!I2C_WAITACK());
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
main.zip
(3 KB, 下載次數(shù): 50)
2018-2-2 10:13 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
hpdell
時間:
2019-11-22 22:39
不錯不錯啊,給力
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1