|
| 1 | +/******************************************************************************* |
| 2 | +Copyright 2016 Microchip Technology Inc. (www.microchip.com) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*******************************************************************************/ |
| 16 | + |
| 17 | +#include "bsp/buttons.h" |
| 18 | +#include "bsp/rgb_led3.h" |
| 19 | +#include "bsp/led1.h" |
| 20 | +#include "bsp/led2.h" |
| 21 | +#include "bsp/timer_1ms.h" |
| 22 | +#include "demo.h" |
| 23 | +#include "mcc_generated_files/adc1.h" |
| 24 | + |
| 25 | +//------------------------------------------------------------------------------ |
| 26 | +//Application related definitions |
| 27 | +//------------------------------------------------------------------------------ |
| 28 | +#define BUTTON_DEBOUCE_TIME_MS 20 |
| 29 | + |
| 30 | +typedef enum |
| 31 | +{ |
| 32 | + BUTTON_COLOR_RED = 0, |
| 33 | + BUTTON_COLOR_GREEN = 1, |
| 34 | + BUTTON_COLOR_BLUE = 2 |
| 35 | +} BUTTON_COLOR; |
| 36 | + |
| 37 | +//------------------------------------------------------------------------------ |
| 38 | +//Global variables |
| 39 | +//------------------------------------------------------------------------------ |
| 40 | +static volatile BUTTON_COLOR button_color = BUTTON_COLOR_RED; |
| 41 | +static uint16_t potentiometer; |
| 42 | +static uint16_t red = 600; |
| 43 | +static uint16_t green = 300; |
| 44 | +static uint16_t blue = 150; |
| 45 | + |
| 46 | +//------------------------------------------------------------------------------ |
| 47 | +//Function prototypes |
| 48 | +//------------------------------------------------------------------------------ |
| 49 | +static void ButtonS1Debounce(void); |
| 50 | +static void ButtonS2Debounce(void); |
| 51 | +static void UpdatePotentiometer(void); |
| 52 | + |
| 53 | +/********************************************************************* |
| 54 | +* Function: void DEMO_Initialize(void) |
| 55 | +* |
| 56 | +* Overview: Initializes the demo by configuring the required peripherals. |
| 57 | +* |
| 58 | +* PreCondition: None |
| 59 | +* |
| 60 | +* Input: None |
| 61 | +* |
| 62 | +* Output: None |
| 63 | +* |
| 64 | +********************************************************************/ |
| 65 | +void DEMO_Initialize(void) |
| 66 | +{ |
| 67 | + LED1_Off(); |
| 68 | + LED2_Off(); |
| 69 | + |
| 70 | + RGB_LED3_SetColor(red, green, blue); |
| 71 | + RGB_LED3_On(); |
| 72 | + |
| 73 | + |
| 74 | + //Turn on a timer, so to generate periodic interrupts. |
| 75 | + TIMER_SetConfiguration(TIMER_CONFIGURATION_1MS); |
| 76 | + |
| 77 | + //Register the ButtonDebounce() callback function, so it gets called periodically |
| 78 | + //when the timer interrupts occur (in this case at 1:1 rate, so ButtonDebounce() |
| 79 | + //executes once per 1ms). |
| 80 | + TIMER_RequestTick(&ButtonS1Debounce, 1); |
| 81 | + TIMER_RequestTick(&ButtonS2Debounce, 1); |
| 82 | +} |
| 83 | + |
| 84 | +/********************************************************************* |
| 85 | +* Function: void DEMO_Tasks(void) |
| 86 | +* |
| 87 | +* Overview: Demo task function which updates the brightness of the currently |
| 88 | +* selected color channel on the RGB LED . |
| 89 | +* |
| 90 | +* PreCondition: None |
| 91 | +* |
| 92 | +* Input: None |
| 93 | +* |
| 94 | +* Output: None |
| 95 | +* |
| 96 | +********************************************************************/ |
| 97 | +void DEMO_Tasks(void) |
| 98 | +{ |
| 99 | + UpdatePotentiometer(); |
| 100 | + |
| 101 | + //Use the potentiometer ADC value to set the brightness of the currently |
| 102 | + //selected color channel on the RGB LED. The "currently selected channel" |
| 103 | + //is manually selected by the user at runtime by pressing the pushbuttons. |
| 104 | + switch(button_color) |
| 105 | + { |
| 106 | + case BUTTON_COLOR_RED: |
| 107 | + red = (potentiometer); |
| 108 | + break; |
| 109 | + |
| 110 | + case BUTTON_COLOR_GREEN: |
| 111 | + green = (potentiometer); |
| 112 | + break; |
| 113 | + |
| 114 | + case BUTTON_COLOR_BLUE: |
| 115 | + blue = (potentiometer); |
| 116 | + break; |
| 117 | + |
| 118 | + default: |
| 119 | + break; |
| 120 | + } |
| 121 | + |
| 122 | + RGB_LED3_SetColor(red, green, blue); |
| 123 | +} |
| 124 | + |
| 125 | +//Helper function that advances the currently selected RGB color channel that |
| 126 | +//is to be adjusted next. This function is called in response to user pushbutton |
| 127 | +//press events. |
| 128 | +static void ChangeColor(void) |
| 129 | +{ |
| 130 | + switch(button_color) |
| 131 | + { |
| 132 | + case BUTTON_COLOR_RED: |
| 133 | + button_color = BUTTON_COLOR_GREEN; |
| 134 | + break; |
| 135 | + |
| 136 | + case BUTTON_COLOR_GREEN: |
| 137 | + button_color = BUTTON_COLOR_BLUE; |
| 138 | + break; |
| 139 | + |
| 140 | + case BUTTON_COLOR_BLUE: |
| 141 | + button_color = BUTTON_COLOR_RED; |
| 142 | + break; |
| 143 | + |
| 144 | + default: |
| 145 | + button_color = BUTTON_COLOR_RED; |
| 146 | + break; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/********************************************************************* |
| 151 | +* Function: static void ButtonS1Debounce(void) |
| 152 | +* |
| 153 | +* Overview: This callback function gets called periodically (1/1ms) by the timer interrupt event |
| 154 | +* handler. This function is used to periodically sample the pushbutton and implements |
| 155 | +* a de-bounce algorithm to reject spurious chatter that can occur during press events. |
| 156 | +* |
| 157 | +* PreCondition: None |
| 158 | +* |
| 159 | +* Input: None |
| 160 | +* |
| 161 | +* Output: None |
| 162 | +* |
| 163 | +********************************************************************/ |
| 164 | +static void ButtonS1Debounce(void) |
| 165 | +{ |
| 166 | + static uint16_t debounceCounter = 0; |
| 167 | + |
| 168 | + //Sample the button S1 to see if it is currently pressed or not. |
| 169 | + if(BUTTON_IsPressed(BUTTON_S1)) |
| 170 | + { |
| 171 | + //The button is currently pressed. Turn on the general purpose LED. |
| 172 | + LED1_On(); |
| 173 | + |
| 174 | + //Check if the de-bounce blanking interval has been satisfied. If so, |
| 175 | + //advance the RGB color channel user control selector. |
| 176 | + if(debounceCounter == 0) |
| 177 | + { |
| 178 | + ChangeColor(); |
| 179 | + } |
| 180 | + |
| 181 | + //Reset the de-bounce countdown timer, so a new color change operation |
| 182 | + //won't occur until the button is released and remains continuously released |
| 183 | + //for at least BUTTON_DEBOUCE_TIME_MS. |
| 184 | + debounceCounter = BUTTON_DEBOUCE_TIME_MS; |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + //The button is not currently pressed. Turn off the LED. |
| 189 | + LED1_Off(); |
| 190 | + |
| 191 | + //Allow the de-bounce interval timer to count down, until it reaches 0. |
| 192 | + //Once it reaches 0, the button is effectively "re-armed". |
| 193 | + if(debounceCounter != 0) |
| 194 | + { |
| 195 | + debounceCounter--; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +/********************************************************************* |
| 201 | +* Function: static void ButtonS2Debounce(void) |
| 202 | +* |
| 203 | +* Overview: This callback function gets called periodically (1/1ms) by the timer interrupt event |
| 204 | +* handler. This function is used to periodically sample the pushbutton and implements |
| 205 | +* a de-bounce algorithm to reject spurious chatter that can occur during press events. |
| 206 | +* |
| 207 | +* PreCondition: None |
| 208 | +* |
| 209 | +* Input: None |
| 210 | +* |
| 211 | +* Output: None |
| 212 | +* |
| 213 | +********************************************************************/ |
| 214 | +static void ButtonS2Debounce(void) |
| 215 | +{ |
| 216 | + static uint16_t debounceCounter = 0; |
| 217 | + |
| 218 | + //Sample the button S2 to see if it is currently pressed or not. |
| 219 | + if(BUTTON_IsPressed(BUTTON_S2)) |
| 220 | + { |
| 221 | + //The button is currently pressed. Turn on the general purpose LED. |
| 222 | + LED2_On(); |
| 223 | + |
| 224 | + //Check if the de-bounce blanking interval has been satisfied. If so, |
| 225 | + //advance the RGB color channel user control selector. |
| 226 | + if(debounceCounter == 0) |
| 227 | + { |
| 228 | + ChangeColor(); |
| 229 | + } |
| 230 | + |
| 231 | + //Reset the de-bounce countdown timer, so a new color change operation |
| 232 | + //won't occur until the button is released and remains continuously released |
| 233 | + //for at least BUTTON_DEBOUCE_TIME_MS. |
| 234 | + debounceCounter = BUTTON_DEBOUCE_TIME_MS; |
| 235 | + } |
| 236 | + else |
| 237 | + { |
| 238 | + //The button is not currently pressed. Turn off the LED. |
| 239 | + LED2_Off(); |
| 240 | + |
| 241 | + //Allow the de-bounce interval timer to count down, until it reaches 0. |
| 242 | + //Once it reaches 0, the button is effectively "re-armed". |
| 243 | + if(debounceCounter != 0) |
| 244 | + { |
| 245 | + debounceCounter--; |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | + |
| 251 | +/********************************************************************* |
| 252 | +* Function: static void UpdatePotentiometer(void) |
| 253 | +* |
| 254 | +* Overview: Reads the value of the Potentiometer to update the RGB LED brightness. |
| 255 | +* |
| 256 | +* PreCondition: None |
| 257 | +* |
| 258 | +* Input: None |
| 259 | +* |
| 260 | +* Output: None |
| 261 | +* |
| 262 | +********************************************************************/ |
| 263 | +static void UpdatePotentiometer(void) |
| 264 | +{ |
| 265 | + volatile uint16_t i=0; |
| 266 | + //Enable ADC module |
| 267 | + ADC1_Enable(); |
| 268 | + |
| 269 | + //Select the PotentioMeter ADC Channel |
| 270 | + ADC1_ChannelSelect(channel_AN5); |
| 271 | + //Start Sampling |
| 272 | + ADC1_SoftwareTriggerEnable(); |
| 273 | + //ADC sampling delay |
| 274 | + for(i=0;i<65535;i++) |
| 275 | + { |
| 276 | + //Do Nothing |
| 277 | + } |
| 278 | + ADC1_SoftwareTriggerDisable(); |
| 279 | + //Check if the ADC conversion is completed |
| 280 | + while(!ADC1_IsConversionComplete(channel_AN5)) |
| 281 | + { |
| 282 | + //Do Nothing |
| 283 | + } |
| 284 | + // Get the Potentiometer ADC values |
| 285 | + potentiometer = ADC1_ConversionResultGet(channel_AN5); |
| 286 | +} |
0 commit comments