CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Nested loop problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

Nested loop problem
PostPosted: Mon Feb 04, 2019 10:55 am     Reply with quote

Hi,
I am using compiler 5.015. The circuit uses 3 push buttons for user selections.
After start up, the program enters a for(;;) loop and waits for a button to be pressed.
The centre button calls a function to set the RTC, the right button switches the display backlight on. So far, so good!
The left button calls the doCycle() function, which also has a for(;;) loop in it and here lies the problem. On returning from this function, the buttons no longer behave! Pressing the centre does not call the RTC setting function, but runs the doCycle function again! It appears that the 2nd for(;;) loop is screwing things up. I can find no way to cure this and don't understand why it's behaving as it is.
The code below shows the main.c and switch.c, but I have omitted the ds1302.c and LCD.c functions:
main.c
Code:

#include <18F2550.h>
#device *=16
#device HIGH_INTS=TRUE

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HSPLL                    //Crystal osc <= 4MHz
#FUSES PLL1
#FUSES CPUDIV1
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM is not write protected
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOPBADEN                 //Port B pins are digital on reset
#FUSES NOWRTC                   //configuration registers not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOLPT1OSC                //Timer1 not configured for low-power operation
//#FUSES NOMCLR                 //Master Clear pin disabled (proto only)
#FUSES MCLR                     //for PCB version
#FUSES NOXINST                  //Must be used with CCS compiler

#use delay(clock=48000000)    //CPU clocks at this due to the PLL

//Cooking Timer, February 2019
/*
Designed for backlit 20x4 LCD
*/
#include "k:\PICC\v5l\Projects\CookTimer\CookTimer.h"
#include "k:\PICC\v5l\Projects\CookTimer\DS1302.C"
#include "k:\PICC\v5l\Projects\CookTimer\Flex_LCD420.C"
#include "k:\PICC\v5l\Projects\CookTimer\Switch.C"
#include <math.h>
#include <stdlib.h>

#define BL PIN_A3
#define LED PIN_B3
#define RTCC_PRESET  64750

void set_line_4();
void doCycle();
void setup();
void delay_minute(int16 counter, int1 state);

void get_value(int8 min, int8 max, int8 lcd_cur_x, int8 index);
void get_td(int8 min, int8 max, int8 lcd_cur_x, int8 index, int8 val);

int8 rtc_update_count = 0;
int8 year = 19, mth = 1, day = 1, hr = 0, min = 0, dow, sec;
int1 backlight;
int16 count;
int16 wait_hr, wait_min, cook_hr, cook_min;
int16 wait_minutes, cook_minutes;
char version[6];
//holds waiting & cooking times
int8 data_array[4];
//holds date, time & dow
int8 td_array[6];

char const dayOfWeek[8][4] =
 {
  "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
 }; //days of week, 1 = Sun

char const msg[5][21] =
{
 "START    CLK      BL",
 "DOWN     NEXT     UP",
 "Set the date & time:",
 "NO               YES",
 "Clear    OK         "       
}; //4th line messages
//---------------------------------------------------------------------------
#int_rtcc
void rtcc_isr()
{
 if(rtc_update_count > 9)
  {
   rtc_update_count = 0;
   rtc_get_date(day, mth, year, dow);
   rtc_get_time(hr, min, sec);        //seconds not displayed
  }
 else
  rtc_update_count++;
 
 update_switch_status();   //poll the switches
 set_rtcc(RTCC_PRESET);

}
//---------------------------------------------------------------------------
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED, 0, 1);
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

lcd_init();
rtc_init();

strcpy(version, "1.20"); //set version here

output_bit(BL, true);

clear_interrupt(INT_RTCC);
enable_interrupts(INT_RTCC);
enable_interrupts(global);

//rtc_get_date(day, mth, year, dow);
//rtc_get_time(hr, min, sec);        //seconds not displayed

//Read and save the initial state of the switches.
gc_old_switch_status = read_switches();

set_rtcc(RTCC_PRESET);

output_bit(BL, true);

printf(lcd_putc, "\fCooking Timer\n(c) 2019 B.L. Lonnon\nVersion %S",
        version);
delay_ms(3000);
output_bit(BL, false);
lcd_putc('\f');
lcd_gotoxy(1, 4);
printf(lcd_putc, "%s", msg[0]);

for(;;)
 {
  CBFlag = false;
  //backlight control
 if(gc_rt_button_state == SW_PRESSED && backlight == false)
  {
   count = 0;
   backlight = true;
  }
 if(backlight == true)
  count++;
 if(count > 5000)
  backlight = false;
 output_bit(BL, backlight);
 
 lcd_gotoxy(1, 1);
 printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                   dayOfWeek[dow], day, mth, year, hr, min);
 
  if(gc_lt_button_state == SW_PRESSED)
   doCycle();
     
  if(CBFlag == true)
   {
    CBFlag = false;
    setup();
   }
 }//end for
}//end main
//---------------------------------------------------------------------------
void doCycle()
 {
  set_line_4();
  value = 0;
  lcd_gotoxy(1,2);
  printf(lcd_putc, "Waiting time: ");
  //rtc_get_date(day, mth, year, dow);
  //rtc_get_time(hr, min, sec); 
  lcd_gotoxy(1, 1);
  printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                    dayOfWeek[dow], day, mth, year, hr, min);
 
  //set waiting hr
  get_value(0, 23, 15, 0);
  wait_hr = data_array[0];

  //set waiting minute
  get_value(0, 59, 18, 1);
  wait_min = data_array[1];
 
  printf(lcd_putc, "Cooking time: ");
  //rtc_get_date(day, mth, year, dow);
  //rtc_get_time(hr, min, sec); 
  lcd_gotoxy(1, 1);
  printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                    dayOfWeek[dow], day, mth, year, hr, min);
                   
  //set waiting hr
  get_value(0, 23, 15, 2);
  cook_hr = data_array[2];
 
  //set cooking minute
  get_value(0, 59, 18, 3);
  cook_min = data_array[3];
 
  //clear setting line and line4
  lcd_gotoxy(1,2);
  lcd_putc("                    ");
  lcd_gotoxy(1,3);
  lcd_putc("                    ");
  lcd_gotoxy(1,4);
  lcd_putc(msg[4]);
  lcd_gotoxy(1,2);

  lcd_putc("Press OK to start");

  for(;;)
   {/*
    if(gc_lt_button_state == SW_PRESSED)  //NO
     {
      wait_hr = 0;
      wait_min = 0;
      cook_hr = 0;
      cook_min = 0;
      lcd_putc("\f");
      lcd_gotoxy(1,1);
      printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                      dayOfWeek[dow], day, mth, year, hr, min);
      lcd_gotoxy(1, 4);
      printf(lcd_putc, "%s", msg[0]);
      goto end;
     }
 */
  if(CBFlag == true)
   {
    lcd_putc("\f");
    CBFlag = false;
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                      dayOfWeek[dow], day, mth, year, hr, min);
    lcd_gotoxy(1,3);
    wait_minutes = (60L * wait_hr) + wait_min;
    cook_minutes = (60L * cook_hr) + cook_min;
    output_low(PIN_C7);
    delay_minute(wait_minutes, false);
    output_high(PIN_C7);
    output_high(LED);
    delay_minute(cook_minutes, true);
    output_low(PIN_C7);
    lcd_putc("\f");
    lcd_gotoxy(1, 1);
    //rtc_get_date(day, mth, year, dow);
    //rtc_get_time(hr, min, sec);
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                      dayOfWeek[dow], day, mth, year, hr, min);
    lcd_gotoxy(1,3);
    printf(lcd_putc,"Cooking Finished! %02u", value);
    delay_ms(3000);
    lcd_putc("\f");
    rtc_get_date(day, mth, year, dow);
    rtc_get_time(hr, min, sec);
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                      dayOfWeek[dow], day, mth, year, hr, min);
    lcd_gotoxy(1,4);
    lcd_putc(msg[0]);
    output_low(LED);
  }//end if
 }//endfor
 end:;
}
//---------------------------------------------------------------------------
void set_line_4()
{
lcd_putc('\f');
//DOWN  NEXT  UP
lcd_gotoxy(1, 4);
lcd_putc(msg[1]);
}
//---------------------------------------------------------------------------
//void get_td(int8 min, int8 max, int8 lcd_cur_x, int8 index, int8 val)
void setup()
{
output_bit(BL, true);

lcd_gotoxy(1, 1);
lcd_putc(msg[2]);
lcd_gotoxy(1, 3);
lcd_putc("                    ");
lcd_gotoxy(1, 4);
lcd_putc(msg[1]);

//read RTC stored values
//rtc_get_date(day, mth, year, dow);
//rtc_get_time(hr, min, sec);

lcd_gotoxy(1, 2);
printf(lcd_putc, "%02u/%02u/%02u %02u:%02u (%s)",
                  day, mth, year, hr, min, dayOfWeek[dow]);

//set day
get_td(0, 31, 1, 0, day);

//set mth
get_td(0, 12, 4, 1, mth);

//set year
get_td(0, 50, 7, 2, year);

//set hr
get_td(0, 23, 10, 3, hr);

//set min
get_td(0, 59, 13, 4, min);

//set dow
get_td(1, 7, 16, 5, dow);

day = td_array[0];
mth = td_array[1];
year = td_array[2];
hr = td_array[3];
min = td_array[4];
dow = td_array[5];
rtc_set_datetime(day, mth, year, dow, hr, min);

//rtc_get_date(day, mth, year, dow);
//rtc_get_time(hr, min, sec);

lcd_putc('\f');
lcd_gotoxy(1, 1);
printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                  dayOfWeek[dow], day, mth, year, hr, min);
lcd_gotoxy(1, 4);
lcd_putc(msg[0]);
output_bit(BL, false);
}
//---------------------------------------------------------------------------
void delay_minute(int16 counter, int8 state)
{
 int8 k;
 ldiv_t lidiv;
 //delays for count mimutes
 int16 j;
 lcd_gotoxy(1,3);
 if(state == false)
  lcd_putc("Waiting for ");
 else
  lcd_putc("Cooking for ");
 for (j = counter; j > 0L; j--)
  {
   lidiv=ldiv(j, 60L);
   lcd_gotoxy(13,3);
   printf(lcd_putc, "%02lu:%02lu..", lidiv.quot, lidiv.rem);
   rtc_set_datetime(day, mth, year, dow, hr, min);
   rtc_get_date(day, mth, year, dow);
   rtc_get_time(hr, min, sec);
   lcd_gotoxy(1, 1);
   printf(lcd_putc, "%s %02u/%02u/%02u %02u:%02u",
                     dayOfWeek[dow], day, mth, year, hr, min);
   for(k = 0; k < 60; k++)
    delay_ms(1000);
  }
}
//---------------------------------------------------------------------------
void get_value(int8 min, int8 max, int8 lcd_cur_x, int8 index)
 {
  //set waiting & cooking values
  value = 0;
  do
   {
    //place marker on line 3 to show what's being altered
    lcd_gotoxy(lcd_cur_x, 3);
    lcd_putc("°°");
    if(value > max)
     value = min;
    if(value < min)
     value = max;
    lcd_gotoxy(lcd_cur_x, 2);
    if((index == 0) || (index == 2))
      printf(lcd_putc, "%02u:", value);
     else
       printf(lcd_putc, "%02u", value);
    delay_ms(100);
    if(CBFlag == true)
     break;
   }while(true);
  CBFlag = false;
  //erase marker
  lcd_gotoxy(lcd_cur_x, 3);
  lcd_putc("   ");
  data_array[index] = value;
  value = 0;
 }
 //--------------------------------------------------------------------------
 void get_td(int8 min, int8 max, int8 lcd_cur_x, int8 index, int8 val)
 {
  //set date, time & dow
  value = val;
  do
   {
    //place marker on line 3 to show what's being altered
    lcd_gotoxy(lcd_cur_x, 3);
    lcd_putc("°°");
    if(value > max)
     value = min;
    if(value < min)
     value = max;
    lcd_gotoxy(lcd_cur_x, 2);
   
    //date, so needs / separator
    if(index < 2)
     printf(lcd_putc, "%02u/", value);
     //time, so needs : separator
    if(index == 3)
     printf(lcd_putc, "%02u:", value);
    if((index == 2) || (index == 4))
      printf(lcd_putc, "%02u", value);
    //dow, so needs a string
    if(index == 5)
     printf(lcd_putc, "(%s)", dayOfWeek[value]);
     
    delay_ms(100);
    if(CBFlag == true)
     break;
   }while(true);
  CBFlag = false;
  //erase marker
  lcd_gotoxy(lcd_cur_x, 3);
  lcd_putc("   ");
  td_array[index] = value;
  value = 0;
 }
 //--------------------------------------------------------------------------

switch.c
Code:

#include "K:\Picc\v5l\Projects\CookTimer\Switch.h"
//switch.c -- Debounces the switches.

//Note: Only the Right and Left buttons have auto-repeat code.
#define ENTER_SWITCH_PIN   PIN_B6
#define RT_SWITCH_PIN      PIN_B7
#define LT_SWITCH_PIN      PIN_B5

signed int value; //value from up/down buttons
signed int value_min = 0; //minimum lower limit for value (normally = 0,
//but is -1 for backlight to allow toggling on either button)
boolean CBFlag; //indicates whether centre button is pressed or not
//---------------------------------------------------------------------
//Return the status of the switches. A "1" bit value means the switch is open,
//and a "0" bit means the switch is pressed.
SW_STATE read_switches(void)
{
 SW_STATE switch_values;

 switch_values = 0xFF;   //Init all switch values to "open" (ie not pressed)

 switch_values.rec  = input(ENTER_SWITCH_PIN);
 switch_values.up   = input(RT_SWITCH_PIN);
 switch_values.down = input(LT_SWITCH_PIN);
 return(switch_values);
}
//---------------------------------------------------------------------
//This task handles debouncing, auto-repeat, and the switch event flags.
//This task is called every 10 ms.  The following events are stored in
//a separate global state variable for each button.

//Key Up         (Duration -- 1 or more Ticks)
//Key Pressed    (Duration -- 1 Tick)
//Key Down       (Duration -- 1 or more Ticks)
//Key Held Down  (Duration -- 1 or more Ticks)
//Key Released   (Duration -- 1 Tick)
//Key Up         (Duration -- 1 or more Ticks) (Back to idle state)

//When the program starts, the button state will be initialized to "UP".
void update_switch_status(void)
{
 SW_STATE current_switch_status;
 //Read the switches.
 current_switch_status = read_switches();
 //-------------------------
 //Handle the "ENTER" button.
 //If the switch is closed, then increment a counter.  Based on the
 //value of the counter, set the switch status byte to "Pressed",
 //"Down", or "Held Down".
 if(current_switch_status.rec == SW_CLOSED)
  {
   if(gc_ENTER_button_counter != 0xFF)  //Limit count to 0xFF max
      gc_ENTER_button_counter++;        // because it's a byte variable

   //Set the state, based on value of the down counter.
   //The down counter will be a value from 1 to 0xFF.
   if(gc_ENTER_button_counter == 1)
    {
      gc_ENTER_button_state = SW_PRESSED;
      CBFlag = true; //MB
    }
   else
    if(gc_ENTER_button_counter < UP_DOWN_SW_HELD_DOWN_DELAY)
     gc_ENTER_button_state = SW_DOWN;
   else
    gc_ENTER_button_state = SW_HELD;
 }

else  //If the switch is open:
 {
  if(gc_ENTER_button_counter)  //Has the rec sw. been down previously ?
   gc_ENTER_button_state = SW_RELEASED; //If so, show it is now released
  else
   gc_ENTER_button_state = SW_UP;  //If not, it's been up for at least 2+ ticks

   gc_ENTER_button_counter = 0;  //Always clear down ctr if switch is up.
 }

//Handle the "RIGHT" button.
if(current_switch_status.up == SW_CLOSED)
 {
  if(gc_rt_button_counter != 0xFF)  //Limit count to 0xFF max
   gc_rt_button_counter++;        // because it's a byte variable

  //Set the state, based on value of the down counter.
  //The down counter will be a value from 1 to 0xFF.
  //It's called a "down counter" because it counts the number of 10 ms
  //"ticks" that the button has been held down continuously.
  if(gc_rt_button_counter == 1)
   {
    gc_rt_button_state = SW_PRESSED;
     if(value < 99)
      value++; //LB
   }
  else if(gc_rt_button_counter < UP_DOWN_SW_HELD_DOWN_DELAY)
        gc_rt_button_state = SW_DOWN;
  else
   gc_rt_button_state = SW_HELD;

   //Check if auto-repeat should be enabled.
  if(gc_rt_button_counter == UP_DOWN_SW_AUTO_REPEAT_DELAY)
   {
    gc_rt_button_auto_repeat_flag = TRUE;
    gc_rt_button_auto_repeat_counter = 0;
   }

  //Check if we're in auto-repeat mode. If so, check if it's
  //time to issue a "pressed" state.
  if(gc_rt_button_auto_repeat_flag)
   {
    gc_rt_button_auto_repeat_counter++;

    if(gc_rt_button_auto_repeat_counter == UP_DOWN_SW_AUTO_REPEAT_INTERVAL)
     {
      gc_rt_button_state = SW_PRESSED;
      gc_rt_button_auto_repeat_counter = 0;
      if(value < 99)
       value++;
     }
    }
  }
else  //If the switch is open:
 {
  if(gc_rt_button_counter)
   gc_rt_button_state = SW_RELEASED; //Indicate if it was just released
  else
   gc_rt_button_state = SW_UP;  //Otherwise, it's been up for 2+ ticks

  gc_rt_button_auto_repeat_flag = FALSE;

  gc_rt_button_counter = 0;  //Always clear down ctr if switch is up.
 }

//Handle the "LEFT" button.
if(current_switch_status.down == SW_CLOSED)
 {
  if(gc_lt_button_counter != 0xFF)  //Limit count to 0xFF max
   gc_lt_button_counter++;        // because it's a byte variable

   //Set the state, based on value of the down counter.
   //The down counter will be a value from 1 to 0xFF.
  if(gc_lt_button_counter == 1)
   {
    if(value > value_min)
      value--;
    gc_lt_button_state = SW_PRESSED;
   }
  else if(gc_lt_button_counter < UP_DOWN_SW_HELD_DOWN_DELAY)
   gc_lt_button_state = SW_DOWN;
  else
   gc_lt_button_state = SW_HELD;

   //Check if auto-repeat should be enabled.
  if(gc_lt_button_counter == UP_DOWN_SW_AUTO_REPEAT_DELAY)
   {
    gc_lt_button_auto_repeat_flag = TRUE;
    gc_lt_button_auto_repeat_counter = 0;
   }
   //Check if we're in auto-repeat mode.  If so, check if it's
   //time to issue a "pressed" state.
  if(gc_lt_button_auto_repeat_flag)
   gc_lt_button_auto_repeat_counter++;

   if(gc_lt_button_auto_repeat_counter == UP_DOWN_SW_AUTO_REPEAT_INTERVAL)
    {
     gc_lt_button_state = SW_PRESSED;
     gc_lt_button_auto_repeat_counter = 0;
     if(value > 0)
      value--;
    }
  }
  else  //If the switch is open:
   {
    if(gc_lt_button_counter)
     gc_lt_button_state = SW_RELEASED; //Indicate if it was just released
    else
     gc_lt_button_state = SW_UP;  //Otherwise, it's been up for 2+ ticks

    gc_lt_button_auto_repeat_flag = FALSE;
    gc_lt_button_counter = 0;  //Always clear down ctr if switch is up.
   }

 gc_button_status_updated = TRUE;
 gc_old_switch_status = current_switch_status;
}
//---------------------------------------------------------------------------

switch.h
Code:

//switch.h
//These are the delay periods (in 10 ms "ticks") for switch events.
//These values are used in the check_switches() function.
#define UP_DOWN_SW_AUTO_REPEAT_DELAY    25   //25 x 10ms = 250 ms
#define UP_DOWN_SW_AUTO_REPEAT_INTERVAL 10   //10 x 10ms = 100 ms = 10/sec

//These two lines specify the amount of time (in units of 10ms ticks)
//that a switch must be continuously held down, in order to be
//detected as "held down".
#define UP_DOWN_SW_HELD_DOWN_DELAY     100  // 200 x 10ms = 2 sec

//Logic levels for the switches.
#define SW_CLOSED 0
#define SW_OPEN   1

//The switch states are ordered 1 to 5 because we will clear the
//state variable to 0 in the user code to show that it has been read.
enum sw_states {SW_UP=1, SW_PRESSED, SW_DOWN, SW_HELD, SW_RELEASED};
//               1        2           3        4        5
//---------------------------------------------------------------------------
//This structure holds the state of the switches after they are read
//from the i/o port.
typedef struct
{
int1 rec;
int1 up;
int1 down;
}SW_STATE;

//These enum values provide names for the indices into the array of
//gat_sw_info structures, below, instead of just using 0, 1, 2, 3, 4, 5.
enum switches {FREQ_ENTER_SW, FREQ_UP_SW, FREQ_DOWN_SW};

