Hi… I am transferring “TEST” string from the USART 1 to the hyper terminal. But at hyper terminal end I get only the char “T”. After that microcontroller unable to transfer the data. Please go through the following program, is there any problem in the code.This code is written for stm32 arm controller.
#include <stm32f10x_lib.h> #include <stdio.h> #include"stm32_reg.h"
int main() { unsigned char a[]="TEST",i; RCC->CFGR=0x001D8402; RCC->CR= 0x01010082; RCC->APB2ENR |= RCC_APB2ENR_AFIOEN ; AFIO->MAPR &= ~(1<<2) ; RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; GPIOA->CRH &= ~(0xFFUL << 4); GPIOA->CRH |= (0x0BUL << 4); GPIOA->CRH |= (0x04UL << 8); RCC->APB2ENR |= RCC_APB2ENR_USART1EN; USART1->BRR |= 0x27<<4; USART1->BRR |=0x1; USART1->CR1= 0x0000; USART1->CR2=0x0000; USART1->CR3=0x0000; USART1->CR1|=0x8; USART1->CR1 |= USART_CR1_UE; for(i=0;i<7;i++) { USART1->DR=a[i] ; while (!(USART1->SR & USART_FLAG_TXE)); } }
The first error can be seen after max 5 seconds of looking at the code. If there are such obvious errors, it's quite likely to be more errors hidden there too.
This is an forum .Right. Where people get the solution. But you are critic's. Good!!!!!
To be precise, the scope & purpose of the forum are defined thus:
"The Keil Discussion Forum is an open forum where you may post questions and comments about Keil products."
See here: http://www.keil.com/forum/
"Where people get the solution"
Incorrect - see above.
If you just want someone to give (sic) you a solution, then you need a Consultant: http://www.keil.com/condb/
Thorsten kindly formatted your original post so others might be able to determine what was wrong with your code.
Andy gave you some sensible advice.
There is a clear fault in it your code. It may not be the problem you are seeing at the moment, but the fault is there.
I was going to tell you what that fault is, but the style of your responses make me feel that there is no point.
Your loss.
I know that my first posting of code on the forum is wrong.Go through following code.
#include <stm32f10x_lib.h> #include <stdio.h> #include "stm32_reg.h" int main() { unsigned char a[]="TEST", i; RCC->CFGR = 0x001D8402; RCC->CR = 0x01010082; RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; AFIO->MAPR &= ~(1<<2); RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; GPIOA->CRH &= ~(0xFFUL << 4); GPIOA->CRH |= (0x0BUL << 4); GPIOA->CRH |= (0x04UL << 8); RCC->APB2ENR |= RCC_APB2ENR_USART1EN; USART1->BRR |= 0x27<<4; USART1->BRR |= 0x1; USART1->CR1 = 0x0000; USART1->CR2 = 0x0000; USART1->CR3 = 0x0000; USART1->CR1 |= 0x8; USART1->CR1 |= USART_CR1_UE; for(i=0;i<7;i++) { USART1->DR=a[i]; while (!(USART1->SR & USART_FLAG_TXE)); } }
Without looking at the config bytes, just from the code, I corrected you some errors and did a better coding:
#include <stdio.h> #include <stm32f10x_lib.h> #include "stm32_reg.h" int main() { unsigned char text[] = "TEST"; unsigned int i, len; // Enable Peripheral clocks RCC->APB2ENR |= RCC_APB2ENR_USART1EN; RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // Config RCC RCC->CFGR = 0x001D8402; RCC->CR = 0x01010082; // Config Alternate Function AFIO->MAPR &= ~(1<<2); // Config GPIO A GPIOA->CRH &= ~(0xFFUL << 4); GPIOA->CRH |= (0x0BUL << 4); GPIOA->CRH |= (0x04UL << 8); // Config USART 1 USART1->BRR |= 0x27<<4; USART1->BRR |= 0x1; USART1->CR1 = 0x0000; USART1->CR2 = 0x0000; USART1->CR3 = 0x0000; USART1->CR1 |= 0x8; USART1->CR1 |= USART_CR1_UE; len = strlen(text); for(i=0; i<len; i++) { while (!(USART1->SR & USART_FLAG_TXE)); USART1->DR = text[i]; } }
In a forum you may friendly ask others to _help_ you with a problem, a forum is not an answer-machine.
. BR, /th.
This is an example of how I use the UART, you may use this as example.
uart.h:
#ifndef __uart_h__ #define __uart_h__ #define USART1_EN 14 // enable clocks for UART1EN #define UART1EN 13 // UART1 enable #define UART1_DATA_BITS 12 // UART1 Data Bits (8 or 9) #define UART1_PARITY 10 // UART1 Parity (on, off) #define UART1_BREAK 0 // UART1 Break Control #define UART1_STOPBIT0 12 // UART1 Stop Bit ConfBit0 #define UART1_STOPBIT1 13 // UART1 Stop Bit ConfBit1 #define RXEN 2 // RX Enable #define TXEN 3 // TX enable #define RXINT 5 // RX Interrupt #define TXE 7 // TX Reg empty #define RXNE 5 // TX Not Empty #define TERM_CR 13 // Carriage Return #define TERM_LF 10 // Line Feed /* USART Flags ---------------------------------------------------------------*/ #define USART_FLAG_CTS ((uint16_t)0x0200) #define USART_FLAG_LBD ((uint16_t)0x0100) #define USART_FLAG_TXE ((uint16_t)0x0080) #define USART_FLAG_TC ((uint16_t)0x0040) #define USART_FLAG_RXNE ((uint16_t)0x0020) #define USART_FLAG_IDLE ((uint16_t)0x0010) #define USART_FLAG_ORE ((uint16_t)0x0008) #define USART_FLAG_NE ((uint16_t)0x0004) #define USART_FLAG_FE ((uint16_t)0x0002) #define USART_FLAG_PE ((uint16_t)0x0001) // Prototypes, standard void uart1_init(int baud); void uart2_init(int baud); int u1_putchar(int c); int u2_putchar(int c); void USART1_IRQHandler(void); void USART2_IRQHandler(void); #endif
uart.c:
#include <stdio.h> #include <stdint.h> #include "stm32.h" #include "core_cm3.h" #include "main.h" #include "gpio.h" #include "uart.h" #include "rx_buffer.h" volatile char linebuf[120] = {0}; volatile int lbufcnt=0, lbuflen=0; void uart1_init(int baud) { int baud_tmp=0; bit_on(RCC->APB2ENR, IOPA_EN); // enable clock for GPIOA bit_on(RCC->APB2ENR, AFIO_EN); // alternate function clocks enable bit_on(RCC->APB2ENR, USART1_EN); // enable clocks for UART1 bit_on(GPIOA->CRH, (MODE0 + GPIO_CONF_BIT(9))); // USART1 Tx (PA9) alternate output bit_on(GPIOA->CRH, (MODE1 + GPIO_CONF_BIT(9))); // push-pull, 50MHz (1011) bit_off(GPIOA->CRH, (CONF0 + GPIO_CONF_BIT(9))); bit_on(GPIOA->CRH, (CONF1 + GPIO_CONF_BIT(9))); bit_off(GPIOA->CRH, (MODE0 + GPIO_CONF_BIT(10))); // USART1 Rx (PA10) bit_off(GPIOA->CRH, (MODE1 + GPIO_CONF_BIT(10))); // input floating (0100) bit_on(GPIOA->CRH, (CONF0 + GPIO_CONF_BIT(10))); bit_off(GPIOA->CRH, (CONF1 + GPIO_CONF_BIT(10))); baud_tmp =((unsigned int)(CPU_CLOCK/(16*baud))) << 4; // Calculate Baud Rate (without Frac) USART1->BRR = baud_tmp; // set baudrate bit_off(USART1->CR1, UART1_DATA_BITS); // set Data bits bit_off(USART1->CR1, UART1_PARITY); // no parity bit_off(USART1->CR1, UART1_BREAK); // no break bit_off(USART1->CR2, UART1_STOPBIT0); // one stop bit bit_off(USART1->CR2, UART1_STOPBIT1); // ----- UART Interrupt ----------------------- bit_on(USART1->CR1, RXINT); // enable RX Interrupt NVIC->ISER[1] |= (1 << (USART1_IRQn & 0x1F)); // enable interrupt in NVIC bit_on(USART1->CR1, RXEN); // RX enable bit_on(USART1->CR1, TXEN); // TX enable USART1->CR1 |= (1 << UART1EN); // USART enable } void uart2_init(int baud) { int baud_tmp=0; RCC->APB2ENR |= 1; // enable clock for Alternate Function AFIO->MAPR &= ~(1 << 3); // clear USART2 remap RCC->APB2ENR |= 4; // enable clock for GPIOA GPIOA->CRL &= ~(0xFFUL << 8); // Clear PA2, PA3 GPIOA->CRL |= (0x0BUL << 8); // USART2 Tx (PA2) alternate output push-pull GPIOA->CRL |= (0x04UL << 12); RCC->APB1ENR |= 0x00020000; // enable clock for USART2 baud_tmp =((unsigned int)(CPU_CLOCK/(16*baud))) << 3; // Calculate Baud Rate (without Frac) USART2->BRR = baud_tmp; bit_off(USART2->CR1, UART1_DATA_BITS); // set Data bits bit_off(USART2->CR1, UART1_PARITY); // no parity bit_off(USART2->CR1, UART1_BREAK); // no break bit_off(USART2->CR2, UART1_STOPBIT0); // one stop bit bit_off(USART2->CR2, UART1_STOPBIT1); // ----- UART Interrupt ----------------------- bit_on(USART2->CR1, RXINT); // enable RX Interrupt NVIC->ISER[1] |= (1 << (USART2_IRQn & 0x1F)); // enable interrupt in VIC bit_on(USART2->CR1, RXEN); // RX enable bit_on(USART2->CR1, TXEN); // TX enable USART2->CR1 |= (1 << UART1EN); // USART enable } /** * \author Th. de Buhr * \date 07.09.2007 * \param (int) character * \return (int) character * \brief LowLevel-Function to write a character to the UART0. */ int u1_putchar(int c) { while (!(USART1->SR & (1<<TXE))); USART1->DR = (c & 0xFF); return(c); } /** * \author Th. de Buhr * \date 07.09.2007 * \param (int) character * \return (int) character * \brief LowLevel-Function to write a character to the UART0. */ int u2_putchar(int c) { while (!(USART2->SR & (1<<TXE))); USART2->DR = (c & 0xFF); return(c); } /** * \author Th. de Buhr * \date 07.09.2007 * \param (FILE*) filepointer * \return (int) character * \brief Blocking function to read one character from UART0 */ int u1_fgetc(FILE *f) { char c=0; while (!(USART1->SR & (1<<RXNE))); c = ((int)(USART1->DR & 0xFF)); fflush(f); return(c); } /** * \author Th. de Buhr * \date 07.09.2007 * \param (FILE*) filepointer * \return (int) character * \brief Blocking function to read one character from UART0 */ int u2_fgetc(FILE *f) { char c=0; while (!(USART2->SR & (1<<RXNE))); c = ((int)(USART2->DR & 0xFF)); fflush(f); return(c); } #if (USE_UART_NO == 2) // unblocking! int fgetc_unblocking(FILE *f) { if ((USART2->SR & USART_FLAG_RXNE)) return ((int)(USART2->DR & 0xFF)); else return(0); } #endif /** * \author Th. de Buhr * \date 07.09.2007 * \param (void) * \return (void) * \brief ISR for UART0 RX. If one char is received by the UART0, this function is called. It writes the recv. character into a buffer and clears the interrupt. */ void USART1_IRQHandler (void) { volatile unsigned int IIR; IIR = USART1->SR; if (IIR & USART_FLAG_RXNE) { USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt u1_rxBuffer_putchar(USART1->DR & 0xFF); // write buffer } } void USART2_IRQHandler (void) { volatile unsigned int IIR; IIR = USART2->SR; if (IIR & USART_FLAG_RXNE) { USART2->SR &= ~USART_FLAG_RXNE; // clear interrupt u2_rxBuffer_putchar(USART2->DR & 0xFF); // write buffer } }
main.h:
#ifndef __main_h__ #define __main_h__ /* Clock Definitions */ #define CPU_CLOCK 72000000 // CPU Clock #define TICKS_ONE_MS CPU_CLOCK/7400 // approp. one millisecond // Bit_on, bit_off definitions #define bit_on(BYTE, BIT) { BYTE |= 1 << BIT; } #define bit_off(BYTE, BIT) { BYTE &= ~(1 << BIT); } // TRUE/FALSE #define TRUE 1 #define FALSE 0 // prototypes void delay_ms(int ms); #endif
gpio.h:
#ifndef __GPIO_H__ #define __GPIO_H__ // AF enable #define AFIO_EN 0 // Alternate Function Enable // GPIO enable #define IOPA_EN 2 // GPIO A enable #define IOPB_EN 3 // GPIO B enable #define IOPC_EN 4 // GPIO C enable #define IOPD_EN 5 // GPIO D enable #define IOPE_EN 6 // GPIO E enable #define IOPF_EN 7 // GPIO F enable #define IOPG_EN 8 // GPIO G enable // DAC Enable #define DAC_EN 29 #define GPIO_CONF_BIT(BIT) ((BIT>7? BIT-8 : BIT) << 2) // 4Bits per port pin // Mode and Conf Bits #define MODE0 (unsigned int)0 #define MODE1 (unsigned int)1 #define CONF0 (unsigned int)2 #define CONF1 (unsigned int)3 // Port Mode #define GPIO_MODE_INPUT (((unsigned int)0<<MODE0) | ((unsigned int)0<<MODE1)) // GPIO is input #define GPIO_SPEED_2MHZ (((unsigned int)0<<MODE0) | ((unsigned int)1<<MODE1)) // Max output Speed 2MHz #define GPIO_SPEED_10MHZ (((unsigned int)1<<MODE0) | ((unsigned int)0<<MODE1)) // Max output Speed 10MHz #define GPIO_SPEED_50MHZ (((unsigned int)1<<MODE0) | ((unsigned int)1<<MODE1)) // Max output Speed 50MHz // Port Conf #define GPIO_OUT_PUSH_PULL (((unsigned int)0<<CONF0) | ((unsigned int)0<<CONF1)) // general purpose output push-pull #define GPIO_AF_PUSHPULL (((unsigned int)0<<CONF0) | ((unsigned int)1<<CONF1)) // alternate function push-pull #define GPIO_IN_FLOATING (((unsigned int)1<<CONF0) | ((unsigned int)0<<CONF1)) // input floating #define GPIO_IN_ANALOG (((unsigned int)0<<CONF0) | ((unsigned int)0<<CONF1)) // input analog #define GPIO_IN_PULL_DOWN (((unsigned int)0<<CONF0) | ((unsigned int)1<<CONF1)) // alternate function push-pull #define GPIO_IN_PULL_UP (((unsigned int)0<<CONF0) | ((unsigned int)1<<CONF1)) // alternate function push-pull // prototypes void port0_init(void); #endif
TABs went a bit wrong :-)
There is only one certain thing about interpreting TABs: they will go wrong!
Therefore, never use TABs - especially not on an internet forum!
Set your editor for "insert spaces for TABs"
I do not use Tabs anymore, but this code is old ...