stm32f103c8t6控制雨滴传感器并显示数据在LCD上的代码
时间: 2023-12-18 15:05:36 浏览: 73
雨滴传感器模块
这里给出一个基于STM32F103C8T6控制雨滴传感器并显示数据在LCD上的代码,具体实现如下:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "lcd.h"
#define RAIN_SENSOR_PIN GPIO_Pin_0
#define RAIN_SENSOR_PORT GPIOA
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
ADC_InitTypeDef ADC_InitStruct;
TIM_TimeBaseInitTypeDef TIM_InitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
// 初始化系统时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM16, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
SystemInit();
// 初始化GPIOA
GPIO_InitStruct.GPIO_Pin = RAIN_SENSOR_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(RAIN_SENSOR_PORT, &GPIO_InitStruct);
// 初始化ADC1
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_InitStruct.ADC_ScanConvMode = DISABLE;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_Cmd(ADC1, ENABLE);
// 配置ADC通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
// 初始化TIM1
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct.TIM_Prescaler = 7200 - 1;
TIM_InitStruct.TIM_Period = 10000 - 1;
TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1, &TIM_InitStruct);
// 初始化TIM1 OC通道
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_Pulse = 5000;
TIM_OC1Init(TIM1, &TIM_OCInitStruct);
TIM_Cmd(TIM1, ENABLE);
// 初始化TIM2
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct.TIM_Prescaler = 7200 - 1;
TIM_InitStruct.TIM_Period = 10000 - 1;
TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_InitStruct);
// 启用TIM2中断
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_EnableIRQ(TIM2_IRQn);
// 初始化TIM16
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct.TIM_Prescaler = 7200 - 1;
TIM_InitStruct.TIM_Period = 10000 - 1;
TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM16, &TIM_InitStruct);
// 启用TIM16中断
TIM_ITConfig(TIM16, TIM_IT_Update, ENABLE);
NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);
// 初始化LCD
LCD_Init();
while (1)
{
// 等待中断触发
}
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// 启动ADC转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
}
void TIM1_UP_TIM16_IRQHandler(void)
{
if (TIM_GetITStatus(TIM16, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM16, TIM_IT_Update);
// 读取ADC值
uint16_t adc_value = ADC_GetConversionValue(ADC1);
// 显示ADC值
char adc_str[5];
sprintf(adc_str, "%d", adc_value);
LCD_ShowString(0, 0, adc_str);
// 控制LED
if (adc_value > 500)
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
else
{
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
}
}
```
上述代码主要实现的功能是:通过定时器2触发ADC转换,然后通过定时器16的中断处理函数读取ADC值,并将其显示在LCD上,同时根据ADC值控制LED的亮灭。其中,定时器1用于控制PWM输出,定时器2和定时器16分别用于触发ADC转换和读取ADC值。需要注意的是,此代码中使用的是PA0引脚作为雨滴传感器输入,如果使用其他引脚,需要对应修改代码。
阅读全文