Author Topic: XC8 - how can I pass bits to and from a function  (Read 10883 times)

0 Members and 1 Guest are viewing this topic.

Offline DeltaTopic starter

  • Super Contributor
  • ***
  • Posts: 1221
  • Country: gb
Re: XC8 - how can I pass bits to and from a function
« Reply #25 on: November 10, 2015, 07:06:35 pm »
Just to add, I am fully aware that the code I am writing is going to be shite, however at this stage into my foray with C, I'm deliberately writing code that I can easily follow and understand.  I don't mean to sound rude or react badly to criticism, but I'm just trying to keep it simple for now.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: XC8 - how can I pass bits to and from a function
« Reply #26 on: November 10, 2015, 07:49:08 pm »
Just to add, I am fully aware that the code I am writing is going to be shite, however at this stage into my foray with C, I'm deliberately writing code that I can easily follow and understand.  I don't mean to sound rude or react badly to criticism, but I'm just trying to keep it simple for now.
No problem.  Exploring simple code and eventually more complex code and the far corners of the ANSI C language and specific compiler implementation is how you learn.  If you try to jump straight to the top of the learning curve, you'll be likely to end up as a cargo cult coder!  :(

However, I'd do something like this:
Caution: Air code warning! ;) I've just thrown this together on the fly so I can freely disclose it. It should compile OK as part of a full program, unless I've made a typo.
Code: [Select]
// From Jack Ganssle's Example 3 debouncing algorithm
// See page 2 of: http://www.ganssle.com/debouncing.htm
// with actual debouncing extracted to main program for efficiency

#define MAX_CHECKS 10 // # checks before a switch is debounced
uint8_t Debounced_State=0,Old_Debounced_State; // Current and previous debounced
                                               // state of the switches.
volatile uint8_t State[MAX_CHECKS]; // Array that maintains bounce status

// Service routine called by a timer interrupt
void DebounceSwitch()
   {
       static uint8_t Index; // Persistent pointer into State
       
       State[Index++]=~PORTA; // All switches active low on Port A.
                              // Leave out ~ for active high

       if(Index>=MAX_CHECKS) Index=0;
   }

// Routine to debounce all keys
void DebounceSwitches()
   {
       uint8_t i,j;

       Old_Debounced_State=Debounced_State;
       j=0xff;
       for(i=0; i<MAX_CHECKS;i++)
          j=j & State[i];
       
       Debounced_State= j;
   }

// Macro to read Switch N where N is the bit number. Returns TRUE if pressed.
#define ReadSwitch(n) (!!(Debounced_State & 1<<n))

// Macro to read if Switch N has just been pressed.  Returns True if newly pressed.
#define ReadNewSwitch(n) (!!((Debounced_State^Old_Debounced_State) & Debounced_State & 1<<n))


//Usage:
#if 0  //Don't compile sample usage, its only a snippet!

   DebounceSwitches();  'freshen' the debounceed state, call this ONCE in your current polling loop.

   if(ReadSwitch(0))  // used as a shift key so want pressed state, not newly pressed state
      {
         if(ReadNewSwitch(1)) { /* Shifted Key1 routine */ }
         if(ReadNewSwitch(2)) { /* Shifted Key2 routine */ }
      } else {
         if(ReadNewSwitch(1)) { /* Key1 routine */ }
         if(ReadNewSwitch(2)) { /* Key2 routine */ }
      }

#endif
It can easily be extended for 16 switches and it isn't too tricky to extend it to scan and debounce a 16 key 4x4 matrix. 

--
I'm no C guru. If I ever look like one its because I'm  standing on the shoulder of giants.
« Last Edit: November 10, 2015, 08:05:31 pm by Ian.M »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf