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.

UART Implementation for dsPIC33FJ16GS504........

Status
Not open for further replies.

smart1

Newbie level 4
Joined
Mar 13, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,327
Dear all.... I am trying to executing the UART module of dspic33fj16gs504. But its not working... The code is as bellow...Please revert back.
Thanks in advance.
================================================================================

Code:
#include "p33FJ16GS504.h"

// Configuration Bit Settings
_FOSCSEL(FNOSC_FRCPLL)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)

#define CLOCK_FREQ 40000000 // Assuming 5.00MHz and X8 PLL
#define BAUD 19200

void U1_Putch(unsigned char Data)
{
	while (U1STAbits.UTXBF);
	while(1)
	U1TXREG = Data;
}

void setup_uart(void)
{
 	U1MODE = 0x0000;
 	U1STA = 0x0000;

 	U1BRG = ((CLOCK_FREQ/16)/BAUD) - 1;	// set baudrate to BAUD rate

 	IEC0bits.U1RXIE = 1;

 	U1MODEbits.UARTEN=1;		// UART1 is enabled
 	U1STAbits.UTXEN = 1;           // Initiate transmission
} 

void main()
{
	TRISBbits.TRISB1=1;				// Used for Rx as input
	TRISBbits.TRISB2=0;				// Used for Tx as output

	// Remmappable INPUT PIN configuration for UART1
	RPINR18bits.U1RXR=1;			// PORT PIN NO. 32, This pin is for Rx	
	// Remmappable OUTPUT PIN configuration for UART1
	RPOR1bits.RP2R=0x000011;		// PORT PIN NO. 33, This pin is for Tx
	
	setup_uart();
	U1_Putch('A');	
	while(1);
}

void __attribute__((__interrupt__)) _U1TXInterrupt(void)
{
	IFS0bits.U1TXIF = 0;	// clear interrupt flag
}

void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
	IFS0bits.U1RXIF = 0;	// clear interrupt flag
}

================================================================================
 
Last edited by a moderator:

Here is the correct code:
Code:
#include "p33FJ16GS504.h"

// Configuration Bit Settings
_FOSCSEL(FNOSC_FRCPLL)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)

#define FCY 20000000
//40MHz clock is set up as FRC = 7.37MHz, M=44, N1=2, N2=4
//Thus FCY = 40/2 = 20MHz
#define BAUD 19200

void U1_Putch(unsigned char Data)
{
while (U1STAbits.UTXBF);
U1TXREG = Data;
}

void setup_uart(void)
{
AD1PCFGL = 0x1FFF; //Disable ADC

U1MODE = 0x0000;
U1STA = 0x0000;

U1BRG = ((FCY/16)/BAUD) - 1; // set baudrate to BAUD rate

IEC0bits.U1RXIE = 1;

U1MODEbits.UARTEN=1; // UART1 is enabled
U1STAbits.UTXEN = 1; // Initiate transmission
}

void setup_clock(void){
	CLKDIVbits.PLLPOST = 1; //N2 = 4;
	CLKDIVbits.PLLPRE = 0; //N1 = 2;
	PLLFBD = 42; //M = 44
//Value of Fin is 7.37MHz
//M=44, N1=2 and N2=4 gives ~40MHz

while(OSCCONbits.LOCK!=1) {};  // Wait for PLL to lock
}

void main()
{
unsigned int i;
setup_clock();

//Not needed:
//TRISBbits.TRISB1=1; // Used for Rx as input
//TRISBbits.TRISB2=0; // Used for Tx as output

// Remmappable INPUT PIN configuration for UART1
RPINR18bits.U1RXR=1; // RB1 This pin is for Rx
// Remmappable OUTPUT PIN configuration for UART1
RPOR1bits.RP2R=3; // RB2 This pin is for Tx

setup_uart();

for (i=0;i<40000;i++); //Wait for some time

U1_Putch('A');

while(1);

}

void __attribute__((__interrupt__)) _U1TXInterrupt(void)
{
IFS0bits.U1TXIF = 0; // clear interrupt flag
}

void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0; // clear interrupt flag
}

There are errors in your code.

Code:
_FOSCSEL(FNOSC_FRCPLL)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)
#define CLOCK_FREQ 40000000 // Assuming 5.00MHz and X8 PLL
#define BAUD 19200

U1BRG = ((CLOCK_FREQ/16)/BAUD) - 1; // set baudrate to BAUD rate

