打印
[STM8]

STM8 在STVD+COSMIC下,一段printf输出数组的程序出错

[复制链接]
8591|14
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
stm8官方库下面,有一个UART1_Printf 例程,修改其main.c,添加printf()打印输出一个数组元素语句,发现输出结果有问题。同样的程序,使用IAR编译,结果符合预期。

main.c
==================================
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stdio.h"
/**
  * @addtogroup UART1_Printf
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
unsigned char ChipUniqueID[12]={1,2,3,4,5,6,7,8,9,10,11,12};  

void main(void)
{
  unsigned char i=0;
char ans;
  /*High speed internal clock prescaler: 1*/
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
   
  UART1_DeInit();
  /* UART1 configuration ------------------------------------------------------*/
  /* UART1 configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Receive and transmit enabled
        - UART1 Clock disabled
  */
  UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
              UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  /* Output a message on Hyperterminal using printf function */
  printf("\n\rUART1 Example :retarget the C library printf()/getchar() functions to the UART\n\r");
  printf("\n\rEnter Text\n\r");
  printf("\n\r\n\r");
  for(i=0;i<12;i++)
  {
    printf(" 0x%x ",ChipUniqueID);

  }
  
  
  
  while (1)
  {
    ans = getchar();
    printf("%c", ans);  
  }
}
/**
  * @brief Retargets the C library printf function to the UART.
  * @param c Character to send
  * @retval char Character sent
  */
PUTCHAR_PROTOTYPE
{
  /* Write a character to the UART1 */
  UART1_SendData8(c);
  /* Loop until the end of transmission */
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
  return (c);
}
/**
  * @brief Retargets the C library scanf function to the USART.
  * @param None
  * @retval char Character to Read
  */
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
  char c = 0;
#else
  int c = 0;
#endif
  /* Loop until the Read data register flag is SET */
  while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
    c = UART1_ReceiveData8();
  return (c);
}
#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

======================================================

COSMIC的输出结果


IAR的输出结果


======================================================
请教这是什么问题?






沙发
yaoyyie2003|  楼主 | 2013-9-29 17:34 | 只看该作者
求解答

使用特权

评论回复
板凳
wangdezhi| | 2013-9-29 20:15 | 只看该作者
你的字符输出都是正确的。

使用特权

评论回复
地板
wangdezhi| | 2013-9-29 20:15 | 只看该作者
你看看你的变量定义的怎么样的。

使用特权

评论回复
5
yaoyyie2003|  楼主 | 2013-9-29 21:10 | 只看该作者
wangdezhi 发表于 2013-9-29 20:15
你的字符输出都是正确的。

STVD那个输出是错的

使用特权

评论回复
6
yaoyyie2003|  楼主 | 2013-9-29 21:10 | 只看该作者
wangdezhi 发表于 2013-9-29 20:15
你看看你的变量定义的怎么样的。

什么意思?我的变量定义有问题吗?

使用特权

评论回复
7
dronefly| | 2015-7-18 09:20 | 只看该作者
我也遇到同样的问题,我用简单的串口输出我的字符,输出是正确的,说明定义的没有问题,那么问题在printf这个函数上,输出变成了双字节。。

使用特权

评论回复
8
dronefly| | 2015-7-18 09:22 | 只看该作者
可能跟COSMIC有关。输出的右边两个字节是不对的,左边两个是正确的。我现在也没有找到问题在哪。

使用特权

评论回复
9
dronefly| | 2015-8-1 10:15 | 只看该作者
求解答

使用特权

评论回复
10
lihai00| | 2015-8-2 21:32 | 只看该作者
进来看看,个人也觉得跟COSMIC有关。。

使用特权

评论回复
11
yaoyyie2003|  楼主 | 2015-8-3 10:50 | 只看该作者
已经解决了,最后发现选择 full 的输入输出库就不会出错

使用特权

评论回复
12
曾经in| | 2015-8-9 01:38 | 只看该作者
yaoyyie2003 发表于 2015-8-3 10:50
已经解决了,最后发现选择 full 的输入输出库就不会出错

请问在哪里选择。没找到啊

使用特权

评论回复
13
dexis| | 2015-8-9 08:28 | 只看该作者
改成 printf("0x%u",ChipUniqueID);//是 u 哦

使用特权

评论回复
14
CBeginners| | 2016-6-27 18:42 | 只看该作者
13楼改成u不行哦,楼主有解决这个问题吗?

使用特权

评论回复
15
Condi| | 2017-6-6 18:11 | 只看该作者
你试试改成u16,
有些MCU,u8压栈的时候是1byte对齐,
但是函数库的printf函数取VA_LIST是以2byte对齐。

使用特权

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

本版积分规则

7

主题

96

帖子

1

粉丝