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

Question about LCD display (most likely to be interrupts)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

Question about LCD display (most likely to be interrupts)
PostPosted: Wed Jul 02, 2014 10:40 pm     Reply with quote

#include"flex_lcd.c"
int count=0;
int RunMilege=0;
int mode=0;
int flag=1;
unsigned long Price;
int hun;
int ten;
int one;
void price1()
{
int TotalPrice;
if(RunMilege<=2300)
TotalPrice=800;
if((RunMilege>2300)&&(RunMilege<=35000))
TotalPrice=800+(RunMilege-2300)*0.26;
if(RunMilege>35000)
TotalPrice=1112+(RunMilege-35000)*0.35;
Price=(unsigned long)(TotalPrice/100);
}
void end()
{
lcd_putc('\f');
putc('\n');
mode=0;
count=0;
RunMilege=0;
flag=0;
Price=0;
}
void arrive()
{
lcd_putc('\f');
lcd_putc('\n');
hun=Price/100;
ten=(Price-hun*100)/10;
one=Price-hun*100-ten*10;
if(hun!=0){lcd_putc(hun+0x30);lcd_putc(ten+0x30);lcd_putc(one+0x30);}
else if(ten!=0){lcd_putc(ten+0x30);lcd_putc(one+0x30);}
else{lcd_putc(one+0x30);}
//display"Price"
}
void vacant_car()
{
lcd_putc('\f');
lcd_putc('\n');
lcd_putc('V');
lcd_putc('a');
lcd_putc('c');
lcd_putc('a');
lcd_putc('n');
lcd_putc('t');
//Display"vacant"
}
void welcome()
{
lcd_putc('\f');
lcd_putc('\n');
lcd_putc('W');
lcd_putc('e');
lcd_putc('l');
lcd_putc('c');
lcd_putc('o');
lcd_putc('m');
lcd_putc('e');
//display“Welcome”
}
#int_ext
void breakloop()
{
flag=1;
mode=(mode+1)%3;
}
void carrying()
{
setup_timer_1(T1_EXTERNAL);
set_timer1(0);
while(flag==0){}
//count pulse of TMR1
}
void main()
{
lcd_init();
enable_interrupts(global);
ext_int_edge(H_TO_L);
enable_interrupts(int_ext);
while(1)
{
if(mode==0)
{
if(flag==1)
{
count=0;
vacant_car();
flag=0;
}
}
else if(mode==1)
{
if(flag==1)
{
welcome();
carrying();
clear_interrupt(int_ext);
flag=0;
}
}
else if(mode==2)
{
if(flag=1)
{
price1();
arrive();
flag=0;
}
}
else if(mode==3)
{
if(flag==0)
{
end();
flag=0;
}
}
}
}


Last edited by HWGonly1 on Thu Jul 03, 2014 3:26 am; edited 3 times in total
naughty_mark



Joined: 29 Aug 2012
Posts: 97

View user's profile Send private message

PostPosted: Wed Jul 02, 2014 10:58 pm     Reply with quote

Hi HWGonly1

I am also a newbee or say a little old newbee with CCS compiler. Not sure for your question, but:

1. It is better you tell people what microcontroller you are using and what is the version of CCS you are using. Otherwise, people will be hard to help you.

2. Most of your function named by Chinese PIN YIN, it may doesn't make sense to English speaker who is most likely to help you here. I suggest you change that names for easily reading.

3. If possible, you may general describe what is your project doing, so others can easily check if there is any logic problem in your code. Sounds like a bus transport dispatch simulate project

At last, sorry for my poor English as well

Embarassed

Regards
Mark


Last edited by naughty_mark on Thu Jul 03, 2014 12:46 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19329

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 12:40 am     Reply with quote

Some other comments:

1) Don't use float....
It is _not_ suitable for anything involving money. Use a scaled integer. So count in 'cents' in an int32. Problem is that floats have rounding errors, and you will end up with the wrong result long term....
Use cents, and hundredths of a mile (perhaps).

2) Use printf.
For instance your 'welcome' routine, can be done as:

printf(lcd_putc,"\f\nWelcome");

There is no point in the delay between the line feed and the text, the LCD driver will wait till the text can be displayed.
Also printf supports a 'scaled integer' format, that can display a 'cent' count as decimal dollars.

4) Add one delay before lcd_init.
Generally LCD's take quite a time to wake. Often at least half a second _later_ than the PIC. Hence you need to pause before starting.
5) Read the 'sticky' at the top of the forum about Proteus(Isis). Problem is that you can build circuits in Proteus that won't work when you make them for real (so the 'development' with it is wasted time), and perhaps worse, you can build designs in the real world that don't work in Proteus....

6) Initialise the interrupt with:
Code:

  enable_interrupts(global);
  ext_int_edge(H_TO_L);
  clear_interrupts(INT_EXT);
  enable_interrupts(int_ext);

The act of setting the 'edge', can result in the interrupt triggering, hence the sequence must be 'set the edge', 'clear the interrupt', then 'enable'.

7) Why are you clearing the interrupt in the main loop?. Don't. The interrupt is automatically cleared when you exit the handler routine.

8) Your code in the main loop, should only update things, if 'flag==1'. To say that the count has changed.

8) Then back to the float and printf. Printf can format you numbers for you. No need for the fiddling around with 'hun', 'ten', and 'one'.....
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

Thank you very much, I know there are many advantage with my
PostPosted: Thu Jul 03, 2014 3:18 am     Reply with quote

Thank you very much, I know there are many disadvantage with my program.
I have correct them but it still doesn't change what LCD display. Crying or Very sad

LCD keep displaying "Vacant" whatever i do.....

I'll go try built it in real.

Please help me, experienced programmers

I'm just a poor student try to complete my work Crying or Very sad
[img]http://t1.qpic.cn/mblogpic/10494df6ccbb928d9e68/460[/img]
Ttelmah



Joined: 11 Mar 2010
Posts: 19329

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 4:22 am     Reply with quote

Learn to use the code buttons
Code:

//Processor setup here - post this when posting your code.

#include"flex_lcd.c"
int count=0; //Don't know what you use this for.
int32 RunMileage=0;
int mode=0;
int1 flag=TRUE;

int32 Price_calc()
{
   int32 Price; //running with this at 100*
   if(RunMileage<=2300)
      Price=80000;
   else if((RunMileage>2300)&&(RunMileage<=35000))
      Price=80000+(RunMileage-2300)*26;
   else if(RunMileage>35000)
      Price=111200+(RunMileage-35000)*35;
   return price;
}
//No idea if this calculation is right....
 
void end()
{
   lcd_putc('\f');
   mode=0;
   count=0;
   RunMileage=0;
}

