Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stm32f0xx.h>
- /*
- #define LED_PIN 5
- #define LED_ON() GPIOA->BSRRL |= (1 << 5)
- #define LED_OFF() GPIOA->BSRRH |= (1 << 5)
- */
- // NOTE: Can also write to the GPIOC->BSRR ( bit set reset register )
- // Depending on position, it either sets the bit in the ODR or clears it.
- #define LED_PIN 9
- #define LED_ON() GPIOC->ODR |= (1 << LED_PIN )
- #define LED_OFF() GPIOC->ODR &= ~(1 << LED_PIN )
- #define PAUSE 1 * 1000 * 1000
- void delay(unsigned long count) {
- unsigned long x;
- for (x=0; x < count; x++) {
- __asm("nop");
- }
- }
- /*
- * sets up pins and what not for UART2 use.
- */
- void uart2_init() {
- // enable GPIO register clock which is on GPIOA
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- // enable UART2 clock
- RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
- // configure TX pin (PA2) for output by setting MODER2 bit 0
- GPIOA->MODER |= GPIO_MODER_MODER2_0;
- GPIOA->MODER &= ~GPIO_MODER_MODER2_1;
- // ensure pullup/pulldown resistors disabled
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR8|GPIO_PUPDR_PUPDR9);
- // set the output type to push pull by clearing the OTYPER bit ( on reset is set to push/pull )
- GPIOA->OTYPER &= ~GPIO_OTYPER_OT_2;
- // set the output speed of the pin to LOW SPEED by setting both bits low
- //GPIOA->OSPEEDR &= ~GPIO_OSPEEDR_OSPEEDR2;
- GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR2;
- // the USART2 registers should all be set to sane defaults on reset
- // disable the UART by clearing the UE bit
- USART2->CR1 &= ~USART_CR1_UE;
- // set baud rate
- GPIOA->BRR = (480000 / 96);
- // enable USART, transmit enable
- USART2->CR1 |= USART_CR1_UE | USART_CR1_TE;
- // clear Transmit Complete flag
- USART2->ICR |= USART_ICR_TCCF;
- }
- void uart2_tx(char c) {
- USART2->TDR = c;
- while ( ! (USART2->ISR & USART_ISR_TC) ) {
- __asm("nop");
- }
- USART2->ICR |= USART_ICR_TCCF;
- }
- int main() {
- /* Enbale GPIOA clock */
- //RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
- // enable PC register clock
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- /* Configure GPIOA pin 5 as output */
- //GPIOC->MODER |= (1 << (LED_PIN << 1));
- GPIOC->MODER |= GPIO_MODER_MODER9_0;
- /* Configure GPIOA pin 5 in max speed */
- GPIOC->OSPEEDR |= (3 << (LED_PIN << 1));
- uart2_init();
- while (1) {
- LED_OFF();
- uart2_tx('0');
- uart2_tx('\n');
- delay(PAUSE);
- LED_ON();
- uart2_tx('1');
- uart2_tx('\n');
- delay(PAUSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement