STM32F1串口和光敏都能控制灯的代码
时间: 2024-05-15 22:15:30 浏览: 56
以下是一个使用STM32F1的串口和光敏控制灯的代码示例:
```C
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_adc.h"
#define LED_PIN GPIO_Pin_13
#define LED_GPIO_PORT GPIOC
#define LED_GPIO_CLK RCC_APB2Periph_GPIOC
#define USART1_CLK RCC_APB2Periph_USART1
#define USART1_GPIO_CLK (RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO)
#define USART1_TX_PIN GPIO_Pin_9
#define USART1_RX_PIN GPIO_Pin_10
#define USART1_GPIO GPIOA
#define ADC_CLK RCC_APB2Periph_ADC1
#define ADC_GPIO_PIN GPIO_Pin_0
#define ADC_GPIO_PORT GPIOA
#define ADC_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADC_CHANNEL ADC_Channel_0
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
void GPIO_Configuration(void);
void USART1_Configuration(void);
void ADC_Configuration(void);
int main(void)
{
uint16_t adc_value = 0;
uint8_t uart_data = 0;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);
RCC_APB2PeriphClockCmd(ADC_CLK, ENABLE);
GPIO_Configuration();
USART1_Configuration();
ADC_Configuration();
while (1)
{
// 读取串口数据
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
uart_data = USART_ReceiveData(USART1);
if (uart_data == '1')
{
GPIO_SetBits(LED_GPIO_PORT, LED_PIN);
}
else if (uart_data == '0')
{
GPIO_ResetBits(LED_GPIO_PORT, LED_PIN);
}
}
// 读取光敏电阻值
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
adc_value = ADC_GetConversionValue(ADC1);
// 根据光敏电阻值控制灯
if (adc_value < 1000)
{
GPIO_SetBits(LED_GPIO_PORT, LED_PIN);
}
else
{
GPIO_ResetBits(LED_GPIO_PORT, LED_PIN);
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
void USART1_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = USART1_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART1_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void ADC_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = ADC_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_GPIO_PORT, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_RegularChannelConfig(ADC1, ADC_CHANNEL, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
```
该代码使用USART1和光敏电阻控制LED。当串口接收到字符'1'时,LED亮起;当接收到字符'0'时,LED熄灭。同时,通过ADC读取光敏电阻的值,如果小于1000则LED亮起,否则LED熄灭。需要注意的是,该代码中的光敏电阻必须连接到ADC1的PA0通道上。
阅读全文