17.02.2015 Views

CCS C Compiler Manual PCB / PCM / PCH

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Common Questions & Answers<br />

How can I use two or more RS-232 ports on one PIC®?<br />

The #USE RS232 (and I2C for that matter) is in effect for GETC, PUTC, PRINTF and KBHIT<br />

functions encountered until another #USE RS232 is found.<br />

The #USE RS232 is not an executable line. It works much like a #DEFINE.<br />

The following is an example program to read from one RS-232 port (A) and echo the data to<br />

both the first RS-232 port (A) and a second RS-232 port (B).<br />

#USE RS232(BAUD=9600, XMIT=PIN_B0, RCV=PIN_B1)<br />

void put_to_a( char c ) {<br />

put(c);<br />

}<br />

char get_from_a( ) {<br />

return(getc()); }<br />

#USE RS232(BAUD=9600, XMIT=PIN_B2,RCV=PIN_B3)<br />

void put_to_b( char b ) {<br />

putc(c);<br />

}<br />

main() {<br />

char c;<br />

put_to_a("Online\n\r");<br />

put_to_b("Online\n\r");<br />

while(TRUE) {<br />

c=get_from_a();<br />

put_to_b(c);<br />

put_to_a(c);<br />

}<br />

}<br />

The following will do the same thing but is more readable and is the recommended method:<br />

#USE RS232(BAUD=9600, XMIT=PIN_B0, RCV=PIN_B1, STREAM=COM_A)<br />

#USE RS232(BAUD=9600, XMIT=PIN_B2, RCV=PIN_B3, STREAM=COM_B)<br />

main() {<br />

char c;<br />

fprintf(COM_A,"Online\n\r");<br />

fprintf(COM_B,"Online\n\r");<br />

while(TRUE) {<br />

c = fgetc(COM_A);<br />

fputc(c, COM_A);<br />

fputc(c, COM_B);<br />

}<br />

}<br />

How can the RB interrupt be used to detect a button press?<br />

The RB interrupt will happen when there is any change (input or output) on pins B4-B7. There is<br />

only one interrupt and the PIC® does not tell you which pin changed. The programmer must<br />

determine the change based on the previously known value of the port. Furthermore, a single<br />

button press may cause several interrupts due to bounce in the switch. A debounce algorithm<br />

will need to be used. The following is a simple example:<br />

#int_rb<br />

rb_isr() {<br />

355

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

Saved successfully!

Ooh no, something went wrong!