2022-04-09 07:53 AM
STM32F103C(Bluepill) Tx:
#include "stm32f103x6.h"
#define USART2CLKEN (0x01UL << 17U)
#define SYS_FREQ 72000000
#define APB1_CLK 36000000
#define UART_BAUDRATE 9600
void uart2_write(int ch);
void uart2_tx_init(void);
int main(void)
{
uart2_tx_init();
while(1)
{
uart2_write(13);
}
}
void uart2_tx_init(void)
{
/* APB1ENR at page 111*/
RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; /*(0x1 << 2) Enabling GPIOA */
GPIOA->CRL |= GPIO_CRL_MODE2_1;/*(0x02 << 8u) */ /*Enable Alternate function for Pin A2 */
GPIOA->CRL |= GPIO_CRL_MODE3_1; /*(0x02<<12)/*Enable Alternate function for Pin A3 */
RCC ->APB1ENR |= RCC_APB1ENR_USART2EN; /* (0x1 << 2)Enabling UART */
USART2->CR1 |= 0x02008;
USART2->BRR = 0xEA6; //Calculated using the formulae 36MHz/16*9600.
}
void uart2_write(int ch)
{
/*Make sure transmit data register is empty*/
while(!(USART2 ->SR & USART_SR_TXE)) {}
/* Write to transmit data register*/
USART2->DR |= (ch &0xFF);
}
Arduino Code: For Rx
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2,3);//Rx,Tx
void setup() {
Serial1.begin(9600);
}
void loop()
{
int Data_rcvd;
if(Serial1.available())
{
Data_rcvd = Serial1.read();
Serial1.print(Data_rcvd);
}
}
Can anyone please guide me to solve the issue?
2022-04-09 10:05 AM
I don't use the 'F1, but for Alternate mode Push-pull Output, you must set the CNF field of GPIOx_CRL to 0b10 and the MODE field to nonzero.
Note, that the CNF field is by default set to 0b01, so by ORing 0b10 to is you get 0b11, which would be Alternate mode Open-drain.
JW
2022-04-09 11:37 AM
You can't change the BRR on the UART after you've enabled it.
I'd imagine the Arduino loop would print numbers
void uart2_tx_init(void)
{
RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN; /*(0x1 << 2) Enabling GPIOA */
RCC ->APB1ENR |= RCC_APB1ENR_USART2EN; /* (0x1 << 2)Enabling UART */
// Probably,
GPIOA->CRL |= GPIO_CRL_MODE2_1;/*(0x02 << 8u) */ /*Enable Alternate function for Pin A2 */
GPIOA->CRL |= GPIO_CRL_MODE3_1; /*(0x02<<12)/*Enable Alternate function for Pin A3 */
USART2->CR1 |= 0x0008; // Transmit Enable
USART2->BRR = (36000000 / (16 * 9600)); // Calculated using the formulae 36MHz/16*9600. compiler folds the math for constants
USART2->CR1 |= 0x2000; // Uart Enable
}
void uart2_write(int ch)
{
/*Make sure transmit data register is empty*/
while(!(USART2 ->SR & USART_SR_TXE)) {}
/* Write to transmit data register*/
USART2->DR = (ch & 0xFF);
}
2022-04-09 01:24 PM
Clive, @Community member
> USART2->BRR = (36000000 / (16 * 9600));
No, the comment is lying.
JW
2022-04-09 02:55 PM
USART2->BRR = (36000000 / 9600); // compiler folds the math for constants
2022-04-10 01:12 AM
There are errors in almost every line of your code.
USART2->BRR = (APB1_CLK + UART_BAUDRATE / 2) / UART_BAUDRATE;
USART2->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;
To write data, use USART2->DR = ch; DO NOT use |=.
2022-04-12 04:36 AM
I have changed the code as per your comment but I am not able to transmit the data. I have also altered the CRL and now it reads 'B' - 1011 for the pin A2 and A3. Can you please tell me any changes to be done?
2022-04-12 04:49 AM
Thank you for your response , I have changes the bit as per your suggestion but i still see no communication, I have changed the data as
GPIOA->CRL |= GPIO_CRL_MODE2_Msk; /*Eanble Max frequency of 50 MHz(11)*/
GPIOA->CRL^= (GPIO_CRL_CNF2);/*Enable Alternate function for Pin A2 (03<<10U) */
2022-04-12 04:51 AM
I have obtained the value 0xEA6 as per the example shown in the reference manual. PFA