Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My question is how do I get the RAMPUP and RAMPDOWN modes to reset to the original LED bit value so that it loops over

My question is how do I get the RAMPUP and RAMPDOWN modes to reset to the original LED bit value so that it loops over and over again?

Code is in C

code currently ramps the leds up and down only once but fails to reset, I tried using an if statement in my case statement (commented below) but when I uncomment that statement my board only flashes the original LED it was set to.

/* BTNR Shifts LEDS right - BTNL Shifts LEDS left - BTNU ramps the LDs ON (starting with all OFF) in sequence from LD0 to LD7 and then restarts. - BTND ramps the LDs OFF (starting with all ON) in sequence from LD7 to LD0 and then restarts - BTNC pauses/freezes the LED state until another button is pushed =============================================================================*/

#ifndef _SUPPRESS_PLIB_WARNING //turn off MPLAB PLIB warnings #define _SUPPRESS_PLIB_WARNING #endif

#include #include #include //include C standard IO library for sprintf function #include "btn.h" #include "led.h" #include "lcd.h" #include "aic.h"

#pragma config JTAGEN = OFF #pragma config FNOSC = PRIPLL #pragma config FSOSCEN = OFF #pragma config POSCMOD = XT #pragma config FPLLIDIV = DIV_2 #pragma config FPLLMUL = MUL_20 #pragma config FPLLODIV = DIV_1 #pragma config FPBDIV = DIV_8 #define SYS_FREQ (80000000L) #define INT_SEC 1000 #define CORE_TICK_RATE (SYS_FREQ/2/INT_SEC) #define Min_ms 10 //minimum update/shift time in ms #define Max_ms 1000 int msDelay; int valLED; int buttonLock; enum LEDmode { STOP, LEFT, RIGHT, RAMPUP, RAMPDOWN }; int LEDmode = STOP; int main (void) { DDPCONbits.JTAGEN = 0; int valPot; valLED = 0b00000000; LED_Init(); BTN_Init(); LCD_Init(); ADC_Init(); char strMsg[80]; LCD_WriteStringAtPos("Team:19", 0, 0); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); OpenCoreTimer(CORE_TICK_RATE); mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_5 | CT_INT_SUB_PRIOR_0)); INTEnableSystemMultiVectoredInt(); //Enable Timer interrupts int i; while (1) { valPot = ADC_AnalogRead(2); msDelay = (valPot * (Max_ms - Min_ms)) / 1023 + Min_ms; sprintf(strMsg, "ms Delay :%d ", msDelay); LCD_WriteStringAtPos(strMsg, 1, 0); if (BTN_GetValue('C') && !buttonLock) { //If BTNC is pressed stop timer and shifting delay_ms(50); LEDmode = STOP; buttonLock = 1; } else if (BTN_GetValue('L') && !buttonLock) {//If BTNL shift mode to left delay_ms(50); LEDmode = LEFT; valLED= 0x01; LED_SetGroupValue(valLED); buttonLock = 1; } else if (BTN_GetValue('R') && !buttonLock) {//If BTNR shift mode to right for (i = 0; i < 8000; i++) { //delay } valLED= 0x80; LEDmode = RIGHT; LED_SetGroupValue(valLED); buttonLock = 1; } else if (BTN_GetValue('U') && !buttonLock){ delay_ms(50); LEDmode = RAMPUP; valLED = 0x01; buttonLock = 1; } else if (BTN_GetValue('D') && !buttonLock) { delay_ms(50); valLED = 0xFF; LEDmode = RAMPDOWN; buttonLock = 1; } if (buttonLock && !BTN_GetValue('C') && !BTN_GetValue('L') && !BTN_GetValue('R')) { delay_ms(50); buttonLock = 0; } LED_SetGroupValue(valLED); } }

void __ISR(_CORE_TIMER_VECTOR, IPL5SOFT) CoreTimerHandler(void) { mCTClearIntFlag(); // clear the interrupt flag switch (LEDmode) { //Update 8 LEDs and LCD display case LEFT:

if (valLED & 0x80) { // if LD7 is high set LD0 valLED = (valLED << 1) | 0x01; } else valLED = valLED << 1; // shift bits left by 1, could also x2 LED_SetGroupValue(valLED); break; case RIGHT: if (valLED & 0x01) { // if LD0 is high set LD7 valLED = (valLED >> 1) | 0x80; } else valLED = valLED >> 1; // shift bits right by 1, could also /2 LED_SetGroupValue(valLED); break; case STOP: LED_SetGroupValue(valLED); break; case RAMPUP: /* if (valLED = 0xFF){ valLED = 0x01; LED_SetGroupValue(valLED); } else{*/ valLED = (valLED << 1) +1 ; LED_SetGroupValue(valLED); // } break; case RAMPDOWN: /*if (valLED = 0x01){ valLED = 0xFF; LED_SetGroupValue(valLED); } else{*/ valLED = (valLED >> 1) ; LED_SetGroupValue(valLED); // } break; default: break; } LED_SetGroupValue(valLED); UpdateCoreTimer(CORE_TICK_RATE * msDelay); } void delay_ms(int ms) { int i, counter; for (counter = 0; counter < ms; counter++) { for (i = 0; i < 300; i++) { } //software delay ~1 millisec } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions