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 CCS Technical Support

debouncing missing out on presses!
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sun Jun 09, 2019 10:06 am     Reply with quote

Ttelmah wrote:
The issues here were with a very early V5 compiler, that was still really a
'beta' at best.


Hmm... There must be something else at play for me then. I was experiencing it in 5.082. I still haven't gone to investigate that yet.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Sun Jun 09, 2019 10:20 am     Reply with quote

Do you possibly have them in structures?.
There is an 'insidious' problem sometimes with int1's used in structures.
Seems not to happen if you use bit fields instead. So in a structure
declaration:
Code:

//instead of
struct something {
    int1 something;
}

//use
struct something {
   int8 something:1;
}
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Sun Jun 09, 2019 10:25 am     Reply with quote

Ttelmah wrote:
Do you possibly have them in structures?.
There is an 'insidious' problem sometimes with int1's used in structures.
Seems not to happen if you use bit fields instead. So in a structure
declaration:
Code:

//instead of
struct something {
    int1 something;
}

//use
struct something {
   int8 something:1;
}


I may have... I don't actually remember which ones of my flags initially caused the problem. I just know that I switched them all to int8 for my current project. I will try the tip with bit fields when I have some time.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jun 09, 2019 3:51 pm     Reply with quote

I recall a 'quirky problem that dealt with bits (int1s) and the 'cure' seemed to be to have 8 int1 even though you only needed, say 5. I assuming this forced the compiler to use a whole byte and data was 'aligned'.
While this was probably fixed, I've always used the 'full' byte method.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Jun 10, 2019 1:49 am     Reply with quote

This may well relate.
Using bit fields, the bits are generated as bits in a byte. Hence the 'int8'
at the start of the declaration. If you don't use all the bits, a byte as an
entity is still allocated. Now, while you perhaps would expect the int1
declaration to do the same, if it doesn't, and the 'optimiser' tries then
to save space by using 'spare' bits elsewhere, I can easily see errors
occurring (particularly perhaps with bank switching when these are accessed).
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

Interrupts disabled during call to prevent re-entrancy
PostPosted: Mon Jun 10, 2019 1:45 pm     Reply with quote

The Problem:

A code redo for array based compiles with the two warnings below with a halt on the timer0 interrupt init on actual test! Crying or Very sad

Code:
>>> Warning 216 "main.c" Line 139(1,2): Interrupts disabled during call to prevent re-entrancy:  (@READBITA)
>>> Warning 216 "main.c" Line 139(1,2): Interrupts disabled during call to prevent re-entrancy:  (@WRITEBITA)


The Success:

The non-array version posted earlier compiles clean and works fine.

The Notes:

I searched and read many posts here on the subject issue, and that the CCS C compiler does not support this kind of thing, and sort of understood that they were suggesting to change the entire code routine and structure.

Below is the code here for a review and request for valuable guidances.

Code:

/*
   // ************************************************ Array version ************************************************ //
   
   Project:    Demo Controller Board - 8 Channels Relay with status LEDs and push buttons for an ON/OFF toggle
   Compiler:   CCS-C-PIC - PCWHD Version 5.076
   Author:     MassaM, June 2019
   Chip:       PIC16F877A@20Mhz
   Copyright:  No copy-rights nor copy-wrongs! :)
   Request:    Make it better and post it back on this thread, so we all learn please!
*/

#include <16F877A.h>

#fuses XT, BROWNOUT, PUT, NOWDT
#use delay(clock=20M)
#use rs232(stream=SERIAL, baud=19200, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)  // settings for Serial/Terminal outputs

#define FOSC getenv("CLOCK")  // Get PIC oscillator frequency
#define TIMER0_PRELOAD (256 - (FOSC/4/256/100))

/* ****************************************************************************************************************** */
// Array version variables
int16 switches[] = {PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7};
int16 leds[] = {PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6,PIN_D7};
int8 switch_is_down[] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
/* ****************************************************************************************************************** */

/* ****************************************************************************************************************** */
// Array version functions
/* ****************************************************************************************************************** */

void clearSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
      //printf("[Cleared]:\r\n");
      //printf("Switch No: %d\r\n", switch_is_down[i]);
   }   
}

void pollSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      if(switch_is_down[i] == TRUE)
      {
         switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
         output_toggle(leds[i]);
         //printf("[Polled] switch No: %d\r\n", switch_is_down[i]);
      }
   }   
}

void checkStates_Array()
{
   int8 active_state[], previous_state[];
   
   int8 i;
   
   for(i=0; i < 8; i++)
   {
      active_state[i] = input(switches[i]);  // Read the button

      if((previous_state[i] == 1) && (active_state[i] == 0))
      {
         switch_is_down[i] = TRUE;   
      }
     
      previous_state[i] = active_state[i];  // Save current value for next time
     
      //printf("[Timer0] switch No: %d, previous_state %d, active_state %d\r\n", i, previous_state[i], active_state[i]);

   }
}

void timer0_init(void)
{
   setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
   set_timer0(TIMER0_PRELOAD);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);   
   
   //printf("[initAll]:\r\n");     // It hangs here with the array version
   //printf("T0_INTERNAL: %d\r\nT0_DIV_256: %d\r\nT0_8_BIT: %d\r\n", T0_INTERNAL , T0_DIV_256 , T0_8_BIT);
   //printf("set_timer0: %d\r\nenable_interrupts: %d\r\nGLOBAL: %d\r\n", TIMER0_PRELOAD ,INT_TIMER0, GLOBAL);
   
}

#int_timer0
void timer0_isr(void)
{   
   set_rtcc(TIMER0_PRELOAD); // Reload Timer0 for 10ms rate
   
   //printf("\r\n[timer0_isr] (%d) Waiting...\r\n", get_rtcc());
   
   checkStates_Array();       // This not working aka hangs

}

void initAll(){
   
   timer0_init();
   
   clearSwitches_Array();

}

void main()
{

   initAll();

   printf("-------------------------------------------------\r\n");
   printf("Ready!\r\n");
     
   while(TRUE)
   {
   
/*

Notes:

-> Array version compiles with the two warnings below with a halt on the timer0 interrupt init on actual test! :(
-> Non Array version posted earlier compiles clean and works fine.

>>> Warning 216 "main.c" Line 139(1,2): Interrupts disabled during call to prevent re-entrancy:  (@READBITA)
>>> Warning 216 "main.c" Line 139(1,2): Interrupts disabled during call to prevent re-entrancy:  (@WRITEBITA)

*/
   
   pollSwitches_Array();            // <- Warnings and notes as above

   } // while true
   
} // main


Appreciated.

MassaM
_________________
while(!dead)
{
keepLearning();
}
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 10, 2019 1:57 pm     Reply with quote

Are you trying to use printf's to debug within your interrupts?

They are very slow and will mess with you. It is much better to set a flag in the ISR, and print in your main loop instead. A good rule for ISRs are to get in, set/check flags or increment counters, and then get out as soon as possible.

Also, in checkStates_Array():
You are using a int8 array to loop through store your switch states, when in fact you could store all of the values within a single int8 using the function input_b(). That will capture the value of all the pins on that port and store them in a single variable so you don't need to loop through.

Then you can do a bitwise comparison of previous and current states.

Another thing is that you are declaring previous_state[] inside the function, but you have not assigned anything to it. Therefore, previous_state's values can be just about anything by the next time you run checkStates_array(). You should either make that a global variable or static variable so its lifetime lasts beyond the function. Otherwise once the function returns, something else can use that memory area and you will get unexpected results.
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 10, 2019 6:03 pm     Reply with quote

dluu13 wrote:
Are you trying to use printf's to debug within your interrupts?


I did in the first, but then noticed so they are commented out when compiled and tested.

dluu13 wrote:

They are very slow and will mess with you. It is much better to set a flag in the ISR, and print in your main loop instead. A good rule for ISRs are to get in, set/check flags or increment counters, and then get out as soon as possible.


Noted and agree.

dluu13 wrote:
Also, in checkStates_Array():
You are using a int8 array to loop through store your switch states, when in fact you could store all of the values within a single int8 using the function input_b(). That will capture the value of all the pins on that port and store them in a single variable so you don't need to loop through.

Then you can do a bitwise comparison of previous and current states.


Thanks for the great input. I will have to study this further and guess I dived into a deep project heads first! Very Happy

dluu13 wrote:

Another thing is that you are declaring previous_state[] inside the function, but you have not assigned anything to it. Therefore, previous_state's values can be just about anything by the next time you run checkStates_array(). You should either make that a global variable or static variable so its lifetime lasts beyond the function. Otherwise once the function returns, something else can use that memory area and you will get unexpected results.


Great point! Idea
So, basically it needs to sit back, look from afar, recode and test and hopefully I get to sort this out and post the full working code here for all.

All tips and input so far are very much appreciated.

Cheers all Smile
_________________
while(!dead)
{
keepLearning();
}
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

Should we call it done? Or it can be better?
PostPosted: Mon Jun 10, 2019 8:45 pm     Reply with quote

Ok! Very Happy

Sorry, stupid question follows alert: Rolling Eyes

>>> Warning 216 "main.c" Line 148(1,2): Interrupts disabled during call to prevent re-entrancy: (@READBITA)
>>> Warning 216 "main.c" Line 148(1,2): Interrupts disabled during call to prevent re-entrancy: (@WRITEBITA)


If all working smooth and nice, but have compiler warnings as above - can we ignore safely or is there a fix?

Finally, here is the final working code:

Code:

/*
   // ************************************************ Array version ************************************************ //
   
   Project:    Demo Controller Board - 8 Channels Relay with status LEDs and push buttons for an ON/OFF toggle
   Compiler:   CCS-C-PIC - PCWHD Version 5.076
   Author:     MassaM, June 2019
   Chip:       PIC16F877A@20Mhz
   Copyright:  No copy-rights nor copy-wrongs! :)
   Request:    Make it better and post it back on this thread, so we all learn please!
*/

#include <16F877A.h>

#fuses XT, BROWNOUT, PUT, NOWDT
#use delay(clock=20M)
#use rs232(stream=SERIAL, baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)  // settings for Serial/Terminal outputs

#define FOSC getenv("CLOCK")  // Get PIC oscillator frequency
#define TIMER0_PRELOAD (256 - (FOSC/4/256/100))

/* ****************************************************************************************************************** */
// Array version variables
int16 switches[] = {PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7};
int16 leds[] = {PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6,PIN_D7};
int8 switch_is_down[] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
int8 active_state[] = {0,0,0,0,0,0,0,0};
int8 previous_state[] = {0,0,0,0,0,0,0,0};
int8 portb_states;

/* ****************************************************************************************************************** */

/* ****************************************************************************************************************** */
// Array version functions
/* ****************************************************************************************************************** */

void display_binary(char c) // Used for printing binary out to terminal as debugging (Credit to: PCM Programmer)
{
   char i;
   
   putc('0');
   putc('b');
   
   for(i = 0; i < 8; i++)
      {
       if(c & 0x80)
          putc('1');
       else
          putc('0');   
   
       c <<= 1;
      }
}

void clearSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
   }
}

void pollSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      if(switch_is_down[i] == TRUE)
      {
         switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
         output_toggle(leds[i]);
      }
   }   
}

void checkStates_Array()

   int8 i;
   
   for(i=0; i < 8; i++)
   {
      active_state[i] = input(switches[i]);  // Read the button

      if((previous_state[i] == 1) && (active_state[i] == 0))
      {
         switch_is_down[i] = TRUE;
      }
     
      previous_state[i] = active_state[i];  // Save current value for next time

   }
   
}

