/*
** COMM2
** UART2 RS232 asynchronous communication demonstration code
*/
#include <p24Fxxxx.h>
#if defined( __PIC24FJ256GB110__ ) || defined( __PIC24FJ256GB106__ )
#define PLL_96MHZ_ON 0xF7FF
_CONFIG1(ICS_PGx2 & JTAGEN_OFF & FWDTEN_OFF) // JTAG off, watchdog timer off
_CONFIG2( FNOSC_PRIPLL & PLL_96MHZ_ON & PLLDIV_DIV2 & OSCIOFNC_OFF & FCKSM_CSDCMD & POSCMOD_XT)
#else // PIC24FJ128GA010 and PIC24FJ256GA110
_CONFIG1( ICS_PGx2 & JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF)
_CONFIG2( FNOSC_PRIPLL & OSCIOFNC_OFF & FCKSM_CSDCMD & POSCMOD_HS)
#endif
//void init_PPS (int use_sdi1);
// I/O definitions for the Explorer16
#define CTS _RF12 // Clear To Send, input, HW handshake
#define RTS _RF13 // Request To Send, output, HW handshake
#define TRTS TRISFbits.TRISF13 // tris control for RTS pin
// timing and baud rate calculations
#define BRATE 34 // 115200 baud (BREGH=1)
#define U_ENABLE 0x8008 // enable the UART peripheral (BREGH=1)
#define U_TX 0x0400 // enable transmission
// initialize the UART2 serial port
void initU2 ( void)
{
U2BRG = BRATE;
U2MODE = U_ENABLE;
U2STA = U_TX;
TRTS = 0; // make RTS output
RTS = 1; // set RTS default status
} // initU2
// send a character to the UART2 serial port
int putU2 ( int c)
{
while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2
// wait for a new character to arrive to the UART2 serial port
char getU2 ( void)
{
RTS = 0; // assert Request To Send !RTS
while ( !U2STAbits.URXDA); // wait for a new character to arrive
RTS = 1;
return U2RXREG; // read the character from receive buffer
}// getU2
main()
{
char c;
// 0. init the PPS configuration
// init_PPS (0);
// 1. init the UART2 serial port
initU2();
// 2. prompt
putU2( '>');
// 3. main loop
while ( 1)
{
// 3.1 wait for a character
c = getU2();
// 3.2 echo the character
putU2( c);
} // main loop
}// main
// UART2.c - for PIC24.
#include "uart2.h"
#ifdef _RP0R
#include "pps.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#define BRG_DIV2 16
#define BRGH2 0
// initialize UART
// Input: baud rate
// Output: 1 for OK, -1 if baud rate error > 2%
int UART2Init(const unsigned long int BAUDRATE)
{
unsigned long int BAUDRATEREG = (((SYSCLK/2)+(8ul*BAUDRATE))/BRG_DIV2/BAUDRATE-1);
unsigned long int BAUD_ACTUAL = ((SYSCLK/2)/BRG_DIV2/(BAUDRATEREG+1));
unsigned long int BAUD_ERROR = ((BAUD_ACTUAL > BAUDRATE) ? BAUD_ACTUAL-BAUDRATE : BAUDRATE-BAUD_ACTUAL);
unsigned long int BAUD_ERROR_PRECENT = ((BAUD_ERROR*100+BAUDRATE/2)/BAUDRATE);
__C30_UART=2; // if UART2 set for printf output to this UART
// reconfigure the I/O pins if required
#ifdef _RP0R
iPPSOutput(OUT_PIN_PPS_RP10, OUT_FN_PPS_U2TX); // TXD
iPPSInput (IN_FN_PPS_U2RX,IN_PIN_PPS_RP17); // RXD
#endif
U2BRG = BAUDRATEREG; // set up baudrate
U2MODE = 0; // clear mode register
U2MODEbits.BRGH = BRGH2; //divider is 16
U2STA = 0; // clear status
U2MODEbits.UARTEN = 1; // enable UART
U2STAbits.UTXEN = 1; // enable reansmit
_U2RXIF = 0; // clear interrupt flag
if (BAUD_ERROR_PRECENT > 2)
{ printf("UART2 baud rate %lu error %lu%%\n", BAUDRATE, BAUD_ERROR_PRECENT); return -1; }
printf("UART2 baud rate %lu set up\n", BAUDRATE);
return 1;
}
//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UART2CharReady()
{
return _U2RXIF; // none zero if UART has received data
}
//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UART2GetChar()
{
while(! _U2RXIF); // wait for data
_U2RXIF = 0; // clear interrupt flag
return U2RXREG;
}
// Function:UART GetString - read a string until \n received or length reached
void UART2GetString(char *str , int length)
{
char ch=0;
while(ch != '\n')
if(UART2CharReady())
{
ch=*str=UART2GetChar();
str++;
*str=0;
if(--length<1) return;
}
}
// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar(const char ch )
{
while(U2STAbits.TRMT == 0); // wait for transmit ready
U2TXREG = ch; // transmit character
}
//Function:UART PrintString - prints a string of characters to the UART.
void UART2PrintString(const char *str )
{
char c;
while( (c = *str++) )
UART2PutChar(c);
}
// Setup configuration bits
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL )
// main program code
int main(void)
{
UART2Init(57600ul); // Setup the UART
UART2PrintString("\n\nPIC24 TEST1, check timer\n\r");
}
_CONFIG1( ICS_PGx2 & JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF)
_CONFIG2( FNOSC_PRIPLL & OSCIOFNC_OFF & FCKSM_CSDCMD & POSCMOD_HS)
// test1.c - blink LEDs, test printf,
#include "p24Fxxxx.h"
#include <stdio.h>
#include "libpic30.h" // to change default printf output, see __C30_UART below
#ifdef _RP0R
#include "pps.h"
#endif
// Setup configuration bits
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL )
#define SYSCLK 32000000UL
#define BRG_DIV2 16
#define BRGH2 0
// initialize UART
// Input: baud rate
// Output: 1 for OK, -1 if baud rate error > 2%
int UART2Init(const unsigned long int BAUDRATE)
{
unsigned long int BAUDRATEREG = (((SYSCLK/2)+(8ul*BAUDRATE))/BRG_DIV2/BAUDRATE-1);
unsigned long int BAUD_ACTUAL = ((SYSCLK/2)/BRG_DIV2/(BAUDRATEREG+1));
unsigned long int BAUD_ERROR = ((BAUD_ACTUAL > BAUDRATE) ? BAUD_ACTUAL-BAUDRATE : BAUDRATE-BAUD_ACTUAL);
unsigned long int BAUD_ERROR_PRECENT = ((BAUD_ERROR*100+BAUDRATE/2)/BAUDRATE);
__C30_UART=2; // if UART2 set for printf output to this UART
// reconfigure the I/O pins if required
#ifdef _RP0R
iPPSOutput(OUT_PIN_PPS_RP10, OUT_FN_PPS_U2TX); // TXD
iPPSInput (IN_FN_PPS_U2RX,IN_PIN_PPS_RP17); // RXD
#endif
U2BRG = BAUDRATEREG; // set up baudrate
U2MODE = 0; // clear mode register
U2MODEbits.BRGH = BRGH2; //divider is 16
U2STA = 0; // clear status
U2MODEbits.UARTEN = 1; // enable UART
U2STAbits.UTXEN = 1; // enable reansmit
_U2RXIF = 0; // clear interrupt flag
if (BAUD_ERROR_PRECENT > 2)
{ printf("UART2 baud rate %lu error %lu%%\n", BAUDRATE, BAUD_ERROR_PRECENT); return -1; }
printf("UART2 baud rate %lu set up\n", BAUDRATE);
return 1;
}
//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UART2CharReady()
{
fflush(stdout);
return _U2RXIF; // none zero if UART has received data
}
//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UART2GetChar()
{
fflush(stdout);
while(! _U2RXIF); // wait for data
_U2RXIF = 0; // clear interrupt flag
return U2RXREG;
}
// Function: UART flush - flush input data
void UART2flush(void)
{
#ifdef UART2_RX_INTERRUPT
U2rxIn= U2rxOut= U2rxBuffer;
U2number=U2error=0;
#endif
while(UART2CharReady())UART2GetChar();
}
// Function:UART GetString - read a string until \n received or length reached
void UART2GetString(char *str , int length)
{
char ch=0;
while(ch != '\n')
if(UART2CharReady())
{
ch=*str=UART2GetChar();
str++;
*str=0;
if(--length<1) return;
}
}
// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar(const char ch )
{
while(U2STAbits.TRMT == 0); // wait for transmit ready
U2TXREG = ch; // transmit character
}
//Function:UART PrintString - prints a string of characters to the UART.
void UART2PrintString(const char *str )
{
char c;
while( (c = *str++) )
UART2PutChar(c);
}
// main program code
int main(void)
{
int i=0;
long int j=0;
// Setup the digital outputs, e.g. for LEDs
AD1PCFG = 0xffff; // Setup PortA IOs as digital
TRISA=0;
while (i++<20) // blink LEDs ON/OFF for a few seconds
{
for(j=0;j<200000l;j++) ; PORTA=~PORTA;
}
UART2Init(57600ul); // Setup the UART
UART2PrintString("\n\nPIC24 TEST UART\n\r");
printf("\n\nPIC24 printf() test\n\rtype characters on keyboard");
// blink LEDs and check UART2 input
while (1)
{
for(j=0;j<200000l;j++) ; PORTA=~PORTA;
if(UART2CharReady())printf("UART2 %c\n", (char) UART2GetChar());
}
}
#ifdef _RP0R
iPPSOutput(OUT_PIN_PPS_RP10, OUT_FN_PPS_U2TX); // TXD
iPPSInput (IN_FN_PPS_U2RX,IN_PIN_PPS_RP17); // RXD
#endif
#include "p24Fxxxx.h"
// Setup configuration bits
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL )
#define SYSCLK 32000000UL
// initialize UART
// Input: baud rate
void UART2Init(const unsigned long int BAUDRATE)
{
unsigned long int BAUDRATEREG = (((SYSCLK/2)+(8ul*BAUDRATE))/16/BAUDRATE-1);
U2BRG = BAUDRATEREG; // set up baudrate
U2MODE = 0; // clear mode register
U2MODEbits.BRGH = 16; //divider is 16
U2STA = 0; // clear status
U2MODEbits.UARTEN = 1; // enable UART
U2STAbits.UTXEN = 1; // enable reansmit
_U2RXIF = 0; // clear interrupt flag
}
//Function:UART CharReady - return true if there is a new byte in UART reception buffer.
char UART2CharReady()
{
return _U2RXIF; // none zero if UART has received data
}
//Function:UART GetChar : waits for a byte to be received. It then returns that byte.
char UART2GetChar()
{
while(! _U2RXIF); // wait for data
_U2RXIF = 0; // clear interrupt flag
return U2RXREG;
}
// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar(const char ch )
{
while(U2STAbits.TRMT == 0); // wait for transmit ready
U2TXREG = ch; // transmit character
}
// main program code
int main(void)
{
UART2Init(57600ul); // Setup the UART
// blink LEDs and check UART2 input
UART2PutChar('>');
while (1)
{
if(UART2CharReady()) UART2PutChar(UART2GetChar());
}
}
the baudrate register calculationBut i need to kno whats that - "define SYSCLK 32000000UL"
How did u get that...???/
unsigned long int BAUDRATEREG = (((SYSCLK/2)+(8ul*BAUDRATE))/16/BAUDRATE-1);
// Setup configuration bits
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL )
#define SYSCLK 32000000UL
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?