基于stm32的控制两个光源开关,双通道电压读取电压并用串口输出的程序
时间: 2024-05-21 19:11:09 浏览: 9
以下是基于STM32的控制两个光源开关、双通道电压读取电压并用串口输出的程序示例:
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_usart.h"
#define LED1_PIN GPIO_Pin_8 //LED1引脚
#define LED2_PIN GPIO_Pin_9 //LED2引脚
#define ADC1_PIN GPIO_Pin_0 //ADC1通道0引脚
#define ADC2_PIN GPIO_Pin_1 //ADC1通道1引脚
#define USART_TX_PIN GPIO_Pin_9 //USART1_TX引脚
#define USART_RX_PIN GPIO_Pin_10 //USART1_RX引脚
void GPIO_Configuration(void);
void ADC_Configuration(void);
void USART_Configuration(void);
void USART_SendString(USART_TypeDef* USARTx, char* str);
int main(void)
{
GPIO_Configuration();
ADC_Configuration();
USART_Configuration();
while(1)
{
//控制LED1亮灭
GPIO_SetBits(GPIOB, LED1_PIN);
Delay(500);
GPIO_ResetBits(GPIOB, LED1_PIN);
Delay(500);
//控制LED2亮灭
GPIO_SetBits(GPIOB, LED2_PIN);
Delay(500);
GPIO_ResetBits(GPIOB, LED2_PIN);
Delay(500);
//读取ADC1和ADC2的电压值并串口输出
uint16_t adc1_value = ADC_GetConversionValue(ADC1);
uint16_t adc2_value = ADC_GetConversionValue(ADC2);
char str[50];
sprintf(str, "ADC1 Value: %d, ADC2 Value: %d\r\n", adc1_value, adc2_value);
USART_SendString(USART1, str);
Delay(1000);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//配置LED1和LED2引脚为推挽输出,初始状态为低电平
GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//配置ADC1通道0引脚为模拟输入
GPIO_InitStructure.GPIO_Pin = ADC1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置ADC1通道1引脚为模拟输入
GPIO_InitStructure.GPIO_Pin = ADC2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//使能ADC1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//配置ADC1为扫描模式,连续转换模式,采样周期为28.5个时钟周期
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
//配置ADC1通道0为采样周期为28.5个时钟周期,采样时间为239.5个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
//配置ADC1通道1为采样周期为28.5个时钟周期,采样时间为239.5个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);
//使能ADC1
ADC_Cmd(ADC1, ENABLE);
//校准ADC1
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//启动ADC1转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//使能USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//配置USART1_TX引脚为复用推挽输出
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART1_RX引脚为浮空输入
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART1为波特率为115200,8位数据位,1位停止位,无奇偶校验位,无硬件流控制
USART_InitStructure.USART_BaudRate = 115200;
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_Init(USART1, &USART_InitStructure);
//启动USART1
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(USART_TypeDef* USARTx, char* str)
{
while(*str)
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}
void Delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)