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

using single pin input to scroll menu

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







using single pin input to scroll menu
PostPosted: Sun Oct 20, 2002 8:52 am     Reply with quote

Hello guys & ladies
i have upgraded from picbasic to ccs and i am trying to
get my head round how the following can be implemented.
i have done this using picbasic
i have 3 push button connected to RA0,RA1 & RA2
RA0 is a scroll up button
RA1 is a scroll down button
RA2 is a scroll select button

when to pic powers up it displays a user prompt which
should remain displayed until either up_button or down_button
is pressed.
this i managed to do using a do_while loop so that part works
ok.
the problem now is i can't seem to detect a key pressed after
the 1st if statement i.e if(!up_key)
when i am i the above loop i don't detect another key press when my program gets to if((!up_key)&&(mykey==1)) because i thought that at this point the value of mykey should be 1 which
is what is displayed on my LCD.
is there another way of doing this.without using anothar pin
my program is below and please feel free to give me stick
cos i can only learn from it
Isaac

#if defined(__PCM__)
#include <16F873.H>
#fuses XT,WDT,PUT,NOPROTECT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 1
#include <INPUT.C>
#include <stdio.h>
#include <lcd.c>

main()
{
int mykey;
int mykey, sec_key,up_key,down_key;
mykey=0;
set_tris_a(00001111); /*RA0-RA3 set as inputs */
set_tris_b(0x00);
set_tris_c(10000000);

up_key = input(PIN_A0) ;
down_key = input(PIN_A1) ;
sec_key = input(PIN_A2) ;


do
{ // This loop should continue to display
lcd_init(); // the protocol test msg until either
printf(lcd_putc,"\fProtocol Test\n"); // up_key or down_key is pressed
printf(lcd_putc,"Select Test"); // which breaks out of the loop
delay_ms(500); // delay for lcd to settle
}
while((up_key)&&(down_key)); // continue to display until either up_key or
// down_key is pressed

if(!up_key)
{
mykey=++;
printf(lcd_putc,"\f This is Msg1\n");
printf(lcd_putc,"mykey =\%u",mykey);
delay_ms(500);
if((!up_key)&&(mykey==1)) // doesn't do this bit?
{
mykey++;
printf(lcd_putc,"\fThis is Msg2\n");
printf(lcd_putc,"mykey =\%u",mykey);
delay_ms(500);
}
}

}

___________________________
This message was ported from CCS's old forum
Original Post ID: 8004
Tony H.
Guest







Re: using single pin input to scroll menu
PostPosted: Sun Oct 20, 2002 9:15 am     Reply with quote

Hi!

a few things I see...

first off....

input(xyz) (where xyz is the pin constant) is a function call that returns a value.

your line like....

up_key = input(PIN_A0) ;

is getting the current value of PIN_A0, not assigning the function of input the variable.

the quick and dirty fix...

move the 3 key assignments to within the loop so those variables will be set each time the loop passes

there are more elegant fixes, but this will fix it quick.


second - unless you need to for some reason - you shouldn't have the lcd init function in the do loop to be run every pass - it's a waste of cycles. Initialize the LCD before entering the loop :)

You will probably want to re-work your code around what it is printing anyway - with the loop - if no key is pressed, it will keep re-printing the initial line - this ends up being a bit of a waste of cycles, and it may look a little funny with flicker I don't know if this is what you want it to do, but i'm guessing that its not exactly what you are aiming for :)

Although you are only using pins A0-A2, you're setting pins A0-A4 for input - I assume you are planning on using the other pin for input at some point?

just some ideas :)

-T



:=
:=#if defined(__PCM__)
:=#include <16F873.H>
:=#fuses XT,WDT,PUT,NOPROTECT,BROWNOUT,NOLVP
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 1
:=#include <INPUT.C>
:=#include <stdio.h>
:=#include <lcd.c>
:=
:=main()
:={
:= int mykey;
:= int mykey, sec_key,up_key,down_key;
:= mykey=0;
:= set_tris_a(00001111); /*RA0-RA3 set as inputs */
:= set_tris_b(0x00);
:= set_tris_c(10000000);
:=
:= up_key = input(PIN_A0) ;
:= down_key = input(PIN_A1) ;
:= sec_key = input(PIN_A2) ;
:=
:=
:= do
:= { // This loop should continue to display
:= lcd_init(); // the protocol test msg until either
:= printf(lcd_putc,"\fProtocol Test\n"); // up_key or down_key is pressed
:= printf(lcd_putc,"Select Test"); // which breaks out of the loop
:= delay_ms(500); // delay for lcd to settle
:= }
:= while((up_key)&&(down_key)); // continue to display until either up_key or
:= // down_key is pressed
:=
:= if(!up_key)
:= {
:= mykey=++;
:= printf(lcd_putc,"\f This is Msg1\n");
:= printf(lcd_putc,"mykey =\%u",mykey);
:= delay_ms(500);
:= if((!up_key)&&(mykey==1)) // doesn't do this bit?
:= {
:= mykey++;
:= printf(lcd_putc,"\fThis is Msg2\n");
:= printf(lcd_putc,"mykey =\%u",mykey);
:= delay_ms(500);
:= }
:= }
:=
:=}
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 8005
Tomi
Guest







Re: using single pin input to scroll menu
PostPosted: Sun Oct 20, 2002 9:41 am     Reply with quote

<font face="Courier New" size=-1>You have some errors in your code. First of all, your WDT is enabled, so it would be better to use:
#use delay(clock=4000000,restart_wdt)
to ensure CCS C to insert CLRWDT instructions in your delay_ calls.
Additionally, use this:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,RESTART_WDT,ERRORS)
See the manual for these two options.

Another problem is that you read in the port only once at the start of main(); then you make something and after that the hidden Sleep() instruction inserted by CCS C sends the PIC into sleep state.

Maybe you want to use something like this:
#define up_key input(PIN_A0) // no semicolons!
#define down_key input(PIN_A1)
#define sec_key input(PIN_A2)
Of course, you must delete the declarations of "_key" variables.
In this case e.g. every "down_key" is replaced by an "input(PIN_A0)" what gives the result you want.
To prevent PIC from sleep, add a:
while (1) Restart_wdt();
loop after your last if() statement.

Finally, I think your code doesn't do what you want. If I strongly understand you must walk up and down in a single-level menu system. In this case it would be better to use something like this:
............
mykey = 0; // is this the menu ordinal number?
while(1) {
Restart_wdt();
if (up_key && down_key && sec_key) continue; // no keypress
if (!up_key && mykey < MAXITEMS) mykey++; // limit to MAXITEMS
if (!down_key && mykey > 0) mykey--; // prevent underflow
if (!sec_key) DoSelection();
DisplayMenuItem(mykey);
}

DisplaymenuItem() function could be something like this:
void DisplaymenuItem(char inp)
{
switch (inp) {
case 0: printf(lcd_putc,"\fProtocol Test\n"); // up_key or down_key is pressed
printf(lcd_putc,"Select Test"); // which breaks out of the loop
break;
case 1: printf(lcd_putc,"\f This is Msg1\n");
printf(lcd_putc,"mykey =\%u",inp);
break;
case 2: printf(lcd_putc,"\fThis is Msg2\n");
printf(lcd_putc,"mykey =\%u",inp);
break;
........ // additional case statements
default: printf(lcd_putc,"\fIllegal item\n");
printf(lcd_putc,"mykey =\%u",inp);
break;

}
// call delay_ms(500) if you really need it
}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 8006
aiyanyo
Guest







Re: using single pin input to scroll menu
PostPosted: Sun Oct 20, 2002 10:03 am     Reply with quote

Thanks for your quick replies
Like i said i am new to Pic C
i am still trying to take it all in .
i am gonna get my kit on in the next few mins
Isaac
___________________________
This message was ported from CCS's old forum
Original Post ID: 8007
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