Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f877 source code for lm35

Status
Not open for further replies.

maww

Newbie level 3
Newbie level 3
Joined
Nov 21, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,309
plz any one tell me the coding for lm35 using pic16f877. i m using ccs compiler,
 

Where you having a problem
here is an sample program

Code:
void main()
{
   float adc1,adc2;
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   
   while(1)
  {
   set_adc_channel(0);
   delay_ms(10);
   adc1=read_adc();
   adc2=(adc1*5)/1023;
   printf("adc=%f\r\n",adc2);
  }
}
 

did u know how to use adc? and what is your output..describe your project clearly..we cant imagine what your project looks like..
 

i want that the temperater from the lm35 is read by pic16f877 and display on lcd.
but the problem is that, it is not showing the o/p on lcd.
below is my coding.

float value,i,value1;

command()
{
portc0=0;
portc1=0;
portc2=1;
delay_ms(2);
portc2=0;
}
data()
{
portc0=1;
portc1=0;
portc2=1;
delay_ms(2);
portc2=0;
}
void main()
{
trisa0=1;
TRISC=0X00;
TRISD=0X00;
PORTD=0X38;
command();

PORTD=0X0F;
command();
while(1)
{
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_CHANNEL(0);
value=read_ADC();
value1 = value*((5.0*100.0)/1023.0);
delay_ms(2000);
write_eeprom(0X12,value1);

delay_ms(20);
i=read_eeprom(0X12);


PORTD=i;
data();
delay_ms(2000);
PORTD=0X01;
command();
}

}
 

the lcd doesn't show up the output because i see there is no LCD command in your program..what compiler that u use?
 

this is the source code using HITECH C compiler.... i am new here so nt really know how to use the forum to post pictures or folder.
hope this can help...




//==================inckude=================================
#include<pic.h>

//===============configuration==============================
__CONFIG (0x3F32);

//===============define IO port=============================
#define lcd PORTC
#define RS RA2
#define E RA5
#define CHANNEL0 0b10000001 // AN0
#define CHANNEL1 0b10001001 // AN1
#define buzzer RB5
#define fanA RB4
#define fanB RB3
#define ledA RB2
#define ledB RB1

//==============FUNCTION PTOTOTYPE=========================
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void read_adc(void);
unsigned short read_temp(void);

//====================MAIN================================
unsigned short result;
unsigned short temp,tempA,tempB;

void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result

//setting ADCON1 Register
ADCON1=0b11000101; // A/D result right justified,
// configure RA2 and RA5 as digital I/O

TRISA=0b11011011; //configure PORTA I/O direction
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output

PORTA=0;
PORTB=0;

while(1)
{
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //Lcd Return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //diplay on, cursor off and cursor blink off
send_config(0b00111000); //function set

lcd_goto(0); //cursor start from beginning

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('A');
send_char('=');

lcd_goto(20); //cursor go to 2nd line of the LCD

//display character on LCD
send_char(' ');
send_char('T');
send_char('E');
send_char('M');
send_char('P');
send_char('.');
send_char('B');
send_char('=');

while(1) //infinity loop
{
//sensor A
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
lcd_goto(8);

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempA=temp;

//sensor B
ADCON0=CHANNEL1; //CHANNEL0=0b10000001

lcd_goto(28);

read_adc();

temp=read_temp();
dis_num(temp/10);
send_char('.');
dis_num(temp%10);
send_char(0b11011111);
send_char('C');
send_char(' ');
send_char(' ');

tempB=temp;

if((tempA>400)&&(tempB<350)) // *****************************************
{ // * LED A and Fan A activated only for *
ledA=1; // * temperature A greater than 40'C *
ledB=0; // * and temperature B less than 35'C *
fanA=1; // *****************************************
fanB=0;
buzzer=0;
}

else if((tempB>350)&&(tempA<400)) // *****************************************
{ // * LED B and Fan B activated only for *
ledA=0; // * temperature A less than 40'C and *
ledB=1; // * temperature B greater than 35'C *
fanA=0; // *****************************************
fanB=1;
buzzer=0;
}

else if((tempB>350)&&(tempA>400)) // *****************************************************
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=1; // * activated for temperature A greater than 40'C *
ledA=1; // * and temperature B greater than 35'C *
fanA=1; // *****************************************************
fanB=1;
buzzer=1;
}

else if((tempB<350)&&(tempA<400)) // *****************************************************
{ // * All LED A & LED B, Fan A & Fan B and Buzzer *
ledB=0; // * disactivated for temperature A less than 40'C *
ledA=0; // * and temperature B less than 35'C *
fanA=0; // *****************************************************
fanB=0;
buzzer=0;
}

delay(2000);

}

}

}



