Page 1 of 1

HELP: read RS232 Write LCD

Posted: 10 Jul 2010 11:14
by george2209
Hi,
I've made a simple program:

Code: Select all

sbit LCD_RS at LATD2_bit;
sbit LCD_EN at LATD3_bit;
sbit LCD_D4 at LATD4_bit;
sbit LCD_D5 at LATD5_bit;
sbit LCD_D6 at LATD6_bit;
sbit LCD_D7 at LATD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;

char uart_rd[]="  ";

void initializeLCD(){
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out( 1, 1, "STARTING" );
  Delay_ms(1000);
}

void main() {
  ADCON1 |= 0x0F;                  // Configure AN pins as digital
  CMCON  |= 7;                     // Turn off comparators
  
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  initializeLCD();
  
  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);
  
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd[0] = UART1_Read();     // read the received data,
      Lcd_Out( 1, 1, uart_rd );
      Delay_ms(100);
    }
  }
}
Now, the problem is that if I send via serial:

123456789

On the LCD appears:
1 then 2 then 3
the rest of the 4,5,6,7,8,9 are lost. Any hint why?

MB: EasyPic4
PIC: 18LF4520
QZ: 8,000MHz
Serial cable to PC COM1 (no USB to Serial convertor)

PLS HELP! :(

Edited by administrator: Don't use Quote for Code!

Re: HELP: read RS232 Write LCD

Posted: 10 Jul 2010 14:30
by Sobrietytest
Hi George, I'm not really sure why you've declared uart_rd as an array, within the context of your program it only needs to be a single char.

In the line Lcd_Out(1,1,uart_rd) if you're using an array you must specify which element you want to be displayed, i.e. Lcd_Out(1,1,uart_rd[0]).

You may be losing data because of the delay on the while loop, I can't find the actual size of the RCREG for the 4520 but if this register overflows then you will lose incoming data. The idea with serial comms is to read the data when it comes in then do something with it, if you set your processor to do something else whilst data is still arriving then you will lose it.

Try this...

Code: Select all

char uart_rd;   //Declare as char, not char array.

...

while (1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
Lcd_Out_Cp( uart_rd );   //write the next byte in the next cursor position.
//Delay_ms(100);           //Remove the delay


Re: HELP: read RS232 Write LCD

Posted: 10 Jul 2010 14:33
by nart_schinackow
Why not try using UART_READ_TEXT(..,..,..); ?

this function reads a string from the pc and waits a speciefied charecter called delemiter !!
this is better


ex:

use this :
char a[10];

Code: Select all

while(1)
{
if(uart_data_ready())
{
uart_read_text(a,";",10);
}
//note your pc must send "1234567;"
}


Re: HELP: read RS232 Write LCD

Posted: 11 Jul 2010 10:10
by george2209
Hi guys!

First of all many thanky for your answers!

As you've both mentioned the main problem is the time. When you read from the serial port you have to do it fast, if possible nothing in between the reading steps.
As I cannot fulfil this within the "main" function, I've made it with the interrupt function.
I read inside the interrupt and then inside the "main" i display the text.
However, thank you!

Re: HELP: read RS232 Write LCD

Posted: 12 Jul 2010 11:53
by ranko.rankovic
Hello george2209,

You can find tested and working example in this post: http://www.mikroe.com/forum/viewtopic.p ... 76#p128476.
It does exactly what you need, receives data over RS232 and sends it to LCD.

Simply readjust the example for your chip, because it is built for PIC18F452

Hope this helped.

Best regards

Re: HELP: read RS232 Write LCD

Posted: 12 Jul 2010 14:08
by george2209
Hi,
Thank you for your answer!

I'll do it today and come back with the results.

HOWEVER, the problem is, I think, not with the software. The problem is that:

I make an application( I've already try more examples) and burn it on the PIC.
Then, when I start try the app, 80% is not working, but, sometimes, without changing anythink in the Software is working..I guess is a contact or some hardware issue.

I'll come back later with the results.

Re: HELP: read RS232 Write LCD

Posted: 13 Jul 2010 19:49
by drdoug
It does sound like a hardware problem and I would look first at the MCLR to make sure there is a pull up (or disable MCLR). Also maker sure comparators are off as well.
May also make sure to have a .1uF cap across the V+ and Gnd of the microcontroller.

Re: HELP: read RS232 Write LCD

Posted: 14 Jul 2010 09:09
by george2209
Hi drdoug,

Thank you for your sugestion.
The 100nF condensator is on my design missing...maybe I'll put it too. I have a very good filtered source and this is why I didn't care about that one.

Regarding the LCD problem is solved: it was a contact issue.

Thank you!

Re: HELP: read RS232 Write LCD

Posted: 07 Aug 2010 17:29
by Lmxung
Hi, You can try this link.
http://aynkaran.blogspot.com/2008/12/us ... pc-vb.html
Enjoy your self.