void timer0_init(void)
{
   setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
   set_timer0(TIMER0_PRELOAD);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);     
}

#int_timer0
void timer0_isr(void)
{   
   set_rtcc(TIMER0_PRELOAD); // Reload Timer0 for 10ms rate
   
   checkStates_Array();       // All working now! :)

}

void initAll(){
   
   timer0_init();
   
   clearSwitches_Array();
   
   portb_states = input_b();                 // read the state of all switches as full port
//   printf("Initial PORT B States\r\n");      // print the initial port state
//   display_binary(portb_states);             // print the port state to terminal in binary
//   printf("\r\n---------------------\r\n");  // print separation lines

}

void main()
{

   initAll();
     
   while(TRUE)
   {
   
      /*
         >>> Warning 216 "main.c" Line 148(1,2): Interrupts disabled during call to prevent re-entrancy:  (@READBITA)
         >>> Warning 216 "main.c" Line 148(1,2): Interrupts disabled during call to prevent re-entrancy:  (@WRITEBITA)
      */

      pollSwitches_Array();                     // <- Compiler warnings as above - Should we ignore! or is there a fix?
     
      portb_states = input_b();                 // read the state of all switches as full port
//      printf("Updated PORT B States\r\n");      // print the updated port state
//      display_binary(portb_states);             // print the port state to terminal in binary
//      printf("\r\n---------------------\r\n");  // print separation lines

   } // while true
   
} // main


Thanks to all.

Cheers
_________________
while(!dead)
{
keepLearning();
}
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Jun 11, 2019 7:04 am     Reply with quote

The warning is just letting you know that there is some function that you have used inside your ISR that you are also using elsewhere in your program. During those function calls, the ISR has been disabled.

It looks as if it is the input() functions that probably are causing this.
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

Release day! :)
PostPosted: Tue Jun 11, 2019 10:09 am     Reply with quote

dluu13 wrote:
The warning is just letting you know that there is some function that you have used inside your ISR that you are also using elsewhere in your program. During those function calls, the ISR has been disabled.

It looks as if it is the input() functions that probably are causing this.


Idea That tip up there my dear sir, was the releaser! Idea

Already then! All Working, smooth and awesome with credits mentioned! Cool

Here you go:

Code:
*
   // ************************************************ Array version ************************************************ //
   
   Project:    Demo Controller Board - 8 Channels Relay with status LEDs and push buttons for an ON/OFF toggle
   Compiler:   CCS-C-PIC - PCWHD Version 5.076
   Author:     MassaM, June 2019
   Chip:       PIC16F877A@20Mhz
   Copyright:  No copy-rights nor copy-wrongs! :)
   Request:    Make it better and post it back on this thread, so we all learn please! And always give credits where due, as what goes around, comes around! ;)
   Forum Link: http://www.ccsinfo.com/forum/viewtopic.php?t=58066
   Credits:    Ttelmah -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=14537&sid=42ae004153892ee2adcd40879d100ea4
               temtronic -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=15187
               dluu13 -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=24556&sid=42ae004153892ee2adcd40879d100ea4
               PCM programmer -> http://www.ccsinfo.com/forum/profile.php?mode=viewprofile&u=4
               
*/

#include <16F877A.h>

#fuses XT, BROWNOUT, PUT, NOWDT
#use delay(clock=20M)
#use rs232(stream=SERIAL, baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)  // settings for Serial/Terminal outputs

#define FOSC getenv("CLOCK")  // Get PIC oscillator frequency
#define TIMER0_PRELOAD (256 - (FOSC/4/256/100))

/* ****************************************************************************************************************** */
// Array version variables
int16 switches[] = {PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7};
int16 leds[] = {PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6,PIN_D7};
int8 switch_is_down[] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
int8 active_state[] = {0,0,0,0,0,0,0,0};
int8 previous_state[] = {0,0,0,0,0,0,0,0};
int8 portb_states;

