Facebook
From ja, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 299
  1. /***************************************************************************//**
  2.  * @file
  3.  * @brief LCD controller demo for EFM32GG_STK3700 development kit
  4.  * @version 5.3.5
  5.  *******************************************************************************
  6.  * # License
  7.  * <b>Copyright 2015 Silicon Labs, Inc. http://www.silabs.com</b>
  8.  *******************************************************************************
  9.  *
  10.  * This file is licensed under the Silabs License Agreement. See the file
  11.  * "Silabs_License_Agreement.txt" for details. Before using this software for
  12.  * any purpose, you must agree to the terms of that agreement.
  13.  *
  14.  ******************************************************************************/
  15.  
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "em_device.h"
  19. #include "em_chip.h"
  20. #include "em_cmu.h"
  21. #include "em_gpio.h"
  22. #include "bsp.h"
  23. #include "segmentlcd.h"
  24. #include "bsp_trace.h"
  25. #include "gpiointerrupt.h"
  26.  
  27. volatile uint32_t msTicks; /* counts 1ms timeTicks */
  28. volatile uint32_t led0_time = 0;
  29. volatile uint32_t led1_time = 0;
  30. volatile uint8_t led0_start = 0;
  31. volatile uint8_t led1_start = 0;
  32. volatile uint8_t toggle_leds = 0;
  33.  
  34. /* Locatl prototypes */
  35. void Delay(uint32_t dlyTicks);
  36.  
  37. volatile uint8_t state = 4;
  38. volatile uint8_t sw0_pushed = 0;
  39. volatile uint8_t sw1_pushed = 0;
  40. volatile uint16_t counter;
  41. volatile uint32_t sumarum = 0;
  42. volatile uint8_t display = 0;
  43. volatile uint8_t clear = 0;
  44.  
  45. /***************************************************************************//**
  46.  * @brief SysTick_Handler
  47.  *   Interrupt Service Routine for system tick counter
  48.  * @note
  49.  *   No wrap around protection
  50.  ******************************************************************************/
  51. void SysTick_Handler(void)
  52. {
  53.  
  54.         msTicks++;
  55.  
  56.         static uint8_t done0 = 0;
  57.         static uint8_t done1 = 0;
  58.         static uint8_t cnt = 10;
  59.  
  60.  
  61.         switch(state)
  62.         {
  63.                 case 0:
  64.                         if (!GPIO_PinInGet(gpioPortB, 10)) {
  65.                                 led0_time++;
  66.                                 sw0_pushed = 1;
  67.                         }
  68.                         else {
  69.                                 if (sw0_pushed == 1) {
  70.                                         state = 1;
  71.                                         sw0_pushed = 0;
  72.                                 }
  73.                         }
  74.                         break;
  75.                 case 1:
  76.                         if (!GPIO_PinInGet(gpioPortB, 9)) {
  77.                                 led1_time++;
  78.                                 sw1_pushed = 1;
  79.                         }
  80.                         else {
  81.                                 if (sw1_pushed == 1) {
  82.                                         sw1_pushed = 0;
  83.                                         counter = 2000;
  84.                                         sumarum = led0_time + led1_time;
  85.                                         display = 1;
  86.                                         state = 2;
  87.                                 }
  88.                         }
  89.                         break;
  90.                 case 2:
  91.                         if (counter) counter--;
  92.  
  93.                         if (!counter) {
  94.                                 clear = 1;
  95.  
  96.                                 if (!GPIO_PinInGet(gpioPortB, 10) && !GPIO_PinInGet(gpioPortB, 9)) {
  97.                                         state = 3;
  98.                                 }
  99.                         }
  100.                         break;
  101.                 case 3:
  102.                         if (led0_time > 0) {
  103.                                 BSP_LedSet(0);
  104.                                 led0_time--;
  105.                         }
  106.                         else {
  107.                                 BSP_LedClear(0);
  108.                                 done0 = 1;
  109.                         }
  110.  
  111.                         if (led1_time > 0) {
  112.                                 BSP_LedSet(1);
  113.                                 led1_time--;
  114.                         }
  115.                         else {
  116.                                 BSP_LedClear(1);
  117.                                 done1 = 1;
  118.                         }
  119.  
  120.                         if (done0 && done1) {
  121.                                 done0 = 0;
  122.                                 done1 = 0;
  123.                                 state = 4;
  124.                         }
  125.  
  126.                         break;
  127.                 case 4:
  128.                         if (cnt > 0) {
  129.                                 cnt--;
  130.                         }
  131.                         else {
  132.                                 cnt = 10;
  133.                                 state = 0;
  134.                         }
  135.                         break;
  136.         }
  137. }
  138.  
  139. /***************************************************************************//**
  140.  * @brief Delays number of msTick Systicks (typically 1 ms)
  141.  * @param dlyTicks Number of ticks to delay
  142.  ******************************************************************************/
  143. void Delay(uint32_t dlyTicks)
  144. {
  145.   uint32_t curTicks;
  146.  
  147.   curTicks = msTicks;
  148.   while ((msTicks - curTicks) < dlyTicks) ;
  149. }
  150.  
  151. /***************************************************************************//**
  152.  * @brief  Gpio callback
  153.  * @param  pin - pin which triggered interrupt
  154.  ******************************************************************************/
  155. void gpioCallback(uint8_t pin)
  156. {
  157.   /*if (pin == 9) {
  158.           if (GPIO_PinInGet(gpioPortB, 9))
  159.                   led0_start = 1;
  160.           else {
  161.                   if (!GPIO_PinInGet(gpioPortB, 10)) {
  162.                           toggle_leds = 1;
  163.                   }
  164.                   else
  165.                           led0_time = 0;
  166.           }
  167.   }
  168.   else if (pin == 10) {
  169.           if (GPIO_PinInGet(gpioPortB, 10))
  170.                   led1_start = 1;
  171.           else {
  172.                   if (!GPIO_PinInGet(gpioPortB, 9)) {
  173.                           toggle_leds = 1;
  174.                   }
  175.                   else
  176.                           led1_time = 0;
  177.           }
  178.   }*/
  179.  
  180. }
  181.  
  182. /***************************************************************************//**
  183.  * @brief  Gpio setup. Setup button pins to trigger falling edge interrupts.
  184.  *  Register callbacks for that interrupts.
  185.  ******************************************************************************/
  186. void gpioSetup(void)
  187. {
  188.   /* Enable GPIO in CMU */
  189.   CMU_ClockEnable(cmuClock_GPIO, true);
  190.  
  191.   /* Initialize GPIO interrupt dispatcher */
  192.   GPIOINT_Init();
  193.  
  194.   /* Configure PB9 and PB10 as input */
  195.   GPIO_PinModeSet(gpioPortB, 9, gpioModeInput, 0);
  196.   GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0);
  197.  
  198.   /* Register callbacks before setting up and enabling pin interrupt. */
  199.   GPIOINT_CallbackRegister(9, gpioCallback);
  200.   GPIOINT_CallbackRegister(10, gpioCallback);
  201.  
  202.   /* Set falling edge interrupt for both ports */
  203.   GPIO_IntConfig(gpioPortB, 9, false, false, true);
  204.   GPIO_IntConfig(gpioPortB, 10, false, false, true);
  205. }
  206.  
  207.  
  208. /***************************************************************************//**
  209.  * @brief  Main function
  210.  ******************************************************************************/
  211. int main(void)
  212. {
  213.   //uint32_t sum;
  214.   //uint32_t led0_time_tmp;
  215.   //uint32_t led1_time_tmp;
  216.  
  217.   /* Chip errata */
  218.   CHIP_Init();
  219.  
  220.   /* If first word of user data page is non-zero, enable eA Profiler trace */
  221.   BSP_TraceProfilerSetup();
  222.  
  223.   /* Enable two leds to show we're alive */
  224.   BSP_LedsInit();
  225.  
  226.   /* Setup SysTick Timer for 1 msec interrupts  */
  227.   if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) {
  228.     while (1) ;
  229.   }
  230.  
  231.   /* Enable LCD without voltage boost */
  232.   SegmentLCD_Init(false);
  233.  
  234.   /* Initialize gpio */
  235.   gpioSetup();
  236.  
  237.   while (1) {
  238.           if (display) {
  239.                   SegmentLCD_Number(sumarum);
  240.                   display = 0;
  241.           }
  242.  
  243.           if (clear) {
  244.                   //sum = 0;
  245.                   SegmentLCD_AllOff();
  246.                   clear = 0;
  247.           }
  248.  
  249.   }
  250. }
  251.