stm32温度调节系统的风扇转速和温度串口打印输出代码
时间: 2024-01-14 09:05:09 浏览: 55
C语言开发基于stm32的智能风扇系统源码.zip
5星 · 资源好评率100%
以下是一个简单的基于STM32的温度调节系统的风扇转速和温度串口打印输出代码。代码中使用了STM32的定时器和ADC模块来实现温度的采集和风扇转速的控制。同时,使用了串口通信模块将温度和风扇转速输出到PC端进行监控。
```
#include "stm32f10x.h"
#define FAN_PIN GPIO_Pin_0
#define FAN_PORT GPIOA
#define TEMP_PIN GPIO_Pin_1
#define TEMP_PORT GPIOA
#define UART_TX_PIN GPIO_Pin_9
#define UART_TX_PORT GPIOA
#define UART_RX_PIN GPIO_Pin_10
#define UART_RX_PORT GPIOA
#define BAUD_RATE 9600
uint16_t adc_value = 0;
float temperature = 0;
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
// FAN_PIN
GPIO_InitStructure.GPIO_Pin = FAN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(FAN_PORT, &GPIO_InitStructure);
// TEMP_PIN
GPIO_InitStructure.GPIO_Pin = TEMP_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(TEMP_PORT, &GPIO_InitStructure);
// UART_TX_PIN
GPIO_InitStructure.GPIO_Pin = UART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART_TX_PORT, &GPIO_InitStructure);
// UART_RX_PIN
GPIO_InitStructure.GPIO_Pin = UART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART_RX_PORT, &GPIO_InitStructure);
}
void TIM_Config()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void ADC_Config()
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_28Cycles5);
}
void USART_Config()
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = BAUD_RATE;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void send_char(char c)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, c);
}
void send_string(char* str)
{
while (*str)
{
send_char(*str);
str++;
}
}
void print_temperature()
{
char buf[100];
sprintf(buf, "Temperature: %.2f\n", temperature);
send_string(buf);
}
void print_fan_speed(uint16_t speed)
{
char buf[100];
sprintf(buf, "Fan Speed: %d rpm\n", speed);
send_string(buf);
}
void update_fan_speed()
{
uint16_t speed = adc_value / 4;
TIM2->CCR1 = speed;
print_fan_speed(speed);
}
void update_temperature()
{
float voltage = (float)adc_value / 4096 * 3.3;
temperature = (voltage - 0.76) / 0.0025;
print_temperature();
}
int main(void)
{
GPIO_Config();
TIM_Config();
ADC_Config();
USART_Config();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
adc_value = ADC_GetConversionValue(ADC1);
update_temperature();
update_fan_speed();
}
}
```
需要注意的是,本代码仅供参考,具体实现还需要根据实际情况进行调整。另外,串口通信部分需要与PC端的监控程序相对应才能正常工作。
阅读全文