新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2018-4-16
- 在线时间
- 3 小时
|
12金钱
代码如下
主函数
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
u8 buffer[20] = {0};
u8 count=0; //计接收字符个数
u8 rx_buffer[20] = {0};
u8 rx_flag = 0;
void USART1_IRQHandler(void){ //串口1中断服务程序(固定的函数名不能修改)
u8 i;
//若是非空,则返回值为1,与RESET(0)判断,不相等则判断为真
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
//判断为真后,为下次中断做准备,则需要对中断的标志清零
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
/* DR读取接受到的数据*/
buffer[count++] = USART_ReceiveData(USART1);
//接收到0A时 count == 7
if(buffer[count-2]== 0x0D && buffer[count-1]==0x0A )
{
for(i=0; i<count-2; i++)
{
rx_buffer = buffer; //将数据赋值给rx_buffer
}
rx_flag = 1; //接收标志置1
count = 0; //从buffer[0]开始接收数据
memset(buffer,0,sizeof(buffer)); //清空数组
}
}
}
int main (void){//主程序
u8 a;
//初始化程序
RCC_Configuration(); //时钟设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Init(115200); //串口初始化(参数是波特率)
//主循环
while(1)
{
if(rx_flag == 1)
{
printf("rx_buffer = %s\n",rx_buffer); //打印输入字符串
//比较数据包,判断动作
if(strstr(rx_buffer,"add")) //当字符串里含有add
{
printf("Hello\n"); //打印Hello
}
//比较数据包,判断动作
if(strstr(rx_buffer,"del")) //当字符串里含有del
{
printf("Hi\n"); //打印Hi
}
memset(rx_buffer,0,sizeof(rx_buffer)); //清空数组
rx_flag = 0; //接收标志置0
}
}
return 0;
}
串口设置
#include "sys.h"
#include "usart.h"
//使UASRT串口可用printf函数发送
//在usart.h文件里可更换使用printf函数的串口号
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE {
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x){
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f){
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
#endif
/*
USART1串口相关程序
*/
#if EN_USART1
//USART1使用与屏蔽选择
//u8 USART1_RX_BUF[USART1_REC_LEN];
//接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART1_RX_STA=0;
//接收状态标记
/*
USART1专用的printf函数
*/
void USART1_Init(u32 bound){ //串口1初始化并启动
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
//使能USART1,GPIOA时钟
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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, DISABLE);//开启ENABLE/关闭DISABLE中断
USART_Cmd(USART1, ENABLE); //使能串口
}
想实现串口接收并字符判断
调试编译可以但是一直实现不了功能
奈何一直找不到那里出了问题
请各位前辈帮帮忙
|
|