|
|
View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
tactile switch - efficient program |
Posted: Sun Jan 12, 2020 11:12 pm |
|
|
Hi, I'm using PIC18F2620, internal osciallator 1Mhz
When I press the button(tactile switch), the following units should be displayed on LCD.
Code: |
void units()
{
switch(uom_id)
{
case 0 : lcd_cmd(0x80);
printf(lcd_data, " %6.3f ", disp_value); // bar
lcd_cmd(0xC0);
printf(lcd_data, " bar ");
break;
case 1 : lcd_cmd(0x80);
printf(lcd_data, " %6.2f ", disp_value * 14.50386); // psi
lcd_cmd(0xC0);
printf(lcd_data, " psi ");
break;
case 2 : lcd_cmd(0x80);
printf(lcd_data, " %6.3f ", disp_value * 1.0196); // KSC
lcd_cmd(0xC0);
printf(lcd_data, " Kg/cm2 ");
break;
case 3 : lcd_cmd(0x80);
printf(lcd_data, " %6.1f ", disp_value * 100); // kPa
lcd_cmd(0xC0);
printf(lcd_data, " kPa ");
break;
case 4 : lcd_cmd(0x80);
printf(lcd_data, " %6.4f ", disp_value * 0.1); // MPa
lcd_cmd(0xC0);
printf(lcd_data, " MPa ");
break;
case 5 : lcd_cmd(0x80);
printf(lcd_data, " %6.0f ", disp_value * 1019.7466); // cmWC
lcd_cmd(0xC0);
printf(lcd_data, " cmWC ");
break;
case 6 : lcd_cmd(0x80);
printf(lcd_data, " %6.0f ", disp_value * 1000); // mbar
lcd_cmd(0xC0);
printf(lcd_data, " mbar ");
break;
default : lcd_cmd(0x80);
printf(lcd_data, " %6.3f ", disp_value); // bar
lcd_cmd(0xC0);
printf(lcd_data, " bar ");
break;
};
if(input(DOWN) == 0)
{
uom_id = uom_id + 1;
if(uom_id > 6)
uom_id = 0;
write_eeprom(uom_id_address, uom_id);
uom_id = read_eeprom(uom_id_address);
delay_ms(200);
}
} |
Is this program is efficient. For some press, it is not quick to react. Could you please help to improve the code. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Jan 12, 2020 11:29 pm |
|
|
Get rid of the default.
Instead test yourself for this, with a line like:
Code: |
switch ((uom_id) {
case 0:
//Through to your case 6
}
if ((uom_id >6)
{
//have your default code
}
|
If you code a switch for more than 3 conditions, without a default, the
compiler generates a jump table for the cases. Add the 'default', and
it instead switches to testing each case separately....
However 'be aware' that the time it'll take to go through this test will
be determined by the time taken to update the LCD, and the delay
in your default case, and this will take hundreds of times the time
needed for the test. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Mon Jan 13, 2020 7:28 am |
|
|
I see some of those switch options have FP math and THAT takes a LOT of time !
If possible use 'scaled integers'. They are light speed faster than Floating Point.
Also if possible, increase the PIC clock speed. 1 MHz is slow by today's standards.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Jan 13, 2020 8:49 am |
|
|
Yes.
It is worth understanding that the simple switch here takes perhaps 5uSec,
while the maths _and_ (very importantly) the print of a floating point
value, together take perhaps 50mSec..... Nothing else happens while this
is going on. |
|
|
|
|
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
|