ARQuattr
Member level 2
- Joined
- Sep 7, 2010
- Messages
- 52
- Helped
- 11
- Reputation
- 22
- Reaction score
- 11
- Trophy points
- 1,288
- Activity points
- 1,880
I have some basic code I'm trying to get working on a PIC18F25K80, which I've had running on various PIC16 variants with no issues. There is a function "writeStringToUART" that is intended to send out strings via UART2. This function gives no output (I'm watching the TX line on a logic analyzer), although when I step through it in the debugger it appears to work properly and I can see the correct characters supposedly being sent.
I also have a direct TXREG2 = 0xAA and that comes out of the TX line no problem. That would seem to suggest a compiler or function call issue. I tried multiple variations on the code (checking TRMT instead of TX2IF, checking before setting TXREG2 or after, using pointers referencing of the string, or array notation), and I got the same result - writeStringToUART doesn't send characters while just setting TXREG2 directly does. As I said the writeStringToUART has been used on previous projects (after some register name changes) with no problems. One difference is that I just installed MPLABX v1.9 while before I was using MPLAB 8. Also the compiler now is XC8 v1.2 and before it was HiTechC 9.82.
One other thing I haven't figured which might be related is that I'm setting OSCCON to 0x30 to get 4MHz, when I feel it should be 0x50. It's running 4x as fast, but as far as I can tell I don't have any PLL or scalers enabled.
I appreciate any thoughts you have.
I also have a direct TXREG2 = 0xAA and that comes out of the TX line no problem. That would seem to suggest a compiler or function call issue. I tried multiple variations on the code (checking TRMT instead of TX2IF, checking before setting TXREG2 or after, using pointers referencing of the string, or array notation), and I got the same result - writeStringToUART doesn't send characters while just setting TXREG2 directly does. As I said the writeStringToUART has been used on previous projects (after some register name changes) with no problems. One difference is that I just installed MPLABX v1.9 while before I was using MPLAB 8. Also the compiler now is XC8 v1.2 and before it was HiTechC 9.82.
One other thing I haven't figured which might be related is that I'm setting OSCCON to 0x30 to get 4MHz, when I feel it should be 0x50. It's running 4x as fast, but as far as I can tell I don't have any PLL or scalers enabled.
I appreciate any thoughts you have.
Code:
void initPorts(void) {
//Set oscillator options
OSCCON = 0x30; //4MHz (this is not correct - it should be 0x50 for 4MHz - why?)
//Analog configuration
ANCON0 = 0b00010000; //AIN4 is used as analog input
ADCON1 = 0b00000111; //AIN8,9, and 10 are used as analog input
ADCON2 = 0xBC; //20 TAD, Fosc/4
ADON = 1; //enable ADC
//Digital configurations
PORTA = 0b00000000; // Initial state of PORTA
TRISA = 0b11111111; // Set PORTA pin directions: all input
PORTB = 0b01001000; // Initial state of PORTB (driver and receiver disabled, TX high, other outputs off)
TRISB = 0b10010011; // Set PORTB pin directions: RB2, RB3, RB5, RB6 output, all others input
PORTC = 0b00000000; // Initial state of PORTC (turn LEDs, outputs off)
TRISC = 0b11100000; // Set PORTC pin directions: RC0-RC4 output, RC5-RC7 input
}
void initComms()
{
/* Serial port initialization */
TXSTA2bits.BRGH = 1;
TXSTA2bits.SYNC = 0;
SPBRGH2 = 0;
SPBRG2 = 25; //((FCY/16)/BAUD) - 1; // set baud to 9600 FCY=4000000
BAUDCON2 = 0x08; //BRGH16 = 1
TXSTA2bits.TXEN = 1; //Enables transmitter
RCSTA2bits.CREN = 1; //Enables receiver
RCSTA2bits.SPEN = 1; //Enable UART
}
void writeStringToUART (const char* msg)
{
while (msg[0] != 0) {
while (PIR3bits.TX2IF == 0) {}
TXREG2 = msg[0];
msg++;
}
}
void main(void)
{
/***Initialization***/
initPorts(); // Initialize ports to startup state
initComms(); // Initialize the serial port
while(1) {
writeStringToUART ("test\n");
DelayMs(250);
while (PIR3bits.TX2IF == 0) {}
TXREG2 = 0xAA;
DelayMs(250);
}
}