CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pic18F1330 Blinking LED not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dwb
Guest







Pic18F1330 Blinking LED not working
PostPosted: Wed Nov 26, 2008 12:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 26, 2008 12:26 pm     Reply with quote

Quote:
CMCON=0x07;

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







PostPosted: Wed Nov 26, 2008 12:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 26, 2008 1:01 pm     Reply with quote

Here are some articles that explain it:
http://www.piclist.com/techref/readmodwrite.htm
http://www.marcansoft.com/subidos/readmodifywrite.pdf
http://www.microchip.com/wiki/Wiki.aspx?WikiID=50
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group