CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

16f628a + lcd

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

16f628a + lcd
PostPosted: Sat Sep 24, 2016 11:54 am     Reply with quote

Hi there,

I'm trying to display a string into the lcd2x16 via Bluetooth HC-06. I use the pic 16f628a and the compiler version 5.008. But i have no success. Here is the code:

Code:
#include "16f628a.h"
#INCLUDE <stdlib.h>
#fuses XT, NOWDT, NOPROTECT
#use delay (clock=4000000)
#include "flex_lcd.c"
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)

char input_str[10];

void main ()
   {
   lcd_init();
 
   while (True)
         {               
         gets(input_str);
         lcd_gotoxy(1,1);
         printf(lcd_putc,"%s",input_str);
         }
   }

Can you help me?
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Sat Sep 24, 2016 12:47 pm     Reply with quote

Hi!
Here are some tips:
First divide the system into 2. Does the receiver receive the data? If you don't have a 'scope or logic analyzer a crude way is to send a string every 1 second and place a digital voltmeter on the receiving side. It should show a slight change every second.

Next check that you are using correct voltages. If you use a Bluetooth module I assume you use 3.3V. Does the LCD also work with 3.3V ?

What is flex_lcd.c ?
How about trying to receive characters instead of strings, and see if you are getting ANYTHING?
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 1:24 pm     Reply with quote

Yes the system works i think because if i change the follow
Code:

char input_str[10]; -> char input_str;
gets(input_str); -> input_str = getc();
printf(lcd_putc,"%s", input_str); -> printf(lcd_putc,"%c", input_str);


Now with these changes if i send a character from my phone the lcd displays the character.
The flex_lcd is thishttps://www.ccsinfo.com/forum/viewtopic.php?t=24661.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 3:48 pm     Reply with quote

What are the exact keys that you are typing into gets() ? List them all.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 4:11 pm     Reply with quote

from the CCS manual...
GETS().....Reads characters (using getc()) into the string until a RETURN (value 13) is encountered. The string is terminated with a 0.

I betting whatever is sending the 'string' to the PIC is NOT sending a RETURN ( value 13) so the GETS function is never fulfilled ??

Jay
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 4:42 pm     Reply with quote

I have built a simple program from MIT app inventor. I send 0 to 1023 from a slider bar.
http://www.hlektronika.gr/forum/attachment.php?attachmentid=66755&d=1474651036
What exactly i have to do? I must add a button which gonna send the value of 0x13?
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 4:52 pm     Reply with quote

