I'm trying to communicate with a PIC32MX795F512H using UART but I get scrambled signs in my terminal.
I know it's a timing/clock issue but I have no idea where and how to fix it. I am not using an external crystal and nothing is connected to the PIC (except the necessary minimum (caps, wires and a resistor)).
The code:
#include <p32xxxx.h>
#include <plib.h>
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define DESIRED_BAUDRATE (9600)
#define SYSCLK (80000000L)
#define CONFIG1 (ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON)
#define CONFIG2 (ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF)
#define CONFIG3 (ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15)
#define CONFIGPORT (ENABLE_AN15_ANA)
#define CONFIGSCAN (SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN4 | SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14)
unsigned char data;
int pbClk;
void initializeADC(){
CloseADC10(); // Generally, you should disable the ADC before setup.
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF);
OpenADC10( CONFIG1, CONFIG2, CONFIG3, CONFIGPORT, CONFIGSCAN);
// Setup for the ADC10.
EnableADC10(); // Enables the ADC10.
while(!mAD1GetIntFlag() ) {}; // mAD1GetIntFlag() checks the interrupt flag for the AD10.
// Waits till a conversion is finished so that there's
// values in the ADC result registers.
}
void initializeLEDBar(){
TRISDCLR = 0xFF; // Sets pins RD0 to RD7 as digital outputs.
}
void initializeUART()
{
// Optimize PIC32 performance and return peripheral bus frequency
pbClk=SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
// Enable UART1 and set baud rate to DESIRED_BAUDRATE=9600
OpenUART1(UART_EN, UART_RX_ENABLE | UART_TX_ENABLE, pbClk/16/DESIRED_BAUDRATE-1);
while(BusyUART1()); // Wait until the UART module is free
putsUART1("Turning the potentiometer will change characters on screen!");
}
// ==============================================================================================
int main(){
initializeUART(); // Initialize the UART module
initializeADC(); // Initialize the ADC10
initializeLEDBar(); // Initialize pins RD0 - RD7 as digital outputs
while(1){
// Map the 10 bit analog value to a value between 33 and 126, and bit-mask it to 8-bits
data = ((ReadADC10(0)/11) + 33) & 0xFF;
LATD = data; // Latch the data to the port D pins
while(BusyUART1()); // Wait until UART2 module is not busy
putcUART1(data); // Transmit 'data' through UART
putcUART1('13'); // Transmit '13' (carriage return)
}
return 1;
}