Cheerful thanks', Mike. This is extremely valuable. I have a problem understanding the bits, bytes, hex and addressing the entire port of the mcu, but I'm getting there . I will look at porting this to PICAXE at some point after I try the I2C I/O expander solution since I will need shift registers later on even if I escape them for this specific application.It seems putting 5 inputs and 3 outputs on each 74HC595 isn't the software headache I thought it might be;
Rich (BB code):/****************************************************************** * variables and constants * ******************************************************************/ unsigned long swnew = 0; unsigned long swold = 0; unsigned long swcol = 0x00000008; unsigned long flags = 0; unsigned long outputs = 0; unsigned long packet = 0; #define swinput (PORTA & 1)
Not sure how difficult it would be to convert to BASIC (sorry).Rich (BB code):/* * * sample one switch during each 1-msec update interval * * */ swnew = swold | swcol; // indicate 'pressed' if(swinput) // if not pressed (1) swnew ^= swcol; // indicate 'released' /* * * K8LH parallel switch state logic ('new press' filter) * * * * swnew __---___---___---__ sample active low buttons * * swold ___---___---___---_ switch state latch * * swnew __-__-__-__-__-__-_ changes, press or release * * swnew __-_____-_____-____ filter out 'release' bits * * flags __------______----- toggle flag bits for main * * */ swnew ^= swold; // changes, press or release swold ^= swnew; // update switch state latch swnew &= swold; // filter out 'release' bits flags ^= swnew; // toggle flag bits for main /* * * advance matrix column select bit (skip over output bits) * * sssssooo sssssooo sssssooo sssssooo (s=switch o=output) * * */ if(swcol & 0x00000080) swcol = 0x00000400; if(swcol & 0x00008000) swcol = 0x00040000; if(swcol & 0x00800000) swcol = 0x04000000; if(swcol & 0x80000000) swcol = 0x00000004; swcol <<= 1; // advance column select bit /* * * update the 32 bit 74HC595 chain * * */ packet = ~swcol; // invert for active low select packet &= 0xF8F8F8F8; // mask off output bit positions packet |= outputs; // add the output bits SPIOut(packet); // refresh 74HC595 chain
Food for thought.
Cheerful regards, Mike
Edmunds