条件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 16:02:03 浏览: 119
我理解你的问题是需要我编写一个程序,根据6个引脚的电平高低生成二进制数,并使用串口1以字符串形式发送该二进制数到PC端。同时,需要统计该二进制数中0的次数记作zeo_count,并记录1分钟内条件2中的最大值zeo_count_max。如果zeo_count_max大于zeo_count,则打开蜂鸣器;如果zeo_count_max小于等于zeo_count,则zeo_count_max等于zeo_count。如果PA11电平为1且串口1接收数据不为空,则根据接收到的字符串执行相应的操作。
以下是我编写的程序:
```c
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
#define MAX_COUNT 60 // 最大计数时间,单位为秒
#define FRAME_HEAD 0xFF // 帧头
#define FRAME_TAIL1 0x0D // 结束符1
#define FRAME_TAIL2 0x0A // 结束符2
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
volatile uint32_t tick_count = 0; // 计数器,每秒加1
volatile uint32_t zero_count = 0; // 0的个数
volatile uint32_t zero_count_max = 0; // 最大0的个数
volatile uint8_t receive_buffer[32]; // 接收缓冲区
volatile uint8_t receive_index = 0; // 接收缓冲区索引
volatile uint8_t receive_flag = 0; // 接收标志位
volatile uint8_t buzzer_flag = 0; // 蜂鸣器标志位
void GPIO_Configuration(void)
{
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);
}
void USART_Configuration(void)
{
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);
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 TIM_Configuration(void)
{
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);
TIM_Cmd(TIM2, ENABLE);
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART1);
if (data == FRAME_HEAD)
{
receive_index = 0;
receive_flag = 0;
}
else if (data == FRAME_TAIL1)
{
receive_flag |= 0x01;
}
else if (data == FRAME_TAIL2)
{
receive_flag |= 0x02;
}
else
{
receive_buffer[receive_index++] = data;
if (receive_index >= sizeof(receive_buffer))
{
receive_index = 0;
receive_flag = 0;
}
}
}
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
tick_count++;
if (tick_count >= MAX_COUNT)
{
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_RESET || receive_flag == 0)
{
if (zero_count > zero_count_max)
{
zero_count_max = zero_count;
buzzer_flag = 1;
}
else
{
zero_count_max = zero_count;
}
zero_count = 0;
}
tick_count = 0;
}
uint8_t value = 0;
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == Bit_RESET)
{
value |= 0x01;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == Bit_RESET)
{
value |= 0x02;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == Bit_RESET)
{
value |= 0x04;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == Bit_RESET)
{
value |= 0x08;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == Bit_RESET)
{
value |= 0x10;
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == Bit_RESET)
{
value |= 0x20;
}
zero_count = __builtin_popcount(value);
char buffer[10];
sprintf(buffer, "%06X", value);
USART_SendData(USART1, buffer);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
int main(void)
{
GPIO_Configuration();
USART_Configuration();
TIM_Configuration();
while (1)
{
if (buzzer_flag)
{
// 打开蜂鸣器
}
else
{
// 关闭蜂鸣器
}
if (receive_flag == 0x03)
{
if (memcmp(receive_buffer, "1", 1) == 0)
{
// 关闭蜂鸣器
}
else if (memcmp(receive_buffer, "2", 1) == 0)
{
// 打开蜂鸣器
}
else if (memcmp(receive_buffer, "3", 1) == 0 && zero_count == 1)
{
USART_SendData(USART1, "拿取数量正确");
}
else if (memcmp(receive_buffer, "4", 1) == 0 && zero_count == 2)
{
USART_SendData(USART1, "拿取数量正确");
}
else if (memcmp(receive_buffer, "5", 1) == 0 && zero_count == 3)
{
USART_SendData(USART1, "拿取数量正确");
}
else if (memcmp(receive_buffer, "6", 1) == 0 && zero_count == 4)
{
USART_SendData(USART1, "拿取数量正确");
}
else if (memcmp(receive_buffer, "7", 1) == 0 && zero_count == 5)
{
USART_SendData(USART1, "拿取数量正确");
}
else if (memcmp(receive_buffer, "8", 1) == 0 && zero_count == 6)
{
USART_SendData(USART1, "拿取数量正确");
}
else
{
USART_SendData(USART1, "拿取数量异常");
}
receive_flag = 0;
}
}
}
```
该程序使用了定时器2来实现计数器功能,每秒钟触发一次中断。在中断处理函数中,读取6个引脚的电平高低,统计0的个数,并将6个引脚的状态以16进制字符串的形式发送到串口1。同时,如果计数器达到1分钟,且PA11电平为0或者串口1没有接收到数据,则记录1分钟内0的最大个数,并根据最大0的个数打开或关闭蜂鸣器。如果PA11电平为1且串口1接收到数据,则根据接收到的字符串执行相应的操作。
请注意,这只是一个简单的示例程序,可能存在一些问题,需要根据实际情况进行修改和优化。
阅读全文