|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 11, 2011 11:26 am |
|
|
Version 4.109 has a lot of bugs with the 18F46K22 and 18F45K22.
I tested using the 2nd UART with vs. 4.116 and it works.
In the test program shown below, I get the following output:
Quote: |
Hi there
asdfasdfasdf
|
I just typed "asdf" a few times on the keyboard and it echoes the
characters correctly, so it's working.
Remember, this is with vs. 4.116. You are using vs. 4.109.
Code: |
#include <18F45K22.h>
#fuses INTRC_IO,NOWDT,PUT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_D6, rcv=PIN_D7, ERRORS, stream=COM2)
#int_rda2
void rda2_isr(void)
{
char c;
c = fgetc(COM2);
fputc(c, COM2);
}
//======================================
void main(void)
{
fprintf(COM2, "Hi there\n\r");
enable_interrupts(INT_RDA2);
enable_interrupts(GLOBAL);
while(1);
} |
Is there anyway you can upgrade to vs. 4.116 ? I think all your RS232
problems with the 18F46K22 would go away.
If you can't upgrade, I can look at the problem in vs. 4.109 and try to
come up with a solution. But it would be easier if you upgraded. |
|
|
daniel82
Joined: 05 Jan 2011 Posts: 11
|
|
Posted: Tue Jan 11, 2011 8:25 pm |
|
|
My PC is locked by IT dept, I will upgrade it but really take time to get all the internal approval, the project really urgent, can you help me to look into first? Thank you very much! |
|
|
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). |
|
|
daniel82
Joined: 05 Jan 2011 Posts: 11
|
|
Posted: Thu Jan 13, 2011 1:33 am |
|
|
Thank you very very much |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|