dip switch problem

Discussion in 'Programmer's Corner' started by gatoulisss, Feb 4, 2015.

  1. gatoulisss

    Thread Starter Member

    Jan 23, 2015
    34
    0
    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?
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    My first idea is to let us see your schematic so we know how you connected this switch.
     
    kubeek likes this.
  3. gatoulisss

    Thread Starter Member

    Jan 23, 2015
    34
    0

    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
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  6. gatoulisss

    Thread Starter Member

    Jan 23, 2015
    34
    0
    here is my connections in the foto and this is my code below


    Code (Text):
    1. void main() {
    2. osccon=0x60;
    3. ansel=0;
    4. trisb4_bit=1;
    5. trisb5_bit=1;
    6. trisb6_bit=1;
    7. trisb7_bit=1;
    8. trisb0_bit=0;
    9. for (;; ) {
    10. if (porta.f7==0) { // i change the port here to test what port i want here is the RB7 testing
    11. portb.f0=1; }
    12. else {portb.f0=0;}
    13.  
    14. }
    15. }
    16.  

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

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

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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 (Text):
    1. // This is for 16F88
    2.  
    3. void main() {
    4.      OSCCON=0x60;
    5.      ANSEL=0;
    6.   // Do NOT set TRIS using individual bits!!
    7.   //trisb4_bit=1;
    8.   //trisb5_bit=1;
    9.   //trisb6_bit=1;
    10.   //trisb7_bit=1;
    11.   //trisb0_bit=0;
    12.  
    13.   // Set up ALL the I/0 in the chip - leave nothing un-initalized
    14.   PORTA = 0x00;  // First, init PORT data to avoid transients on the outputs
    15.   PORTB = 0x00;  // LEDs OFF
    16.                  // Then write TRIS as a byte, NOT individual bits..
    17.   TRISA = 0x00;  // PORTA is all OUT (nothing hooked to it)
    18.   TRISB = 0xf0;  // PORTB 7-4 is input, 3-0 is output
    19.  
    20. //for (;; ) {
    21. //if (porta.f7==0) { // i change the port here to test what port i want here is the RB7 testing
    22.                    // .. but you are reading PORT A, not PORT B.
    23. //portb.f0=1; }      // also, the names of the ports should be UPPER CASE to conform to Microchip's convention..
    24. //else {portb.f0=0;} // Running MikroC with case-sensitivity off is a BAD IDEA.
    25.  
    26. //}
    27.     // Try this..
    28.    for(;;){
    29.    if (PORTB.F7 == 0)  // 0 = switch closed - reads RB7
    30.        PORTB.F1 = 1;    // RB1 LED ON when switch closed (that's what's on your schematic)
    31.    else
    32.        PORTB.F1 = 0;
    33.    }// for
    34. }// main
     
Loading...