View previous topic :: View next topic |
Author |
Message |
HWGonly1
Joined: 02 Jul 2014 Posts: 15
|
Question about LCD display (most likely to be interrupts) |
Posted: Wed Jul 02, 2014 10:40 pm |
|
|
#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
|
|
Posted: Wed Jul 02, 2014 10:58 pm |
|
|
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
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: 19536
|
|
Posted: Thu Jul 03, 2014 12:40 am |
|
|
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
|
Thank you very much, I know there are many advantage with my |
Posted: Thu Jul 03, 2014 3:18 am |
|
|
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.
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
[img]http://t1.qpic.cn/mblogpic/10494df6ccbb928d9e68/460[/img] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19536
|
|
Posted: Thu Jul 03, 2014 4:22 am |
|
|
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
|
|
Posted: Thu Jul 03, 2014 4:50 am |
|
|
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: 19536
|
|
Posted: Thu Jul 03, 2014 4:57 am |
|
|
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
|
|
Posted: Thu Jul 03, 2014 5:11 am |
|
|
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
|
|
Posted: Thu Jul 03, 2014 5:21 am |
|
|
[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: 19536
|
|
Posted: Thu Jul 03, 2014 6:54 am |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Jul 03, 2014 10:48 am |
|
|
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
|
|
Posted: Thu Jul 03, 2014 2:10 pm |
|
|
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
|
Thank you very much, I've already solved my problem!Thanks!! |
Posted: Thu Jul 03, 2014 8:33 pm |
|
|
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
|
Thank you for offering me a helpful hand!!! |
Posted: Thu Jul 03, 2014 8:33 pm |
|
|
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
|
Your comment is right what works in mine,thank you!!! |
Posted: Thu Jul 03, 2014 8:36 pm |
|
|
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
-
|
|
|
|
|
|