Modify your simple program to send a 13 ( that's decimal 13 NOT 0x13 BTW) as the LAST character of the 'string'.

Most, if not all 'terminal programs' have an option to to this.

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 5:10 pm     Reply with quote

Your link doesn't work. It says:
Quote:

You are not logged in or do not have permission to access this page.
This may be due to one of the following reasons:


How to post an image on the CCS forum:
Go to this website: http://postimage.org/
Upload your image. Select family safe. Then click the first button for
"Hotlink to Forum" to get a link to the image.
Then go to the CCS forum and type Ctrl-V to paste the link into a post.

If postimage.org doesn't work in your country, then use Google to find
another free image hosting site for forums.
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 5:32 pm     Reply with quote

free photo hosting
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 24, 2016 8:39 pm     Reply with quote

I'm not familiar with that program, but it looks like you should be able
to append a carriage return byte (13 or 0x0D) to the slider text that
is sent out.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sun Sep 25, 2016 3:44 am     Reply with quote

As a general slightly further comment, gets, is a routine that can have very significant problems.
As you have already had pointed out, it 'needs' the line feed terminator, but worse than this, there is no form of limiting to deal with this not arriving. If you have a 'receive' space set up to take 16 characters, and then somebody types 20, the code will keep writing into places it shouldn't, and overwrite other variables. Ugh...

There is a slightly 'improved' version of gets, which ensures that if a user just keeps typing, it'll exit anyway when the end of space is reached, called 'get_string', supplied in input.c.

So:
Code:

#include "16f628a.h"
#fuses XT, NOWDT, NOPROTECT
#use delay (clock=4000000)
#use rs232(UART1, baud=9600, parity=N, bits=8, ERRORS)
//end of chip setup

#INCLUDE <stdlib.h>
#include <input.c>
#include "flex_lcd.c"
//end of includes

char input_str[10];

void main (void)
{
   delay_ms(500);
   lcd_init();
 
   while (TRUE)
   {               
       get_string(input_str, strlen(input_str));
       lcd_gotoxy(1,1);
       printf(lcd_putc,"%s",input_str);
   }
}

Generally you should avoid using gets, except for 'fixed length' strings, where you know that there should be a line feed automatically send by the code at the other end. Otherwise it is potentially a very dangerous tool.

As another comment, don't include things till _after_ the chip is set-up. Look at the order I have used. Some functions in the libraries _require_ parts of the chip configuration to be finished first. The compiler has got better at dealing with this on recent versions (it treats the fuse, and clock lines as if they has a #PRAGMA), but it is still safer to endure that everything is configured before loading other code.

Then, if using the hardware UART, you should always have 'ERRORS' in the #USE RS232. It should only be omitted, if _you_ are adding your own code to handle errors. The errors do need handling (or the UART can become locked). Note also how I have just used the UART name (this is a 'shorthand' saying to select the UART pins, and use the hardware UART - this syntax is 'required' on later chips with selectable pins - on these you select the pins, then tell the code to talk to the already setup UART), so it is easier and safer to get into the habit of using this... Smile

Finally, I have added a delay before initialising the LCD. Most PIC's start to wake up before LCD's do. Many will not work reliably is you try to wake them immediately the PIC wakes. Much better to have a little pause. Lcd_init has a small pause but for something in excess of perhaps 75% of LCD's/PIC's....

As an 'extra' comment, 5.008, was still a little bit 'beta'. Not like the early V4 compilers where a lot didn't work, but you will find if you start to write more complex code, that there are things in this compiler that do not function correctly....
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

PostPosted: Sun Sep 25, 2016 8:15 am     Reply with quote

PCM programmer wrote:
I'm not familiar with that program, but it looks like you should be able
to append a carriage return byte (13 or 0x0D) to the slider text that
is sent out.


Thanks PCM Programmer i add into the phone program to send a return byte and now the display print the string.
Ventouza



Joined: 21 Apr 2014
Posts: 12
Location: Kefalonia, Greece

View user's profile Send private message

PostPosted: Sun Sep 25, 2016 8:27 am     Reply with quote

Ttelmah wrote:
As a general slightly further comment, gets, is a routine that can have very significant problems.
As you have already had pointed out, it 'needs' the line feed terminator, but worse than this, there is no form of limiting to deal with this not arriving. If you have a 'receive' space set up to take 16 characters, and then somebody types 20, the code will keep writing into places it shouldn't, and overwrite other variables. Ugh...

There is a slightly 'improved' version of gets, which ensures that if a user just keeps typing, it'll exit anyway when the end of space is reached, called 'get_string', supplied in input.c.

So:
Code:

#include "16f628a.h"
#fuses XT, NOWDT, NOPROTECT
#use delay (clock=4000000)
#use rs232(UART1, baud=9600, parity=N, bits=8, ERRORS)
//end of chip setup

#INCLUDE <stdlib.h>
#include <input.c>
#include "flex_lcd.c"
//end of includes

char input_str[10];

void main (void)
{
   delay_ms(500);
   lcd_init();
 
   while (TRUE)
   {               
       get_string(input_str, strlen(input_str));
       lcd_gotoxy(1,1);
       printf(lcd_putc,"%s",input_str);
   }
}

Generally you should avoid using gets, except for 'fixed length' strings, where you know that there should be a line feed automatically send by the code at the other end. Otherwise it is potentially a very dangerous tool.

As another comment, don't include things till _after_ the chip is set-up. Look at the order I have used. Some functions in the libraries _require_ parts of the chip configuration to be finished first. The compiler has got better at dealing with this on recent versions (it treats the fuse, and clock lines as if they has a #PRAGMA), but it is still safer to endure that everything is configured before loading other code.

Then, if using the hardware UART, you should always have 'ERRORS' in the #USE RS232. It should only be omitted, if _you_ are adding your own code to handle errors. The errors do need handling (or the UART can become locked). Note also how I have just used the UART name (this is a 'shorthand' saying to select the UART pins, and use the hardware UART - this syntax is 'required' on later chips with selectable pins - on these you select the pins, then tell the code to talk to the already setup UART), so it is easier and safer to get into the habit of using this... Smile

Finally, I have added a delay before initialising the LCD. Most PIC's start to wake up before LCD's do. Many will not work reliably is you try to wake them immediately the PIC wakes. Much better to have a little pause. Lcd_init has a small pause but for something in excess of perhaps 75% of LCD's/PIC's....

As an 'extra' comment, 5.008, was still a little bit 'beta'. Not like the early V4 compilers where a lot didn't work, but you will find if you start to write more complex code, that there are things in this compiler that do not function correctly....


Thanks Ttelmah for your reply and for your tips. I'm gonna keep your routine and i build my program base on your tips and your upgrade routine.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group