Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Main code of firmware
- */
- #include "stm32f10x.h"
- /* General defines */
- #define LED_PORT GPIOC
- #define IO_PORT GPIOA
- #define BUTTON (1 << 0)
- #define LED_BLUE (1 << 8)
- #define LED_GREEN (1 << 9)
- void main();
- /* provided by linker script */
- extern unsigned long _estack;
- /* Reset handler */
- void Reset_Handler(void) {
- main();
- }
- /* Table of Cortex vectors */
- void *vector_table[] __attribute__ ((section(".vectors"))) = {
- &_estack,
- Reset_Handler,
- };
- /* Simple delay function */
- void delay_ms(unsigned int delay) {
- volatile unsigned int delay_tick = delay * 2000;
- while (delay_tick--);
- }
- /* Main function */
- void main(void) {
- /* setup i/o port c */
- RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
- LED_PORT->CRH |= GPIO_CRH_MODE8_0 | GPIO_CRH_MODE9_0;
- LED_PORT->CRH &= ~(GPIO_CRH_CNF8 | GPIO_CRH_CNF9);
- /* setup i/o port a */
- RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
- IO_PORT->CRL &= ~(GPIO_CRL_CNF0);
- IO_PORT->CRL |= GPIO_CRL_CNF0_1;
- /* pull-down to pin 0 port a */
- IO_PORT->ODR &= BUTTON;
- /* enable leds */
- LED_PORT->ODR = LED_BLUE | LED_GREEN;
- /* infinity loop */
- while (1) {
- if (IO_PORT->IDR & BUTTON) {
- /* disable leds */
- LED_PORT->ODR &= ~LED_BLUE;
- LED_PORT->ODR |= LED_GREEN;
- /* delay ~500ms */
- delay_ms(500);
- /* enable leds */
- LED_PORT->ODR |= LED_BLUE;
- LED_PORT->ODR &= ~LED_GREEN;
- /* delay ~500ms */
- delay_ms(500);
- }
- else {
- /* disable leds */
- LED_PORT->ODR &= ~(LED_BLUE | LED_GREEN);
- /* delay ~500ms */
- delay_ms(500);
- /* enable leds */
- LED_PORT->ODR |= LED_BLUE | LED_GREEN;
- /* delay ~500ms */
- delay_ms(500);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement