Forums

UART interfacing pc and pic problem

Started by zheng December 10, 2008
Hi all, 

I am using dspic30f6010a interfacing pc with rs232. 
when I use hyperterminal, I can only receive something like
<<x<<||||||&uuml;&uuml;&uuml;&uuml;. why is that? And how can I send signal from
hyperterminal. 
when I use matlab, the program automatically terminates when I write:
s=serial('com1','BaudRate',9600); fopen(s) 
why is that? 
The pic program is as follow, anyone can help? 

#define PLLODE 1 
#define FCY XTFREQ*PLLODE 
#define BAUDRATE 9600 
#define BRGVAL ((FCY/BAUDRATE)/16)-1 
char Buf[80]; 
char * Receiveddata = Buf; 
void initU1(void) 
{ 
    U1BRG  = BRGVAL;        // initialize the baud rate generator 
    U1MODE = 0x8400;      // ENable, 8data, no parity, 1 stop 
    U1STA  = 0x0440;     // enable the Transmitter 
} //initU2 

//This is UART1 transmit ISR 
void __attribute__((__interrupt__)) _U1TXInterrupt(void) 
{ 
IFS0bits.U1TXIF = 0; 
} 
//This is UART1 receive ISR 
void __attribute__((__interrupt__)) _U1RXInterrupt(void) /*Declare 232
interrupt ISRs*/ 
{ 
   IFS0bits.U1RXIF = 0; 
//read the receive buffer till atleast one or more character can be read 
while (U1STAbits.URXDA) 
{ 
 ( *(Receiveddata)++) = U1RXREG; 
} 
} 
int main (void) 
{ 
// Datta to be transmitted 
char Txdata[] = {'M','i','c','r','o','c','h','i','p'}; 
initU1();/* Call function to initialize */ 
while(1) 
{ 
     while( U1STAbits.UTXBF);  // wait while Tx buffer full 
        U1TXREG = (unsigned int *)Txdata; 
  while(U1STAbits.TRMT) 
        ( *(Receiveddata)++) = U1RXREG; 
   } 
} 


>Hi all, > >I am using dspic30f6010a interfacing pc with rs232. >when I use hyperterminal, I can only receive something like ><<x<<||||||&uuml;&uuml;&uuml;&uuml;. why is that? And how can I send signal from >hyperterminal. >when I use matlab, the program automatically terminates when I write: >s=serial('com1','BaudRate',9600); fopen(s) >why is that? >The pic program is as follow, anyone can help? > >#define PLLODE 1 >#define FCY XTFREQ*PLLODE >#define BAUDRATE 9600 >#define BRGVAL ((FCY/BAUDRATE)/16)-1 >char Buf[80]; >char * Receiveddata = Buf; >void initU1(void) >{ > U1BRG = BRGVAL; // initialize the baud rate generator > U1MODE = 0x8400; // ENable, 8data, no parity, 1 stop > U1STA = 0x0440; // enable the Transmitter >} //initU2 > >//This is UART1 transmit ISR >void __attribute__((__interrupt__)) _U1TXInterrupt(void) >{ >IFS0bits.U1TXIF = 0; >} >//This is UART1 receive ISR >void __attribute__((__interrupt__)) _U1RXInterrupt(void) /*Declare 232 >interrupt ISRs*/ >{ > IFS0bits.U1RXIF = 0; >//read the receive buffer till atleast one or more character can be read
>while (U1STAbits.URXDA) >{ > ( *(Receiveddata)++) = U1RXREG; >} >} >int main (void) >{ >// Datta to be transmitted >char Txdata[] = {'M','i','c','r','o','c','h','i','p'}; >initU1();/* Call function to initialize */ >while(1) >{ > while( U1STAbits.UTXBF); // wait while Tx buffer full > U1TXREG = (unsigned int *)Txdata; > while(U1STAbits.TRMT) > ( *(Receiveddata)++) = U1RXREG; > } >}
While I am not familiar with the dspic UART, it looks to me like you might not be putting the *contents* of the Txdata buffer into the TXREG. I imagine you will need something like... char Txdata[] = {'M','i','c','r','o','c','h','i','p','\0'}; int i=0; while(Txdata[i]) { while( U1STAbits.UTXBF); // wait while Tx buffer full U1TXREG = Txdata[i]; i++; } You also appear to be handling received data in both the main loop and an ISR. You also appear to have no guard for when received data overruns your RX buffer.
zheng wrote:
> Hi all, > > I am using dspic30f6010a interfacing pc with rs232. > when I use hyperterminal, I can only receive something like > <<x<<||||||&uuml;&uuml;&uuml;&uuml;. why is that? And how can I send signal from > hyperterminal.
I don't want to start yet another "my terminal emulator is better than your terminal emulator" (so please don't give more suggestions unless the OP specifically asks for them!), but there is a wide consensus that hyperterminal is terrible, and is often the cause of problems when trying to do this sort of communication. Get Tera Term Pro from the link below. It's not the most powerful or sophisticated terminal emulator available, but it is small, free, easy-to-use, reliable, and a standard tool for many embedded developers. <http://hp.vector.co.jp/authors/VA002416/teraterm.html> Of course, there are *also* bugs in your program, as "srl100" pointed out. Tera Term won't fix them, but it *will* make your development and testing easier, and eliminate a cause of problems and irritation.
>>Hi all, >> >>I am using dspic30f6010a interfacing pc with rs232. >>when I use hyperterminal, I can only receive something like >><<x<<||||||&uuml;&uuml;&uuml;&uuml;. why is that? And how can I send signal from >>hyperterminal. >>when I use matlab, the program automatically terminates when I write: >>s=serial('com1','BaudRate',9600); fopen(s) >>why is that? >>The pic program is as follow, anyone can help? >> >>#define PLLODE 1 >>#define FCY XTFREQ*PLLODE >>#define BAUDRATE 9600 >>#define BRGVAL ((FCY/BAUDRATE)/16)-1 >>char Buf[80]; >>char * Receiveddata = Buf; >>void initU1(void) >>{ >> U1BRG = BRGVAL; // initialize the baud rate generator >> U1MODE = 0x8400; // ENable, 8data, no parity, 1 stop >> U1STA = 0x0440; // enable the Transmitter >>} //initU2 >> >>//This is UART1 transmit ISR >>void __attribute__((__interrupt__)) _U1TXInterrupt(void) >>{ >>IFS0bits.U1TXIF = 0; >>} >>//This is UART1 receive ISR >>void __attribute__((__interrupt__)) _U1RXInterrupt(void) /*Declare 232 >>interrupt ISRs*/ >>{ >> IFS0bits.U1RXIF = 0; >>//read the receive buffer till atleast one or more character can be
read
> >>while (U1STAbits.URXDA) >>{ >> ( *(Receiveddata)++) = U1RXREG; >>} >>} >>int main (void) >>{ >>// Datta to be transmitted >>char Txdata[] = {'M','i','c','r','o','c','h','i','p'}; >>initU1();/* Call function to initialize */ >>while(1) >>{ >> while( U1STAbits.UTXBF); // wait while Tx buffer full >> U1TXREG = (unsigned int *)Txdata; >> while(U1STAbits.TRMT) >> ( *(Receiveddata)++) = U1RXREG; >> } >>} > >While I am not familiar with the dspic UART, it looks to me like you
might
>not be putting the *contents* of the Txdata buffer into the TXREG. I >imagine you will need something like... > > char Txdata[] = {'M','i','c','r','o','c','h','i','p','\0'}; > int i=0; > while(Txdata[i]) > { > while( U1STAbits.UTXBF); // wait while Tx buffer full > U1TXREG = Txdata[i]; > i++; > } > >You also appear to be handling received data in both the main loop and
an
>ISR. > >You also appear to have no guard for when received data overruns your RX >buffer. > >
Hi, thanks for your suggestion. I tried just send a signal char like
> while( U1STAbits.UTXBF); // wait while Tx buffer full > U1TXREG = 'a';
And I run step by step and find that U1TXREG never read in anything.