Hi guys
I think it may be a long short to get an answer without the hardware for this, but I am stuck and want to try my luck here. So I basically want to write a function to drive and read a digital IO (the IO need to switch between input and output). And I need to do that to a lot of IOs. So I wrote an function to do that so I don't need to copy and paste the code many many times.
The thing is it works if I duplicate the code manually, but it doesn't work with my function.
So here is the codes:
codes that works:
my function that doesn't work:
I think it may be a long short to get an answer without the hardware for this, but I am stuck and want to try my luck here. So I basically want to write a function to drive and read a digital IO (the IO need to switch between input and output). And I need to do that to a lot of IOs. So I wrote an function to do that so I don't need to copy and paste the code many many times.
The thing is it works if I duplicate the code manually, but it doesn't work with my function.
So here is the codes:
Code:
typedef struct
{
// b type input 1 - 6
uint8_t set1 : 1;
uint8_t reset1 : 1;
uint8_t set2 : 1;
uint8_t reset2 : 1;
uint8_t set3 : 1;
uint8_t reset3 : 1;
uint8_t set4 : 1;
uint8_t reset4 : 1;
uint8_t set5 : 1;
uint8_t reset5 : 1;
uint8_t set6 : 1;
uint8_t reset6 : 1;
// VoIP calling status
uint8_t connecting : 1;
uint8_t connected : 1;
// intercom control
uint8_t intercomEnable : 1;
}INPUTS;
Code:
run_every_1ms()
{
static uint8_t states = 0;
static uint8_t db1 = 0;
static uint8_t db2 = 0;
states++;
// driving and reading first IO
if (states == 1)
{
/* start reading button state*/
TRISCbits.TRISC5 = 1; // input
__delay_us(1);
if (!PORTCbits.RC5)
{
if (db1 < INPUT_BTN_DEBOUNCE_DELAY)
db1++;
else
inputs.set6 = 1;
}
else
{
if (db1 != 0)
db1--;
else
inputs.set6 = 0;
}
/* drive buttons LEDs */
TRISCbits.TRISC5 = 0; // output
LATCbits.LATC5 = 1; // high, LED off
}
else if (states == STATE_NUM)
{
/* drive buttons LEDs */
LATCbits.LATC5 = 0; // low, LED on
}
// driving and reading 2nd IO
if (states == 1)
{
/* start reading button state*/
TRISCbits.TRISC4 = 1; // input
__delay_us(1);
if (!PORTCbits.RC4)
{
if (db2 < INPUT_BTN_DEBOUNCE_DELAY)
db2++;
else
inputs.reset6 = 1;
}
else
{
if (db2 != 0)
db2--;
else
inputs.reset6 = 0;
}
/* drive buttons LEDs */
TRISCbits.TRISC4 = 0; // output
LATCbits.LATC4 = 1; // high, LED off
}
else if (states == STATE_NUM)
{
/* drive buttons LEDs */
LATCbits.LATC4 = 0; // low, LED on
}
// drive and read another io....
// drive and read another io....
// drive and read another io....
if (states == STATE_NUM) states = 0; // reset states
}
Code:
run_every_1ms()
{
static uint8_t states = 0;
static uint8_t db1 = 0;
static uint8_t db2 = 0;
states++;
inputs.set6 = buttonScan(&SET6_TRIS, &SET6_PORT, &SET6_LAT, SET6_PIN, &db1, states);
inputs.reset6 = buttonScan(&RESET6_TRIS, &RESET6_PORT, &RESET6_LAT, RESET6_PIN, &db2, states);
// drive and read another io....
// drive and read another io....
if (states == STATE_NUM) states = 0;
}
Code:
uint8_t buttonScan(volatile unsigned char *TRIS,
volatile unsigned char *PORT,
volatile unsigned char *LAT,
uint8_t pinNum,
uint8_t *db,
uint8_t states)
{
uint8_t result;
if (states == 1)
{
/* start reading button state*/
SET_BIT(*TRIS, pinNum); // input
_delay(10);
if (!CHECK_BIT(*PORT, pinNum))
{
if ((*db) < INPUT_BTN_DEBOUNCE_DELAY)
(*db)++;
else
result = 1;
}
else
{
if ((*db) != 0)
(*db)--;
else
result = 0;
}
/* drive buttons LEDs */
CLR_BIT(*TRIS, pinNum); // output
SET_BIT(*LAT, pinNum); // high, LED off
}
else if (states == STATE_NUM)
{
/* drive buttons LEDs */
CLR_BIT(*LAT, pinNum); // low, LED on
}
return result;
}