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.
Re: Pic24fj128ga010 programming to UART & I2C

Horace 1:
Thanks a lot ..!
I'm done with UART, n now
In this same project i need to configure "I2C"...


Can u help me by getting me a program..!???
 

Re: Pic24fj128ga010 programming to UART & I2C

Horace 1:
Thanks a lot ..!
I'm done with UART, n now
In this same project i need to configure "I2C"...


Can u help me by getting me a program..!???

there are several examples of I2C for the PIC24 on Microchip's example code page
**broken link removed**

what are you trying to communicate with?
 
Re: Pic24fj128ga010 programming to UART & I2C

Horace 1 :

thanks a lot..!
right now i'm able to execute the program..!!!

But,I still need to configure I2C for the same stuff's..>!!

Can u help me here???



Horace 1 :

i'm actually working on a project in which I need to config both UART & I2C ports so that using Microcontroller PIC24FJ128GA010 mounted on Explorer 16 board..
Aft doing this stuffs i should use this to get information about the smart battery lik, if i give some codes in pc,i should receive some info abt the battery....
(0x66 code - battery charge %age, etc...)
I do've those codes...

But it would be helpful if i get a prog as i got it for UART????

Do u'v any...........??? !
 

Re: Pic24fj128ga010 programming to UART & I2C

horace 1:

should i club this prog with the UART???
O only this prog mentioned above makes my project done???

If i need to club, any changes to be made ??
............
 

Re: Pic24fj128ga010 programming to UART & I2C

horace 1 :

with the link given above i cn't program cuz i'm using UART in-between i2c n pc...

So, i need a program in which i config both UART n i2c so that i can send command codes of the battery to via pc-UART-i2c-smart battery the back from samrt battery-i2c-UART-pc...!..........????
 

Re: Pic24fj128ga010 programming to UART & 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");
}


Could you help me How to use "UART2GetString(char *str , int length)" in "// main program code" , please ?
I am a beginner so it's too difficult to use that ! Thank you so much !
Sincerely !
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top