本帖最后由 startostar 于 2011-6-8 15:31 编辑
使用串口发送,采用中断方式,不好用,代码如下:请教?
//使能串口2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// A2 做T2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// A3 做R2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART2, &USART_ClockInitStructure);
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
//串口2使用接收中断 .发送中断
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART2,USART_IT_TXE, ENABLE);
//NVIC设置
//设置所有的中断允许
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*UART2*/
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//RCC设置
void RCC_Configuration(void)
{
SystemInit();
}
//中断处理
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
USART_SendData(USART2,0x55) ;
// USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
上面这段程序采用中断发送不好用,把USART_SendData(USART2,0x55) ;
单独加在主程序里就能发数了,但这就是查询了方式了,证明程序时钟,引脚配置是对的,其它哪有错,请教?
} |