01-05-2017, 02:06 AM
(Сообщение последний раз редактировалось: 01-05-2017, 02:11 AM Altor Audio.)
как-то так:
Код:
#define IN_1 TRISB1
#define IN_2 TRISB2
#define IN_3 TRISB3
#define IN_4 TRISB4
#define INP_1 RB1
#define INP_2 RB2
#define INP_3 RB3
#define INP_4 RB4
//----------------------------------------------
#define SEL_STATE (PORTB | (~0x1E)) // set all unused bits to 1
//----------------------------------------------
static void SetTris(u8 key){
switch (key){
case 0: IN_1=1;IN_2=1; IN_3=1;IN_4=1; break;
case 1: IN_1=0;IN_2=1; IN_3=1;IN_4=1; break;
case 2: IN_1=1;IN_2=0; IN_3=1;IN_4=1; break;
case 3: IN_1=1;IN_2=1; IN_3=0;IN_4=1; break;
case 4: IN_1=1;IN_2=1; IN_3=1;IN_4=0; break;
}
}
//----------------------------------------------------------------
void sel_init(void){ // call once at the program start
INP_1 =0; INP_2 =0; INP_3 =0; INP_4 =0; // set port bits to 0 forever
//
sel=1; // set input 1 at the start
// sel=ReadEE(LastInputAddress); // or read the last value from EEPROM
//
S_data_send(sel-1); // установка мультиплексора
SetTris(sel); // set dedicated pin as output=0;
}
//----------------------------------------------------------------
void selector(void){ // call periodically (from timer interrupt or main loop)
static unsigned char selstate;
unsigned char currentstate, newsel=0;
SetTris(0); // set all pins as inputs
currentstate=SEL_STATE;
if(currentstate!=0xFF){
if (selstate != currentstate)) {
selstate = currentstate;
if(!INP_1) newsel++; else
if(!INP_2) newsel++; else
if(!INP_3) newsel++; else
if(!INP_4) newsel++;
if(newsel &&(newsel!=sel)){
sel=newsel; // sel and newsel = 1,2,3,4
S_data_send(sel-1); // установка мультиплексора
}
}
}
SetTris(sel); // set dedicated pin as output=0;
}