Created
November 27, 2016 03:43
-
-
Save jsantanders/5e9d5d5995a5c9c030ccc6e775bc8e03 to your computer and use it in GitHub Desktop.
MikroC code for MPX4115 sensor and PIC18F4550
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************** | |
'* Nombre : Altimetro.c * | |
'* Autores : I. Franchi, J. Santander * | |
'* Fecha : 03-06-2015 * | |
'* Version : 1.0 * | |
'* MCU : PIC18F4550 * | |
'* Módulos : MPX4115, TERMOCUPLA, LCD * | |
'****************************************************************/ | |
#include <math.h> | |
//Variables globales | |
long int dat, datemp; //Variable para la data del ADC | |
float tv, tr, temp, y, TF, ERROR, datf; //Variables de las ecuaciones | |
float pr, presion, pres_atm, pres_psi, alt; //Variables cálculo presión y altura | |
int cnt=0, cnt1=0, b=0, k=1; | |
char txt[15]; | |
// Conexiones para la LCD | |
sbit LCD_RS at LATB4_bit; | |
sbit LCD_EN at LATB5_bit; | |
sbit LCD_D4 at LATB0_bit; | |
sbit LCD_D5 at LATB1_bit; | |
sbit LCD_D6 at LATB2_bit; | |
sbit LCD_D7 at LATB3_bit; | |
sbit LED at LATB7_bit; | |
sbit LCD_RS_Direction at TRISB4_bit; | |
sbit LCD_EN_Direction at TRISB5_bit; | |
sbit LCD_D4_Direction at TRISB0_bit; | |
sbit LCD_D5_Direction at TRISB1_bit; | |
sbit LCD_D6_Direction at TRISB2_bit; | |
sbit LCD_D7_Direction at TRISB3_bit; | |
void main() { | |
TRISC = 0xFF; // PORTC como entrada | |
ADCON0 = 0x05; // AN5, AN7 Anlg | |
TRISB7_bit = 0; | |
Lcd_Init(); // Inicialización LCD | |
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off | |
Lcd_Cmd(_LCD_CLEAR); // Limpiar pantalla | |
Lcd_Out(1, 5, "INICIANDO"); | |
Delay_ms(1000); //Tiempo para permitir estabilizar el captor | |
Lcd_Cmd(_LCD_CLEAR); // Limpiar pantalla | |
do { | |
dat = ADC_Read(5); // Lectura tensión MPX4115 | |
datf = (float)dat*5/1024; // Valor decimal equivalente de la conversión | |
presion= (0.475+datf)/0.045; //Lectura presión en Kpa | |
delay_us(20); | |
datemp = ADC_Read(7); | |
tv = 5.0 * datemp / 1024.0; | |
tr = tv * 10000.0 / (5.0 - tv); | |
y = log(tr/20000.0); | |
y = (1.0/298.15) + (y *(1.0/4050.0)); | |
temp=1.0/y; | |
temp = temp -273.15; | |
if (temp>=0 && temp<=85) TF=1.0; | |
else TF=3.0; | |
ERROR = TF * 1.5; //Cálculo del error de presión con la temperatura | |
presion=presion-ERROR; //Presión en Kpa | |
pres_atm = presion * 0.0098692; //Presión en Atm | |
pres_psi = presion * 0.1450377; //Presión en Psi | |
alt = -7990.652789*log(presion/101.304); //Altura | |
if(presion<16 || presion>=100){ | |
b=1; //Mantener el captor dentro del rango de variación prmitida | |
k=0; | |
} | |
else { | |
b=0; | |
if(k==0){ | |
Lcd_Cmd(_LCD_CLEAR); | |
k=1; | |
} | |
} | |
if(Button(&PORTC, 0, 110, 0) ) | |
{ | |
cnt++; | |
Lcd_Cmd(_LCD_CLEAR); | |
} | |
if(cnt>=3) cnt=0; | |
if(b==0){ | |
switch (cnt) { //Según número veces pulsa botón se elige menú | |
case 0: | |
Lcd_Out(1, 1, "Altura:"); | |
sprintf(txt,"%5.2f ",alt); // Variable a cadena de caracteres | |
Lcd_Out(2,1,txt); | |
Lcd_Out(2,9, " m "); | |
break; | |
case 1: | |
Lcd_Out(1, 1, "Presion:"); | |
if(Button(&PORTC, 1, 110, 0)) | |
{ | |
cnt1++; | |
Lcd_Cmd(_LCD_CLEAR); | |
} | |
if(cnt1>=3) cnt1=0; | |
if(cnt1==0) | |
{ | |
sprintf(txt,"%3.2f ",presion); // Variable a cadena de caracteres | |
Lcd_Out(2,1,txt); | |
Lcd_Out(2,9, "Kpa"); | |
} | |
else if(cnt1==1) | |
{ | |
sprintf(txt,"%1.3f ",pres_atm); | |
Lcd_Out(2,1,txt); | |
Lcd_Out(2,9, "atm"); | |
} | |
else if(cnt1==2) | |
{ | |
sprintf(txt,"%3.2f ",pres_psi); // Variable a cadena de caracteres | |
Lcd_Out(2,1,txt); | |
Lcd_Out(2,9, "psi"); | |
} | |
break; | |
case 2: | |
Lcd_Out(1, 1, "Temperatura:"); | |
sprintf(txt,"%3.2f ",temp); // Variable a cadena de caracteres | |
Lcd_Out(2,1,txt); | |
Lcd_Out(2,9, "C"); | |
break; | |
} | |
} | |
else if(b==1){ | |
Lcd_Out(1, 1, "ERROR: "); | |
lcd_Out(2,1, "OUT OF RANGE"); | |
} | |
Delay_ms(1); // Retardo arbitrario | |
}while(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment