打印

keil 的USART问题

[复制链接]
3490|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
fymok|  楼主 | 2011-8-8 10:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用的3.4固件库
大家好,请大家帮忙指点一下
实现的功能:PC13接按键,用USART1,每一次按键按下,MCU向上位机(串口调试助手)发送字符串 "sTm32f103vct6";如果串口调试助手向MCU发送 "0x45",MCU返回“ok”
以下程序在IAR和KEIL环境下 编译均无错误。
遇到的问题:IAR编译的程序下到板子上,功能正常运行
                        keil 编译的程序下到板子上,按键时,MCU向串口发送乱码且每次发送的都一样(波特率什么的都正常),调试时按键进得了中断。串口向MCU发送“0x45”,mcu无响应,进不了中断。

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stdio.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define Tim_1ms 0x15a9              // 5545                  
#define Tim_2ms Tim_1ms * 2
/* Private macro -------------------------------------------------------------*/
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Private variables ---------------------------------------------------------*/
uint8_t RxBuf[20];
extern uint16_t RX_dat;
volatile uint8_t RX_CFlag = RESET;
extern volatile uint8_t flag;
extern volatile uint8_t uart_flag;
ErrorStatus HSEStartUpStatus;
// char _str[25] = "sTm32f103vct6";
// char _str1[3] = "OK";
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void) ;
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug() ;
#endif
char _n;
  char _str[25] = "sTm32f103vct6";
char _str1[3] = "OK";
  /* Configure the system clocks */
RCC_Configuration();

/* Configure the GPIO ports */
GPIO_Configuration();
/* Configure the EXTI */
NVIC_Configuration();
EXTI_Configuration() ;
USART_Configuration();
  while (1)
  {   
     
   if (flag  == 1)
      {
              flag = 0;
                       for ( _n = 0; _n < 13; _n++)
                       {
                           USART_SendData(USART1, _str[_n]);
                        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
                                Delay(5545);
                        }
            
            }
          if (uart_flag == 1)
           {
               uart_flag = 0;
                       for (_n = 0; _n < 2; _n++)
                       {
                           USART_SendData(USART1, _str1[_n]);
                         while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
                                Delay(5545);
                        }     
             }   
}
}
void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}
void RCC_Configuration(void)
{
  //必须开时钟才能 使用起来 相应IO
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
}
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;      //选择引脚2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //输出频率最大50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //带上拉电阻输出
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_ResetBits(GPIOE,GPIO_Pin_2);               //将PE.2引脚设置为低电平输出

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_5; //选择引脚2 3 5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //选择输入模式为浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;          //输出频率最大50MHz
GPIO_Init(GPIOC,&GPIO_InitStructure);                  //设置PC.2/PC.3/PC.5

*******************************************************************************/
void GPIO_Configuration(void)
{
    /* Configure PC13 as input floating (EXTI Line13) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure  PA9 as USART1 Tx*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;          //输出频率最大50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure  PA10 as USART1 Rx*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures the NVIC
* Input          : None
* Output         : None
* Return         : None
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);          //选择中断分组2
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQChannel;     //选择中断通道2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;          //使能中断
NVIC_Init(&NVIC_InitStructure);
*******************************************************************************/
void NVIC_Configuration(void)
{
   #ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
   /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //先占优先级分组为1

  /* Enable the EXTI15_10 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
  }

    /*******************************************************************************
用GPIO来触发外部中断的时候需要设置将GPIO相应的引脚和中断线连接起来
EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line5; //选择中断线路2 3 5
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;                      //外部中断使能
EXTI_Init(&EXTI_InitStructure);
*******************************************************************************/
void EXTI_Configuration(void)
{
  /* Configure EXTI Line13 to generate an interrupt on falling edge */
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);   //选择中断管脚PC13
EXTI_InitStructure.EXTI_Line=EXTI_Line13;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
/*通过软件触发一次EXTI line13中断*/
// EXTI_GenerateSWInterrupt(EXTI_Line13);
}

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
//  USART_ClockInitTypeDef USART_ClockInitStructure;

  /* USART1 configuration ----------------------------------------------------*/
  /* USART1 configured as follow:
        - BaudRate = 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
        - Hardware flow control disabled (RTS and CTS signals)
  */
  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_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_Init(USART1, &USART_InitStructure);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);
}

  #ifdef DEBUG
  /*描述:当程序出错时,返回出错的文件名及在源程序中的行号
   输入:—file:指向文件名的指针
   —line:在源程序中的行号
   输出:无
   返回:无
   */
   void assert_failed(u8*file,u32 line)
   {
   while(1) {}
   }
#endif

中断程序
/**
  ******************************************************************************
  * @file    SysTick/stm32f10x_it.c
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    10/15/2010
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "stdio.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */
/** @addtogroup SysTick
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint16_t RX_dat = 0;
uint8_t RxCnt = 0;                 // 接收计数
extern volatile uint8_t RX_CFlag;
volatile uint8_t flag = 0;
volatile uint8_t uart_flag = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/
/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}
/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}
/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}
/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{

}
/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/
/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
  RX_dat = USART_ReceiveData(USART1);
  USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  if (RX_dat == 0x45)
   uart_flag = 1;   
}
}

void EXTI15_10_IRQHandler(void)
{

  if(EXTI_GetITStatus(EXTI_Line13) != RESET) //检查指定的EXTI线路触发请求发生与否
  {
                flag = 1;
    /* Clear EXTI_LINE13 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line13);
  }
}

/*void PPP_IRQHandler(void)
{
}*/
/**
  * @}
  */
/**
  * @}
  */
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
沙发
fymok|  楼主 | 2011-8-8 11:42 | 只看该作者
木人回答?
奇怪死了

使用特权

评论回复
板凳
fymok|  楼主 | 2011-8-9 10:51 | 只看该作者
问题已经解决  还是得靠自己

使用特权

评论回复
地板
desomond| | 2011-8-9 11:04 | 只看该作者
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);应该是这句的问题吧

使用特权

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

本版积分规则

7

主题

40

帖子

0

粉丝