17.02.2015 Views

CCS C Compiler Manual PCB / PCM / PCH

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

byte changes;<br />

changes = last_b ^ port_b;<br />

last_b = port_b;<br />

if (bit_test(changes,4 )&& !bit_test(last_b,4)){<br />

//b4 went low<br />

}<br />

if (bit_test(changes,5)&& !bit_test (last_b,5)){<br />

//b5 went low<br />

}<br />

.<br />

.<br />

.<br />

delay_ms (100); //debounce<br />

The delay=ms (100) is a quick and dirty debounce. In general, you will not want to sit in an ISR<br />

for 100 MS to allow the switch to debounce. A more elegant solution is to set a timer on the first<br />

interrupt and wait until the timer overflows. Do not process further changes on the pin.<br />

How do I directly read/write to internal registers?<br />

A hardware register may be mapped to a C variable to allow direct read and write capability to<br />

the register. The following is an example using the TIMER0 register:<br />

#BYTE timer 0 = 0x 01<br />

timer0= 128; //set timer0 to 128<br />

while (timer 0 ! = 200); // wait for timer0 to reach 200<br />

Bits in registers may also be mapped as follows:<br />

#BIT T 0 IF = 0x 0B.2<br />

.<br />

.<br />

.<br />

while (!T 0 IF); //wait for timer0 interrupt<br />

Registers may be indirectly addressed as shown in the following example:<br />

printf ("enter address:");<br />

a = gethex ();<br />

printf ("\r\n value is %x\r\n", *a);<br />

The compiler has a large set of built-in functions that will allow one to perform the most common<br />

tasks with C function calls. When possible, it is best to use the built-in functions rather than<br />

directly write to registers. Register locations change between chips and some register<br />

operations require a specific algorithm to be performed when a register value is changed. The<br />

compiler also takes into account known chip errata in the implementation of the built-in<br />

functions. For example, it is better to do set_tris_ A (0); rather than *0x 85 =0;<br />

How do I do a printf to a string?<br />

The following is an example of how to direct the output of a printf to a string. We used the \f to<br />

indicate the start of the string.<br />

356

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!