modified uart example from book

modified uart example from book

Postby indigohedgehog » Mon Jul 14, 2014 6:56 pm

I'm trying the following code, which comes on "Programming 16-bit microcontroller in C". I added a vector wih some chars and then looped thru it. But I get only garbage.
The original example code works fine, but the loop only sends garbage, instead of 0x30 and 0x31.
So,
How do I sync cpu and serial?
A good book/reference for pic & uart?
Is there a book, guide or anything to pic architecture and patterns?

Code: Select all
//removed headers

// 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;
 char s[] = {"0"......"1"} //1000 random 1 and 0
 int sLength = 1000;
int i =0;
 // 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
//original code
// c = getU2();
   // 3.2 echo the character
// putU2( c);
for(i=0;i<sLength;i++)
{
  putU2(s[0]);
};
 } // main loop
}// main
 
indigohedgehog
 
Posts: 1
Joined: Mon Jul 14, 2014 6:38 pm

Re: modified uart example from book

Postby ric » Tue Jul 15, 2014 12:40 am

Your loop is only sending the first character over and over
Code: Select all
putU2(s[0]);


I suspect you don't have the baudrate set correctly.
You haven't mentioned what PIC you are using, or what terminal program you are running on your PC.
You also have not mentioned what hardware you are running on. Do you have an RS232 buffer (i.e. MAX232), between the PIC's TX pin and the PC's receive pin?

I would suggest the hardware handshaking code (RTS and CTS) is an unnecessary complication before you get basic transmission working.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: modified uart example from book

Postby ric » Tue Jul 15, 2014 1:50 am

indigohedgehog wrote:...
A good book/reference for pic & uart?
Is there a book, guide or anything to pic architecture and patterns?
...

There are some good tutorials at http://www.gooligum.com.au/tutorials.html
but they are aimed at "base" and "mid range" PIC devices.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 11 guests

cron