打印

串口中断发送问题

[复制链接]
3058|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
startostar|  楼主 | 2011-6-8 15:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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) ;
单独加在主程序里就能发数了,但这就是查询了方式了,证明程序时钟,引脚配置是对的,其它哪有错,请教?


}
沙发
610095871| | 2011-6-9 19:50 | 只看该作者
void usartinit()
{
        USART_InitTypeDef USART_InitStructure;  
        USART_StructInit(&USART_InitStructure);

//        USART_ClockInitTypeDef  USART_ClockInitStructure;//定义串口模式初始化结构体
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1,ENABLE);
       
/*  
          USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;//
          USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
          USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
          USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
          USART_ClockInit(USART1,&USART_ClockInitStructure);
*/
       
         
        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;
  /* Configure USART1 */
          USART_Init(USART1, &USART_InitStructure);
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART1, USART_IT_TXE, ENABLE);                 

  /* Enable the USART1 */
          USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


另外        USART_ClearFlag(USART1,USART_FLAG_TC);初始化要把这一步加上
中断服务程序
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {       
                  USART_SendData(USART1, 'a'); //发送一位数据
                  GPIO_SetBits(GPIOC,GPIO_Pin_7);
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);

                USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  }
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
   
            USART_SendData(USART1, 'b');
              USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
   
  }       

}

最后切记在stm32f10X_it.c文件下加上#include "stm32f10x_usart.h" 不然无法调用串口函数 好了
注意好这些,你的串口就能通讯了,如果还不行请回复

使用特权

评论回复
板凳
startostar|  楼主 | 2011-6-10 09:50 | 只看该作者
USART_ClearFlag(USART1,USART_FLAG_TC);初始化要把这一步加上,初始化这句有啥用?其它的没看出区别!
谢谢,我试试看

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

30

帖子

1

粉丝