首先,我想确认下,只有回车,或者换行才会产生0X0D,0X0A,是吗,其他的字符都不行,对吧?
这张图是我一直发下面那几个字符得到的结果,而且感觉我一次发许多字符过去,就能收到1个或者2个。
我是调试成收到什么返回什么,写成功后了,在中断函数再继续写原子哥的那个程序,所以感觉问题多半再中断服务函数里。
还有,感觉自己上了32,东西别多了,感觉自己一点都不会调试,你们能不能分享一下你们调试学习的方法和这道题拿到的思路。
我写中断服务函数前,画了个图。。。
代码如下:
#include "stm32f10x.h"
#include "USART.H"
int main()
{
unsigned char i, j;
My_UART_Init();
while(1)
{
if(USART_State & (1 << 15))
{
for(i = 0, j = (USART_State & 0x3fff); i < j; i++)
{
USART_SendData(USART1, USART1_Buf
);
}
USART_State = 0x00;
}
}
}
#include "USART.H"
#include "stm32f10x.h"
unsigned short int USART_State = 0;
unsigned char USART1_Buf[USART1_Buf_MAX_SIZE];
void My_UART_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_Mode = USART_Mode_Rx | USART_Mode_Tx; //£¿£¿£¿£¿
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void USART1_IRQHandler()
{
unsigned char dat;
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE))
{
dat = USART_ReceiveData(USART1);
// USART_SendData(USART1, dat);
if((USART_State & 0x8000) == 0)
{
if(USART_State & (1 << 14))
{
if(dat == 0x0a)
{
USART_State |= 0x8000;
}
else
{
USART_State = 0x00;
}
}
else
{
if(0x0d == dat)
{
USART_State |= (1 << 14);
}
else
{
USART1_Buf[USART_State & 0x3fff] = dat;
USART_State++;
if((USART_State & 0x3fff) >= USART1_Buf_MAX_SIZE)
{
USART_State = 0x00;
}
}
}
}
}
}
一周热门 更多>