#int_ext
void breakloop()
{
   flag=1;
   if (++mode==3)
      mode=0; //'% division, and should be avoided in an interrupt
}
void carrying()
{
    setup_timer_1(T1_EXTERNAL_SYNC | T1_DIV_BY_1);
    set_timer1(0);
}
void main()
{
   int16 old_timer, temp_timer;
   int32 Price;

   delay_ms(500);
   lcd_init();

   ext_int_edge(H_TO_L);
   clear_interrupt(INT_EXT);
   enable_interrupts(INT_EXT;
   enable_interrupts(GLOBAL;

   while(TRUE)
   {
      if (flag==TRUE)
      {
          //note where the flag test is...
            flag=FALSE;
            switch (MODE) {
            case 0:
               count=0;
               printf(lcd_putc,"\f\nVacant Car");
               break;
            case 1:
               printf(lcd_putc,"\f\nWelcome");
               delay_ms(1000);
               carrying();
               printf(lcd_putc,"\f\nFare");
               old_timer=get_timer1();
               mode=2; //automatically advance to 'running'
               break;
           }
       }
       //Now nowhere do you read the timer. Guessing this is actually
       //The 'mileage'. What units though?. 1/10th mile?. 1/100th mile?.
       temp_timer=get_timer1();
       if (temp_timer != old_timer)
       {
           //timer has changed so calculate
           if (mode==2)
           {
               lcd_gotoxy(1,1);
               old_timer=temp_timer;
               //assuming 'RunMileage' is the timer value
               RunMileage=temp_timer;
               Price=Price_calc(); //calculate price so far
               Price/=100; //because I use *100 in factors
               printf(lcd_putc,"%5.2Lw",Price);
           }
       }       
   }
}


Now this won't right, but shows some points:

First, I'm assuming the timer input (counting something), is actually counting odometer pulses. So the count on this is the 'RunMiileage'.
Then the code automatically moves on to mode==2, after displaying the welcome, so it displays the fare as the ride continues. Nobody wants nothing displayed while riding....
Most fare systems have a basic in time as well as mileage. Are you sure your's is this simple?.
Then the button input, and (possibly) the odometer pulse (if this is what it is), will need pull-up resistors.
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 4:50 am     Reply with quote

Thank you very much for rewrite the code for me. However it's still troubling with just display "Vacant car".
Seems the #int_ext still don't work.
Do you have any idea,I'm sure my simulation circuit is right.
Ttelmah



Joined: 11 Mar 2010
Posts: 19329

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 4:57 am     Reply with quote

As I said:

"Then the button input, and (possibly) the odometer pulse (if this is what it is), will need pull-up resistors."

A button on it's own, won't generate a signal the PIC can see.
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 5:11 am     Reply with quote

Ttelmah wrote:
As I said:

"Then the button input, and (possibly) the odometer pulse (if this is what it is), will need pull-up resistors."

A button on it's own, won't generate a signal the PIC can see.





Probably it's my circuit that's wrong,would you please have a look on my simulation circuit?
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 5:21 am     Reply with quote

[quote="HWGonly1"]
Ttelmah wrote:
As I said:

"Then the button input, and (possibly) the odometer pulse (if this is what it is), will need pull-up resistors."

A button on it's own, won't generate a signal the PIC can see.





Oh,I'm sorry,what I did is a little amusing,I was just mocked by my teacher, thank you very much,I was out of my mind.
Ttelmah



Joined: 11 Mar 2010
Posts: 19329

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 6:54 am     Reply with quote

Think.

You are programming the interrupt to trigger when the pin goes high to low.

The pin needs to be pulled _high_ when the button is not pressed, and then pulled _low_ by the button.

So the standard circuit is a resistor from the pin to Vdd, and then the button from the pin to Vss. This way, when the button is pressed, the pin goes low.
temtronic



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

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 10:48 am     Reply with quote

also you'll probably need to add a small 'filter' cap across the switch contacts to minimize the 'bounce' or you'll get multiple inputs!

hth
jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 03, 2014 2:10 pm     Reply with quote

This is a schematic of how you should connect the pushbutton, per
Ttelmah's description:
Code:
          +5v
            |
            <
            > 4.7K       
            <         ___  Switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
B0                            --- GND
                               -   
 
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

Thank you very much, I've already solved my problem!Thanks!!
PostPosted: Thu Jul 03, 2014 8:33 pm     Reply with quote

Ttelmah wrote:
Think.

You are programming the interrupt to trigger when the pin goes high to low.

The pin needs to be pulled _high_ when the button is not pressed, and then pulled _low_ by the button.

So the standard circuit is a resistor from the pin to Vdd, and then the button from the pin to Vss. This way, when the button is pressed, the pin goes low.


If you didn't help me,I haven't realize something wrong with my circuit,thank u!


Last edited by HWGonly1 on Thu Jul 03, 2014 8:35 pm; edited 1 time in total
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

Thank you for offering me a helpful hand!!!
PostPosted: Thu Jul 03, 2014 8:33 pm     Reply with quote

temtronic wrote:
also you'll probably need to add a small 'filter' cap across the switch contacts to minimize the 'bounce' or you'll get multiple inputs!

hth
jay
HWGonly1



Joined: 02 Jul 2014
Posts: 15

View user's profile Send private message

Your comment is right what works in mine,thank you!!!
PostPosted: Thu Jul 03, 2014 8:36 pm     Reply with quote

PCM programmer wrote:
This is a schematic of how you should connect the pushbutton, per
Ttelmah's description:
Code:
          +5v
            |
            <
            > 4.7K       
            <         ___  Switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
B0                            --- GND
                               -   
 
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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