help in mikroC code plz

Thread Starter

muza1988

Joined Jun 17, 2010
1
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


Rich (BB code):
int i;
char txt[2];
void main()

{

  i = 0;
  PORTD = 0;
  LCD_Config(&PORTD,1,0,2,7,6,5,4);
  PORTB = 0;
  TRISB = 0b00000011;

 {
 do
 {
   if(PORTB.F0 == 0)
   {
  LCD_Cmd(LCD_CLEAR);
  Lcd_Cmd(Lcd_CURSOR_OFF);
  IntToStr(i, txt);
  Lcd_Out(2,2 ,txt);
  i=i+1;
  delay_ms(500);

   }

   


 if ( PORTB.F1 == 0)
  {
  LCD_Cmd(LCD_CLEAR);
  Lcd_Cmd(Lcd_CURSOR_OFF);
  LCD_Out(1,4, "DINNAR BOX");
  LCD_Out(2,1,"GOLD DINNAR BOX !");
  Delay_ms(1000);
  }


  }while(1);

  }


  }
 

retched

Joined Dec 5, 2009
5,207
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?
 
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).
 
Top