View previous topic :: View next topic |
Author |
Message |
young
Joined: 24 Jun 2004 Posts: 285
|
combined register reading |
Posted: Fri Mar 18, 2005 7:45 am |
|
|
In microchip, there are a lot of pairs of registers, like (TIMER1H,TIMER1L),(TIMER0H,TIMER0L), (CCPR1H,CCPR1L), we could put high byte and low byte together like the following tow methods, right?
//this is for 18f458
#BYTE timer1h 0xFD7h
#BYTE timer1l 0xFD6h
int16 time1
timer1=timer1h<<8+timer1l;
or
union timer1{int16 t1, int8 t1[2]} T1D
T1D.t1[0]=timer1l;
T1D.t1[1]=timer1h;
timer1=T1D.t1;
right?
timer1 T1 |
|
|
valemike Guest
|
|
Posted: Fri Mar 18, 2005 8:05 am |
|
|
not sure about the union stuff, but i always read a Timer using CCS libraries and 'unsigned long'.
e.g.
unsigned long timer_value;
timer_value = get_timer1(); |
|
|
Ttelmah Guest
|
|
Posted: Fri Mar 18, 2005 11:02 am |
|
|
You can add:
int16 timer;
#BYTE timer=0xwhatever_you_want.
Then reading/writing 'timer', will access the whole 16bit value.
'#BYTE', locates a variable at a location, and if it does not exist, creates an int8. However if the variable already exixts of a larger type, it locates the whole larger variable at the location, and subsequent addresses. It is another useful shortcut. :-)
This is what you are seeing in the libraries with the 'unsigned long'.
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Mar 18, 2005 12:44 pm |
|
|
use #locate instead of #byte. |
|
|
Felix Althaus
Joined: 09 Sep 2003 Posts: 67 Location: Winterthur, Switzerland
|
|
Posted: Fri Mar 18, 2005 1:09 pm |
|
|
Hello
It's not exactly the same as orginally asked, but something similar:
What should I do, if the two bytes of a long aren't in consecutive oder. For example I want to do this:
long any_var;
any_var = counter;
counter is a long with low byte at 0xXY and high byte at 0xAB
How can I do this without #locate (I have 2.734). It seem's I can't get the trick...
Thanks
Felix |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Mar 18, 2005 8:06 pm |
|
|
If a variable is a long or 16bits then it must be consecutive. I said must be. |
|
|
bluetooth
Joined: 08 Jan 2005 Posts: 74
|
|
Posted: Fri Mar 18, 2005 8:14 pm |
|
|
Without asking why the bytes wouldn't be next to each other, I suggest you take a look at the CCS function make16() - you can build it up that way with the high and low bytes any where you want them.... |
|
|
Ttelmah Guest
|
|
Posted: Sat Mar 19, 2005 3:18 am |
|
|
Mark wrote: | use #locate instead of #byte. |
It depends what you are doing.
#locate, prevents C from using the area. Otherwise the two functions behave identically. For an area in the main data memory, #locate would be 'choice', but I'd not want to risk what might happen if you block C from using variables in the register area. Hopefully the compiler is 'smart' enough to ignore this, but I'd really say that #byte is the 'right' command to use in this case...
Best Wishes |
|
|
|