Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with a code for pic16f777 4 way switch

Status
Not open for further replies.

Tom2

Full Member level 5
Full Member level 5
Joined
Nov 11, 2006
Messages
318
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
3,457
setup_timer_2();

I wrote a code for a 4 way switch as is show in picture below.The problem is that the circuit is not working.Is anyone who know what is the problem????(i used pcm compiler and pic16f777 microcontroller)
the code is :

if ((input(PIN_A3)==0)) { // if A3 is low
setup_timer_2(T2_DIV_BY_1, 255,1);
output_high(PIN_D2);
}

else if (((input(PIN_A3)==0))&& (input(PIN_A4)==0)) { // if A_4 is low
setup_timer_2(T2_DIV_BY_4, 255,1);
output_high(PIN_D3);

}

else if (((input(PIN_A3)==0) && (input(PIN_A4)==0)&&(input(PIN_A5)==0))) { // if A_5 is low
setup_timer_2(T2_DIV_BY_16, 255,1);
output_high(PIN_C4);
}

else if (((input(PIN_E0)==0)&&(input(PIN_A3)==0) && (input(PIN_A4)==0)&&(input(PIN_A5)==0))) { // if
setup_timer_2(T2_DISABLED , 255,1);
}
 

setup_timer_2

First, i find the circuit a little bit strange. Output 1 if input 1, output 2 if input1 and input 2 and so on. Anyway, for any output to go high, pin 1 must be high. But that means only the first part of the code is executed and the program skips down to the end of the if, else if, and else clauses. So the logic is faulty. Test each case sequentially with if statements, no else if stuff.

Also if all inputs are low then all of the clauses are true and will be executed. All conditons should be unique for each clause. Like if pin 1 do A, else if pin 2 do B and so on. In this case the pin 1 clause would have the highest priority. You could check to make sure that one and only one pin was set and so on before you perform any action but that is up to you.

Also, are the dip switches debounced? run the inputs through a CMOS buffer chip (the input gates must be CMOS) and use the appropriate values for the resistor and capacitor (it's just a filter). I can't remember the values, just google switch debouncing, something should come up. Or just use the delay_ms() statement to double-check that the pin is still low.

Also just use if(input(PIN_E0), forget the ==0 stuff, it's not necessayr since the input function returns a 1 or a 0 (it's boolean) already.

Hope that helps.
 

Re: pic16f777_switch

Please check whether the ports are configured as digital i/o
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top