help in mikroC code plz

Discussion in 'Programmer's Corner' started by muza1988, Jun 17, 2010.

  1. muza1988

    Thread Starter New Member

    Jun 17, 2010
    1
    0
    dear all,

    i am using PIC16F877A. i want that wen switch at B0 is pressed it shud display the count and wen switch at B1 is pressed it shud print the text on LCD. but the count n and the text appear on lcd without pressing any of the switches.even though the switches are pressed, the programm runs..
    can anybody help..plz


    Code ( (Unknown Language)):
    1.  
    2. int i;
    3. char txt[2];
    4. void main()
    5.  
    6. {
    7.  
    8.   i = 0;
    9.   PORTD = 0;
    10.   LCD_Config(&PORTD,1,0,2,7,6,5,4);
    11.   PORTB = 0;
    12.   TRISB = 0b00000011;
    13.  
    14.  {
    15.  do
    16.  {
    17.    if(PORTB.F0 == 0)
    18.    {
    19.   LCD_Cmd(LCD_CLEAR);
    20.   Lcd_Cmd(Lcd_CURSOR_OFF);
    21.   IntToStr(i, txt);
    22.   Lcd_Out(2,2 ,txt);
    23.   i=i+1;
    24.   delay_ms(500);
    25.  
    26.    }
    27.  
    28.    
    29.  
    30.  
    31.  if ( PORTB.F1 == 0)
    32.   {
    33.   LCD_Cmd(LCD_CLEAR);
    34.   Lcd_Cmd(Lcd_CURSOR_OFF);
    35.   LCD_Out(1,4, "DINNAR BOX");
    36.   LCD_Out(2,1,"GOLD DINNAR BOX !");
    37.   Delay_ms(1000);
    38.   }
    39.  
    40.  
    41.   }while(1);
    42.  
    43.   }
    44.  
    45.  
    46.   }
    47.  
     
  2. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Did you set portB as input or output?

    Can you post your ENTIRE code, including initialization parameters?

    That will help.

    Also, do you have a schematic of the circuit in question?
     
  3. BenjaminSweet

    New Member

    Jun 4, 2010
    8
    7
    From your code, it looks like PORTB.F0 and PORTB.F1 inputs are supposed to be pulled high, and go low when the button is pressed. As retched suggests, your schematic will help to clarify that. (Or is there in internal pull-up that needs to be enabled?) If these pins are not pulled-up, then the input will ALWAYS be == 0, and the bodies of both "if" statements will always run whether the buttons are pressed or not.

    I also noticed that you are just reading the digital inputs (PORTB.F0 and PORTB.F1) presumably connected to a switch. Are these digital inputs filtered? The delays at the bottom of each "if" statement MAY be long enough to avoid switch "bounce", but perhaps not. (See Jack Ganssle's "A Guide to Debouncing": http://www.ganssle.com/debouncing.htm) Perhaps after the delay you might wait at for the associated button to be released (== 1, presuming the input is pulled-up).
     
Loading...