条件1.使用stm32f103c8t6,c语言编程条件2.使用PA1,PA2,PA3,PA4,PA5,PA6配置成上拉输入,根据6个引脚的电平高低生成。例如000000表示6引脚电平全为0,并使用串口1以字符串形式发送该二进制数到PC端。统计该二进制数中0的次数记作zeo_count;条件3.配置PA11引脚为下拉输入;条件3.串口1使用中断接收数据,帧头FF,结束符0X0D,0X0A.帧头不是FF则重新接收,不以0X0D,0X0A结束符则重新接收;条件4.若PA11电平为0或者串口1没有接收到数据,则记录1分钟内条件2中的最大值zeo_count;记作zeo_count_max;条件5.若zeo_count_max大于zeo_count,打开蜂鸣器;若zeo_count_max小于等于zeo_count,则zeo_count_max==zeo_count;条件6.若PA11电平为1且串口1接收数据不为空,则若串口接收字符串为"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,则发送字符串"拿取数量正确",否则发送拿取数量异常;
时间: 2023-05-22 18:01:06 浏览: 147
我理解你的问题是要我编写一个程序,根据6个引脚的电平高低生成二进制数,并使用串口1以字符串形式发送该二进制数到PC端。同时,程序还需要统计该二进制数中0的次数记作zero_count,并记录1分钟内条件2中的最大值zero_count_max。如果zero_count_max大于zero_count,则打开蜂鸣器;如果zero_count_max小于等于zero_count,则zero_count_max等于zero_count。如果PA11电平为1且串口1接收数据不为空,则根据接收到的字符串执行相应的操作。
以下是程序的代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
#define MAX_BUF_SIZE 100
volatile uint8_t buf[MAX_BUF_SIZE];
volatile uint8_t buf_pos = 0;
volatile uint8_t data_received = 0;
volatile uint32_t zero_count = 0;
volatile uint32_t zero_count_max = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART1);
if (data == 0xFF)
{
buf_pos = 0;
}
else if (data == 0x0D && buf_pos > 0 && buf[buf_pos - 1] == 0x0A)
{
buf[buf_pos] = 0;
data_received = 1;
}
else if (buf_pos < MAX_BUF_SIZE - 1)
{
buf[buf_pos++] = data;
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_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_InitTypeDef NVIC_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_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++)
{
__NOP();
}
}
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
uint8_t binary = 0;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) << 0;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) << 1;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) << 2;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) << 3;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) << 4;
binary |= GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) << 5;
char str[10];
sprintf(str, "%d\r\n", binary);
USART_SendData(USART1, (uint8_t *)str);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
uint32_t count = 0;
for (int i = 0; i < 6; i++)
{
if (binary & (1 << i))
{
count++;
}
}
zero_count = 6 - count;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == 0 || !data_received)
{
if (zero_count > zero_count_max)
{
zero_count_max = zero_count;
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
else
{
zero_count_max = zero_count;
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
}
else if (data_received)
{
if (strcmp((char *)buf, "1") == 0)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
else if (strcmp((char *)buf, "2") == 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
else if (strcmp((char *)buf, "3") == 0 && zero_count == 1)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else if (strcmp((char *)buf, "4") == 0 && zero_count == 2)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else if (strcmp((char *)buf, "5") == 0 && zero_count == 3)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else if (strcmp((char *)buf, "6") == 0 && zero_count == 4)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else if (strcmp((char *)buf, "7") == 0 && zero_count == 5)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else if (strcmp((char *)buf, "8") == 0 && zero_count == 6)
{
USART_SendData(USART1, (uint8_t *)"拿取数量正确\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
else
{
USART_SendData(USART1, (uint8_t *)"拿取数量异常\r\n");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
;
}
buf_pos = 0;
data_received = 0;
}
delay_ms(1000);
}
}
```
阅读全文