Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stm32f0xx.h>
  2.  
  3. /*
  4. #define LED_PIN 5
  5. #define LED_ON() GPIOA->BSRRL |= (1 << 5)
  6. #define LED_OFF() GPIOA->BSRRH |= (1 << 5)
  7. */
  8.  
  9.  
  10.  
  11. // NOTE: Can also write to the GPIOC->BSRR ( bit set reset register )
  12. // Depending on position, it either sets the bit in the ODR or clears it.
  13.  
  14. #define LED_PIN 9
  15. #define LED_ON() GPIOC->ODR |= (1 << LED_PIN )
  16. #define LED_OFF() GPIOC->ODR &= ~(1 << LED_PIN )
  17.  
  18.  
  19. #define PAUSE 1 * 1000 * 1000
  20.  
  21. void delay(unsigned long count) {
  22.     unsigned long x;
  23.     for (x=0; x < count; x++) {
  24.         __asm("nop");
  25.     }
  26. }
  27.  
  28. /*
  29.  * sets up pins and what not for UART2 use.
  30.  */
  31. void uart2_init() {
  32.  
  33.   // enable GPIO register clock which is on GPIOA
  34.   RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  35.   // enable UART2 clock
  36.   RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  37.  
  38.   // configure TX pin (PA2) for output by setting MODER2 bit 0
  39.   GPIOA->MODER |= GPIO_MODER_MODER2_0;
  40.   GPIOA->MODER &= ~GPIO_MODER_MODER2_1;
  41.  
  42.   // ensure pullup/pulldown resistors disabled
  43.   GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR8|GPIO_PUPDR_PUPDR9);
  44.  
  45.   // set the output type to push pull by clearing the OTYPER bit ( on reset is set to push/pull )
  46.   GPIOA->OTYPER &= ~GPIO_OTYPER_OT_2;
  47.  
  48.   // set the output speed of the pin to LOW SPEED by setting both bits low
  49.   //GPIOA->OSPEEDR &= ~GPIO_OSPEEDR_OSPEEDR2;
  50.   GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR2;
  51.  
  52.   // the USART2 registers should all be set to sane defaults on reset
  53.   // disable the UART by clearing the UE bit
  54.   USART2->CR1 &= ~USART_CR1_UE;
  55.  
  56.   // set baud rate
  57.   GPIOA->BRR = (480000 / 96);
  58.  
  59.   // enable USART, transmit enable
  60.   USART2->CR1 |= USART_CR1_UE | USART_CR1_TE;
  61.  
  62.   // clear Transmit Complete flag
  63.   USART2->ICR |= USART_ICR_TCCF;
  64.  
  65. }
  66.  
  67. void uart2_tx(char c) {
  68.   USART2->TDR = c;
  69.   while ( ! (USART2->ISR & USART_ISR_TC) ) {
  70.     __asm("nop");
  71.   }
  72.   USART2->ICR |= USART_ICR_TCCF;
  73.  
  74. }
  75.  
  76. int main() {
  77.     /* Enbale GPIOA clock */
  78.     //RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  79.     // enable PC register clock
  80.     RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  81.     /* Configure GPIOA pin 5 as output */
  82.     //GPIOC->MODER |= (1 << (LED_PIN << 1));
  83.     GPIOC->MODER |= GPIO_MODER_MODER9_0;
  84.  
  85.     /* Configure GPIOA pin 5 in max speed */
  86.     GPIOC->OSPEEDR |= (3 << (LED_PIN << 1));
  87.  
  88.     uart2_init();
  89.  
  90.     while (1) {
  91.         LED_OFF();
  92.         uart2_tx('0');
  93.         uart2_tx('\n');
  94.         delay(PAUSE);
  95.         LED_ON();
  96.         uart2_tx('1');
  97.         uart2_tx('\n');
  98.         delay(PAUSE);
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement