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

Write a 16 bit register
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

Write a 16 bit register
PostPosted: Sat Feb 25, 2012 6:29 am     Reply with quote

I have in a variable a value that I want to write a 16 bit register with it. How do I do that? The numbers are in ascii ex: 445. How can I convert them to hex and split them to write the 16 bit register properly using set bit function? Thanks.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Feb 25, 2012 6:42 am     Reply with quote

Where is the 16 bit register? Is in in your PIC or part of an external chip? If it is external, what is the communication channel to the external chip (parallel, serial, SPI, I2C, etc.)?
_________________
The search for better is endless. Instead simply find very good and get the job done.
temtronic



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

View user's profile Send private message

PostPosted: Sat Feb 25, 2012 6:44 am     Reply with quote

When you have your project open, press F11, and the CCS Help files open up ! Move the mouse over to the table of contents, look for functions, find 'make16' or 'make8' and doubleclik...

Probably 99% of what you need to know can be found through F11 either in the description of functions, data types or in the FAQ section
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 7:58 pm     Reply with quote

Quote:
numbers are in ascii ex: 445.

Are those bytes already in an array ? Are they a string ? A string has
a 0x00 byte at the end. If so, then you can use the atol() function to
convert an ASCII string to an int16 value and load it into a int16 variable.
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 8:47 pm     Reply with quote

a cleaner explication:
.................... set_uart_speed(5931); //random baudrate
0BEA: BSF FB8.3 //bit 3 set , 16 bit baud rate generator is used.
0BEC: MOVLW A1
0BEE: MOVWF FAF
0BF0: MOVLW 02
0BF2: MOVWF FB0
0BF4: MOVLW E6 //HS BAUD RATE ( BRGH)
0BF6: MOVWF FAC

and in my top program declared:
int16 SPBRGH=455; //DECIMAL SPBRG

what i want is to load the SPBRGH variable in the proper way in the FB0 and FAF registers so i could get:
0BEC: MOVLW C7
0BEE: MOVWF FAF
0BF0: MOVLW 01
0BF2: MOVWF FB0
( i transformed the SPBRGH value to hexadecimal and put it in manually in the registers) the value of 1C7...

hope now is easy to understand...thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 9:58 pm     Reply with quote

Post your PIC and your compiler version.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 2:04 am     Reply with quote

I think, CCS C has built-in functions to write all 16-Bit registers of interest, e.g timers. You can however define word registers and read and write data from/to it.

Code:
#word myreg = 0xFAF
myreg = 1234;
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:39 am     Reply with quote

CCS PCH C Compiler, Version 4.124, 52848 , device 18f25k22
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Tue Feb 28, 2012 5:31 am     Reply with quote

FvM , the registers are 8+8 bit(so i need to write both 0XFAF and 0XFB0 ....can you elaborate a little your example? Thanks
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Feb 28, 2012 8:16 am     Reply with quote

Please inspect the generated assembly code in the list file.
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Tue Feb 28, 2012 9:09 pm     Reply with quote

Your brilliant FvM , worked like a charm!
Ttelmah



Joined: 11 Mar 2010
Posts: 19548

View user's profile Send private message

PostPosted: Wed Feb 29, 2012 3:34 am     Reply with quote

Just to expand fractionally on this (particularly for anyone you wants to write something like an int32!).
There is one 'core' function in CCS to map a variable into a location in memory. #byte.
You can put an 8bit variable 'at' a specific location with this, so:

#byte myreg=0xFAF

creates a variable 'myreg', that is placed at 0xFAF.

There is then an expansion to this, that if a variable already exixts, the whole variable is mapped at the location. So:

int16 myreg
#byte myreg=0xFAF

places the 16bit variable 'myreg' at 0xFAF and 0xFB0

The #word function is effectively a macro doing exactly this.

Then though you can locate even larger things.

int32 myreg
#byte myreg=0xFAF

Now places the 32bit variable 'myreg' at the specified memory address.

You can even get really sneaky, and place (for example), a structure or union with a series of bytes laid out exactly the way you want using this.

The built in functions that FvM mentions are in fact just declared in the .h file using the #byte, and #word directives.

There is then a sub function to access a bit, which codes rather neatly, so if (for instance) you wanted to be able to control just the top bit of byte 0xFAF, you can use:

#byte myreg=0xFAF
#bit myregMSb=myreg.7

Which then gives access to the single bit as 'myregMSb', or the whole byte as 'myreg'. Very powerful, and very useful, especially since you can map a bit to refer to a bit in any existing variable by name.

Makes potentially for nice readable code, as well as generating quick assembler.

Best Wishes
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Wed Feb 29, 2012 3:07 pm     Reply with quote

It works but now I have a problem with the UART. I'm trying to change the baudrate dynamically by loading the 0xFAF AND 0xFB0 with the proper values, but my data has parts where its corrupted. Is possible that I will need to reset the uart after I change the baudrate or do I need to stop the uart module before I write to registers? Or how? Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19548

View user's profile Send private message

PostPosted: Wed Feb 29, 2012 4:07 pm     Reply with quote

Why not just use the set_uart_speed function....

Though CCS has the ability to do direct access to registers, for 99.9% of things this should not be needed or used.

Best Wishes
maria100



Joined: 01 Feb 2012
Posts: 63

View user's profile Send private message

PostPosted: Wed Feb 29, 2012 4:09 pm     Reply with quote

Because set_uart_speed needs the value to be a constant. I'm working with non standard baud rates that are changed in the running program. I assume when I do set_uart_speed, the compiler generates code that stops the uart and puts the new value"...but I didn't find that type of code in the LST file...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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