本帖最后由 yuchengze 于 2016-12-27 10:20 编辑
u8 TxBuffer[2];
u8 TxCounter=0;
u8 count=0;
void USART_SendData_Int(u8 *str)
{
while(*str != '\0')
{
TxBuffer[count++] = *str++;
}
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
void delay(void)
{
long i,j;
for(i=0;i<1500;i++)
for(j=0;j<2000;j++);
}
int main(void)
{
USART_Configuration();
NVIC_Configuration();
while (1)
{
USART_SendData_Int("012345678901234567890123456789012345\n");
USART_SendData_Int("abcdefghijklmn\n");
delay();
}
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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_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);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_SendData(USART1, TxBuffer[TxCounter++]);
USART_ClearITPendingBit(USART1, USART_IT_TXE);
if(TxCounter == count)
{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}
这个程序经过调试,在串口调试助手上一点问题都没有!!!不过似乎从程序上看问题很多:1、我定义的串口缓存TxBuffer[2],为什么大小只有两个字节(应该说是任意字节大小都可以),我程序在发送数据时,数据大小远远超过两个字节,但是却没报错,更是还能正确运行。。。没懂!?2、我定义的两个全局变量u8 TxCounter=0;u8 count=0;每发送一次数据,都会相应自加上数据的长度,程序中没有对其清零, 却似乎永远没有溢出的迹象。怎么解释? |