Please help me!!!

General discussion on mikroC.
Post Reply
Author
Message
tuancdc
Posts: 45
Joined: 02 Jul 2005 08:32
Location: Vietnam
Contact:

Please help me!!!

#1 Post by tuancdc » 05 Jul 2005 03:26

:?:
I'm beginer. I'm building a test project with Led_Blinking and Button. My code below (MCU P16F628A, 10MHz, RA0-RA3 is connecting buttons with 'Pull-up' resister):

Code: Select all

void Led_Blinking();
void OnLED();
void OffLED();
void main()
{
     TRISB = 0; //PORT B is output
     TRISA = 0x0f; //RA0-RA3 is input, RA4 is output
     PORTA = 0xff;
     PORTB = 0;
     do{
           if (PORTA.F0 == 0)         //Key pressed?
           {
              Delay_ms(100);          //Delay for key debounce
              if (PORTA.F0 == 0) OnLED();
              else OffLED();
           }
           else OffLED();

           Led_Blinking();
     }while(1);

}

void Led_Blinking()
{
           PORTB.F0 = 1;        // Turn on diodes(LED 0) on PORTB
//           PORTA.F4 = 1;
           Delay_ms(500);      // Wait 1 second
           PORTB.F0 = 0;        // Turn off diodes(LED 0) on PORTB
//           PORTA.F4 = 0;
           Delay_ms(500);    // Wait 1 second
}

void OnLED()
{
     PORTB.F2 = 1; //LED 2 is ON
     PORTB.F3 = 1; //LED 3 is ON
}

void OffLED()
{
     PORTB.F2 = 0; //LED 2 is OFF
     PORTB.F3 = 0; //LED 3 is OFF
}
Result:
- :( LED 2,3 are always ON. It don't change status when I pressed key.
- :D LED 0 is blinking.

Anyone tell me why my buttons don't effect?

Thank.
Tuan

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Please help me!!!

#2 Post by pizon » 05 Jul 2005 08:09

PICs that have A/D converter and/or analog comparators use PORTA by default for analog inputs. So, depending on PIC being used, you mostly have to explicitly declare PORTA to be digital I/O. On 16F877A, for example, you do this by setting the ADCON1 register to 0x06.
pizon

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#3 Post by Charlie » 07 Jul 2005 23:02

Hi ,

try adding CMCON=7 at the beginning of your code.CMCON=7 turns off the comparitors and makes I/O digital.
Regards Charlie M.

Post Reply

Return to “mikroC General”