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 support@ccsinfo.com

Problem using register declaration PIC 18F46K42

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



Joined: 19 Aug 2019
Posts: 2

View user's profile Send private message

Problem using register declaration PIC 18F46K42
PostPosted: Mon Aug 19, 2019 11:55 pm     Reply with quote

I'm new using this pic 18F46K42 and I have not been able to blink PORT B using register declaration, my compiler is version 5.083, I would appreciate some help, thanks.

Code:
#include <18F46K42.h> 
#fuses HS,NOWDT
#use delay(crystal=20MHz)

#BYTE TRISB = 0x3FC3
#BYTE PORTB = 0x3FCB

void main()

{
  TRISB=0b00000000;

  while(true)
  {
      PORTB=0b11111111;
      delay_ms(1000);
      PORTB=0b00000000;
      delay_ms(1000);
  }
 
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19216

View user's profile Send private message

PostPosted: Tue Aug 20, 2019 12:49 am     Reply with quote

Use the compiler's ability to fill the addresses in for you:

#BYTE PORTB=(SFR:PORTB")

Then on the PIC18 you should really always write to LATB, not PORTB.
Difference between PIC16, and PIC18. On the PIC16, the LAT and the PORT
are the same register. On the PIC18, the output latch is separate from
the physical PORT. Now on most chips a write to the port is meant to
automatically update the latch, but on a lot of the newer chips it doesn't,
(see below) so for an output it is better to write to the latch.

If you look at the data sheet, you see:
Quote:

Each port has ten registers to control the operation.
These registers are:
• PORTx registers (reads the levels on the pins of
the device)
• LATx registers (output latch)
• TRISx registers (data direction)
• ANSELx registers (analog select)
• WPUx registers (weak pull-up)
• INLVLx (input level control)
• SLRCONx registers (slew rate control)
• ODCONx registers (open-drain control)


Note what 'PORTx' says it does. _reads_ the pins. Not set the output...

The data sheet does say that a write to the PORTB register is meant to
update the latch, but this chip is one where this has been found not to
work. So general 'rule', on a PIC18, always write to the latch, not the
port....
PAVEL241



Joined: 19 Aug 2019
Posts: 2

View user's profile Send private message

PostPosted: Wed Aug 21, 2019 7:28 am     Reply with quote

You were right, now it works fine, thank you very much.
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