/* ****************************************************************************************************************** */

/* ****************************************************************************************************************** */
// Array version functions
/* ****************************************************************************************************************** */

void display_binary(char c) // Used for printing binary out to terminal as debugging (Credit to: PCM Programmer)
{
   char i;
   
   putc('0');
   putc('b');
   
   for(i = 0; i < 8; i++)
      {
       if(c & 0x80)
          putc('1');
       else
          putc('0');   
   
       c <<= 1;
      }
}

void clearSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
   }
}


void pollSwitches_Array()
{
   int8 i;
   for(i=0; i < 8; i++)
   {
      if(switch_is_down[i] == TRUE)
      {
         switch_is_down[i] = FALSE;  // Clear the switch is pushed flag
         output_toggle(leds[i]);
      }
   }   
}


void checkStates_Array()

   int8 i;
   for(i=0; i < 8; i++)
   {
      active_state[i] = input(switches[i]);  // Read the button

      if((previous_state[i] == 1) && (active_state[i] == 0))
      {
         switch_is_down[i] = TRUE;
      }
     
      previous_state[i] = active_state[i];  // Save current value for next time

      pollSwitches_Array();                 // <- Moved this function call from the main's while(TRUE) to here and THE Compiler is HAPPY NOW! :)
                                            // Credit: dluu13 -> For the awesome clue! Thanks! :)
   }
   
}

void timer0_init(void)
{
   setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
   set_timer0(TIMER0_PRELOAD);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);     
}

#int_timer0
void timer0_isr(void)
{   
   set_rtcc(TIMER0_PRELOAD);  // Reload Timer0 for 10ms rate
   
   checkStates_Array();       // All working now! :)

}

void initAll(){
   
   timer0_init();
   
   clearSwitches_Array();
   
   portb_states = input_b();                 // read the state of all switches as full port

   // Uncomment the block below for port B states debug to terminal output
   /*
   printf("Initial PORT B States\r\n");      // print the initial port state
   display_binary(portb_states);             // print the port state to terminal in binary
   printf("\r\n---------------------\r\n");  // print separation lines
   */
}

void main()
{

   initAll();
     
   while(TRUE)
   { 
      portb_states = input_b();                 // read the state of all switches as full port

    // Uncomment the block below for port B states debug to terminal output
    /*
      printf("Updated PORT B States\r\n");      // print the updated port state
      display_binary(portb_states);             // print the port state to terminal in binary
      printf("\r\n---------------------\r\n");  // print separation lines
   */

   } // while true
   
} // main




Next project coming soon and hopefully with uglier bugs! Twisted Evil

Cheers!

MassaM
_________________
while(!dead)
{
keepLearning();
}
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Jun 11, 2019 11:16 am     Reply with quote

Glad you got it working.

One thing I would try and do is to instead of looping through an array, just use a single int8 as a representation of the entire portb. You can copy that byte value to output to portd for your LEDs.

Current state and previous state variables can instead of being 8 bytes long, be only one byte long, and so on.

so instead of that loop, you can do something like:

Code:
uint8_t active_state;
uint8_t prev_state;
uint8_t switch_is_down;
uint8_t leds;

void checkStates() // this replaces checkStates_Array()
{
   active_state = input_b();
   switch_is_down = prev_state & ~active_state;
   prev_state = active_state;

   // the following three lines replace pollSwitches_Array()
   leds ^= switch_is_down;
   output_d(leds);
   switch_is_down = 0;
}


I have not tested this on a chip, but this is the idea, to use less memory, and take advantage of being able to capture a whole port in one variable. You also eliminate the loops, and work with a byte at a time. Compile both, and compare .lst file generated by the compiler and look at how much more compact it can be.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Tue Jun 11, 2019 1:20 pm     Reply with quote

What you have posted doesn't debounce. Just detects the key going down:
Code:

int8 pressed;
#byte PORTB=getenv("SFR:PORTB")

#INT TIMER1
void scankeys(void)
{
    static int8 old_keys=0xFF; //for eight keys
    int8 current_keys;
    current_keys=PORTB; //read the keys without changing TRIS
    pressed = (current_keys | old_keys) ^ 9xFF;
    old_keys=current_keys;
}

The bits in 'pressed' will go true when the key has been pressed for
two successive interrupts.
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Tue Jun 11, 2019 4:32 pm     Reply with quote

dluu13 wrote:
Glad you got it working.


Thanks to your clever input.

dluu13 wrote:

One thing I would try and do is to instead of looping through an array, just use a single int8 as a representation of the entire portb. You can copy that byte value to output to portd for your LEDs.

Current state and previous state variables can instead of being 8 bytes long, be only one byte long, and so on.


I did put your code into action and it indeed works faster, but, in case I need to have a per switch state to have it further used in LCD display or a 4 digits 7Segments visual feedback and EEPROM stuff at later stages in this project, this wouldn't provide the future features data, perhaps?

Quote:

I have not tested this on a chip, but this is the idea, to use less memory, and take advantage of being able to capture a whole port in one variable. You also eliminate the loops, and work with a byte at a time. Compile both, and compare .lst file generated by the compiler and look at how much more compact it can be.


As you suggested, did the two versions comparison and below are the results:

My array version
Code:

ROM used:   384 words (5%)
Largest free fragment is 2048
RAM used:   69 (19%) at main() level
81 (22%) worst case
Stack used: 1 locations (0 in main + 1 for interrupts)
Stack size: 8


Your single int8 version
Code:

ROM used:   123 words (2%)
Largest free fragment is 2048
RAM used:   17 (5%) at main() level
17 (5%) worst case
Stack used: 0 locations
Stack size: 7


What I am trying is to have a timer based interrupt with polling state machine, if that makes sense! Smile

Also trying to add a counting routine where a multi function switch will have dual duty with short and long press/hold feature.
_________________
while(!dead)
{
keepLearning();
}
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

PostPosted: Sun Jun 16, 2019 9:11 am     Reply with quote

dluu13 wrote:

so instead of that loop, you can do something like:

Code:
uint8_t active_state;
uint8_t prev_state;
uint8_t switch_is_down;
uint8_t leds;

void checkStates() // this replaces checkStates_Array()
{
   active_state = input_b();
   switch_is_down = prev_state & ~active_state;
   prev_state = active_state;

   // the following three lines replace pollSwitches_Array()
   leds ^= switch_is_down;
   output_d(leds);
   switch_is_down = 0;
}



Arrow Is there a way to introduce debouncing with your approach?

Using your code and after adding the eeprom save/load functions, and a global state to select
between 3 operating modes, I am facing the debouncing issue on the mode select button.

Mode Select given below is polling the while A0 low state and at the end of the
function you suggested.


Code:


   if(!input(PIN_A0))
   {
     
      do // Toggle active mode between manual select OR memory address OR full override
      { 
         
         if (global_active_mode == MANUAL_MODE_ACTIVE)
         {
            global_active_mode = MEMORY_MODE_ACTIVE;
         }
         else if(global_active_mode == MEMORY_MODE_ACTIVE)
         {
            global_active_mode = MANUAL_MODE_ACTIVE;
         }
         /*
         else // <---- Override check to be done based on a counter for long press
         {
            if(long_press)
               {
                   global_active_mode = OVERRIDE_MODE_ACTIVE;
               }
         }
       */

     

      }while(!input(PIN_A0));
   }



Also that the 3rd mode as OVERRIDE_MODE_ACTIVE is going to be a counter based checking for duration od press which will need to be debounced as well, as per the commented lines for that mode in the code above.

Thanks in advance.
_________________
while(!dead)
{
keepLearning();
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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