strahd_von_zarovich
Advanced Member level 4
Hi , i am trying to use serial port of my pic(32MX460512L). I can receive bytes but i can't send them to MyComputer. I use Coolterm as a terminal program. I use example code but i didn't understand why it doesn't work . When i type a character on terminal , i want my pic to send it back. Here my example code :
Code:
#include <plib.h>
#if defined (__32MX360F512L__) || (__32MX460F512L__) || (__32MX795F512L__) || (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__)
// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 80 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define SYS_FREQ (80000000L)
#pragma config ICESEL = ICS_PGx2
#elif defined (__32MX220F032D__) || (__32MX250F128D__)
// Configuration Bit settings
// SYSCLK = 48 MHz (8MHz Crystal / FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 48 MHz (SYSCLK / FPBDIV)
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
#pragma config FPLLMUL = MUL_24, FPLLIDIV = DIV_2, FPLLODIV = DIV_2, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#define SYS_FREQ (48000000L)
#endif
#define GetPeripheralClock() (SYS_FREQ/(1 << OSCCONbits.PBDIV))
#define GetInstructionClock() (SYS_FREQ)
#if defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX460F512L__)
#define UART_MODULE_ID UART1 // PIM is connected to Explorer through UART1 module
#else
#define UART_MODULE_ID UART2 // PIM is connected to Explorer through UART2 module
#endif
#define DESIRED_BAUDRATE (9600) //The desired BaudRate
void WriteString(const char *string);
char myChar;
int main(void)
{
#if defined (__32MX220F032D__) || defined (__32MX250F128D__)
PPSInput(2,U2RX,RPB5); // Assign RPB5 as input pin for U2RX
PPSOutput(4,RPB0,U2TX); // Set RPB0 pin as output for U2TX
#elif defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX470F512L__)
PPSInput(2,U1RX,RPF4); // Assign RPF4 as input pin for U1RX
PPSOutput(2,RPF5,U1TX); // Set RPF5 pin as output for U1TX
#endif
// Configure the device for maximum performance but do not change the PBDIV
// Given the options, this function will change the flash wait states, RAM
// wait state and enable prefetch cache but will not change the PBDIV.
// The PBDIV value is already set via the pragma FPBDIV option above.
SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
// Explorer-16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled.
mJTAGPortEnable(DEBUG_JTAGPORT_OFF);
PORTClearBits(IOPORT_B,BIT_10|BIT_11|BIT_12|BIT_13 );
PORTSetPinsDigitalOut (IOPORT_B, BIT_10|BIT_11|BIT_12|BIT_13);
//mPORTBClearBits(BIT_10); // Turn off RA7 on startup.
//mPORTASetPinsDigitalOut(BIT_10); // Make RA7 as output.
// Configure UART module, set buad rate, turn on UART, etc.
UARTConfigure(UART_MODULE_ID, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART_MODULE_ID, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART_MODULE_ID, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART_MODULE_ID, GetPeripheralClock(), DESIRED_BAUDRATE);
UARTEnable(UART_MODULE_ID, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
// Configure UART RX Interrupt
INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID), INT_ENABLED);
INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_PRIORITY_LEVEL_2);
INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID), INT_SUB_PRIORITY_LEVEL_0);
// Enable multi-vector interrupts
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
INTEnableInterrupts();
WriteString("***version 1.0***\r\n");
// Let interrupt handler do the work
while (1);
}
// Helper functions
void WriteString(const char *string)
{
while(*string != '\0')
{
while(!UARTTransmitterIsReady(UART_MODULE_ID))
;
UARTSendDataByte(UART_MODULE_ID, *string);
string++;
while(!UARTTransmissionHasCompleted(UART_MODULE_ID))
;
}
}
void PutCharacter(const char character)
{
while(!UARTTransmitterIsReady(UART_MODULE_ID))
;
UARTSendDataByte(UART_MODULE_ID, character);
while(!UARTTransmissionHasCompleted(UART_MODULE_ID))
;
}
// UART 1 interrupt handler, set at priority level 2
#if defined (__32MX430F064L__) || (__32MX450F256L__) || (__32MX460F512L__)
void __ISR(_UART_1_VECTOR, ipl2) IntUart1Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID)))
{
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));
PutCharacter(UARTGetDataByte(UART_MODULE_ID));
myChar = UARTGetDataByte(UART_MODULE_ID);
if(myChar=='a'){
PORTToggleBits(IOPORT_B , BIT_10|BIT_11|BIT_12|BIT_13);
WriteString(myChar);
}
//mPORTBToggleBits(BIT_10);
}
// We don't care about TX interrupt
if (INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)))
{
INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
}
}
#else
// UART 2 interrupt handler, set at priority level 2
void __ISR(_UART2_VECTOR, ipl2) IntUart2Handler(void)
{
// Is this an RX interrupt?
if(INTGetFlag(INT_SOURCE_UART_RX(UART_MODULE_ID)))
{
// Clear the RX interrupt Flag
INTClearFlag(INT_SOURCE_UART_RX(UART_MODULE_ID));
// Echo what we just received.
PutCharacter(UARTGetDataByte(UART_MODULE_ID));
// Toggle LED to indicate UART activity
mPORTBToggleBits(BIT_10);
}
// We don't care about TX interrupt
if ( INTGetFlag(INT_SOURCE_UART_TX(UART_MODULE_ID)) )
{
INTClearFlag(INT_SOURCE_UART_TX(UART_MODULE_ID));
}
}
#endif