是用stm的开发板以及keil5的程序包
时间: 2024-02-27 17:58:52 浏览: 59
1-开发板入门资料_stm32_
好的,这里给您提供一个基于STM32开发板和Keil5程序包的浊度传感器程序源码。
首先,您需要在Keil5中新建一个工程,选择适合您的开发板型号,然后在工程文件夹下新建一个.c文件,并将以下代码复制到该文件中。
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_adc.h"
#include "stdio.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
uint16_t ADCConvertedValue;
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
ADC_Cmd(ADC1, ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
int main(void)
{
GPIO_Config();
ADC_Config();
while (1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_0);
ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
ADCConvertedValue = ADC_GetConversionValue(ADC1);
printf("Turbidity Value: %d%%\n", ADCConvertedValue*100/4095);
GPIO_ResetBits(GPIOC, GPIO_Pin_0);
Delay(1000);
}
}
void ADC_IRQHandler(void)
{
if (ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET)
{
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
}
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
```
该程序使用了STM32F4系列的ADC模块和GPIO模块,通过读取ADC转换的模拟值,将其转换为百分比形式的浊度值,并将其打印到串口监视器中。同时,程序还使用了一个GPIO口控制LED灯的亮灭,您可以将其与您的浊度传感器模块相连,根据需要进行修改。
最后,编译并下载程序到开发板中进行测试即可。
阅读全文