你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

[复制链接]
老来多遗忘 提问时间:2011-8-8 10:14 /
 
用的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.
  *
  * © COPYRIGHT 2010 STMicroelectronics


  */
/* 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****/
 
收藏 评论8 发布时间:2011-8-8 10:14

举报

8个回答
老来多遗忘 回答时间:2011-8-8 10:19:35

RE:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

乱码16进制显示  DF FF 7F F7 F7 DF DF FF DF FF 7F FF F7 FF FF FF DF FF 7F FF DF FF 7F FF 7F FF
废鱼 回答时间:2011-8-8 11:00:01

RE:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

你的启动文件需要修改为MDK版本的。其他的库通用,就是启动文件不通用。这个问题的原因是:系统时钟不同,然后导致你的波特率不对。
老来多遗忘 回答时间:2011-8-8 11:40:22

RE:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

木人回答?
奇怪死了
老来多遗忘 回答时间:2011-8-8 11:51:28

回复:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

回复第 3 楼 于2011-08-08 03:00:01发表:
你的启动文件需要修改为MDK版本的。其他的库通用,就是启动文件不通用。这个问题的原因是:系统时钟不同,然后导致你的波特率不对。
 
你好,IAR的启动文件和KEIL的一样的啊,附件截图00为KEIL环境,截图01为IAR环境
 
截图00.png
截图01.png
废鱼 回答时间:2011-8-8 14:55:20

RE:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

startup_stm32f10x_hd.s
这个文件IAR和MDK是不一样的。你可以看看ST的官方例程。
老来多遗忘 回答时间:2011-8-8 15:48:45

回复:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

回复第 6 楼 于2011-08-08 06:55:20发表:
startup_stm32f10x_hd.s
这个文件IAR和MDK是不一样的。你可以看看ST的官方例程。
 
 
谢谢版主的回复  就我贴出来的程序  什么地方有bug吗? 我检查了好多遍
烦请版主帮忙看看 谢谢
 
 
老来多遗忘 回答时间:2011-8-9 10:58:20

回复:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

 程序没错  keil没配置好
chinajieli 回答时间:2011-12-4 14:49:55

RE:IAR编译的程序下到板子上,功能正常运行 keil 编译的程序下到板子上,不正常

程序没错  keil没配置好

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版