将STM32-F407VGT6的DS18B20测温模块,雨水检测模块,直流电机模块整合在一起,雨水检测模块检测到雨水时直流电机开始转动,否则就停止。温度显示在该实验班上的四位数码管上,DSB18B20使用的是led.h,key.h,usart1.h,systick.h,timer.h,ds18b20.h,这几个头文件,雨水检测模块使用到的是led.h,stm32f4xx.h,key.h,usart1.h,systick.h,stdio.h,beep.h,timer.h,adc.h,ds18b20.h这些头文件,直流电机使用到了stm32f4xx.h,led.h,key.h,usart1.h,stdio.h,systick.h,beep.h,timer.h这些头文件。其中雨水检测模块只使用了检测了模拟输出A0使用了ADC1_CH12,所以需要使用杜邦线将A0和PC2连接。ds18b20连接在实验板的PB12引脚,直流电机连接在实验板的PA6引脚,写能够实现该功能的主函数代码,所有模块都已具有,也请帮我把初始化代码写上去
时间: 2023-12-10 21:37:18 浏览: 159
以下是该功能的主函数代码和初始化代码,其中涉及到的头文件已经按照您的需求包含了:
```
#include <stdio.h>
#include "stm32f4xx.h"
#include "led.h"
#include "key.h"
#include "usart1.h"
#include "systick.h"
#include "beep.h"
#include "timer.h"
#include "adc.h"
#include "ds18b20.h"
#define MOTOR_PIN GPIO_Pin_6
#define MOTOR_PORT GPIOA
#define MOTOR_RCC RCC_AHB1Periph_GPIOA
#define RAIN_PIN GPIO_Pin_2
#define RAIN_PORT GPIOC
#define RAIN_RCC RCC_AHB1Periph_GPIOC
#define RAIN_ADC ADC1
#define RAIN_CH ADC_Channel_12
void motor_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(MOTOR_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = MOTOR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(MOTOR_PORT, &GPIO_InitStructure);
}
void rain_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RAIN_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = RAIN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(RAIN_PORT, &GPIO_InitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_Init(RAIN_ADC, &ADC_InitStructure);
ADC_RegularChannelConfig(RAIN_ADC, RAIN_CH, 1, ADC_SampleTime_84Cycles);
ADC_Cmd(RAIN_ADC, ENABLE);
}
void display_temperature(float temp)
{
uint8_t disp_buf[4] = {0, 0, 0, 0};
uint16_t temp_int;
uint16_t temp_frac;
temp_int = (uint16_t) temp;
temp_frac = (uint16_t) ((temp - temp_int) * 100);
disp_buf[3] = temp_int % 10;
disp_buf[2] = (temp_int / 10) % 10;
disp_buf[1] = '-';
disp_buf[0] = temp_frac % 10;
LED_Show(disp_buf);
}
int main(void)
{
float temperature;
uint16_t rain_value;
LED_Init();
KEY_Init();
BEEP_Init();
SYSTICK_Init();
TIMER3_Init();
USART1_Init(115200);
motor_init();
rain_init();
DS18B20_Init();
while (1)
{
temperature = DS18B20_GetTemp_SkipRom();
display_temperature(temperature);
rain_value = ADC_GetConversionValue(RAIN_ADC);
if (rain_value > 1000)
{
GPIO_SetBits(MOTOR_PORT, MOTOR_PIN);
}
else
{
GPIO_ResetBits(MOTOR_PORT, MOTOR_PIN);
}
delay_ms(1000);
}
}
```
需要注意的是,初始化代码中没有包含DS18B20和定时器3的初始化,您需要自行添加。同时,如果您的编译器版本较新,可能需要修改ADC初始化代码中的`ADC_ExternalTrigConv_T1_CC1`为`ADC_ExternalTrigConvEdge_None`。
阅读全文