Page 1 of 1

UART1_Read_Text problem

Posted: 09 Aug 2012 09:37
by enryv
Hi, I have linked a PIC16F887 and an Arduino mega 2560 with serial uart (Rx e Tx pin).....I have some problem with UART1_Read_Text...I don't succeed in entering to Dat[1] ..It should be 1 (second data send by arduino)...the buzzer don't play...
Thanks to all
This are my codes:
CODE FOR ARDUINO:

Code: Select all

Int received=0;
void loop()
{
while (received==0)
{
Serial.println(3,DEC);
    delay(100);
    Serial.println(1,DEC);
    delay(100);
    Serial.println(2,DEC);
     delay(100);
    Serial.println(8,DEC);
    received=1;
}
}


CODE FOR PIC16F887:

Code: Select all

Int cont=0;
char dat[15];
char car[1];
int id;
void main()
{
  ANSEL  = 0;                       // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                     // Disable comparators
  C2ON_bit = 0;
 TRISB0_bit = 1;                   // set RB0 pin as input
 TRISD  = 0xF7;                    // Configure RD3 as output
  UART1_Init(9600);
  delay_ms(100);
Sound_Init(&PORTD, 3);


while (cont==0)
{if (UART1_Data_Ready()==1)
{
       UART1_Read_Text(Dat, "8", 20);
       cont=1;
       
}
}
car[0]=Dat[1];  
id=atoi(car);   //convert char to int
if(id==1)
     {
     delay_ms(100);
     Sound_Play(659, 250);  
     delay_ms(100);
}


Re: UART1_Read_Text problem

Posted: 09 Aug 2012 22:21
by Fakir
Hi,
if you are sending just one character at a time, use UART1_Read routine instead, as it is better suited for the task. The UARTx_Read_Text routine is useful for receiving whole strings of characters.
Also, in your code above, you are wasting 17 bytes of RAM to receive just one. 16F887 doesn't actually offer plenty of resources so I just wanted to point it out.

Cheers,
Tom

Re: UART1_Read_Text problem

Posted: 10 Aug 2012 08:49
by enryv
Thank a lot Fakir...I have solved the problem.....serial.println prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r')...i have I replaced it with Serial.print.....it's works :)