Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Pic24fj128ga010 programming to UART & I2C

Status
Not open for further replies.

rakshith

Junior Member level 2
Joined
Aug 5, 2012
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,568
Hi,
I'm working with PIC24FJ128GA010 micro controller, I should establish communication from PC to UART n I2C..
But i'm really not able to configure the UART,...
I'm working on "EXPLORE 16" board..
And i'm using c-30 compiler..
Can anyone help me here,..

thank you,
rakshith
 

The following is some example code from the text:

Programming 16-Bit PIC Microcontrollers in C, Second Edition: Learning to Fly the PIC 24

You may want to consider purchasing a copy, it covers the PIC24 and Explorer 16 Dev Board quite well.

Code:
/*
** 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


BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating
the Microchip code examples cover the UART and I2C
**broken link removed**
 

Re: Pic24fj128ga010 programming to UART &amp; I2C

by using this program can i successfully build this??
n also i'm using c-30 compiler .. does this work on it??

- - - Updated - - -

I'm not able to execute the program using these sample codes..
Actually telme wat to do with these sample codes..
 

Re: Pic24fj128ga010 programming to UART &amp; I2C

this works on an explorer 16 with a PIC24FJ128GA010
Code:
// 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);
}

main would look like
Code:
// 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");
}
 

Re: Pic24fj128ga010 programming to UART &amp; I2C

Hey,
bigdogguru i can execute ur program . thanks a lot..
but i still need ur help , i wanned to kno as what could be the output as??? in ur program??

- - - Updated - - -

Horace 1 : i wanned to kno whr to add this ?
i mean the 2 prog which u'v posted here??
if yes,telme whr to add,..
thank u.
 

The code I posted simply echos the character set from the PC to the PIC24 back to the PC.

If you see the '>' character in the terminal emulation program at the PC side, you know transmission is successful from the PIC24.

As you type various characters they should appear at the PC side.

You can modify the code to transmit virtual any text you wish from the PIC24.

Does this answer you question?

BigDog
 
Re: Pic24fj128ga010 programming to UART &amp;amp;amp;amp;amp; I2C

Horace 1 : i wanned to kno whr to add this ?
i mean the 2 prog which u'v posted here??
if yes,telme whr to add,..
thank u.

- - - Updated - - -

Horace 1 : i wanned to kno whr to add this ?
i mean the 2 prog which u'v posted here??
if yes,telme whr to add,..
thank u.

- - - Updated - - -

actually i'm not getting anything as output ,,n u told me as getting ">" as an output,which s not.. instead i'm getting "///", that too running it once again... is tat i need to keep building it again n again??
And whatever i'm typing on pc i'm not able to get those characters as echo..
and what is meant by "pps.h"

- - - Updated - - -

bigdogguru :actually i'm not getting anything as output ,,n u told me as getting ">" as an output,which s not.. instead i'm getting "///", that too running it once again... is tat i need to keep building it again n again??
And whatever i'm typing on pc i'm not able to get those characters as echo..
and what is meant by "pps.h"...???

- - - Updated - - -

bigdogguru : as soon as i run ur program i cn see outpun in DOCKLIGHT as - "$$$//"
 

Have you configured your PC terminal program for 115200 baud rate and hardware flow control?

The pps.h header file is used in Horace's example code, not in the code I posted. I would have to examine the example code Horace posted.


BigDog
 

Re: Pic24fj128ga010 programming to UART &amp; I2C

bigdog : i need to kno which option to select in "flow control mode" ..
i'v 2 options as
1-hardware handshaking RTS/CTS (sequence mode )
2-hardware handshaking RTS/CTS(single byte mode)

- - - Updated - - -

and also need to kno what are to be made for configuration bits...????
 

I have no idea, I'm not familiar with your app, try them both to see which works.

The configuration bits are set in the code:

Code:
_CONFIG1( ICS_PGx2 & JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF) 
_CONFIG2( FNOSC_PRIPLL & OSCIOFNC_OFF & FCKSM_CSDCMD & POSCMOD_HS)

BigDog
 
Re: Pic24fj128ga010 programming to UART &amp; I2C

bigdog :

i wanned to kno the formula tat u'v used to calculate for baud rate ... u'v mentioned as 34..!
any things to be done .. i'm still stuck to establish communication wit UART...n still i should config i2c...
 

try this program which is a single file with all the code in it for an Explorer 16 with a PIC24FJ128GA010

it should blink the LEDs, output to the UART2 and read characters from UART2
test it with hyperterm or a similar terminal emulator at 57600 baud no handshaking
Code:
// 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());

   }
}

the statement
Code:
#ifdef _RP0R
    iPPSOutput(OUT_PIN_PPS_RP10, OUT_FN_PPS_U2TX);      // TXD
    iPPSInput (IN_FN_PPS_U2RX,IN_PIN_PPS_RP17);       // RXD
#endif
tests if the device has PPS (such as a PIC24FJ256GB110) where one has to configure which pins are connected to UART2 Rx and Tx, see
https://techtrain.microchip.com/webseminars/ArchivedDetail.aspx?Active=129
the header file pps.h contains information about the PPS on the chip - it is not required for the PIC24FJ128GA010
 
Last edited:

Re: Pic24fj128ga010 programming to UART &amp;amp; I2C

Bigdog : Thanks ur above comment was helpful.

Now can u tell me exactly wat to do??
ryt now i'm trying to execute ur prog ,but i'm not getting ">" as an output,
instead i tried some other commands as input ,even though i'm not getting ..


Rakshith

- - - Updated - - -

BigDog:
in ur prog if i include-
"#include<PIC24FJ128GA010.h>"

i do get an error as " no such directories found "
n i can define "#include<uart.h>"

Wat to do with this???

- - - Updated - - -

horace 1 :

I really can't trace ur program..
I really need a simpler prog as HotDog..
Can u ???
 

removed blinking LEDs, printf(), etc
Code:
#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());
   }
}
if should prompt > for you to enter characters which it then echos
 
Horace 1 :

is there anything else which i need to config???
And i'm not getting any output as u said...!

Anything to be done..!
n plz make my prog to execute...
 

the code runs OK on my Explorer 16 with a PIC24FJ128GA010
how are you connected to the board? do any characters appear?
I use a RS232 to USB converter and teraterm pro as a terminal emulator
when I run it looks like (it pronpts > then I type characters which it echos)
teraterm.jpg
 
Re: Pic24fj128ga010 programming to UART &amp; I2C

horace 1 :
Thank u..
But the problem is i'm using "DOCKLIGHT".
n in my explorer 16 board there r few other IC's like MAX3232CSE""....
do i even need to config that???..!

Hmmm...?

- - - Updated - - -

horace 1:

Thanks a lot !
with few modifications ur prog is working..!!


But i need to kno whats that - "define SYSCLK 32000000UL"

How did u get that...???/
 

Re: Pic24fj128ga010 programming to UART &amp; I2C

But i need to kno whats that - "define SYSCLK 32000000UL"

How did u get that...???/
the baudrate register calculation
Code:
     unsigned long int BAUDRATEREG = (((SYSCLK/2)+(8ul*BAUDRATE))/16/BAUDRATE-1);
uses the system clock SYSCLK which is derived from the oscillator set by
Code:
// Setup configuration bits
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL )
POSCMOD_HS selects the high speed oscillator 8MHz in this case
FNOSC_PRIPLL with *4 PLL module - which gives a SYSCLK of 32MHz

hence
Code:
#define SYSCLK  32000000UL
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top