/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / How to use STM32F103VB USART2 ?

Username:     
Password:     
             

Forum

# 1   2008-08-11 04:58:02 How to use STM32F103VB USART2 ?

kidy
New member
Registered: 2008-08-11
Posts: 1

How to use STM32F103VB USART2 ?

Hi,all

I need help,I want to use USART2,but it's not working,thanks
--------------------------------------------------------------------------------------------------------------------------------
#include <stm32f10x_lib.h>              /* STM32F10x Library Definitions      */                 
#include <stdio.h>

unsigned char Clock1s;
extern int  SendChar (int ch);         
void _ttywrch (int ch) { SendChar(ch); }
void _sys_exit (int return_code) { for (;wink; }

int fputc (int ch, FILE *f) { return (SendChar(ch)); }
int ferror (FILE *f) {
  /* Your implementation of ferror */
  return EOF;
}

GPIO_InitTypeDef  GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

void SetupClock (void)
{
  RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */

  /* Wait till HSE is ready                                                   */
  while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);

  RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */

  *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */

  /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  RCC_PLLConfig (RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */

  /* Wait till PLL is ready                                                   */
  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

  /* Select PLL as system clock source                                        */
  RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);

  /* Wait till PLL is used as system clock source                             */
  while (RCC_GetSYSCLKSource() != 0x08);

  /* Enable USART1 and GPIOA clock                                            */
  RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOD, ENABLE);

  /* SysTick event each 10 ms with input clock equal to 9MHz (HCLK/8)         */
  SysTick_SetReload(720000);

  SysTick_ITConfig(ENABLE);             /* Enable SysTick interrupt           */
}


void SetupUART (void)  {

  GPIO_InitTypeDef  GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  /* Enable GPIOA clock                                                       */
  RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

  /* Configure USART1 Rx (PA10) as input floating                             */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

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

  /* USART1 configured as follow:
        - BaudRate = 115200 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            = 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_InitStructure.USART_Clock               = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL                = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA                = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit             = USART_LastBit_Disable;
  USART_Init(USART2, &USART_InitStructure);

  USART_Cmd(USART2, ENABLE);
             /* Enable USART1                      */
   }


/* Implementation of putchar (also used by printf function to output data)    */
int SendChar (int ch)  {                /* Write character to Serial Port     */

  USART_SendData(USART2, (unsigned char) ch);
  while (!(USART2->SR & USART_FLAG_TXE));
  return (ch);
}

-------------------------------------------------------------------------------------------------------------------------------------

/* Includes ------------------------------------------------------------------*/
#include "stdio.h"
#include "stm32f10x_usart.h"

/* Private function prototypes -----------------------------------------------*/
void SetupUART (void);
void SetupClock (void);

int main(){
    int m=0;
    u16 i=0;
    char j;

SetupClock ();
SetupUART ();

printf("\r\n Hello Embest---------");
    while(1    )
    {
        if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET)
        {   
            i = USART_ReceiveData(USART2);
            printf("  %c",i&0xFF);       /* print the input char */
            j=i&0xFF ;
        }
       
        if ( j=='0')             /*if input char equal 0 then print 0 to 100 */
          { for (m=0 ;m<=100;m++)
           printf("%d\t",m);
           j='p';
          }
    }   
}

-------------------------------------------------------------------------------------------------------------------------------------

Offline

 

# 2   2008-08-12 14:31:50 How to use STM32F103VB USART2 ?

matloub
Member
Registered: 2008-02-21
Posts: 98

Re: How to use STM32F103VB USART2 ?

Hi Kidy

I tried to understand your problem but I would need more information.
Reading your post I can not find what does not work.
Can you precise what is the problem
+ if you have error message what is it and at which step
+ if it is while you use the usart describe how the problem appears


Still I have tried to compile the code you posted with my Ride7 version 7.10.0000 and RKit-ARM version 1.10.0000
I had to make some changes in order to manage to build the project.
I was getting error message coming from the fact that the" usart_clock" and some other were not defined in the same structure so it could not be initialized by the same way as the others such as "USART_WordLength"

Best regards,
Matloub

Offline

 

Board footer