problem in reading port value

Discussion in 'Embedded Systems and Microcontrollers' started by Ahmed Adel, Dec 17, 2009.

  1. Ahmed Adel

    Thread Starter Member

    May 12, 2008
    18
    0
    hi everybody ..
    I have button connected to RA0 of PORTA via pull down resistor and an LCD that I want to show the word "ONE" if button is pressed and show "Zero" if the button isn't pressed .. I am using mikroC and this is my code .. this code isn't operational .. I get "Zero" all the time regardless of button status

    Code ( (Unknown Language)):
    1.  
    2.  
    3. // LCD module connections
    4. sbit LCD_RS at RB4_bit;
    5. sbit LCD_EN at RB5_bit;
    6. sbit LCD_D4 at RB0_bit;
    7. sbit LCD_D5 at RB1_bit;
    8. sbit LCD_D6 at RB2_bit;
    9. sbit LCD_D7 at RB3_bit;
    10.  
    11. sbit LCD_RS_Direction at TRISB4_bit;
    12. sbit LCD_EN_Direction at TRISB5_bit;
    13. sbit LCD_D4_Direction at TRISB0_bit;
    14. sbit LCD_D5_Direction at TRISB1_bit;
    15. sbit LCD_D6_Direction at TRISB2_bit;
    16. sbit LCD_D7_Direction at TRISB3_bit;
    17. // End LCD module connections
    18.  
    19.  
    20.  
    21.  
    22. void main() {
    23.  
    24.  
    25. TRISB=0x00;
    26. TRISA=0xFF;
    27.  
    28.  
    29. Lcd_Init();
    30. Lcd_Cmd(_LCD_CLEAR);
    31. Lcd_Cmd(_LCD_CURSOR_OFF);
    32.  
    33. while (1)
    34. {
    35.      if (PORTA.F0==1)      
    36.      {
    37.         Lcd_Cmd(_LCD_CLEAR);
    38.         Lcd_out(1,1,"ONE");
    39.  
    40.         delay_ms(1000);
    41.      }
    42.      else
    43.      {
    44.         Lcd_Cmd(_LCD_CLEAR);
    45.         Lcd_out(1,1,"Zero");
    46.         delay_ms(1000);
    47.  
    48.        }
    49.      
    50.  
    51. }
    52.  
    53. }
    54.  
    55.  
     
    Last edited: Dec 17, 2009
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    You don't say which PIC you are using but in any case portA on most PICs can be configured as either an analogue port or a digital port. It defaults to analogue whereas your application needs it to be a digital port.
    To make it digital you have to turn off any AtoD functions and/or any comparator function assosiated with the port.
    Take a look at the data sheet for your chip to see what registers need to be set.
     
  3. Ahmed Adel

    Thread Starter Member

    May 12, 2008
    18
    0
    thanks AlexR .. it is ADCON1 and CMCON1 in PIC16F877A .. thanks alot ..
     
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    In your code you have:

    if (PORTA.F0==1)

    I think it should be like that:

    if (PORTA.A0==1)

    or try

    if (pin_A0==1)
     
Loading...