View previous topic :: View next topic |
Author |
Message |
dwb Guest
|
Pic18F1330 Blinking LED not working |
Posted: Wed Nov 26, 2008 12:04 pm |
|
|
I am trying to get a LED to blink using a Pic18f1330. I can get the LED to come on, however, it doesn't blink. Any help would be appreciated.
Code: | #include <18F1330.h>
#fuses INTRC_IO,NOBROWNOUT,NOWDT,NOPUT,NOPROTECT
#use delay(internal=4M)
#byte TRISA=0xF92
#byte PORTA=0xF80
#byte ADCON1=0xFC1
#byte CMCON=0xFB4
void main()
{
ADCON1=0x07;
CMCON=0x07;
TRISA=0xFE;
PORTA=0x00;
while(1)
{
delay_ms(1000);
if(PORTA==0x01)
PORTA=0x00;
else
PORTA=0x01;
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 26, 2008 12:26 pm |
|
|
Comparator 0 is on the RA0 pin, and setting a bit = 1 in the CMCON
register enables the comparator. You're using RA0 for digital i/o.
You should not enable the comparator in that case.
Quote: | RA0/AN0/INT0/KBI0/CMP0
bit 0 CMEN0: Comparator 0 Enable bit
1 = Comparator 0 is enabled
0 = Comparator 0 is disabled
|
If you just let the compiler handle the start-up code (which it does for
comparators and analog pins), you will see that it disables the compartors
automatically for you. Here's the start-up code from the .LST file:
Quote: | .................... void main()
.................... {
0004: CLRF TBLPTRU
0006: BCF RCON.IPEN
0008: CLRF FSR0H
000A: CLRF FSR0L
000C: MOVF ADCON1,W
000E: ANDLW E0
0010: MOVWF ADCON1
0012: CLRF CMCON |
Also, you're reading directly from the port. The 18F series PICs have
latch registers. If you read and write to the latch instead of the register,
then the RMW operation is not affected by any loading on the pin.
In truth, your whole program could be reduced to 2 or 3 lines of code
if you used CCS functions. (using the output_toggle() function). |
|
|
dwb Guest
|
|
Posted: Wed Nov 26, 2008 12:58 pm |
|
|
Thanks PCM programmer. I feel stupid now relying on the example code in the datasheet instead of double checking the registers. What is a RMW operation? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|