高级会员

- 积分
- 680
- 金钱
- 680
- 注册时间
- 2013-8-16
- 在线时间
- 37 小时
|
5金钱
大家好,我写了一个发送中断和发送完中断的一个测试程序,可是发送中断发送数据还可以实现,可是使用发送完中断来发送数据就不能实现了。下面是我的代码,大家帮忙看看,问题出在什么地方,我做了很多次尝试都没有成功,还请知道的大神们指点一二,谢谢。
//==============================================================
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
}
void usart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//使能USART2时钟
USART_DeInit(USART2);//复位串口2
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX   A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为115200;
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(USART2, &USART_InitStructure); //初始化串口
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);//关闭中断
USART_ITConfig(USART2, USART_IT_TC, ENABLE);//关闭中断
USART_Cmd(USART2, ENABLE); //使能串口
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
NVIC_Configuration();// 设置中断优先级分组
delay_init();
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化A8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化A8
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
//=============================================================串口初始化
usart_init(115200);
while(1)
{
USART2->DR=0x05; //触发中断
TX_Counter=0;
}
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_TC) != RESET)
{
sei(); //开启所有中断
if(TX_Counter>4)
{TX_Counter=4;}
switch(TX_Counter)
{
case 0:
USART2->DR=0x55;
TX_Counter++;
break;
case 1:
USART2->DR=0x56;
TX_Counter++;
break;
case 2:
USART2->DR=0x57;
TX_Counter++;
break;
case 3:
USART2->DR=0x58;
TX_Counter++;
break;
case 4:
while((USART2->SR&0x40)!=0x40);
TX_Counter++;
break;
}
}
}
|
|