usart1.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * usart.c
  3. * http://www.skybeeper.com/index.php/en/english-en/25-tighten-scanf
  4. */
  5. #include "usart1.h"
  6. /*
  7. +=============================================================================+
  8. /file usart1.c
  9. +=============================================================================+
  10. */
  11. /*
  12. * how to count BRR
  13. * BRR = P_frequency /(16*baudrate)
  14. * for example:
  15. * 24 000 000/(16*9600)= 156,25
  16. * 156 dec = 0x9C
  17. * 0,25*16 = 4dec = 0x4 hex
  18. * BRR = 0x009C+0x0004 = 0x09C4
  19. *
  20. * this way of counting BRR is equal and gives results directly in hex
  21. * BRR = (PLL_FREQUENCY+UART1_BAUDRATE/2)/UART1_BAUDRATE;
  22. */
  23. void Usart1Init(void){
  24. RCC->APB2ENR |= (RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN); //Clock
  25. AFIO->MAPR &= ~(AFIO_MAPR_USART1_REMAP); //UART1 at the original pins
  26. GPIOA->CRH = 0x0090; //GPIO_CRH_BIT9 = Alternate function, 10MHz
  27. USART1->BRR = (PLL_FREQUENCY+UART1_BAUDRATE/2)/UART1_BAUDRATE;
  28. //0x008B; Baud=FREQUENCY/(16*8,69)=115107; // BRR simplified thanks to Uwe Hermann
  29. USART1->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // RX, TX enable
  30. }
  31. void Usart1Put(char ch)
  32. {
  33. USART_SendData(USART1, (char) ch);
  34. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  35. {
  36. }
  37. }
  38. char Usart1Get(void){
  39. while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
  40. return (char)USART_ReceiveData(USART1);
  41. }