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

-can anybody give some comment?-

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



Joined: 04 Jan 2007
Posts: 6

View user's profile Send private message

-can anybody give some comment?-
PostPosted: Sat Jan 20, 2007 8:49 am     Reply with quote

Hi, I'm newbie in PIC.
I want to do a menu by using LCD and push buttons, but I have no idea on it.

But I have found a sample and do some modifying on it.
However, it failed.

Here is my code :
Code:


#include<16f877a.h>          // MCU specific library



#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,NOBROWNOUT
#use delay (clock=20000000)


#include<lcd.c>

#byte pa   =0xF80   
#byte pb   =0xF81
#byte pc   =0xF82   
#byte pd   =0xF83   
#byte pe   =0xF84   


#bit key_enter       =pb.0      
#bit key_plus       =pb.1      
#bit key_min       =pb.2      


void menu(void);

void menu_se2(void);
void menu_se3(void);

void menu_a(void);      
void menu_b(void);
void menu_c(void);

signed char menu_level;

void key_sel(void);            
char k_min,k_plus,k_enter;




void key_sel()
{
int32 keysel_loop;

k_min=0;               // reset
k_plus=0;
k_enter=0;

for (keysel_loop=0;keysel_loop!=500000;keysel_loop++){
   if (key_min){k_min=1;break;}      // sense keys
   if (key_plus){k_plus=1;break;}
   if (key_enter){k_enter=1;break;}
                           }

}

void menu(void)
{

if (key_enter){

   menu_level=0;
   printf(lcd_putc,"\f");lcd_gotoxy(1,1);printf(lcd_putc,"Menu ?");delay_ms(300);key_sel();

      if (k_enter){menu_level=0;menu_se2();menu_se3();}

            }

        k_min=0;            
k_plus=0;               //
k_enter=0;               //
}


void menu_se2(void)
{
printf(lcd_putc,"\f");

lcd_gotoxy(1,1);printf(lcd_putc,"Menu: ");

lcd_gotoxy(2,1);
if (menu_level==0){printf(lcd_putc,"Select submenu");}
else if (menu_level==1){printf(lcd_putc,"menu 1 ?");}
else if (menu_level==2){printf(lcd_putc,"menu 2 ?");}
else if (menu_level==3){printf(lcd_putc,"menu 3?");}

}

void menu_se3(void)
{
char i;

// delay_ms(400);

for (i=0;i!=255;i++){

   delay_ms(300);
   key_sel();

   if (k_enter)break;
   else if (k_plus){menu_level++; if (menu_level>3){menu_level=1;};}
   else if (k_min){menu_level--; if (menu_level<1){menu_level=3;};}


   menu_se2();
               }

if (menu_level==0)

{printf(lcd_putc,"\f");delay_ms(300);return;}


if (menu_level==1){menu_a();}
else if (menu_level==2){menu_b();}
else if (menu_level==3){menu_c();}


printf(lcd_putc,"\f");delay_ms(300);return;}

void menu_a(void)   
{

printf(lcd_putc,"hello");


}


void menu_b(void)      
{

printf(lcd_putc,"good");


}


void menu_c(void)   
{

printf(lcd_putc,"nicer");


}




void main(){

lcd_init();
 delay_ms(200);

printf(lcd_putc,"\fhello");
while(1)
{
menu();

}

}


My LCD only display " Menu ?" instead of other things.


Can anybody spend some time of reviewing the codes?
I will appreciate it.

Thanks.
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Sat Jan 20, 2007 1:12 pm     Reply with quote

Howdy,

I do not immediately see a problem with the code. Perhaps the timing of your loop prevents you from seeing any new state?

Try a much simpler example where you do not implement a menu architecture. Have you verified you can correctly read the state of your inputs? If your lcd has enough space & lines, display the status of each input on your lcd. In this way you can verify the lcd and inputs are functioning as well as writing / clearing your display. Once this is working the way you want, then focus on your menu.

Try to organize your code to minimize the dependence between your functions. How about setting up a switch statement where each case is a combination of all three inputs (2^3 -> cases). This way your different delays and evaluations are not potentially interfering with one another. Have a single function which updates your global variables (read_analog(); evaluate_inputs_for_transition(); etc).

These are only suggestions. There are numerous ways to implement a menu architecture.

Cheers,
JMA
phaychee



Joined: 04 Jan 2007
Posts: 6

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 1:40 am     Reply with quote

jma_1,thanks for ur reply Smile

I had written a simple program for a simple menu:

Code:

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOCPD, NOWRT
#use delay (clock=20000000)
#include<lcd.c>
#define S1      PIN_E1


int a,b,c;

void main()

{
a=0,b=0,c=0;
lcd_init();
printf(lcd_putc,"\ftesting");

      while (1)
      {
if(!input(S1))
 { while(!input(S1))
          delay_ms(20);
          printf(lcd_putc,"\fone");
         a=1;}

 if(!input(S1)&a)
      { while(!input(S1))
          delay_ms(20);
            printf(lcd_putc,"\ftwo");
            b=1;
      }

 if(!input(S1)&b)
      { while(!input(S1))
          delay_ms(20);
          printf(lcd_putc,"\fthree");}
         }
      }


At the first time the menu go smoothly, which displays "one","two" and " three", but if keep pressing for the fourth time, it won't display back the value of "one" but is "two".

I mean the sequence is wrong.
May I know what is the problem?


Thanks a lot:)
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Tue Jan 23, 2007 6:08 am     Reply with quote

In your last "state" , the one that displays a "three", you must clear both <b>a</b> and <b> b </b>.

Otherwise, on reentering the while loop, you will print " two", since <b>a </b> and <b> b </b> are both still set and that is the first branch where your if condition is satisfied.

r.b.
phaychee



Joined: 04 Jan 2007
Posts: 6

View user's profile Send private message

PostPosted: Tue Jan 30, 2007 1:45 am     Reply with quote

rberek,thanks for ur reply:)
I know what's wrong with my existing source code:)

====================================


Erm,i do have a question.
On the main menu, i would like to display both readings of my sensor and real time clock.

But the problem is, i want to display the sensor reading where for example 5 seconds between 2 samples.
However for the real time clock, I have to renew the reading of each second.

If I combain them in main menu, the clock will move slower.

these are my code for main menu:

Code:





void main

{
ds1307_init();

    while(1)

{  delay_ms(1000);
ds1307_get_date(day,mth,yr,dow);
       ds1307_get_time(hr,min,sec);
        LCD_SetPosition ( LINE_1 + 7 );
         printf(LCD_PutChar,"%02u-%02u-%02u",day,mth,yr);
         LCD_SetPosition ( LINE_2 + 7 );
         printf(LCD_PutChar,"%02u:%02u:%02u",hr,min,sec);


         read_sht11();
         LCD_SetPosition ( LINE_1 + 0 );
         printf(LCD_PutChar,"%2.2f%%",fRh_true);
         LCD_SetPosition ( LINE_2 + 0 );
         printf(LCD_PutChar,"%2.2fC",fTemp_true);
         delay_ms(3000);}
}
phaychee



Joined: 04 Jan 2007
Posts: 6

View user's profile Send private message

PostPosted: Tue Jan 30, 2007 1:45 am     Reply with quote

rberek,thanks for ur reply:)
I know what's wrong with my existing source code:)

====================================


Erm,i do have a question.
On the main menu, i would like to display both readings of my sensor and real time clock.

But the problem is, i want to display the sensor reading where for example 5 seconds between 2 samples.
However for the real time clock, I have to renew the reading of each second.

If I combain them in main menu, the clock will move slower.

these are my code for main menu:

Code:





void main

{
ds1307_init();

    while(1)

{  delay_ms(1000);
ds1307_get_date(day,mth,yr,dow);
       ds1307_get_time(hr,min,sec);
        LCD_SetPosition ( LINE_1 + 7 );
         printf(LCD_PutChar,"%02u-%02u-%02u",day,mth,yr);
         LCD_SetPosition ( LINE_2 + 7 );
         printf(LCD_PutChar,"%02u:%02u:%02u",hr,min,sec);


         read_sht11();
         LCD_SetPosition ( LINE_1 + 0 );
         printf(LCD_PutChar,"%2.2f%%",fRh_true);
         LCD_SetPosition ( LINE_2 + 0 );
         printf(LCD_PutChar,"%2.2fC",fTemp_true);
         delay_ms(3000);}
}
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