条件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-17 08:04:32 浏览: 83
以下是符合条件的C语言代码:
#include "stm32f10x.h"
#include "stdio.h"
#define BUFSIZE 64
volatile uint8_t rx_buffer[BUFSIZE];
volatile uint8_t rx_wr_index, rx_rd_index, rx_counter;
volatile uint8_t tx_buffer[BUFSIZE];
volatile uint8_t tx_wr_index, tx_rd_index, tx_counter;
volatile uint8_t rx_flag_end_line = 0;
void USART1_IRQHandler(void)
{
uint8_t rx_data;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
rx_data = USART_ReceiveData(USART1);
if ((rx_data != '\n') && (rx_data != '\r'))
{
rx_buffer[rx_wr_index++] = rx_data;
if (rx_wr_index == BUFSIZE)
{
rx_wr_index = 0;
}
}
else
{
rx_flag_end_line = 1;
}
}
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
if (tx_counter)
{
USART_SendData(USART1, tx_buffer[tx_rd_index++]);
if (tx_rd_index == BUFSIZE)
{
tx_rd_index = 0;
}
tx_counter--;
}
else
{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &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);
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_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
{
for (j = 0; j < 2000; j++);
}
}
int main(void)
{
uint8_t binary_num = 0;
uint8_t last_zero_count = 0;
uint8_t current_zero_count = 0;
uint8_t buzzer_on = 0;
USART1_Init();
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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
while (1)
{
binary_num = 0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET)
{
binary_num |= 0x01;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == RESET)
{
binary_num |= 0x02;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == RESET)
{
binary_num |= 0x04;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == RESET)
{
binary_num |= 0x08;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == RESET)
{
binary_num |= 0x10;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == RESET)
{
binary_num |= 0x20;
}
printf("%d\n", binary_num);
if (rx_flag_end_line)
{
rx_flag_end_line = 0;
if (rx_buffer[0] == '1')
{
buzzer_on = 1;
}
else if (rx_buffer[0] == '2')
{
buzzer_on = 0;
}
rx_counter = 0;
rx_rd_index = 0;
rx_wr_index = 0;
}
current_zero_count = 0;
if ((binary_num & 0x01) == 0)
{
current_zero_count++;
}
if ((binary_num & 0x02) == 0)
{
current_zero_count++;
}
if ((binary_num & 0x04) == 0)
{
current_zero_count++;
}
if ((binary_num & 0x08) == 0)
{
current_zero_count++;
}
if ((binary_num & 0x10) == 0)
{
current_zero_count++;
}
if ((binary_num & 0x20) == 0)
{
current_zero_count++;
}
if (current_zero_count > last_zero_count)
{
buzzer_on = 1;
}
else if (current_zero_count < last_zero_count)
{
buzzer_on = 1;
}
last_zero_count = current_zero_count;
if (buzzer_on)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
delay_ms(500);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
delay_ms(500);
}
else
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
}
}
阅读全文