条件1.使用stm32f103c8t6,c语言编程条件2.使用PA1,PA2,PA3,PA4,PA5,PA6配置成上拉输入,根据6个引脚的电平高低生成。例如000000表示6引脚电平全为0,并使用串口1以字符串形式发送该二进制数到PC端。统计该二进制数中0的次数记作zeo_count;条件3.串口1使用中断接收数据,结束符0X0D,0X0A.条件4.条件2中的zeo_count与下一次条件2中的zeo_count比较,若前一次zeo_count大于后一次zeo_count;则打开蜂鸣器条件5.若串口接收字符串为"1",关闭蜂鸣器; 若串口接收字符串为"2",打开蜂鸣器;若串口接收字符串为"3"且条件2中zeo_count=1,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收字符串为"4"且条件2中zeo_count=2,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收字符串为"5"且条件2中zeo_count=3,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收字符串为"6"且条件2中zeo_count=4,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收字符串为"7"且条件2中zeo_count=5,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收字符串为"8"且条件2中zeo_count=6,则发送字符串"拿取数量正确",否则发送拿取数量异常;若串口接收为空,则进入条件4;条件6.配置PA11引脚为下拉输入,读取PA11电平为高,进入条件5;若PA11电平为低,进入条件4
时间: 2023-05-20 17:03:21 浏览: 107
STM32F103C8T6脚位分布图.pdf
5星 · 资源好评率100%
我可以回答这个问题。以下是代码:
```
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
#define USART1_DR_Base 0x40013804
void USART1_Init(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART1_SendString(char* s);
void USART1_IRQHandler(void);
int main(void)
{
GPIO_Configuration();
USART1_Init();
NVIC_Configuration();
uint8_t last_zero_count = 0;
uint8_t zero_count = 0;
uint8_t buzzer_on = 0;
while (1)
{
// Read the input pins
uint8_t pin1 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
uint8_t pin2 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2);
uint8_t pin3 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3);
uint8_t pin4 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4);
uint8_t pin5 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);
uint8_t pin6 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6);
// Convert the input pins to a binary string
char binary[7];
sprintf(binary, "%d%d%d%d%d%d", pin1, pin2, pin3, pin4, pin5, pin6);
// Count the number of zeros in the binary string
zero_count = 0;
for (int i = 0; i < strlen(binary); i++)
{
if (binary[i] == '0')
{
zero_count++;
}
}
// Send the binary string over USART1
USART1_SendString(binary);
// Compare the zero count to the last zero count
if (zero_count < last_zero_count)
{
buzzer_on = 1;
}
last_zero_count = zero_count;
// Wait for USART1 to receive a string
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
// Read the received string
char received[2];
received[0] = USART_ReceiveData(USART1);
received[1] = '\0';
// Handle the received string
if (strcmp(received, "1") == 0)
{
buzzer_on = 0;
}
else if (strcmp(received, "2") == 0)
{
buzzer_on = 1;
}
else if (strcmp(received, "3") == 0 && zero_count == 1)
{
USART1_SendString("拿取数量正确");
}
else if (strcmp(received, "4") == 0 && zero_count == 2)
{
USART1_SendString("拿取数量正确");
}
else if (strcmp(received, "5") == 0 && zero_count == 3)
{
USART1_SendString("拿取数量正确");
}
else if (strcmp(received, "6") == 0 && zero_count == 4)
{
USART1_SendString("拿取数量正确");
}
else if (strcmp(received, "7") == 0 && zero_count == 5)
{
USART1_SendString("拿取数量正确");
}
else if (strcmp(received, "8") == 0 && zero_count == 6)
{
USART1_SendString("拿取数量正确");
}
else if (received[0] == '\r' || received[0] == '\n')
{
// Do nothing
}
else
{
USART1_SendString("拿取数量异常");
}
// Turn the buzzer on or off
if (buzzer_on)
{
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Enable GPIOA and GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// Configure PA1, PA2, PA3, PA4, PA5, PA6 as input with pull-up
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure PA11 as input with pull-down
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure PB0 as output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void USART1_Init(void)
{
USART_InitTypeDef USART_InitStructure;
// Enable USART1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// Configure USART1 TX and RX pins
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1
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);
// Enable USART1
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// Configure the USART1 interrupt
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);
// Enable the USART1 receive interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void USART1_SendString(char* s)
{
while (*s)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, *s++);
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
```
阅读全文