Facebook
From ja, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 328
  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. /***************************************************************************//**
  38.  * @brief SysTick_Handler
  39.  *   Interrupt Service Routine for system tick counter
  40.  * @note
  41.  *   No wrap around protection
  42.  ******************************************************************************/
  43. void SysTick_Handler(void)
  44. {
  45.  
  46.   msTicks++;       /* increment counter necessary in Delay()*/
  47.  
  48.         if (!GPIO_PinInGet(gpioPortB, 9)) {
  49.                 led0_time++;
  50.         }
  51.         if (!GPIO_PinInGet(gpioPortB, 10)) {
  52.                 led1_time++;
  53.         }
  54. }
  55.  
  56. /***************************************************************************//**
  57.  * @brief Delays number of msTick Systicks (typically 1 ms)
  58.  * @param dlyTicks Number of ticks to delay
  59.  ******************************************************************************/
  60. void Delay(uint32_t dlyTicks)
  61. {
  62.   uint32_t curTicks;
  63.  
  64.   curTicks = msTicks;
  65.   while ((msTicks - curTicks) < dlyTicks) ;
  66. }
  67.  
  68. /***************************************************************************//**
  69.  * @brief  Gpio callback
  70.  * @param  pin - pin which triggered interrupt
  71.  ******************************************************************************/
  72. void gpioCallback(uint8_t pin)
  73. {
  74.   if (pin == 9) {
  75.           if (GPIO_PinInGet(gpioPortB, 9))
  76.                   led0_start = 1;
  77.           else
  78.                   if (!GPIO_PinInGet(gpioPortB, 10))
  79.                           toggle_leds = 1;
  80.                   else
  81.                           led0_time = 0;
  82.   }
  83.   else if (pin == 10) {
  84.           if (GPIO_PinInGet(gpioPortB, 10))
  85.                   led1_start = 1;
  86.           else
  87.                   if (!GPIO_PinInGet(gpioPortB, 9))
  88.                          toggle_leds = 1;
  89.                   else
  90.                           led1_time = 0;
  91.   }
  92.  
  93.  
  94. }
  95.  
  96. /***************************************************************************//**
  97.  * @brief  Gpio setup. Setup button pins to trigger falling edge interrupts.
  98.  *  Register callbacks for that interrupts.
  99.  ******************************************************************************/
  100. void gpioSetup(void)
  101. {
  102.   /* Enable GPIO in CMU */
  103.   CMU_ClockEnable(cmuClock_GPIO, true);
  104.  
  105.   /* Initialize GPIO interrupt dispatcher */
  106.   GPIOINT_Init();
  107.  
  108.   /* Configure PB9 and PB10 as input */
  109.   GPIO_PinModeSet(gpioPortB, 9, gpioModeInput, 0);
  110.   GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0);
  111.  
  112.   /* Register callbacks before setting up and enabling pin interrupt. */
  113.   GPIOINT_CallbackRegister(9, gpioCallback);
  114.   GPIOINT_CallbackRegister(10, gpioCallback);
  115.  
  116.   /* Set falling edge interrupt for both ports */
  117.   GPIO_IntConfig(gpioPortB, 9, true, true, true);
  118.   GPIO_IntConfig(gpioPortB, 10, true, true, true);
  119. }
  120.  
  121.  
  122. /***************************************************************************//**
  123.  * @brief  Main function
  124.  ******************************************************************************/
  125. int main(void)
  126. {
  127.   uint32_t sum;
  128.   uint32_t led0_time_tmp;
  129.   uint32_t led1_time_tmp;
  130.  
  131.   /* Chip errata */
  132.   CHIP_Init();
  133.  
  134.   /* If first word of user data page is non-zero, enable eA Profiler trace */
  135.   BSP_TraceProfilerSetup();
  136.  
  137.   /* Enable two leds to show we're alive */
  138.   BSP_LedsInit();
  139.  
  140.   /* Setup SysTick Timer for 1 msec interrupts  */
  141.   if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) {
  142.     while (1) ;
  143.   }
  144.  
  145.   /* Enable LCD without voltage boost */
  146.   SegmentLCD_Init(false);
  147.  
  148.   /* Initialize gpio */
  149.   gpioSetup();
  150.  
  151.   /* Infinite loop with test pattern. */
  152.   while (1) {
  153.           if (led0_start && led1_start) {
  154.                   sum = led0_time + led1_time;
  155.                   if (!led1_time_tmp) led1_time_tmp = led1_time;
  156.                   if (!led0_time_tmp) led0_time_tmp = led0_time;
  157.                   SegmentLCD_Number(sum);
  158.                   Delay(2000);
  159.                   led1_start = 0;
  160.                   led0_start = 0;
  161.                   SegmentLCD_AllOff();
  162.           }
  163.  
  164.           if (toggle_leds) {
  165.  
  166.                   while (led0_time_tmp) { //&& led1_time_tmp != 0) {
  167.                           if (led0_time_tmp > 0) {
  168.                                   led0_time_tmp--;
  169.                                   BSP_LedSet(0);
  170.                           }
  171.                           else
  172.                                   BSP_LedClear(0);
  173.  
  174.                           /*if (led1_time_tmp > 0) {
  175.                                   led1_time_tmp--;
  176.                                   BSP_LedSet(1);
  177.                           }
  178.                           else
  179.                                   BSP_LedClear(1);
  180. */
  181.                           Delay(1);
  182.                   }
  183.  
  184.                   toggle_leds = 0;
  185.           }
  186.  
  187.  
  188.   }
  189. }
  190.