Embedded Programming Tips for CCS C

From Mech
Revision as of 01:18, 9 December 2008 by Hwang (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Writing Directly to a Memory Address

When configuring the peripherals of the PIC, one usually checks the PIC's datasheet and manually writes the bits corresponding to the desired settings to the special function registers (SFRs). The CCS library attempts to make things easier by wrapping up the SFRs into function calls such as setup_adc, etc. However, some functionality may be "lost in translation," or the documentation may be so vague that it's easier just to the write the correct bits directly to the SFRs. Unfortunately, the SFR names used in the datasheet are not defined in CCS's device .h files, so we'll have to define them ourselves.

As an example, the CCS library doesn't have a function that will write to PR5 (Timer5 period match register) for devices (such as the PIC18F4431) that have them. However, we can look in the datasheet in the "Special Function Registers" section to find memory address of the SFR we want.

Note: When writing to 16-bit SFRs which may be changing (such as a timer register), it is important to write to the high byte first and write to the low byte second.

//put variables at memory location

  1. byte PR5L=0xF90 //PR5 low byte
  2. byte PR5H=0xF91 //PR6 high byte

... PR5H = 10000/256; PR5L = 10000%256;

Can also use pointers: int8* PR5H; int8* PRHL; PR5H = 0xF91; PR5H = 0xF90;

  • PR5H = 10000/256;
  • PR5L = 10000%256;