CoIde ile float sayılarda printf ve sprintf

Başlatan muhittin_kaplan, 18 Ocak 2013, 19:15:28

muhittin_kaplan

Problemliydi, bu konu çeşitli yöntemler denemiştim, olmamıştı.
Çözümü yazıyorum. Çözüm içerisinde printf fonksiyonu ile doğrudan usart a göndermede mevcut.

1. startup_stm32f10x_md_vl.c dosyasının içerisinde değişiklikler.

1.a

/*----------Stack Configuration-----------------------------------------------*/  
#define STACK_SIZE       0x00000100      /*!< Stack size (in Words)           */
__attribute__ ((section(".co_stack")))
unsigned long pulStack[STACK_SIZE];


YERINE

#define STACK_SIZE       0x00000300      /*!< Stack size (in Words)           */
__attribute__ ((section(".co_stack")))
unsigned long pulStack[STACK_SIZE]; 


1.b
(void *)&pulStack[STACK_SIZE-1],     /*!< The initial stack pointer         */


yerine

(void (*)(void))((unsigned long)pulStack + sizeof(pulStack)),     /*!< The initial stack pointer         */



2.syscalls.c içerisindeki değişiklik

int _write(int file, char *ptr, int len)
{
  return len;
}


YERINE

int _write( int file, char *ptr, int len )
{
int txCount;

  (void)file;

  for ( txCount = 0; txCount < len; txCount++)
  {
   USART_SendData(USART1, ptr[txCount]);   //Don't forget to include "stm32f10x_usart.h"

   /* Loop until the end of transmission */
   while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
   {
   }
  }

  return len;
}


unutmadan USART1 i sınıf olarak tanımlayıp fonksiyona parametre olarakta isteyebilirsiniz. burada Sabit olarak kullanılmış. Ayrıca syscall.c içerisine stm32f10x_usart.h dosyasının import edilmesi gerekir.


örnek program main blogu içerisinde hep sprintf ile hem printf ile örnek mevcut.
#include "stm32f10x.h"
#include "stm_lib/inc/stm32f10x_gpio.h"
#include "stm_lib/inc/stm32f10x_rcc.h"
#include "stm_lib/inc/stm32f10x_flash.h"
#include "stm_lib/inc/stm32f10x_usart.h"
#include <stdio.h>



void SetSysClockTo24(void)
{
	int i;
  
	RCC_DeInit();
	RCC_HSEConfig(RCC_HSE_ON);

  for(i=0;i<0x000FFFF;i++);
  	FLASH_SetLatency(FLASH_Latency_0);
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_HCLK_Div1);
    RCC_PCLK1Config(RCC_HCLK_Div1);

    /* PLLCLK = (8MHz/2) * 6 = 24 MHz */
    RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE,RCC_PREDIV1_Div1);
    RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3);
    RCC_PLLCmd(ENABLE); /* Enable PLL */

    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);



    while (RCC_GetSYSCLKSource() != 0x08)
    {
    }


    RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
}


/***************************************************************************//**
 * @brief Init USART1
 ******************************************************************************/
void ConfigUSART()
{
      GPIO_InitTypeDef  GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;

      /* Enable GPIOA clock                                                   */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

      /* Configure USART1 Rx (PA10) as input floating                         */
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);

      /* Configure USART1 Tx (PA9) as alternate function push-pull            */
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
      GPIO_Init(GPIOA, &GPIO_InitStructure);

      /* USART1 configured as follow:
            - BaudRate = 9600 baud
            - Word Length = 8 Bits
            - One Stop Bit
            - No parity
            - Hardware flow control disabled (RTS and CTS signals)
            - Receive and transmit enabled
            - USART Clock disabled
            - USART CPOL: Clock is active low
            - USART CPHA: Data is captured on the middle
            - USART LastBit: The clock pulse of the last data bit is not output to
                             the SCLK pin
      */
      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_Init(USART1, &USART_InitStructure);
      USART_Cmd(USART1, ENABLE);
}

void USART_puts(USART_TypeDef* USARTx, volatile char *s){

	while(*s){
		// wait until data register is empty
		while( !(USARTx->SR & 0x00000040) );
		USART_SendData(USARTx, *s);
		*s++;
	}
}


int main(void)
{
	SetSysClockTo24();
	ConfigUSART();

    while(1)
    {

       	 char dizi[10];
       	 sprintf (dizi,"flt: %4.4f\n", 1.4142135);
       	 USART_puts(USART1,dizi);
       	 printf("flt: %4.4f\n", 1.4142135);


    }
}