struct
{
int8 state;                //This is UP, PRESSED, DOWN, HELD, or RELEASED
int8 counter;              //Number of ticks that the switch has been held down.
int8 auto_repeat_counter;  //When in auto-repeat, this counts off the interval.
int8 auto_repeat_flag;
}gat_sw_info[] =
{
 {0, 0, 0, 0},  //FREQ_ENTER_SW
 {0, 0, 0, 0},  //FREQ_UP_SW
 {0, 0, 0, 0}   //FREQ_DOWN_SW
};

//This variable is used in the check_switches() function.
SW_STATE gc_old_switch_status = {SW_OPEN, SW_OPEN, SW_OPEN};

//Use macros to create easy-to-read names for the switch events.
#define gc_ENTER_button_counter  gat_sw_info[FREQ_ENTER_SW].counter
#define gc_ENTER_button_state    (gat_sw_info[FREQ_ENTER_SW].state)
#define gc_ENTER_button_auto_repeat_counter (gat_sw_info[FREQ_ENTER_SW].auto_repeat_counter)
#define gc_ENTER_button_auto_repeat_flag (gat_sw_info[FREQ_ENTER_SW].auto_repeat_flag)

#define gc_rt_button_counter   gat_sw_info[FREQ_UP_SW].counter
#define gc_rt_button_state     (gat_sw_info[FREQ_UP_SW].state)
#define gc_rt_button_auto_repeat_counter (gat_sw_info[FREQ_UP_SW].auto_repeat_counter)
#define gc_rt_button_auto_repeat_flag (gat_sw_info[FREQ_UP_SW].auto_repeat_flag)

#define gc_lt_button_counter gat_sw_info[FREQ_DOWN_SW].counter
#define gc_lt_button_state   (gat_sw_info[FREQ_DOWN_SW].state)
#define gc_lt_button_auto_repeat_counter (gat_sw_info[FREQ_DOWN_SW].auto_repeat_counter)
#define gc_lt_button_auto_repeat_flag (gat_sw_info[FREQ_DOWN_SW].auto_repeat_flag)

//Short named versions of the button state variables
#define ENTER_button    (gat_sw_info[FREQ_ENTER_SW].state)
#define rt_button     (gat_sw_info[FREQ_UP_SW].state)
#define lt_button   (gat_sw_info[FREQ_DOWN_SW].state)

#define all_buttons_up ((up_button == SW_UP) && (down_button == SW_UP) && (ENTER_button == SW_UP))
#define any_button_pressed ((up_button == SW_PRESSED) || (down_button == SW_PRESSED) || (ENTER_button == SW_PRESSED))

int8 gc_button_status_updated = false;

SW_STATE read_switches(void);
//---------------------------------------------------------------------------

Any help would be much appreciated

Thanks

Brian
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Feb 04, 2019 12:44 pm     Reply with quote

Get rid of your goto.
Goto can cause problems, since it does not ensure the stack is
correctly balanced. May well be your problem.
Simply 'break' from the loop.
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Mon Feb 04, 2019 2:09 pm     Reply with quote

Hi Ttelmah

I removed the goto, only there in desperation, but still the problem persists!

Brian
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Feb 04, 2019 3:00 pm     Reply with quote

and what clears the button state?.
Nothing.

It is still going to be 'SW_PRESSED' when you exit the loop....
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Mon Feb 04, 2019 4:10 pm     Reply with quote

AH...
I better find out out how to clear the button states.

Thanks

Brian
BLL



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

PostPosted: Mon Feb 04, 2019 4:18 pm     Reply with quote

I used the read_switches() function to clear the buttons, but it makes not one jot of difference!

Brian
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Feb 05, 2019 12:08 pm     Reply with quote

Simplify, simplify, simplify.

You say your problem is with the three switches and their three functions, one of which includes a for(;;) loop.

Reduce your program to the minimum which includes all of the above and nothing else.
I.e. three buttons and three LEDS (one for each function).
Centre and right-hand button toggle their LED, the for(;;) loop flashes its LED several times when you press left hand button.

Get rid of everything which is not needed, UART, RTC LCD etc. That way you can more easily see what's going on.
I've got no intention of wading through pages of code to get to the bottom of this one.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group