HELP: read RS232 Write LCD

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
george2209
Posts: 27
Joined: 10 Nov 2008 11:01

HELP: read RS232 Write LCD

#1 Post by george2209 » 10 Jul 2010 11:14

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!

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: HELP: read RS232 Write LCD

#2 Post by Sobrietytest » 10 Jul 2010 14:30

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


nart_schinackow
Posts: 122
Joined: 28 Apr 2009 16:16

Re: HELP: read RS232 Write LCD

#3 Post by nart_schinackow » 10 Jul 2010 14:33

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;"
}


george2209
Posts: 27
Joined: 10 Nov 2008 11:01

Re: HELP: read RS232 Write LCD

#4 Post by george2209 » 11 Jul 2010 10:10

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!

User avatar
ranko.rankovic
Posts: 433
Joined: 11 Jun 2010 09:22

Re: HELP: read RS232 Write LCD

#5 Post by ranko.rankovic » 12 Jul 2010 11:53

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
Ranko Rankovic
mikroElektronika [Support Department]

george2209
Posts: 27
Joined: 10 Nov 2008 11:01

Re: HELP: read RS232 Write LCD

#6 Post by george2209 » 12 Jul 2010 14:08

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.

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: HELP: read RS232 Write LCD

#7 Post by drdoug » 13 Jul 2010 19:49

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.

george2209
Posts: 27
Joined: 10 Nov 2008 11:01

Re: HELP: read RS232 Write LCD

#8 Post by george2209 » 14 Jul 2010 09:09

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!

Lmxung
Posts: 1
Joined: 21 Jul 2010 18:10

Re: HELP: read RS232 Write LCD

#9 Post by Lmxung » 07 Aug 2010 17:29

Hi, You can try this link.
http://aynkaran.blogspot.com/2008/12/us ... pc-vb.html
Enjoy your self.

Post Reply

Return to “mikroC PRO for PIC General”