Advertisement
xgallom

STM Vianocna ozdoba

Jun 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Ac6
  5.   * @version V1.0
  6.   * @date    01-December-2013
  7.   * @brief   Default main function.
  8.   ******************************************************************************
  9. */
  10.  
  11. #include "stm32f4xx.h"
  12.  
  13. /* Memory mapping for registers */
  14. static volatile uint32_t * const gpiob  = (volatile uint32_t *) 0x40020400;
  15. static volatile uint32_t * const gpioc  = (volatile uint32_t *) 0x40020800;
  16. static volatile uint32_t * const rcc    = (volatile uint32_t *) 0x40023800;
  17. static volatile uint32_t * const systick= (volatile uint32_t *) 0xe000e010;
  18.  
  19. /* Global variables */
  20. // Input timer, and led update counter
  21. static volatile uint32_t inputCounter = 0UL, timerCounter = 0Ul;
  22.  
  23. /* Setup method */
  24. static void setup(void)
  25. {
  26.     /*-Setup-SysTick-----------------------------------------------------------*/
  27.     // Set SysTick frequency
  28.     systick[1] = (SystemCoreClock / 1000000) - 0x00000001;
  29.  
  30.     // Set SysTick interrupt priority
  31.     NVIC_SetPriority(SysTick_IRQn, 0x0f);
  32.  
  33.     // Clear counter
  34.     systick[2] = 0x00000000;
  35.  
  36.     // Enable and configure
  37.     systick[0] = 0x00000007;
  38.  
  39.     /*-Setup-GPIO-clocks-------------------------------------------------------*/
  40.     // Enable AHB1 -> GPIOB, GPIOC
  41.     rcc[12] |= 0x00000006;
  42.  
  43.     /*-Setup-GPIOB-(leds)------------------------------------------------------*/
  44.     // Output mode - 0x01
  45.     gpiob[0] &= ~0x3000c003;
  46.     gpiob[0] |=  0x10004001;
  47.  
  48.     // Push pull - 0x0
  49.     gpiob[1] &= 0x00004081;
  50.  
  51.     // Speed fast - 0x02
  52.     gpiob[2] &= ~0x3000c003;
  53.     gpiob[2] |=  0x20008002;
  54.  
  55.     // No pull - 0x00
  56.     gpiob[3] &= ~0x3000c003;
  57.  
  58.     /*-Setup-GPIOC-(button)----------------------------------------------------*/
  59.     // Input mode - 0x00
  60.     gpioc[0] &= ~0x0c000000;
  61.  
  62.     // Pull down - 0x02
  63.     gpioc[3] &= ~0x0c000000;
  64.     gpioc[3] |=  0x08000000;
  65. }
  66.  
  67. /* Update method  */
  68. static void update(void)
  69. {
  70.     /* Stores the state of the operation of the MCU
  71.      *
  72.      * Bits usage:
  73.      * +---+   +-+   +-----+
  74.      * |7 6| 5 |4| 3 |2 1 0|
  75.      * +---+   +-+   +-----+
  76.      *   |      |       |
  77.      *   |      |       +---> [0x07] Mode of operation:   {0}  : Off
  78.      *   |      |                                       {1,2,3}: A single led is on
  79.      *   |      |                                        {4,5} : Time based modes - led is getting toggled
  80.      *   |      |                                                  Also uses bits 6 and 7 to store state
  81.      *   |      |
  82.      *   |      +-----------> [0x10] Queue update: If this bit is set, MCU will update PORTB during next cycle (when button is pressed)
  83.      *   |
  84.      *   +------------------> [0xc0] Mode state: Only used in time based modes, stores which led is on and which to turn on next
  85.      *
  86.      * Initial value: 0x11 - queue update + mode 1
  87.      */
  88.     static uint8_t state = 0x11;
  89.  
  90.     // If button is pressed and input isn't disabled
  91.     if((gpioc[4] & 0x00002000) && !inputCounter) {
  92.         // Toggle to next mode
  93.         ++state;
  94.  
  95.         // Loop modes
  96.         if((state & 0x0f) > 0x05)
  97.             state = 0x00;
  98.  
  99.         // Queue update
  100.         state |= 0x10;
  101.  
  102.         // Add 500ms input delay
  103.         inputCounter = 5e5;
  104.     }
  105.  
  106.     // If update is queued, or we are in a time based mode and we should update
  107.     if((state & 0x10) || ((state & 0x04) && !timerCounter)) {
  108.         // Port output mask
  109.         uint32_t outputMask = 0x00000000;
  110.  
  111.         // If in a time-based mode
  112.         if(state & 0x04) {
  113.             // Add delay
  114.             timerCounter = state & 0x01 ? 1e5 : 3e5;
  115.  
  116.             // Last two bits of the state
  117.             uint8_t modeState = state >> 6;
  118.  
  119.             // Turn on a single led based on modeState
  120.             outputMask = 0x01 << (7 * modeState);
  121.  
  122.             // Increase modeState
  123.             if(++modeState > 2)
  124.                 modeState = 0;
  125.  
  126.             // Store modeState back to state
  127.             state = (state & 0x3f) | (modeState << 6);
  128.         }
  129.         // If in a static mode
  130.         else {
  131.             switch(state & 0x03) {
  132.             case 0x01:
  133.                 // Green
  134.                 outputMask = 0x00000001;
  135.                 break;
  136.  
  137.             case 0x02:
  138.                 // Blue
  139.                 outputMask = 0x00000080;
  140.                 break;
  141.  
  142.             case 0x03:
  143.                 // Red
  144.                 outputMask = 0x00004000;
  145.                 break;
  146.  
  147.             default:
  148.                 // Off
  149.                 break;
  150.             }
  151.         }
  152.  
  153.         // Turn on only active led
  154.         gpiob[5] = (gpiob[5] & ~0x00004081) | outputMask;
  155.  
  156.         // Remove update if queued
  157.         state &= ~0x10;
  158.     }
  159.  
  160. }
  161.  
  162. /* Main method */
  163. int main(void)
  164. {
  165.     // Perform setup
  166.     setup();
  167.  
  168.     // Loop update method
  169.     for(;;)
  170.         update();
  171. }
  172.  
  173. /* SysTick interrupt handler */
  174. void SysTick_Handler(void)
  175. {
  176.     // Each interrupt (t=1us) decrements counters by 1
  177.  
  178.     if(inputCounter)
  179.         inputCounter--;
  180.  
  181.     if(timerCounter)
  182.         timerCounter--;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement