PIC18f4550 led blinking with push button

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

PIC18f4550 led blinking with push button

Postby Hamzah » Mon Mar 26, 2012 12:45 am

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.
Hamzah
Greenhorn
 
Posts: 2
Joined: Mon Mar 26, 2012 12:31 am

Re: PIC18f4550 led blinking with push button

Postby ABSF » Mon Mar 26, 2012 6:45 am

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: PIC18f4550 led blinking with push button

Postby Idris » Mon Mar 26, 2012 7:51 am

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.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: PIC18f4550 led blinking with push button

Postby Hamzah » Mon Mar 26, 2012 4:15 pm

Thanks guys. quick question, does ADCON1 disable analog function for certain port or all the port?? same goes with CMCON.
Hamzah
Greenhorn
 
Posts: 2
Joined: Mon Mar 26, 2012 12:31 am

Re: PIC18f4550 led blinking with push button

Postby ABSF » Mon Mar 26, 2012 4:42 pm

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 0 guests