条件1.使用stm32f103c8t6,c语言编程条件2.使用PA0,PA1,PA2,PA3,PA4,PA5。配置成上拉输入,根据6个引脚的电平高低生成二进制数。例如000000表示6引脚电平全为0;并使用串口1以字符串形式发送该二进制数到PC端。条件3.串口1使用中断接收数据,结束符0X0D,0X0A.条件4.串口1接收字符串"1"打开蜂鸣器,接收字符串"2"关闭蜂鸣器;条件5.统计条件2中二进制数0的次数,若后一次条件2的二进制数0的次数大于前一次二进制数0的次数;若后一次条件2的二进制数0的次数小于前一次二进制数0的次数;打开蜂鸣器。
时间: 2023-05-18 08:04:49 浏览: 99
以下是符合条件的C语言代码:
#include "stm32f10x.h"
#include "stdio.h"
#define BUFSIZE 64
volatile uint8_t rx_buffer[BUFSIZE];
volatile uint8_t rx_wr_index = 0;
volatile uint8_t rx_counter = 0;
volatile uint8_t rx_data = 0;
volatile uint8_t buzzer_on = 0;
volatile uint8_t last_zero_count = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rx_data = USART_ReceiveData(USART1);
if (rx_data == 0x0D || rx_data == 0x0A)
{
rx_buffer[rx_wr_index] = 0;
rx_counter++;
rx_wr_index = 0;
}
else
{
rx_buffer[rx_wr_index] = rx_data;
rx_wr_index++;
if (rx_wr_index == BUFSIZE)
{
rx_wr_index = 0;
}
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
uint8_t zero_count = 0;
uint8_t binary = 0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET)
{
zero_count++;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == RESET)
{
zero_count++;
binary |= 0x01;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == RESET)
{
zero_count++;
binary |= 0x02;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == RESET)
{
zero_count++;
binary |= 0x03;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == RESET)
{
zero_count++;
binary |= 0x04;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == RESET)
{
zero_count++;
binary |= 0x05;
}
if (zero_count != last_zero_count)
{
if (zero_count > last_zero_count)
{
buzzer_on = 1;
}
else
{
buzzer_on = 0;
}
last_zero_count = zero_count;
}
char buffer[10];
sprintf(buffer, "%d\r\n", binary);
USART_SendString(USART1, buffer);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
void USART_SendString(USART_TypeDef* USARTx, char* str)
{
while (*str)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *str++);
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
TIM_Configuration();
while (1)
{
if (rx_counter > 0)
{
if (rx_buffer[0] == '1')
{
buzzer_on = 1;
}
else if (rx_buffer[0] == '2')
{
buzzer_on = 0;
}
rx_counter--;
for (uint8_t i = 0; i < rx_counter; i++)
{
rx_buffer[i] = rx_buffer[i + 1];
}
}
}
}
注意:以上代码仅供参考,可能存在错误或不完整的地方,需要根据实际情况进行修改和完善。
阅读全文