//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
RS=0;
lcd=data;
delay(500);
e_pulse();
}

void e_pulse(void)
{
E=1;
delay(500);
E=0;
delay(500);
}

void send_char(unsigned char data)
{
RS=1;
lcd=data;
delay(500);
e_pulse();
}


void lcd_goto(unsigned char data)
{
if(data<16)
{
send_config(0x80+data);
}
else
{
data=data-20;
send_config(0xc0+data);
}
}


void lcd_clr(void)
{
RS=0;
send_config(0x01);
delay(600);
}


void dis_num(unsigned long data)
{
unsigned char hundred_thousand;
unsigned char ten_thousand;
unsigned char thousand;
unsigned char hundred;
unsigned char tenth;

hundred_thousand = data/100000;
data = data % 100000;
ten_thousand = data/10000;
data = data % 10000;
thousand = data / 1000;
data = data % 1000;
hundred = data / 100;
data = data % 100;
tenth = data / 10;
data = data % 10;

if(hundred_thousand>0)
{
send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code
send_char(ten_thousand + 0x30);
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}

else if(ten_thousand>0)
{
send_char(ten_thousand + 0x30); //0x30 added to become ASCII code
send_char(thousand + 0x30);
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(thousand>0)
{
send_char(thousand + 0x30); //0x30 added to become ASCII code
send_char(hundred + 0x30);
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(hundred>0)
{
send_char(hundred + 0x30); //0x30 added to become ASCII code
send_char(tenth + 0x30);
send_char(data + 0x30);
}
else if(tenth>0)
{
send_char(tenth + 0x30); //0x30 added to become ASCII code
send_char(data + 0x30);
}
else send_char(data + 0x30); //0x30 added to become ASCII code
}

void increment(unsigned long data)
{
unsigned short j;
for(j=10;j>0;j--)
{ lcd_goto(32);
data=data+1;
dis_num(data);
delay(10000);
}

}

//==================subroutine ADC=========================

void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADGO = 1; //ADGO is the bit 2 of the ADCON0 register
while(ADGO==1); //ADC start, ADGO=0 after finish ADC progress
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result_temp+=result;
}
result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;

}

//==================subroutine DELAY==========================
void delay(unsigned short i)
{
for(;i>0;i--);
}
 
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_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;
// End LCD module connections
 

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_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;
// End LCD module connections


While this is correct, this is only for mikroC compiler and won't work with other compilers.

Hope this helps.
Tahmid.
 

Which LCD you are using,Is your LCD connections are same as described in driver files?
Is your LM35 is connected to channel (0) of the analog port.
Simply try to see the lm35 results on PC using Hyperterminal
Have you tried a simple code to display HELLO on the LCD?
If both work fine then try to display the results on LCD.
Post your new code which is based on your compiler, so we can try to figure out the mistakes?

Best Regards
 

the code is work perfectly... i mean i do not know how to upload the schematic diagram...:)
 

My previous question and suggestions was for "maww"

@ PIPT
you can attach your file here by clicking on the attachment button, then we can see the picture or you first upload your picture on any web site and paste that link by click the "insert image" button.

Best Regards
 
@maww
Do the following changes, i am attaching the lcd driver file,include its location in the main file then compile and import into the proteus then see if the lcd works or not,also change the connections as i am mentioning below.
Make your lcd connection like that
LCD-------------Microcontroller
PIN ------------------PIN

1->Gnd
2->Vcc
3->Gnd
4------------------->22
5->Gnd
6->------------------>21
11------------------->27
12------------------->28
13------------------->29
14------------------->30
Code:
#include <16F877A.H> 
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP 
#use delay(clock = 20000000) 

#include <give the location of the lcd driver file here\Flexlcd.c>    

//=================================== 
void main() 
{ 
 // The lcd_init() function should always be called once, 
 // near the start of your program. 
 lcd_init(); 

 // Clear the LCD. 
 printf(lcd_putc, "\f"); 
 delay_ms(500); 

 while(1) 
 { 
  printf(lcd_putc, "\fABCDEFGHIJKLMNOPQRST"); 
  printf(lcd_putc, "\nabcdefghijklmnopqrst"); 
  delay_ms(3000); 
 } 
}
 

Attachments

  • Flexlcd.rar
    2.1 KB · Views: 155

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top