|
|
View previous topic :: View next topic |
Author |
Message |
LITTLE JON
Joined: 20 Oct 2006 Posts: 5
|
Any better way to do this? |
Posted: Thu Mar 22, 2007 3:47 am |
|
|
We have to process the analog inputs of two resistors (sensors of temperature and presure). The values of temperature and presure are not linear with the values of voltage given by the circuit. So we have written a code with an if-else structure to translate the voltage given to real values of temperature.
Our PIC 16F877A does not support such a big code, and shows an error "Out of ROM". Is there any better solution, without having to change the PIC (it's integrated in an SMD card)
This code is a full program to display in an LCD the values of two batteries which power a diesel motor(if the motor is not running) or the value of temperature and oil presure (if the motor is running) We only need help to optimize the library "tablas.h", the rest of the program is to ilustrate
Here is the code:
tablas.h
Code: |
///////////////////////////////////////////////////////////////////////////////
// Funci�n que convierte el c�digo en temperatura //
///////////////////////////////////////////////////////////////////////////////
int temp1 (long cod) //FAE
{
if (cod<1024>848)
return (40);
else if (cod<848>815)
return (45);
else if (cod<815>782)
return (50);
else if (cod<782>745)
return (55);
else if (cod<745>707)
return (60);
else if (cod<707>502)
return(65-20/205*(cod-707));
else if (cod<502>477)
return (90);
else if (cod<477>467)
return (91);
else if (cod<467>375)
return (92-12/92*(cod-467));
else if (cod<375>354)
return (105);
else if (cod<354>324)
return (110);
else if (cod<324>295)
return (115);
else if (cod<295>276)
return (120);
else
return (1); //alarma
}
int temp2 (long cod) //VDO
{
if (cod>=1024 && cod>=860)
return (40);
else if (cod<860>831)
return (45);
else if (cod<831>799)
return (50);
else if (cod<799>764)
return (55);
else if (cod<764>729)
return (60);
else if (cod<729>682)
return (65);
else if (cod<682>643)
return (70);
else if (cod<643>616)
return (75);
else if (cod<616>580)
return (80);
}
int temp3 (long cod) //VDO
{
if (cod<580>547)
return (84);
else if (cod<547>524)
return (87);
else if (cod<524>502)
return (90);
else if (cod<502>473)
return (94);
else if (cod<473>453)
return (97);
else if (cod<453>434)
return (100);
else if (cod<434>412)
return (104);
else if (cod<412>389)
return (107);
else if (cod<389>373)
return (110);
else if (cod<373>358)
return (114);
else if (cod<358>334)
return (117);
else if (cod<334>307)
return (120);
else
return (1); //alarma
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Funci�n que convierte el c�digo en presi�n //
///////////////////////////////////////////////////////////////////////////////
int pres1 (long cod) //FAE
{
if (cod<1024>858)
return (0);
else if (cod<858>838)
return (10);
else if (cod<838>815)
return (20);
else if (cod<815>784)
return (30);
else if (cod<784>745)
return (40);
else if (cod<745>690)
return (50);
else if (cod<690>618)
return (60);
else if (cod<618>516)
return (70);
else if (cod<516>397)
return (80);
else if (cod<397>233)
return (90);
else
return (100);
}
int pres2 (long cod) //VDO
{
if (cod<227>0)
return (0);
else if (cod<319>227)
return (4);
else if (cod<393>319)
return (8);
else if (cod<451>393)
return (12);
else if (cod<498>451)
return (16);
else if (cod<539>498)
return (20);
else if (cod<571>539)
return (24);
else if (cod<600>571)
return (28);
else if (cod<623>600)
return (32);
else if (cod<639>623)
return (35);
else if (cod<655>639)
return (38);
else if (cod<715>655)
return (41+12/60*(cod-655));
else if (cod<748>715)
return (56+9/33*(cod-715));
else if (cod<803>748)
return (68+28/55*(cod-748));
else
return (100);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
|
taco.c
Code: |
#include "taco.h"
//#include "E:\Universidad\beca\Tacodinamo\LCD420.h"
#include <Flex_LCD420.c>
#include "tablas.h"
#include <float.h>
#define dir_min 10
#define dir_horah 11
#define dir_horal 12
long revoluciones(void);
void sobrevelocidad(float vel);
int sec=0,min,horah,horal,i=0;
long hora,rpm;
#int_TIMER1
TIMER1_isr() //interrupci�n del horometro y de lectura de la velocidad y del cad
{
int temperatura,j,var,var2;
float presion,voltA,voltB,ampA,ampB;
long cod_ad;
i+=1;
rpm = revoluciones();
//sobrevelocidad(rpm);
if (rpm >= 800) //en marcha
{
sec+=1;
if (sec == 60) //horometro
{
sec=0;
min = read_EEPROM (dir_min);
min+=1;
if (min == 60)
{
min=0;
horah = read_EEPROM (dir_horah);
horal = read_EEPROM (dir_horal);
hora = make16(horah,horal);
hora+=1;
horah = hora/256;
horal = hora%256;
}
write_eeprom(dir_min,min);
write_eeprom(dir_horah,horah);
write_eeprom(dir_horal,horal);
}
}
if (i == 3) //lecturas cada 3seg
{
i=0;
//////////////////////////////////
//////lecturas del cad////////////
//////////////////////////////////
set_adc_channel(0);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
if (PIN_B5 == 0) //FAE
temperatura= temp1(cod_ad);
else //VDO
if (cod_ad<1024>580)
temperatura= temp2(cod_ad);
else
temperatura= temp3(cod_ad);
set_adc_channel(1);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
/*if (PIN_B5 == 0) //FAE
presion=pres1(cod_ad);
else
presion=pres2(cod_ad);
*/
set_adc_channel(4);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
voltA=(cod_ad*5.0*6.0)/1024.0;
set_adc_channel(5);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
voltB=(cod_ad*5.0*6.0)/1024.0;
set_adc_channel(6);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
ampA=(cod_ad*5.0)/1024.0;
set_adc_channel(7);
delay_ms(1);
cod_ad=read_adc();
delay_ms(1);
ampB=(cod_ad*5.0)/1024.0;
///////////////////////////////
//fin de las lecturas del cad//
///////////////////////////////
///////////////////////////////
/////////uso del lcd///////////
///////////////////////////////
printf(lcd_putc, "\f");// Clear the LCD.
delay_ms(500);
if (rpm >= 800) //en marcha
{
if (PIN_C4)
{
printf(lcd_putc, "\fTemper Motor %3dC�",temperatura);
printf(lcd_putc, "\nPresion Aceite %fBar",presion);
printf(lcd_putc, "\nTacometro %LdRpm",rpm);
printf(lcd_putc, "\nHorometro:%05Ldh-%2dm",hora,min);
delay_ms(3000);
}
else
{
printf(lcd_putc, "\fTemper Motor %3dC�",temperatura);
printf(lcd_putc, "\nPresion Aceite %fBar",presion);
printf(lcd_putc, "\nTacometro %4LdRpm",rpm);
if(j != 0 )
{
j=0;
var=voltA*10;
var2=ampA*100;
printf(lcd_putc, "\nBateriaA %d,%01dV %d,%02dA",var/10,var%10,var2/100,var2%100);
}
else
{
j=1;
var=voltB*10;
var2=ampB*100;
printf(lcd_putc, "\nBateriaB %d,%01dV %d,%02dA",var/10,var%10,var2/100,var2%100);
}
delay_ms(3000);
}
}
else //parado
{
var=voltA*10;
var2=ampA*100;
printf(lcd_putc, "\fBateriaA %d,%01dV %d,%02dA",var/10,var%10,var2/100,var2%100);
var=voltB*10;
var2=ampB*100;
printf(lcd_putc, "\nBateriaB %d,%01dV %d,%02dA",var/10,var%10,var2/100,var2%100);
printf(lcd_putc, "\nHorometro:%5Ldh-%2dm",hora,min);
delay_ms(3000);
}
}
}
///////////////////////////////////////////////////////////////////////////////
/////////////lectura de las revoluciones///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
long revoluciones()
{
long contador;
int salir;
restart_wdt();
while (PIN_C2); //Esperar al 0
restart_wdt();
while (!PIN_C2); //Esperar al 1
restart_wdt();
contador=0;
salir=0;
do{
delay_us(100);
contador+=1;
salir=!PIN_C2;
}while ((salir==0) && (contador<40000));
if (salir==1)
{
salir=0;
do{
delay_us(100);
contador+=1;
salir=PIN_C2;
}while ((salir==0) && (contador<40000));
}
rpm=60*(1/(contador*0.0001));
return(rpm);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////////////alarma de sobrevelocidad//////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void sobrevelocidad(float vel)
{
int conf=0;
float limite;
conf = (PIN_B4<<4)||(PIN_B3<<3)||(PIN_B2<<2)||(PIN_B1<<1>= limite) //alarma
output_high(PIN_C7);
else
output_low(PIN_C7);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
setup_wdt(WDT_2304MS);
// The lcd_init() function should always be called once,
// near the start of your program.
lcd_init();
// Clear the LCD.
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Mar 22, 2007 4:25 am |
|
|
1) When posting code make sure to set the 'disable HTML in this post' option, now some of your posted code is corrupted because it is interpreted as HTML code. You can set this option as a default in your user profile.
2) Float calculations use a lot of program memory and processing power. A much better solution is to use 'fixed point' calculations, also called 'scaled integers'. For an explanation of this technique have a look at http://www.embedded.com/98/9804fe2.htm. |
|
|
Fabri
Joined: 22 Aug 2005 Posts: 275
|
|
|
|
|
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
|