You can't assume 5.00MHz clock. The default frequency of the internal oscillator is 7.37MHz. So, you have to configure it for 40MHz, keeping in mind the 7.37MHz. I've done that through the setup_clock() sub-routine/procedure.
FCY = FOSC/2. So, if you have 40MHz clock, FCY = 20MHz. You have to use this value when calculating the value for the required baud rate.
Code:
void setup_clock(void){
	CLKDIVbits.PLLPOST = 1; //N2 = 4;
	CLKDIVbits.PLLPRE = 0; //N1 = 2;
	PLLFBD = 42; //M = 44
//Value of Fin is 7.37MHz
//M=44, N1=2 and N2=4 gives ~40MHz

while(OSCCONbits.LOCK!=1) {};  // Wait for PLL to lock
}

Code:
RPOR1bits.RP2R=0x000011; // PORT PIN NO. 33, This pin is for Tx

This means that RP2R is assigned a value of 17, but you need to assign 3:
Code:
RPOR1bits.RP2R=3; // RB2 This pin is for Tx

Code:
TRISBbits.TRISB1=1; // Used for Rx as input
TRISBbits.TRISB2=0; // Used for Tx as output
The above 2 lines aren't required, as, when you use the UART module, the TRIS settings are overriden.

You need to disable the ADC multiplexed pins from their analogue circuitry to use them as digital pins. I've done this by completely setting all ADC multiplexed pins as digital:
Code:
AD1PCFGL = 0x1FFF; //Disable ADC

I think you can understand the rest.

Hope this helps.
Tahmid.
 

Dear Tahmid,
Thanks a lot for your reply.
But your code also not working dear.... The same code i put in the dsPIC33F but there is no data transfer after connecting it to the hyper terminal.
Just one statement i changed as follow..........
ADPCFG=0x1FFF; //Disable ADC.
Please guide me... If we are missing something.
Thanks and regards
smart1
Here is the correct code:
Code:
#include "p33FJ16GS504.h"

// Configuration Bit Settings
_FOSCSEL(FNOSC_FRCPLL)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)

#define FCY 20000000
//40MHz clock is set up as FRC = 7.37MHz, M=44, N1=2, N2=4
//Thus FCY = 40/2 = 20MHz
#define BAUD 19200

void U1_Putch(unsigned char Data)
{
while (U1STAbits.UTXBF);
U1TXREG = Data;
}

void setup_uart(void)
{
AD1PCFGL = 0x1FFF; //Disable ADC

U1MODE = 0x0000;
U1STA = 0x0000;

U1BRG = ((FCY/16)/BAUD) - 1; // set baudrate to BAUD rate

IEC0bits.U1RXIE = 1;

U1MODEbits.UARTEN=1; // UART1 is enabled
U1STAbits.UTXEN = 1; // Initiate transmission
}

void setup_clock(void){
	CLKDIVbits.PLLPOST = 1; //N2 = 4;
	CLKDIVbits.PLLPRE = 0; //N1 = 2;
	PLLFBD = 42; //M = 44
//Value of Fin is 7.37MHz
//M=44, N1=2 and N2=4 gives ~40MHz

while(OSCCONbits.LOCK!=1) {};  // Wait for PLL to lock
}

void main()
{
unsigned int i;
setup_clock();

//Not needed:
//TRISBbits.TRISB1=1; // Used for Rx as input
//TRISBbits.TRISB2=0; // Used for Tx as output

// Remmappable INPUT PIN configuration for UART1
RPINR18bits.U1RXR=1; // RB1 This pin is for Rx
// Remmappable OUTPUT PIN configuration for UART1
RPOR1bits.RP2R=3; // RB2 This pin is for Tx

setup_uart();

for (i=0;i<40000;i++); //Wait for some time

U1_Putch('A');

while(1);

}

void __attribute__((__interrupt__)) _U1TXInterrupt(void)
{
IFS0bits.U1TXIF = 0; // clear interrupt flag
}

void __attribute__((__interrupt__)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0; // clear interrupt flag
}

There are errors in your code.

Code:
_FOSCSEL(FNOSC_FRCPLL)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)
#define CLOCK_FREQ 40000000 // Assuming 5.00MHz and X8 PLL
#define BAUD 19200

U1BRG = ((CLOCK_FREQ/16)/BAUD) - 1; // set baudrate to BAUD rate

You can't assume 5.00MHz clock. The default frequency of the internal oscillator is 7.37MHz. So, you have to configure it for 40MHz, keeping in mind the 7.37MHz. I've done that through the setup_clock() sub-routine/procedure.
FCY = FOSC/2. So, if you have 40MHz clock, FCY = 20MHz. You have to use this value when calculating the value for the required baud rate.
Code:
void setup_clock(void){
	CLKDIVbits.PLLPOST = 1; //N2 = 4;
	CLKDIVbits.PLLPRE = 0; //N1 = 2;
	PLLFBD = 42; //M = 44
//Value of Fin is 7.37MHz
//M=44, N1=2 and N2=4 gives ~40MHz

while(OSCCONbits.LOCK!=1) {};  // Wait for PLL to lock
}

Code:
RPOR1bits.RP2R=0x000011; // PORT PIN NO. 33, This pin is for Tx

This means that RP2R is assigned a value of 17, but you need to assign 3:
Code:
RPOR1bits.RP2R=3; // RB2 This pin is for Tx

Code:
TRISBbits.TRISB1=1; // Used for Rx as input
TRISBbits.TRISB2=0; // Used for Tx as output
The above 2 lines aren't required, as, when you use the UART module, the TRIS settings are overriden.

You need to disable the ADC multiplexed pins from their analogue circuitry to use them as digital pins. I've done this by completely setting all ADC multiplexed pins as digital:
Code:
AD1PCFGL = 0x1FFF; //Disable ADC

I think you can understand the rest.

Hope this helps.
Tahmid.
 

Did you construct the hardware properly? Is the MAX232 section correctly configured? Did you use PORTB pin 2 for TX pin?
 

Dear Tahmid,
Very good morning.
I am using MAX3232 instead of MAX232 but both are functionally same. I am using the same pin for TX i. e. PORTB2 (i.e. RB2 PIN number 33 from datasheet).
Here is the whole hardware setup,

PC_TX -------> R1IN (MAX3232)

R1OUT (MAX3232) --------> RX_dsP (RB1, i.e. PIN No. 32)

PC_RX <------- T2OUT (MAX3232)

T2IN (MAX323) <---------- TX_dsp (RB2, i.e. PIN No. 33)
Used one GND and one Vcc pins as per DB9 connector specification
PIN No. 5 is GND (DB9) and PIN No. 15 is GND (MAX3232)
PIN No. 16 is Vcc (MAX3232).
Also used 1uf capacitors on PIN 1 & 3 , 2 n GND, 4 & 5 and 6 & GND of MAX3232.
Thanks.
Please let me know, if i'm missing something.
Warm regards,
smart1

Did you construct the hardware properly? Is the MAX232 section correctly configured? Did you use PORTB pin 2 for TX pin?
 

Dear all,
Please post UART working code for dsPIC33FJ16GS504 or give some suggestion on the code bellow.
Thanks.
Best Regards
smart1

Dear Tahmid,
Very good morning.
I am using MAX3232 instead of MAX232 but both are functionally same. I am using the same pin for TX i. e. PORTB2 (i.e. RB2 PIN number 33 from datasheet).
Here is the whole hardware setup,

PC_TX -------> R1IN (MAX3232)

R1OUT (MAX3232) --------> RX_dsP (RB1, i.e. PIN No. 32)

PC_RX <------- T2OUT (MAX3232)

T2IN (MAX323) <---------- TX_dsp (RB2, i.e. PIN No. 33)
Used one GND and one Vcc pins as per DB9 connector specification
PIN No. 5 is GND (DB9) and PIN No. 15 is GND (MAX3232)
PIN No. 16 is Vcc (MAX3232).
Also used 1uf capacitors on PIN 1 & 3 , 2 n GND, 4 & 5 and 6 & GND of MAX3232.
Thanks.
Please let me know, if i'm missing something.
Warm regards,
smart1
 

Add this at the beginning of the code:
Code:
_FWDT(FWDTEN_OFF)

Do you not get any output on the terminal?

For MAX3232, you can use 0.1uF capacitors. So, use 0.1uF non-electrolytic capacitors between pins 1 and 3, between pin 2 and ground. between pin 6 and ground and between pins 4 and 5. Also connect a 0.1uF capacitor between VCC and ground.

What is the value of VCC - what voltage did you use for VCC?

Hope this helps.
Tahmid.
 

Dear Tahmid,
Thanks a lot for your help on this issue.
As your directed i used following code.
==================================
_FWDT(FWDTEN_OFF)
==================================
But still it is not working. I did the capacitor connection as u mentioned in the your reply. I connected 0.1uF capacitors on the PIN 1 and3,4 and 5, 2 and GND, and 6 and GND. Connected same capacitor on the VCC too. I am using the VCC=3.3V.
But its not transferring any data from dspic33f to PC. Help me on the save, if possible. Could u please let me know exact place to get help on the micro chip controller. If you have any working code for dsPIC33FJ16GS504, please do send it.
Thanks a lot.
Best regards
smart1

Add this at the beginning of the code:
Code:
_FWDT(FWDTEN_OFF)

Do you not get any output on the terminal?

For MAX3232, you can use 0.1uF capacitors. So, use 0.1uF non-electrolytic capacitors between pins 1 and 3, between pin 2 and ground. between pin 6 and ground and between pins 4 and 5. Also connect a 0.1uF capacitor between VCC and ground.

What is the value of VCC - what voltage did you use for VCC?

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top