dip switch problem

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
hello guys!
i have this problem... im using 4way dip switch connected to my pic16f88 RB4-RB7 ports... 2 of the swich selecting a sound so i have four sound to select and the other 2 are just on-off switch.
my problem is that the 2 of the selection switch select only 2 from the four sounds that i have on the programm. im using the proteus as simulation and there everything is ok but in the reallity i have this problem.
any ideas?
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
My first idea is to let us see your schematic so we know how you connected this switch.

actually i test the dip switch as on/off... something else is not ok... RB5-RB6 works ok, RB4-RB7 is only ON no mater what i choose on the switch... (i test 3 different dipswitch so is not there the problem also i change and the microcontroller and still the same problem)
here is my schematic the dip switch is connected to 4 pull up resistor.Photo 4-2-15 - 7 25 45 μ.μ..jpg
 

JohnInTX

Joined Jun 26, 2012
4,787
Make sure TRISB = 11110000; write it as a byte, not individual bits.
Initialize the RB7-RB4 bits to 1 by writing to the PORT - again as a byte, not individual bits.
Be sure ANSEL = 0;
Be sure any on-board peripheral that shares pins is turned OFF explicitly in your init code.
Make sure that your programmer is not interfering with the switches (RB6 and RB7)
You should be able to observe the voltage levels go from 1-0 on each input as you move the switch.

If all that looks OK, post your code using CODE tags. CODE tags can be found on the edit toolbar i.e. Insert->CodeTags. Paste your code into the box.
 

ErnieM

Joined Apr 24, 2011
8,377
As John mentioned ANSEL is the problem here. RB6 and RB7 will always read a zero when AN5 and AN6 are enabled as analog inputs.

Fix that in ANSEL.

I suspect there is a logic inversion problem too. Your schematic hints the inputs go low when the switch is on. That may be a problem depending on how the inputs are scanned.
 

Thread Starter

gatoulisss

Joined Jan 23, 2015
69
Make sure TRISB = 11110000; write it as a byte, not individual bits.
Initialize the RB7-RB4 bits to 1 by writing to the PORT - again as a byte, not individual bits.
Be sure ANSEL = 0;
Be sure any on-board peripheral that shares pins is turned OFF explicitly in your init code.
Make sure that your programmer is not interfering with the switches (RB6 and RB7)
You should be able to observe the voltage levels go from 1-0 on each input as you move the switch.

If all that looks OK, post your code using CODE tags. CODE tags can be found on the edit toolbar i.e. Insert->CodeTags. Paste your code into the box.
here is my connections in the foto and this is my code below


Code:
void main() {
osccon=0x60;
ansel=0;
trisb4_bit=1;
trisb5_bit=1;
trisb6_bit=1;
trisb7_bit=1;
trisb0_bit=0;
for (;; ) {
if (porta.f7==0) { // i change the port here to test what port i want here is the RB7 testing
portb.f0=1; }
else {portb.f0=0;}

}
}

i have tryied with other led other dip switch and other microcontroller still same problem.

Photo 4-2-15 - 7 44 57 μ.μ..jpg
 

JohnInTX

Joined Jun 26, 2012
4,787
You are not reading PORTB in your code. I've modified it according to the posted schematic with comments about how you should handle things..
Good luck.
Code:
// This is for 16F88

void main() {
     OSCCON=0x60;
     ANSEL=0;
  // Do NOT set TRIS using individual bits!!
  //trisb4_bit=1;
  //trisb5_bit=1;
  //trisb6_bit=1;
  //trisb7_bit=1;
  //trisb0_bit=0;
 
  // Set up ALL the I/0 in the chip - leave nothing un-initalized
  PORTA = 0x00;  // First, init PORT data to avoid transients on the outputs
  PORTB = 0x00;  // LEDs OFF
                 // Then write TRIS as a byte, NOT individual bits..
  TRISA = 0x00;  // PORTA is all OUT (nothing hooked to it)
  TRISB = 0xf0;  // PORTB 7-4 is input, 3-0 is output
 
//for (;; ) {
//if (porta.f7==0) { // i change the port here to test what port i want here is the RB7 testing
                   // .. but you are reading PORT A, not PORT B.
//portb.f0=1; }      // also, the names of the ports should be UPPER CASE to conform to Microchip's convention..
//else {portb.f0=0;} // Running MikroC with case-sensitivity off is a BAD IDEA.

//}
    // Try this..
   for(;;){
   if (PORTB.F7 == 0)  // 0 = switch closed - reads RB7
       PORTB.F1 = 1;    // RB1 LED ON when switch closed (that's what's on your schematic)
   else
       PORTB.F1 = 0;
   }// for
}// main
 
Top