条件1.使用stm32f103c8t6,c语言编程条件2.使用PA1,PA2,PA3,PA4,PA5,PA6配置成上拉输入,根据6个引脚的电平高低生成。例如000000表示6引脚电平全为0,统计该二进制数中0的次数记作zeo_count;将该二进制数转成16进制数,并作为条件3中的数据。条件3.串口1发数据,满足串口数据校验 (包头0xee)(数据长度)(...数据...)(数据长度反码)(包尾0xef),按照串口协议通信发送条件2中的二进制数;串口1收数据,满足串口数据校验 (包头0xee)(数据长度)(...数据...)(数据长度反码)(包尾0xef);不满足重新接收。直到成功接收。条件4.规定zeo_count_old.默认值为6;若串口1接收数据为“0xee0x010x010x010xef”则zeo_count_old=1;若串口1接收数据为“0xee0x010x020x010xef”则zeo_count_old=2;若串口1接收数据为“0xee0x010x030x010xef”则zeo_count_old=3;若串口1接收数据为“0xee0x010x040x010xef”则zeo_count_old=4;若串口1接收数据为“0xee0x010x050x010xef”则zeo_count_old=5;若串口1接收数据为“0xee0x010x060x010xef”则zeo_count_old=6;条件5.若zeo_count_old大于zeo_count,打开蜂鸣器;则若zeo_count_old小于zeo_count,串口发送字符串"规定设备未取完",等待1分钟后将zeo_count_old更新为zeo_coun的值;若zeo_count_old等于zeo_count,串口发送字符串"设备数量正常";
时间: 2023-05-26 18:01:49 浏览: 124
以下是参考代码,仅供参考,需要根据实际情况进行修改:
```
#include "stm32f10x.h"
#include "stdio.h"
#define BUFSIZE 100
uint16_t zero_count_old = 6;
uint16_t zero_count = 0;
uint8_t uart_buf[BUFSIZE];
uint8_t uart_buf_index = 0;
void USART1_IRQHandler(void) {
uint8_t ch;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
ch = USART_ReceiveData(USART1);
if (uart_buf_index == 0 && ch != 0xee) {
return;
}
uart_buf[uart_buf_index++] = ch;
if (uart_buf_index == 4) {
if (uart_buf[1] != 0x01 || uart_buf[2] > 0x06) {
// 数据长度不对,重新接收
uart_buf_index = 0;
}
} else if (uart_buf_index == uart_buf[1]+4) {
uint8_t sum = 0;
for (uint8_t i = 0; i < uart_buf[1]+2; i++) {
sum += uart_buf[i];
}
sum = ~sum;
if (sum != uart_buf[uart_buf_index-2] || uart_buf[uart_buf_index-1] != 0xef) {
// 校验错误,重新接收
uart_buf_index = 0;
} else {
// 收到正确的数据,更新 zero_count_old
zero_count_old = uart_buf[2];
uart_buf_index = 0;
}
}
}
}
void delay_ms(uint32_t ms) {
for (uint32_t i = 0; i < ms*8000; i++);
}
void init_uart(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 打开 GPIO 和 USART1 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
// 配置 USART1 引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置 USART1
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
// 配置 USART1 中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 打开 USART1
USART_Cmd(USART1, ENABLE);
}
void init_gpio(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 打开 GPIO 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置 PA1-PA6 为上拉输入
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);
}
uint8_t get_gpio_value(void) {
uint8_t value = 0;
for (uint8_t i = 1; i <= 6; i++) {
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0+i)) {
value |= (1<<(6-i));
}
}
return value;
}
void send_data(uint8_t *data, uint8_t len) {
uint8_t sum = 0;
USART_SendData(USART1, 0xee);
USART_SendData(USART1, len);
for (uint8_t i = 0; i < len; i++) {
USART_SendData(USART1, data[i]);
sum += data[i];
}
sum = ~sum;
USART_SendData(USART1, sum);
USART_SendData(USART1, 0xef);
}
int main(void) {
init_gpio();
init_uart();
while (1) {
zero_count = __builtin_popcount(get_gpio_value());
if (zero_count_old > zero_count) {
// 打开蜂鸣器
}
if (zero_count_old < zero_count) {
send_data("规定设备未取完", sizeof("规定设备未取完")-1);
delay_ms(60000); // 等待 1 分钟
zero_count_old = zero_count;
}
if (zero_count_old == zero_count) {
send_data("设备数量正常", sizeof("设备数量正常")-1);
}
delay_ms(1000);
}
}
```
阅读全文