changing baud rate in dspic30F2011

Status
Not open for further replies.

Navid T

Full Member level 3
Joined
Jan 21, 2005
Messages
186
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Location
Iran
Visit site
Activity points
1,385
Hi friends
I use this code to read from a device with 115200 baud rate and write to other device those data with 9600 bps, but it doesn't change from 115200 to 9600 and sends data with 115200. what should i do to change baud rate just after reading data?
Code:
while(1)
	{
		
		U1BRG=15;
    	U1MODE=0X8400;                //ENABLE UART MODULE AND SOME SETTINGS
	    U1STA=0X8400;
		for(i=0;i<5000;i++);
		get_frame();
		accx = make16(input[2],input[3]);
		accy = make16(input[4],input[5]);
		accx /= 5;
		accy /= 5;
	
		U1BRG=191;
    	U1MODE=0X8400;                //ENABLE UART MODULE AND SOME SETTINGS
	    U1STA=0X8400;
		for(i=0;i<5000;i++);
		send(lsb(accx));
		send(lsb(accy));

	}//end of while(1)
 

not sure why it does not work! you could try disabling the UART by clearing the UARTEN bit in U1MODE (or writing 0 to U1MODE and U1STA) before changing the baud rate (possibly put a few Nops() after the U1MODE=U1STA=0)? If you read U1BRG after setting it is it the correct value?
 

Hi,

So you split the uart lines, sending the TX to one device and the RX to another?

I just want to understand the layout, before I offer advise.
 

I did it but still doesn't work! how should i see U1BRG ? i don't have debugger and there is not enough pin to see 16 bit variable.

---------- Post added at 20:03 ---------- Previous post was at 20:01 ----------

Yes bigdagguru but it's not problem, it works when i don't change baud rate, the problem is with changing baud rate.
 

I tried the following on a dsPIC33FJ256GP710 it switches OK
Code:
    int i;
    U2BRG = BAUDRATEREG1;
    U2MODE = 0;
    U2MODEbits.BRGH = BRGH2;
    U2STA = 0;
    U2MODEbits.UARTEN = 1;
    U2STAbits.UTXEN = 1;
    IFS1bits.U2RXIF = 0;
    UART2PrintString("\r\nat 115200baud\r\n");
    for(i=0;i<5000;i++);

    U2BRG = BAUDRATEREG2;
    U2MODE = 0;
    U2MODEbits.BRGH = BRGH2;
    U2STA = 0;
    U2MODEbits.UARTEN = 1;
    U2STAbits.UTXEN = 1;
    IFS1bits.U2RXIF = 0;
    UART2PrintString("\r\nat 9600baud\r\n");
if I run teraterm at 115200baud it shows the 115200baud message then rubbish, if I set termterm to 9600baud and reset the board it shows rubbish then the 9600baud message
 

there is no BRGH bit in 30F2011, i did another settings that you wrote here but still it doesn't change!! i reduced frequency too but problem didn't solve!
 

I did that work with 18F452 and it's ok but i still can't do this with 30F2011!
 

I don't have a dsPIC30F2011 but the following code runs OK on a dsPIC30F3011
Code:
  initUART1(9600L);								// initialise UART
  putUART1("\r\nAT 9600\r\n");		
  initUART1(115200L);								// initialise UART
  putUART1("\r\nAT 115200\r\n");
using
Code:
// oscillator mode fast internal RC at 7.37MHz
// U1BRG counter = Fcy (4 * 16 * baudrate) - 1
void initUART1(long int baudRate)
  {   
        U1MODE=0x8000;			// enable UART
        U1STA=0;				// clear status register
        // calculate baud rate counter  - add 0.5 to round to nearest integer
        U1BRG= (int) ((FCY/(4*16*baudRate) - 1)+0.5); //U1BRGcounter;	// Baudrate value 
        U1STA=U1STA | 0x400;	// enable TX 
  }
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…