|
|
View previous topic :: View next topic |
Author |
Message |
potato
Joined: 23 Mar 2014 Posts: 16
|
(Solved) displaying string variables in a 4x20 LCD |
Posted: Mon Apr 28, 2014 9:06 am |
|
|
Hi guys!
I want to program a game like Scatergories using a 4x20 LCD and a 18F4550 PIC. (For those of you who don't know the game, you are given a letter and a category and you must think about a word starting with the letter that fits into the category).
For this reason, I want to display in the LCD screen a random letter and a random category.
Code: | const char letter [][*]={"A","B","C","D","E","F","G","H","I","L","M","N","O","P","R","S","T","U","W","Y"};
const char category [][*]={"Actors/Actresses","Adjectives(people)","Adjectives(things)","Body parts","Books"}; |
I don't know why I get errors if I try to define letter [20][1] - it says "string too long". For this reason I've done a variable size ([][*]), seen somewhere in the Internet.
My attempts to display a letter are:
Code: |
unsigned char i=3;
printf(lcd_putc, "\f Here you have.");
printf(lcd_putc, "\n Letter: %s", letter[i]); |
But it displays no letter. How do I access to a random position of the list, and make it written on the LCD?
I would really appreciate that somebody threw a light on the case!
Thanks in advance for your time
PS: I'm using CCS version 4 and I'm simulating the result in Proteus 8.[/quote]
Last edited by potato on Tue Apr 29, 2014 4:15 am; edited 2 times in total |
|
|
potato
Joined: 23 Mar 2014 Posts: 16
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Mon Apr 28, 2014 12:02 pm |
|
|
While your project is open...press F11
The CCS help/ Manual will 'magically' appear
Locate the RAND() function in the 'functions' list....
Double click to open it.....
Beware !!!
Make SURE your program runs correctly BEFORE using the RAND() function !!!
Having random data in your program can give 'funny' or 'odd' results driving you nuts,pulling your hair out and generally have a bad day.....
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Mon Apr 28, 2014 12:08 pm |
|
|
and remember that 'random' functions in processors are no such thing.
They generate 'pseudo random' sequences, that are the same each time, if given the same seed. To generate something that appears 'random', you have to work out a way of seeding the function with a different value each time the chip runs. Classic would be to use a time from an RTC, or the delay between two keystrokes, to give a number to seed the function. |
|
|
potato
Joined: 23 Mar 2014 Posts: 16
|
|
Posted: Mon Apr 28, 2014 12:18 pm |
|
|
Hi Ttelmah and temtronic, thanks for your quick replies.
1) The program is not working yet -have a look at the 1st message, which has been edited, please.. maybe you know how to display a variable on the LCD!
2) I guess it is F1 that has to be pressed! Yes, I see it...
For your recommendations, I'll try it at the end... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Mon Apr 28, 2014 12:24 pm |
|
|
Separately, you have the issue of constants.
Do a search here about this.
Key is that on the PIC, ROM and RAM are two different address spaces. Makes using a constant string more limited, so they can't be used as normal string variables.
Depending on the age of your compiler use rom, rather than the const keyword, or #device PASS_STRINGS=IN_RAM. |
|
|
potato
Joined: 23 Mar 2014 Posts: 16
|
|
Posted: Tue Apr 29, 2014 4:14 am |
|
|
For those of you with the same problem, I have more or less worked it out:
Code: | const char letter [20][2]={"A","B","C","D","E","F","G","H","I","L","M","N","O","P","R","S","T","U","W","Y"}; |
Despite letters are one-single character, 2 characters must be defined for that of the null /0...
Then I define an int8 i, and I give it a value (for the moment that's not random!), eg i=2 (which is going to take letter C, as A is the position 0).
And to print it on the LCD:
Code: | printf(lcd_putc, "\f %s", letter[i]); |
Hope that helps to somebody ;) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Apr 29, 2014 5:18 am |
|
|
Rather a waste of space....
You are storing _strings_, rather than characters.
Store characters:
const char letter [20] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','R','S','T','U','W','Y};
Note the single inverted commas, rather than double ones.
Then look at what %c accepts for output in printf.
Smaller code, faster, and simpler. |
|
|
potato
Joined: 23 Mar 2014 Posts: 16
|
|
Posted: Fri May 02, 2014 9:25 am |
|
|
Ttelmah wrote: | Rather a waste of space....
You are storing _strings_, rather than characters.
Store characters:
const char letter [20] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','R','S','T','U','W','Y};
Note the single inverted commas, rather than double ones.
Then look at what %c accepts for output in printf.
Smaller code, faster, and simpler. |
I didn't know that!
S, if I only want to store single characters I do it this way, but if I have words I do it with double commas and strings?
Thanks a lot |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri May 02, 2014 10:49 am |
|
|
Yes. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri May 02, 2014 10:50 am |
|
|
Yes.
The forum seems to have taken my reply twice, and won't let me delete either....
Apologies. |
|
|
|
|
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
|