1.先新建一个c8t6的工程
如果使用这个程序,可以直接操作,亲测有效。如果你想移植功能,继续往下看。程序中的注释也写得很清楚。
2.串口打印
首先拿到usart.c和usart.h文件放在自己的项目中,放入项目后需要按照下图操作
串口打印已经在这里完成。然后我们在那里main中测试一下
int main(void) { /* 初始化串口 */ USART_Config(); printf("\r\n wecome to usart\n"); }
测试通过后,串口可以正常显示,这里我贴一下usart.c 和 usart.h 文件
usart.c
#include "usart.h" static void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* 嵌套向量中断控制器组选择 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* 配置USART为中断源 */ NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ; /* 抢断优先级*/ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; /* 子优先级 */ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /* 使能中断 */ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* 初始化配置NVIC */ NVIC_Init(&NVIC_InitStructure); } /** * @brief USART GPIO 配置,工作参数配置 * @param 无 * @retval 无 */ void USART_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 打开串口GPIO的时钟 DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE); // 打开串口外设的时钟 DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE); // 将USART Tx的GPIO配置为推挽复用模式 GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure); // 将USART Rx的GPIO配置为浮空输入模式 GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure); // 配置串口工作参数 // 配置波特率 USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE; // 配置 针数据字长 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 配置停止位 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 配置校验位 USART_InitStructure.USART_Parity = USART_Parity_No ; // 配置硬件流控制 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 配置工作模式,收发一起 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 完成串口的初始配置 USART_Init(DEBUG_USARTx, &USART_InitStructure); // 优先配置串口中断 NVIC_Configuration(); // 使串口接收中断 USART_ITConfig(DEBUG_USARTx, USART_IT_RXNE, ENABLE); // 使能串口 USART_Cmd(DEBUG_USARTx, ENABLE); } /***************** 发送字节 **********************/ void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch) { /* 发送字节数据到USART */ USART_SendData(pUSARTx,ch); /* 等待发送数据寄存器为空 */ while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET); } /****************** 发送8位数组 ************************/ void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num) { uint8_t i; for(i=0; i<num; i ) { /* 发送字节数据到USART */ Usart_SendByte(pUSARTx,array[i]); } /* 等待发送完成 */ while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET); } /***************** 发送字符串 **********************/ void Usart_SendString( USART_TypeDef * pUSARTx, char *str) { unsigned int k=0; do { Usart_SendByte( pUSARTx, *(str k) ); k ; } while(*(str k)!='\0'); /* 等待发送完成 */ while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET) {} } /***************** 发送16位数 **********************/ void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch) { uint8_t temp_h, temp_l; /* 取出高八位 */ temp_h = (ch&0XFF00)>>8; /* 取出低八位 */ temp_l = ch&0XFF; /* 发送高八位 */ USART_SendData(pUSARTx,temp_h); while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET); /* 发送低八位 */ USART_SendData(pUSARTx,temp_l); while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET); } ////printf到达串口后,可重定向使用printf函数 int fputc(int ch, FILE *f) { /* 将字节数据发送到串口 */ USART_SendData(DEBUG_USARTx, (uint8_t) ch); /* 等待发送完成 */ while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET); return (ch); } ////scanf到串口,可以在后面重写scanf、getchar等函数 int fgetc(FILE *f) { /* 等待串口输入数据 */ while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(DEBUG_USARTx); }
usart.h
#ifndef _USART_H #define _USART_H #include "stm32f10x.h" #include <stdio.h> #include "sys.h" /** * 串口红定义,不同串口挂载的总线和IO不一样,移植时需要修改这些宏 * 1-修改总线时钟的宏,uart1挂载到apb2总线,其他uart挂载到apb1总线 * 2-修改GPIO的宏 */ // 串口1-USART1 #define DEBUG_USARTx USART1 #define DEBUG_USART_CLK RCC_APB2Periph_USART1 #dfine DEBUG_USART_APBxClkCmd RCC_APB2PeriphClockCmd
#define DEBUG_USART_BAUDRATE 115200
// USART GPIO 引脚宏定义
#define DEBUG_USART_GPIO_CLK (RCC_APB2Periph_GPIOA)
#define DEBUG_USART_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
#define DEBUG_USART_TX_GPIO_PORT GPIOA
#define DEBUG_USART_TX_GPIO_PIN GPIO_Pin_9
#define DEBUG_USART_RX_GPIO_PORT GPIOA
#define DEBUG_USART_RX_GPIO_PIN GPIO_Pin_10
#define DEBUG_USART_IRQ USART1_IRQn
#define DEBUG_USART_IRQHandler USART1_IRQHandler
void USART_Config(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
#endif /* __USART_H */
3 dht11的获取温湿度数据
在这里的话不进行过多的原理阐述,因为在网上已经能够找到很多的这方面的资料。主要就是通过IO口从DHT11中把每一个bit都读取出来,然后再通过处理。
在这里的话也是需要按照第二步的两张图进行操作的,先拿到两个文件后,导入到工程中,设置.h的路径即可。
dht11.c
#include "dht11.h"
GPIO_InitTypeDef GPIO_InitStructure; //后面会改变输入输出状态
//结构体声明在最开头
static void GPIO_SETOUT(void);
static void GPIO_SETIN(void);
static u8 DHT11_Check(void);
/**********************************************
函数名:static void DHT11_Rst(void)
参数说明:无
返回值:无
函数作用:主机发送开始信号
***********************************************/
//这是它的物理工作原理,根据原理拉高或拉低它的引脚来唤醒dht11
static void DHT11_Rst(void)
{
GPIO_SETOUT(); //配置成输出模式
GPIO_ResetBits(DHT11_IO,DHT11_PIN); //拉低数据线
Delay_ms(20); //拉低至少18ms
GPIO_SetBits(DHT11_IO,DHT11_PIN); //拉高数据线
Delay_us(30); //主机拉高20~40us
GPIO_ResetBits(DHT11_IO,DHT11_PIN);
}
/**********************************************
函数名:u8 DHT11_Init(void)
参数说明:无
返回值:u8 ,返回1代表初始化成功,0则失败
函数作用:配置IO口,并发送开始信号
***********************************************/
u8 DHT11_Init(void){
//IO口初始化配置
RCC_APB2PeriphClockCmd(DHT11_APB2PeriphRCC,ENABLE);//换IO口需要修改,时钟设置
GPIO_InitStructure.GPIO_Pin = DHT11_PIN; //调用引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出,如果需要考虑到IC的电流驱动能力时要接上拉电阻(5K)
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; //speed 可随意
GPIO_Init(DHT11_IO,&GPIO_InitStructure);
DHT11_Rst();//发送开始信号
return DHT11_Check();//检测DHT11的响应
}
/**********************************************
函数名:static void GPIO_SETOUT(void)
参数说明:无
返回值:无
函数作用:配置IO口为推挽输出模式
***********************************************/
static void GPIO_SETOUT(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出,如果需要考虑到IC的电流驱动能力时要接上拉电阻(5K)
GPIO_Init(DHT11_IO,&GPIO_InitStructure);
}
/**********************************************
函数名:static void GPIO_SETIN(void)
参数说明:无
返回值:无
函数作用:配置IO口为浮空输入模式
***********************************************/
static void GPIO_SETIN(void)
{
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入模式
GPIO_Init(DHT11_IO,&GPIO_InitStructure);
}
/**********************************************
函数名:static u8 DHT11_Check(void)
参数说明:无
返回值:检测到回应-->返回1,否则0
函数作用:检测DHT11的响应信号
***********************************************/
static u8 DHT11_Check(void)
{
u8 retry=0;
GPIO_SETIN(); //设置为输入模式
while (!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//DHT11会拉低40~50us
{
retry++;
Delay_us(1);
}
if(retry >= 100) //超时未响应/未收到开始信号,退出检测
return 0;
else
retry = 0;
while (GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//DHT11拉低后会再次拉高40~50us
{
retry++;
Delay_us(1);
}
if(retry>=100) //超时,DHT11工作出错,退出检测
return 0;
return 1; //设备正常响应,可以正常工作
}
/**********************************************
函数名:static u8 DHT11_Read_Bit(void)
参数说明:无
返回值:返回从DHT11上读取的一个Bit数据
函数作用:从DHT11上读取一个Bit数据
***********************************************/
static u8 DHT11_Read_Bit(void)
{
u8 retry = 0;
//DHT11的Bit开始信号为12-14us低电平
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//等待变为低电平(等待Bit开始信号)
{
retry++;
Delay_us(1);
}
retry = 0;
while(!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//等待变高电平(代表数据开始传输)
{
retry++;
Delay_us(1);
}
Delay_us(30);//等待30us
//0信号为26-28us,1信号则为116-118us,所以说超过30us去读取引脚状态就可以知道传输的值了
if(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN)) return 1;
else return 0;
}
/***********************************************************************
函数名:static u8 DHT11_Read_Byte(void)
参数说明:无
返回值:返回从DHT11上读取的一个byte数据
函数作用:从DHT11上读取一个byte数据
************************************************************************/
static u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
/**************************************************************************
函数名:u8 DHT11_Read_Data(u8 *temp,u8 *humi)
参数说明:temp:用于存放温度值(范围:0~50°),humi:用于存放湿度值(范围:20%~90%)
返回值:1:成功读取数据,0:读取数据出错
函数作用:从DHT11上读取温湿度数据(这里省略小数值)
***************************************************************************/
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==1) //设备响应正常
{
for(i=0;i<5;i++)//读取40位数据
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])//进行校验
{
*humi=buf[0];
*temp=buf[2];
}
}else return 0; //设备未成功响应,返回0
return 1; //读取数据成功返回1
}
dht11.h
#ifndef __DHT11_H
#define __DHT11_H
#include "stm32f10x.h"
#include "delay.h"
/* 设置GPIO脚,默认为PB11 */
#define DHT11_IO GPIOB
#define DHT11_PIN GPIO_Pin_9
#define DHT11_APB2PeriphRCC RCC_APB2Periph_GPIOB
/* 初始化函数,如果DHT11存在响应则返回1,否则0 */
u8 DHT11_Init(void);
/* 从DHT11读取数据,没有小数部分 */
u8 DHT11_Read_Data(u8 *temp,u8 *humi);
#endif
通过测试,便能使用
4.main函数
#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
#include "dht11.h"
void clock_init(void);
u8 temp = 0,humidity = 0; //全局变量
char y='%';
unsigned char i;
/**************************************************************************
函数名:int main(void)
参数说明:无
返回值:无
函数作用:主函数
***************************************************************************/
int main(void)
{
/* 初始化串口 */
USART_Config();
printf("\r\n wecome to usart\n");
/* LED灯初始化 */
LED_GPIO_Config();
printf("wecome to LED\n");
/* DHT1温湿度传感器的初始化 */
DHT11_Init();
printf("wecome to DHT11\n");
Delay_ms(10); //这里延时10ms主要是因为,刚刚接收到响应信息,要等DHT11发送完信息
printf("所有设备均工作正常 ,开始工作......\r\n");
while(1)
{
if(DHT11_Read_Data(&temp,&humidity)) //&temp的地址对应*temp地址,所以数据一样
{
LED_GPIO_HIGH();
printf("\r\n temp: %d'C ,humidity: %d %c",temp,humidity,y); //'%'有实体意义,只能用字符表示
Delay_ms(500);//延时0.5s左右
}
LED_GPIO_LOW();
Delay_ms(500);
}
}
/**************************************************************************
函数名:void clock_init(void)
参数说明:无
返回值:无
函数作用:开启高速外部时钟,
ADCCLK设置为12MHZ, SYSCLK设置为72Mhz,PCLK1设置为36MHZ,PKLC2设置为72mhz
***************************************************************************/
void clock_init(void)
{
RCC->CR = 0x1010000;
RCC->CFGR = 0x1DC402;
}
5.工程源码
百度网盘:链接:https://pan.baidu.com/s/1xndzYzxgGfyw6CU-4HVUEw?pwd=am9a 提取码:am9a --来自百度网盘超级会员V2的分享
如果已过期的话可以私信博主,或者是到这里下载。