PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 12, 2011 4:56 pm |
|
|
Here is work-around code for UART2 in the 18F46K22, for vs. 4.109.
I only have a 18F45K22, so I used that for testing. It works.
The CCS #use rs232() statement doesn't work for UART2 for vs. 4.109.
So, don't use it. Don't even put it into your program. (It's OK for UART1).
Set the baud rate for UART2 with a #define statement.
For UART2, use the putc2(), getc2(), and kbhit2() functions shown below.
Also, you must call the init_uart2() function one time at start of your
program. You can't use printf with "streams" for UART2. You can
use printf, but you must use the re-direction feature of CCS to send the
output to the putc2() function. All of this is demonstrated below.
The program below displays the following output in the terminal window.
I pressed "asdf" a few times on my keyboard to get the 2nd line.
Quote: |
Hello World
asdfasdfasdfasdf
|
Code: |
#include <18F45K22.h>
#fuses INTRC_IO,NOWDT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)
#define BAUDRATE2 9600
#byte BAUDCON2 = 0xF70
#byte RCSTA2 = 0xF71
#byte TXSTA2 = 0xF72
#byte TXREG2 = 0xF73
#byte RCREG2 = 0xF74
#byte SPBRG2 = 0xF75
#byte SPBRGH2 = 0xF76
#byte PIR3 = 0xFA4
#bit TX2IF = PIR3.4
#bit RC2IF = PIR3.5
#bit CREN2 = RCSTA2.4
#byte ANSELD = 0xF3B
#bit ANSD6 = ANSELD.6
#bit ANSD7 = ANSELD.7
#byte TRISD = 0xF95
#bit TRISD6 = TRISD.6
#bit TRISD7 = TRISD.7
//---------------------------------
// The putc2() must be declared "inline" to prevent
// vs. 4.109 from inserting garbage into the code.
#inline
void putc2(int8 c)
{
while(!TX2IF);
TXREG2 = c;
}
//---------------------------------
// The getc2() must be declared "inline" to prevent
// vs. 4.109 from inserting garbage into the code.
#inline
int8 getc2(void)
{
int8 temp;
int8 retval;
while(!RC2IF);
temp = RCSTA2;
retval = RCREG2;
if(bit_test(temp, 1))
{
CREN2 = 0;
CREN2 = 1;
}
return(retval);
}
//---------------------------------
#define kbhit2() (RC2IF)
//---------------------------------
#int_rda2
void rda2_isr(void)
{
char c;
c = getc2();
putc2(c);
}
//-----------------------------------
void init_uart2(void)
{
// Set the hardware UART2 pins to be digital i/o pins.
ANSD6 = 0;
ANSD7 = 0;
// Set the hardware UART2 pins to be inputs, per the PIC
// data sheet.
TRISD6 = 1;
TRISD7 = 1;
// Setup the UART2 baud rate generator and the other
// control registers.
SPBRG2 = (getenv("CLOCK") / (BAUDRATE2 * 16)) -1;
TXSTA2 = 0x26;
RCSTA2 = 0x90;
}
//======================================
void main(void)
{
init_uart2(); // Call this to setup UART2
// Printf statements for the 2nd UART must be done
// in the following way, with re-direction to putc2.
printf(putc2, "Hello World\n\r");
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
while(1);
}
|
If you have any more bugs, I think you need to upgrade the compiler.
I don't want to do any more extensive work-arounds like this.
(I don't work for CCS). |
|