View previous topic :: View next topic |
Author |
Message |
octopuss83
Joined: 06 Nov 2011 Posts: 13
|
CCPR1 Register |
Posted: Wed Nov 09, 2011 11:45 am |
|
|
Hello I need a piece of code in "C" to put a value in CCPR1 Register (16Bit).
CCPR1 correspond to
CCPR1L Capture/Compare/PWM Register 1 Low Byte (LSB)
CCPR1H Capture/Compare/PWM Register 1 High Byte (MSB)
which address is
CCPR1L 13h
CCPR1H 14h
More generally how to properly declare a 16bit register in order to use it in a program. Same question for an 8bit register. An example with the 16bit register CCPR1.
Thank you for your response. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Nov 09, 2011 4:14 pm |
|
|
Code: |
#BYTE CCPR1L=getenv("sfr:CCPR1L")
#BYTE CCPR1H=getenv("sfr:CCPR1H")
#WORD CCPR1=CCPR1L
|
The 'getenv' function, returns the address of a register on the chip you are compiling for (if it exists). Makes code portable if you change chips latter....
#BYTE, locates an 8bit register (or another type - more later), at a location.
#WORD locates a 16bit register at a location.
However you can also use:
Code: |
#BYTE CCPR1L=getenv("sfr:CCPR1L")
#BYTE CCPR1H=getenv("sfr:CCPR1H")
int16 CCPR1;
#BYTE CCPR1=CCPR1L
|
Which generates an int16 register, and then locates it at the specified location with #byte. Key thing here is that you can put any register type at a location this way (int32, float etc.....).
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 09, 2011 4:18 pm |
|
|
Look in the .h file for your PIC. (You didn't give your PIC).
CCS already has a definition for the 16-bit CCP register in the .h file.
You can load it with a line of code, as shown below:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
//======================================
void main(void)
{
CCP_1 = 0x1234;
while(1);
}
|
Then to see what the compiler is doing, set the List file format to Symbolic
and re-compile. Then look at the .LST file:
Code: |
.................... CCP_1 = 0x1234;
0014: MOVLW 12
0016: MOVWF CCPR1H
0018: MOVLW 34
001A: MOVWF CCPR1L
|
The definitions for the 8-bit "L" and "H" registers of the CCP are also in
the .h file for your PIC. You can see how to declare registers by looking
at these examples in the .h file.
Also, if you have the CCS IDE version of the CCS compiler, then you
can make a .h file with all the register definitions already done.
See this thread for instructions:
http://www.ccsinfo.com/forum/viewtopic.php?t=40862
Edit: I see that Ttelmah and I were typing in an answer at the same time.
He beat me, but I'll leave my post up anyway. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Nov 09, 2011 4:29 pm |
|
|
I forgot to point to the standard defines, so the posts complement one another rather nicely.
Best Wishes |
|
|
octopuss83
Joined: 06 Nov 2011 Posts: 13
|
|
Posted: Thu Nov 10, 2011 6:07 am |
|
|
woow , great . .... it's works fine and it's very clean code
Thanks all
_________________ ______________________
-- Octopuss --- |
|
|
|