Read to an array from serial port and a can baud rate calculation

Status
Not open for further replies.

magvitron

Active Member
Hi, I am building a CAN n/w using pic 18f25k80. Can any one help in the following
1. Calculating the baud rate of CAN
my current setting:
Code:
//Setup the Baud Rate @16 Mhz.
    BRGCON1 = 0xc3;
    BRGCON2 = 0x9e;
    BRGCON3 = 0x03;
2. How to read from the UART to an array
my current code (this is not working) ie there is no update in array.
code:
Code:
/**************************
Initialize the serial port
@ 160000 and 230400bps
***************************
Requires :none
Returns : none
**************************/
void uart_init()
{//Enables Serial port   
/*RCSTA RECIEVER CONTROL REGISTER*/   
RCSTA1bits.SPEN = 1;
RCSTA1bits.RX9=0;//8 bit reception
RCSTA1bits.CREN = 1; //enable the reciever

  TRISCbits.TRISC7=0;
  TRISCbits.TRISC6=0;
/*TXSTAx: TRANSMIT STATUS AND CONTROL REGISTER */
TXSTA1bits.BRGH=1;//baud rate high bits     
TXSTA1bits.SYNC=0;//assynchronous mode       
TXSTA1bits.TX9=0; //8 bit reception
TXSTA1bits.TRMT=1;//reset the buffer
TXSTA1bits.CSRC=0;// Master mode  (clock generated internally from BRG)
/*BAUDCONx: BAUD RATE CONTROL REGISTER*/
BAUDCON1bits.BRG16=1;//16 bit Baud rate is enabled

SPBRGH1 = 0;
SPBRG1 = 16;//2;
//(((_XTAL_FREQ/baud)/4)-1);
//FCY/64)/BAUD) - 1;
// set baud to 9600  FCY=20000000
//Turn On the Tx and Rx
TXSTA1bits.TXEN=1;
RCSTA1bits.CREN=1;
}
///////////////////////////
//Function for read to a buffer
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_read2buff()
{
int count;
while(uart_read()>0)
{
  rxr[count]=uart_read();
  uart_char(uart_read());
  count=count+1;
  uart_num(count);
  if (count>63)
  break;
 
}
uart_string("out of here");
rxr[count]='\0';
count=0;
}
//////////////////////////
//Function for sending char
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_char(char data)
{
TXREG1= data;
  while(!TXSTA1bits.TRMT);
}
///////////////////////////
//Function for reciveing from serial
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
char uart_read()
{
while(!RC1IF);
RC1IF=0;
return RCREG1;
//RCSTA1bits.CREN = 0;
//RCSTA1bits.CREN = 1; 

}


can any one point out how to do it?

P.S. I'm using XC8 compiler basic.
 
There are few problems with your code.

I assume "uart_read()" and "uart_char()" functions work.
Here are my comments about the "uart_read2buff()" -function.
C:
void uart_read2buff()
{
int count;  // You should initialize this to zero here.
while(uart_read()>0)  // This reads a character from the uart and the just throws it away.
{
  rxr[count]=uart_read();  // This should work, I assume rxr is a global variable defined somewhere.
  uart_char(uart_read());  // This reads the next character from uart and echos it back.. this character is never written to the buffer.
  count=count+1;
  uart_num(count); // I have no idea what this should do
  if (count>63)
  break;

}
uart_string("out of here");
rxr[count]='\0';
count=0; // This is useless
}

Here is my version of the same function.
C:
void uart_read2buff()
{
int count;
char ch;

count = 0;
ch = uart_read();
while ((ch > 0) && (count<64)) {
  rxr[count] = ch; // write to buffer
  count=count+1; // increment count
  uart_char(ch); // echo
  ch = uart_read(); // read next char
}
rxr[count]='\0';
uart_string("out of here");
}

EDIT: I edited my version of the code.. better that way. It is still missing the "uart_num(count);" You can add that where eve you like.. I have no idea what it does so I left it out.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…