I am using the PIC18F67K40 on the MikroElectronika Clicker 2, and I am trying to configure EUSART4 to output a string to the terminal. As you can see in the Clicker 2 User Manual EUSART4 TX and RX are connected to the FT232RL chip.
I am using MPLAB X 4.20, XC8 v1.45 compiler, and the ICD3. I set "+NVMREG in the Project->Properties->XC8 Linker->Additional options->Errata box" as instructed in another post.
The following code is supposed to print to the terminal and toggle both of the LEDs on the Clicker 2. The LEDs toggle but nothing appears on the terminal (via Tera Term). I have not tried to probe these pins because they are not connected to any of the break out pins. I did try to setup EUART3 with the same settings as I set for EUSART4 (and made sure to edit my putchar() with the updated peripheral) and probed with a Digilent Analog Discovery 2 on RB5-RX and RB4-TX. I did not see any output. This leads me to believe that my understanding of the way the peripheral is configured is flawed, but there is a very good chance I am missing something else as well. Thank you for your help.
- Code: Select all
#pragma config FEXTOSC = HS
#pragma config RSTOSC = EXTOSC_4PLL
#define _XTAL_FREQ 64000000L
#define LED1 LATDbits.LATD7
#define LED2 LATHbits.LATH3
#include <xc.h>
#include <stdio.h>
void putch(char);
void main(void)
{
INTCONbits.GIE = 0;
INTCONbits.PEIE = 0;
TRISCbits.TRISC0 = 0; //TX4
TRISCbits.TRISC1 = 1; //RX4
TRISDbits.TRISD7 = 0; //LED1 on Clicker2
TRISHbits.TRISH3 = 0; //LED2 on Clicker2
RC0PPS = 0x12; //RC0->EUSART4:TX4
RX4PPS = 0x11; //RC1->EUSART4:RX4
//*****USART4*********************************************************/
SP4BRGH = 0x00; //64MHz Fosc and 9600 baud; SYNC = 0, BRGH = 0, BRG16 = 0
SP4BRGL = 0x67;
TXSTA4bits.BRGH = 0; //High Baud Rate Select bit; 1 = High speed
BAUDCON4bits.BRG16 = 0; //16-Bit Baud Rate Register Enable bit
TXSTA4bits.SYNC = 0; //EUSART Mode Select bit; 0 = Asynchronous mode
RCSTA4bits.SPEN = 1; //Serial Port Enable bit
TXSTA4bits.TX9 = 0; //8bit transmission
TXSTA4bits.TXEN = 1; //Transmit Enable bit
RCSTA4bits.RX9 = 0; //8bit reception
RCSTA4bits.CREN = 1; //Enables Receiver
LED1 = 0;
LED2 = 1;
while (1) {
printf("Hello World!");
__delay_ms(1000);
LED1 = ~LED1;
LED2 = ~LED2;
}
}
void putch(char data)
{
while (!PIR4bits.TX4IF) //USART4 - Terminal
continue;
TXREG4 = data;
}
Edit: I applied all of the recommended changes. The code works as intended. Thank you all for your help.