Page 1 of 1

PIC18f4550 led blinking with push button

PostPosted: Mon Mar 26, 2012 12:45 am
by Hamzah
Hi. I am really new in PIC, i try to make a simple led blinking with push button. I use sk40c with PIC18f4550 and mikroC for PIC. Below is the code:

sbit LED1 at RA1_bit;
sbit sw1 at RB0_bit;
void main(){

TRISA1_bit = 0;
TRISB0_bit = 0xFF;
LED1 =0;
sw1 = 0;
while(1){
if (sw1==1)
{
LED1 = ~LED1;
Delay_ms(1000);
}
}}

i try to use push button on RB0, when RB0 is pushed, the led will start blinking, but it still cannot do what i want. can someone taught me what is wrong in my code?? Thanks in advance.

Re: PIC18f4550 led blinking with push button

PostPosted: Mon Mar 26, 2012 6:45 am
by ABSF
Hamzah WROTE:sbit LED1 at RA1_bit;
sbit sw1 at RB0_bit;
void main(){

TRISA1_bit = 0;
TRISB0_bit = 0xFF;
LED1 =0;
sw1 = 0;
while(1){
if (sw1==1)
{
LED1 = ~LED1;
Delay_ms(1000);
}
}}


The problem is caused by using RA1 for digital output to light your LED. You need to initialize port A to digital just after main() as below

MOVLW 0Fh ; Configure A/D
MOVWF ADCON1 ; for digital purpose
MOVLW 07h ; Configure comparators
MOVWF CMCON ; for digital purpose

And In C it means:
ADCON1 = 0x0F;
CMCON = 0x07;

Allen

Re: PIC18f4550 led blinking with push button

PostPosted: Mon Mar 26, 2012 7:51 am
by Idris
From your code, you need to hold the switch to make LED blinking. since the delay is 1 second, you need to hold more than 2 second in order to see the LED blink. Besides allen suggestion, you can change the pin of LED, try RD0.

Re: PIC18f4550 led blinking with push button

PostPosted: Mon Mar 26, 2012 4:15 pm
by Hamzah
Thanks guys. quick question, does ADCON1 disable analog function for certain port or all the port?? same goes with CMCON.

Re: PIC18f4550 led blinking with push button

PostPosted: Mon Mar 26, 2012 4:42 pm
by ABSF
Hamzah WROTE:Thanks guys. quick question, does ADCON1 disable analog function for certain port or all the port?? same goes with CMCON.


You can disable analog for indivisual or all the port A pins. Please refer to datasheet for details under CCP and ADC sections.

Allen