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 support@ccsinfo.com

(Solved) displaying string variables in a 4x20 LCD

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



Joined: 23 Mar 2014
Posts: 16

View user's profile Send private message

(Solved) displaying string variables in a 4x20 LCD
PostPosted: Mon Apr 28, 2014 9:06 am     Reply with quote

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 Wink


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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 11:46 am     Reply with quote

OK, I've seen how to do an array of strings here: http://www.ccsinfo.com/forum/viewtopic.php?t=33182&view=next

Though it's something, how can I choose one in a random mode?
temtronic



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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 12:02 pm     Reply with quote

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: 19393

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 12:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 12:18 pm     Reply with quote

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: 19393

View user's profile Send private message

PostPosted: Mon Apr 28, 2014 12:24 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 29, 2014 4:14 am     Reply with quote

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: 19393

View user's profile Send private message

PostPosted: Tue Apr 29, 2014 5:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 02, 2014 9:25 am     Reply with quote

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 Wink
Ttelmah



Joined: 11 Mar 2010
Posts: 19393

View user's profile Send private message

PostPosted: Fri May 02, 2014 10:49 am     Reply with quote

Yes. Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19393

View user's profile Send private message

PostPosted: Fri May 02, 2014 10:50 am     Reply with quote

Yes. Smile

The forum seems to have taken my reply twice, and won't let me delete either....
Apologies.
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