Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file main.c
- * @author Ac6
- * @version V1.0
- * @date 01-December-2013
- * @brief Default main function.
- ******************************************************************************
- */
- #include "stm32f4xx.h"
- /* Memory mapping for registers */
- static volatile uint32_t * const gpiob = (volatile uint32_t *) 0x40020400;
- static volatile uint32_t * const gpioc = (volatile uint32_t *) 0x40020800;
- static volatile uint32_t * const rcc = (volatile uint32_t *) 0x40023800;
- static volatile uint32_t * const systick= (volatile uint32_t *) 0xe000e010;
- /* Global variables */
- // Input timer, and led update counter
- static volatile uint32_t inputCounter = 0UL, timerCounter = 0Ul;
- /* Setup method */
- static void setup(void)
- {
- /*-Setup-SysTick-----------------------------------------------------------*/
- // Set SysTick frequency
- systick[1] = (SystemCoreClock / 1000000) - 0x00000001;
- // Set SysTick interrupt priority
- NVIC_SetPriority(SysTick_IRQn, 0x0f);
- // Clear counter
- systick[2] = 0x00000000;
- // Enable and configure
- systick[0] = 0x00000007;
- /*-Setup-GPIO-clocks-------------------------------------------------------*/
- // Enable AHB1 -> GPIOB, GPIOC
- rcc[12] |= 0x00000006;
- /*-Setup-GPIOB-(leds)------------------------------------------------------*/
- // Output mode - 0x01
- gpiob[0] &= ~0x3000c003;
- gpiob[0] |= 0x10004001;
- // Push pull - 0x0
- gpiob[1] &= 0x00004081;
- // Speed fast - 0x02
- gpiob[2] &= ~0x3000c003;
- gpiob[2] |= 0x20008002;
- // No pull - 0x00
- gpiob[3] &= ~0x3000c003;
- /*-Setup-GPIOC-(button)----------------------------------------------------*/
- // Input mode - 0x00
- gpioc[0] &= ~0x0c000000;
- // Pull down - 0x02
- gpioc[3] &= ~0x0c000000;
- gpioc[3] |= 0x08000000;
- }
- /* Update method */
- static void update(void)
- {
- /* Stores the state of the operation of the MCU
- *
- * Bits usage:
- * +---+ +-+ +-----+
- * |7 6| 5 |4| 3 |2 1 0|
- * +---+ +-+ +-----+
- * | | |
- * | | +---> [0x07] Mode of operation: {0} : Off
- * | | {1,2,3}: A single led is on
- * | | {4,5} : Time based modes - led is getting toggled
- * | | Also uses bits 6 and 7 to store state
- * | |
- * | +-----------> [0x10] Queue update: If this bit is set, MCU will update PORTB during next cycle (when button is pressed)
- * |
- * +------------------> [0xc0] Mode state: Only used in time based modes, stores which led is on and which to turn on next
- *
- * Initial value: 0x11 - queue update + mode 1
- */
- static uint8_t state = 0x11;
- // If button is pressed and input isn't disabled
- if((gpioc[4] & 0x00002000) && !inputCounter) {
- // Toggle to next mode
- ++state;
- // Loop modes
- if((state & 0x0f) > 0x05)
- state = 0x00;
- // Queue update
- state |= 0x10;
- // Add 500ms input delay
- inputCounter = 5e5;
- }
- // If update is queued, or we are in a time based mode and we should update
- if((state & 0x10) || ((state & 0x04) && !timerCounter)) {
- // Port output mask
- uint32_t outputMask = 0x00000000;
- // If in a time-based mode
- if(state & 0x04) {
- // Add delay
- timerCounter = state & 0x01 ? 1e5 : 3e5;
- // Last two bits of the state
- uint8_t modeState = state >> 6;
- // Turn on a single led based on modeState
- outputMask = 0x01 << (7 * modeState);
- // Increase modeState
- if(++modeState > 2)
- modeState = 0;
- // Store modeState back to state
- state = (state & 0x3f) | (modeState << 6);
- }
- // If in a static mode
- else {
- switch(state & 0x03) {
- case 0x01:
- // Green
- outputMask = 0x00000001;
- break;
- case 0x02:
- // Blue
- outputMask = 0x00000080;
- break;
- case 0x03:
- // Red
- outputMask = 0x00004000;
- break;
- default:
- // Off
- break;
- }
- }
- // Turn on only active led
- gpiob[5] = (gpiob[5] & ~0x00004081) | outputMask;
- // Remove update if queued
- state &= ~0x10;
- }
- }
- /* Main method */
- int main(void)
- {
- // Perform setup
- setup();
- // Loop update method
- for(;;)
- update();
- }
- /* SysTick interrupt handler */
- void SysTick_Handler(void)
- {
- // Each interrupt (t=1us) decrements counters by 1
- if(inputCounter)
- inputCounter--;
- if(timerCounter)
- timerCounter--;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement