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

Register definitions, SFR

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



Joined: 19 Aug 2020
Posts: 4

View user's profile Send private message

Register definitions, SFR
PostPosted: Wed Aug 19, 2020 2:47 am     Reply with quote

Hi,
I'm new to CCS. Sorry, if this question has been answered already somewhere in the forum.

I want to write to the register but I cannot find the definitions on the header file.

For example on TIMER2, I would like to

Code:
 t2con = 0x50;


or something like this by setting/clearing each bit


Code:
   
        t2con = 0;
   // Pre 16
   t2con.T2CKPS0 = 1;
   t2con.T2CKPS1 = 1;

   // Post 5
   t2con.T2OUTPS0 = 1;
   t2con.T2OUTPS1 = 0;
   t2con.T2OUTPS2 = 0;
   t2con.T2OUTPS3 = 1;

   pr2 = 99;
   t2con.TMR2ON = 1;


I would like to do this for all the modules I need by writing directly to the SFR rather than using the library functions provided.

On my previous compiler, the header file for the device contains all the registers and bits definitions so I can simply write like this.

Code:
ssp1con1.SSPEN=0; //SPI OFF


Many thanks
John
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 2:54 am     Reply with quote

See this thread. It tells how to make the register definitions file.
http://www.ccsinfo.com/forum/viewtopic.php?t=52602
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 3:02 am     Reply with quote

Do you have the command line compiler, or the IDE?.
If you have the IDE, then select:
Tools
Device Editor
Then on the top bar select 'Registers'
Then select 'Make include file'.

This will then create an include file, giving names for every register and bit.

However before doing this, _STOP_. CCS does not require this. It is almost
never necessary to directly access a register in CCS.
The internal functions do pretty much everything, and the key is much more
portably.

So timer2, will have a set of definitions in the processor include file,
something like:
Code:

////////////////////////////////////////////////////////////////// Timer 2
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2
// Timer 2 Prototypes:
_bif void setup_timer_2(int8 mode, int8 period, int8 postscale);
_bif int8 get_timer2(void);
_bif void set_timer2(int8 value);
// Constants used for SETUP_TIMER_2() are:
#define T2_DISABLED         0
#define T2_DIV_BY_1         4
#define T2_DIV_BY_4         5
#define T2_DIV_BY_16        6
#define T2_DIV_BY_64        7

and you can setup timer 2, for (say) a /4 prescaler, and a /6
postscaler, with simply:
setup_timer2(T2_DIV_BY_4, 127,6);

Far easier, and much plainer than what you are doing.

Think one thousand times before doing direct accesses. This is not how
to work in CCS.... Sad
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 4:38 am     Reply with quote

As others have pointed out , it's a lot easier and safer to use the CCS functions or 'way' of accessing the SFRs.
However if you really,really want or need to directly 'diddle bits', I suggest go the next step and learn PIC assembler ! Only 30-35 instructions (less than number of fuses) and you're talking to the PIC directly.
When I bought PCM 2.534, 2+ decades ago, I had to self-teach myself 'C' as I'd only programmed PICs in Assembler. I quickly appreciated CCS for creating the 'functions' to setup/control the SFRs as well as the 100s of examples !
You should take advantage of what they've done. If you manually set the wrong bit in an SFR, you could spend hours if not days, trying to figure it out ! BTDT.... still do on occasion.....sigh.

Jay
jonfs2000



Joined: 19 Aug 2020
Posts: 4

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 6:22 am     Reply with quote

Thank you all for your reply.

I'm pretty old styled and got used to writing directly to registers(specially during debugging) for nearly 19yrs now. I have done the same for PIC, ARM Cortex, MSP420 etc. Though I use the library functions provided, sometimes I need to directly to write to registers.

I read the datasheet and I set/clear the bits accordingly. And it gives me a satisfactions that I have done as per datasheet..

I still need to read/write to registers directly while I'm getting used to new compiler and using CCS library functions.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Aug 19, 2020 7:50 am     Reply with quote

Personally, for the small number of registers it is worth talking to directly, the
'SFR' and 'BIT' abilities of the compiler are by far the most friendly way of
working.

For T1CON (assuming a PIC18):
Code:

#BIT RD16=getenv("BIT:RD16")
#BIT T1RUN=getenv("BIT:T1RUN")
#BIT T1CKPS1=getenv("BIT:T1CKPS1")
#BIT T1CKPS0=getenv("BIT:T1CKPS0")
#BIT T1OSCEN=getenv("BIT:T1OSCEN")
#BIT T1SYNC=getenv("BIT:T1SYNC")
#BIT TMR1CS=getenv("BIT:TMR1CS")
#BIT TMR1ON=getenv("BIT:TMR1ON")
#byte T1CON=getenv("SFR:T1CON")


This then gives you one bit variables corresponding exactly to the data
sheet names, and a byte variable corresponding to the entire register.
jonfs2000



Joined: 19 Aug 2020
Posts: 4

View user's profile Send private message

PostPosted: Sat Aug 22, 2020 3:06 pm     Reply with quote

Thank you Ttelmah, it worked! 👍


Ttelmah wrote:
Do you have the command line compiler, or the IDE?.
If you have the IDE, then select:
Tools
Device Editor
Then on the top bar select 'Registers'
Then select 'Make include file'.

This will then create an include file, giving names for every register and bit.

However before doing this, _STOP_. CCS does not require this. It is almost
never necessary to directly access a register in CCS.
The internal functions do pretty much everything, and the key is much more
portably.

So timer2, will have a set of definitions in the processor include file,
something like:
Code:

////////////////////////////////////////////////////////////////// Timer 2
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2
// Timer 2 Prototypes:
_bif void setup_timer_2(int8 mode, int8 period, int8 postscale);
_bif int8 get_timer2(void);
_bif void set_timer2(int8 value);
// Constants used for SETUP_TIMER_2() are:
#define T2_DISABLED         0
#define T2_DIV_BY_1         4
#define T2_DIV_BY_4         5
#define T2_DIV_BY_16        6
#define T2_DIV_BY_64        7

and you can setup timer 2, for (say) a /4 prescaler, and a /6
postscaler, with simply:
setup_timer2(T2_DIV_BY_4, 127,6);

Far easier, and much plainer than what you are doing.

Think one thousand times before doing direct accesses. This is not how
to work in CCS.... Sad
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