View previous topic :: View next topic |
Author |
Message |
maria100
Joined: 01 Feb 2012 Posts: 63
|
Write a 16 bit register |
Posted: Sat Feb 25, 2012 6:29 am |
|
|
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
|
|
Posted: Sat Feb 25, 2012 6:42 am |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Sat Feb 25, 2012 6:44 am |
|
|
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
|
|
Posted: Sun Feb 26, 2012 7:58 pm |
|
|
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
|
|
Posted: Sun Feb 26, 2012 8:47 pm |
|
|
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
|
|
Posted: Sun Feb 26, 2012 9:58 pm |
|
|
Post your PIC and your compiler version. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Feb 27, 2012 2:04 am |
|
|
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
|
|
Posted: Mon Feb 27, 2012 3:39 am |
|
|
CCS PCH C Compiler, Version 4.124, 52848 , device 18f25k22 |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Tue Feb 28, 2012 5:31 am |
|
|
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
|
|
Posted: Tue Feb 28, 2012 8:16 am |
|
|
Please inspect the generated assembly code in the list file. |
|
|
maria100
Joined: 01 Feb 2012 Posts: 63
|
|
Posted: Tue Feb 28, 2012 9:09 pm |
|
|
Your brilliant FvM , worked like a charm! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19522
|
|
Posted: Wed Feb 29, 2012 3:34 am |
|
|
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
|
|
Posted: Wed Feb 29, 2012 3:07 pm |
|
|
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: 19522
|
|
Posted: Wed Feb 29, 2012 4:07 pm |
|
|
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
|
|
Posted: Wed Feb 29, 2012 4:09 pm |
|
|
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... |
|
|
|