Hi, I am new to PIC programming. I am using an explorer 16 board and I am able to use UART2 to send data from the PC to the PIC and back perfectly. E.G. Use hyperterminal to send keyboard commands to the PIC and then use these commands for UART1. I need to set up a second UART in order to communicate to a USB through a VNC1L chip using these commands. I am using a pictail plus to connect the USB board to the explorer board. I used an oscilloscope and saw the commands can be sent correctly to the VNC1L chip and the correct responses were coming back (I probed the actual PIC pin and they were there)..... However I am struggling to read these commands.... basically the buffer is saying it is empty. I know the correct commands are being sent as I checked them against another board. Initially I thought it was the baud rate however I don't think the commands could have even been sent back correctly if the baud rate was wrong. Both UART board rates are 19200. Code shown below. Clock rate is 8Mhz... Just let me know if I forgot any important information.
Thanks.
------------------------------------------------------------------------------
Thanks.
------------------------------------------------------------------------------
Rich (BB code):
#define SYSCLK 8000000
#define BOARD_VERSION4
#include <P24FJ256GB110.h>
#include "delay.h"
#include <stdio.h>
#pragma udata
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI & DISUVREG_OFF & PLL_96MHZ_ON & IOL1WAY_ON)
void Init_Uart(void);
void UART2PutChar(char Ch);
int i=0;
char data,data2;
int main(void)
{
Init_Uart();
while (1) // repeat continuously
{
while(U1STAbits.URXDA==1) //If input detected from USB
{
UART2PutChar('R'); //Print results to hyperterminal
data = U1RXREG;
UART2PutChar(data);
UART2PutChar('\n');
}
while(U2STAbits.URXDA==1) // Keyboard command automatically sent to USB
{
data2 = U2RXREG; //Take from buffer
UART2PutChar(data2);
U1TXREG = data2;
if(!U1STAbits.UTXBF)
{
U1TXREG = data2; //Send to USB
}
}
}
}
void Init_Uart(void)
{
//PIC TO PC UART INITIALISATION
TRISFbits.TRISF5 = 0;
TRISFbits.TRISF4 = 1;
U2BRG = 12;
U2MODE = 0;
U2STA = 0;
U2MODEbits.UARTEN = 1;
U2STAbits.UTXEN = 1;
// reset RX flag
IFS1bits.U2RXIF = 0;
// UART TO USB INITIALISATION
TRISBbits.TRISB2 = 0;
TRISAbits.TRISA15 = 1;
TRISDbits.TRISD14 = 1;
TRISDbits.TRISD15 = 0;
U1BRG = 12;
U1MODE = 0;
U1STA = 0;
U1MODEbits.UARTEN = 1;
U1STAbits.UTXEN = 1;
U1MODEbits.RXINV=1;
U1MODEbits.UEN1 = 1;
U1MODEbits.UEN0 = 0;
// U4STAbits.UTXINV=1;
// reset RX flag
IFS0bits.U1RXIF = 0;
//RS-232 FROM COMPUTER TO PIC
RPINR19bits.U2RXR = 10; //U2RX = RP19
RPOR8bits.RP17R = 5; //RP25 = U2TX
//USB UART I/O PINS
RPINR18bits.U1RXR = 35; //U1RX
RPOR6bits.RP13R = 3; //U1TX
//RPINR18bits.U1RXR = 30; //U1RX
//RPOR8bits.RP16R = 3; //U1TX
RPINR18bits.U1CTSR = 43; // U1CTS
RPOR2bits.RP5R = 4; //U1RTS
}
void UART2PutChar(char Ch)
{
// wait for empty buffer
while(U2STAbits.UTXBF == 1);
U2TXREG = Ch;
}
Last edited by a moderator: