rahulkrrish
Newbie level 2

hi..what exactly are you making?a temperature sensor?can u send me the schematic diagram of your project?rahulkrrish said:I am using MPLAB IDE for simulating...can u give me a c source code for PIC16f877 and Lm35 interface
// PIC16F877 + Nokia 3310 LCD
// Hi-Tech C
#include <pic.h>
__CONFIG( WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGEN & UNPROTECT);
#include "delay.h"
#include "stdlib.h"
#include "lcd3310.c"
void initialize(void)
{
GIE=0; // disable interrupts
ADCS1 = 0; //select Fosc/8
ADCS0 = 1;
ADCON1=0; // A/D port configuration 0
ADFM = 1; //right justified result
ADON=1; // turn on the AD conversion module
lcd_init(); //initialize 3310LCD
lcd_clear(); //clear screen
}
/* return a 10-bit result */
unsigned int read_adc(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result
}
float mpx4115a_read(void) // for pressure sensor MPX4115A
{
int p_an,adc_sample;
float p_hPa_float,pres_mmHg;
p_an=0; // reset value
DelayMs(5);
adc_sample = read_adc(1);
// 10 bits ADC is 1024 steps --> -Vref is 0 V and +Vref is 5 V
// 760 mmHg = 101.325 kPa-->1 pascal is 0.0075006168 mm Hg at 0 degrees Celsius.
// 1 mm Hg at 0 degrees Celsius is 133.3223684218 pascals.
// so ADC range goes from 0V (0 hPa) to 5 V (1223 hPa) --> each step is 1.12 hPa
if (adc_sample>1023)return; // error detection
p_an=adc_sample; //
p_hPa_float=((float)p_an*(5.0*100/1023.0));
pres_mmHg=p_hPa_float*133.3223684218;
return (pres_mmHg);
}
void main(void)
{
long int temp,pres;
initialize();
lcd_gotoxy(0,0);lcd_puts("Temp: ");
lcd_gotoxy(70,0);lcd_puts("C");
lcd_gotoxy(0,3);
lcd_puts("Pres: ");
lcd_gotoxy(60,3);
lcd_puts("mmHg");
while(1)
{
temp =read_adc(0)*((5.0*100.0)/1023.0);
if (temp>99)
{
display_digit(1,31,(temp/100)%10); //hundreds
display_digit(1,40,(temp/10)%10); //tenths
display_digit(1,50,temp%10); //ones
}
else
{
lcd_gotoxy(30,0); lcd_puts(" ");
lcd_gotoxy(30,1); lcd_puts(" ");
display_digit(1,40,(temp/10)%10); //tenths
display_digit(1,50,temp%10); //ones
}
DelayMs(2500);
pres =mpx4115a_read();
if (pres>99)
{
display_digit(3,30,(pres/100)%10); //hundreds
display_digit(3,40,(pres/10)%10); //tenths
display_digit(3,50,pres%10); //ones
}
else
{
lcd_gotoxy(30,3); lcd_puts(" ");
lcd_gotoxy(30,4); lcd_puts(" ");
display_digit(3,40,(pres/10)%10); //tenths
display_digit(3,50,pres%10); //ones
}
DelayMs(2500);
}
}
// PIC16F877 + Nokia 3310 LCD
// Hi-Tech C
#include <pic.h>
__CONFIG( WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGEN & UNPROTECT);
#include "delay.h"
#include "stdlib.h"
#include "lcd3310.c"
void initialize(void)
{
GIE=0; // disable interrupts
ADCS1 = 0; //select Fosc/8
ADCS0 = 1;
ADCON1=0; // A/D port configuration 0
ADFM = 1; //right justified result
ADON=1; // turn on the AD conversion module
lcd_init(); //initialize 3310LCD
lcd_clear(); //clear screen
}
/* return a 10-bit result */
unsigned int read_adc(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
DelayMs(10);
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result
}
float mpx4115a_read(void) // for pressure sensor MPX4115A
{
int p_an,adc_sample;
float pres_mmHg;
p_an=0; // reset value
DelayMs(5);
adc_sample = read_adc(1);
DelayMs(10);
// 10 bits ADC is 1024 steps --> -Vref is 0 V and +Vref is 5 V
// 760 mmHg = 101.325 kPa
// Vout = VDD*(0.009*P - 0.095) it's a linear ecuation --> y = mX + b
// m = Prange / ADrange = 100 / 920 and b = Pmax - (m * ADmax) = 115 - (0.1087 * 975)
// P = 0.1087 * ADval + 9.0175
if (adc_sample>1023)return; // error detection
p_an=adc_sample;
pres_mmHg=0.1087*p_an+9.0175;
return (pres_mmHg);
}
void main(void)
{
long int temp,pres;
initialize();
lcd_gotoxy(0,0);lcd_puts("Temp: ");
lcd_gotoxy(70,0);lcd_puts("C");
lcd_gotoxy(0,3);
lcd_puts("Pres: ");
lcd_gotoxy(60,3);
lcd_puts("mmHg");
while(1)
{
temp =read_adc(0)*((5.0*100.0)/1023.0);
if (temp>99)
{
display_digit(1,31,(temp/100)%10); //hundreds
display_digit(1,40,(temp/10)%10); //tenths
display_digit(1,50,temp%10); //ones
}
else
{
lcd_gotoxy(30,0); lcd_puts(" ");
lcd_gotoxy(30,1); lcd_puts(" ");
display_digit(1,40,(temp/10)%10); //tenths
display_digit(1,50,temp%10); //ones
}
DelayMs(250);
pres =mpx4115a_read();
if (pres>99)
{
display_digit(3,30,(pres/100)%10); //hundreds
display_digit(3,40,(pres/10)%10); //tenths
display_digit(3,50,pres%10); //ones
}
else
{
lcd_gotoxy(30,3); lcd_puts(" ");
lcd_gotoxy(30,2); lcd_puts(" ");
display_digit(3,40,(pres/10)%10); //tenths
display_digit(3,50,pres%10); //ones
}
DelayMs(250);
}
}
program PressureSensor
'Microcontroller:ATMEGA88
'Compiler: mikroBASIC PRO for AVR
'Programmer: Syed Tahmid Mahbub
dim LCD_RS as sbit at PORTB2_bit
dim LCD_EN as sbit at PORTB3_bit
dim LCD_D4 as sbit at PORTB4_bit
dim LCD_D5 as sbit at PORTB5_bit
dim LCD_D6 as sbit at PORTB6_bit
dim LCD_D7 as sbit at PORTB7_bit
dim LCD_RS_Direction as sbit at DDB2_bit
dim LCD_EN_Direction as sbit at DDB3_bit
dim LCD_D4_Direction as sbit at DDB4_bit
dim LCD_D5_Direction as sbit at DDB5_bit
dim LCD_D6_Direction as sbit at DDB6_bit
dim LCD_D7_Direction as sbit at DDB7_bit
dim Pressure as longword
dim Vout as longword
dim vString as string[7]
dim vWord as word[3]
main:
LCD_Init()
LCD_Cmd(_LCD_CLEAR)
LCD_Cmd(_LCD_CURSOR_OFF)
LCD_Out(1, 1, "Pressure:")
vString = "115kPa"
while true
Vout = (ADC_Read(0) * 500) >> 10 'Get Vout
Pressure = ( (2 * Vout) + 95 ) div 9 'Formula for getting Pressure, as stated in datasheet
vWord[0] = (Pressure div 100) + 48
vWord[1] = ( (Pressure div 10) mod 10) + 48
vWord[2] = (Pressure mod 10) + 48
vString[0] = vWord[0]
vString[1] = vWord[1]
vString[2] = vWord[2]
LCD_Out(1, 11, vString)
wend
end.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?