Page 1 of 1

PIC16F877A, LM35 and LCD

Posted: 29 May 2012 15:24
by Nass
HELP PLZZ!!! :cry:

I need to write a program that
- displays the temperature measured by the lm35 sensor on the first line of LCD display.
- set a threshold temperature using RV1, display the threshold temp on the second line of the LCD display.
- if the measured temperature is larger than the threshold temperature a red LED turns on.

The relevant circuit diagram is attached.

Here's my attempt; (although incomplete..and i don't know where to go from here)

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

unsigned int read_value=0;
unsigned int value=0;
unsigned int temp;

void main()
{
TRISA.F0 = 1; // Config '0'th channel as input.
TRISB = 0x00; // Config PORTB for LCD display.

ADCON1 = 0x00; // All channels are config as analog I/p.

Lcd_Init(); // Initialize LCD
Lcd_cmd(_lcd_clear);
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_out(1,1,"Temperature:");

while(1)
{

read_value = Adc_Read(0)>>2;

value = read_value * (5000/255); // Resolution for 5 volts.

temp = value/1000;
lcd_chr_cp(48+temp);

temp = (value/100)%10;
lcd_chr_cp(48+temp);

temp = (value/10)%10;
lcd_chr_cp(48+temp);

temp = value%10;
lcd_chr_cp(48+temp);

delay_ms(50);
}
}


Re: PIC16F877A, LM35 and LCD

Posted: 29 May 2012 18:23
by MARIO
Well,

I won't discuss the logic but you are not reading AN2 (LM35) neither AN4 (reference) according to the schematic.
The only ADC reading you did is:

Code: Select all

read_value = Adc_Read(0)>>2;
This is AN0.

I'm not sure but I think you have do disable comparators too. See CMCON register.

And please, put your code inside code tags.
Code_Tags.JPG
Code_Tags.JPG (8.92 KiB) Viewed 17184 times
BR

Re: PIC16F877A, LM35 and LCD

Posted: 29 May 2012 18:55
by jtemples
i don't know where to go from here
My advice: start with something so simple that you can get it done.

Take a logical approach to your task and divide it up into sub-tasks that you can handle:

1) can you display something on the lcd?
2) can you adc a channel?
3) once you have conquered the two, integrate them together.

At this point, you can do neither so it is not possible. For you to move forward, you have to go back to basics.

Re: PIC16F877A, LM35 and LCD

Posted: 29 May 2012 19:22
by Nass
The problem is i really have no way of figuring out how to write this program. I'm self-studying because our engineer was really of no help. He gave us really basic stuff like converting from voltage to binary and sending it to LEDs. That I can do but how to we display from a temp sensor to an LCD??

And plus I have no reference book to refer to..if someone could at least recommend a text book I can refer to plz?

@MARIO: Did you mean enable comparators?

Any help would be appreciated.
Thanks in advance.

Re: PIC16F877A, LM35 and LCD

Posted: 30 May 2012 00:04
by jtemples
The problem is i really have no way of figuring out how to write this program.
Then go back to a point where you do know how to write a program. Start from there.
I'm self-studying because our engineer was really of no help. He gave us really basic stuff like converting from voltage to binary and sending it to LEDs.
1. looks like you have a very good teacher;
2. looks like you didn't learn from him, at least not exhibited in your code.
That I can do but how to we display from a temp sensor to an LCD??
Again, go back to the basics. You have to start walking before you attempt running. You are trying to accomplish a complex task without having the skills to accomplish its basic elements.

Re: PIC16F877A, LM35 and LCD

Posted: 30 May 2012 00:39
by MARIO
@Nass.

No. DISable comparators. But I tested and no need to disable them. So no worry about it for now.

Ok. I understand your difficulty. But you have to study MKC libraries and datasheet. Specially Conversion and ADC libraries.
I am providing you, based on your schematic and on your code. You should also study MKC provided examples.

Code: Select all

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

unsigned int read_value=0, ref_value;
unsigned int value=0;
unsigned int temp;
char aux[7];

void main()
{
     TRISD0_bit = 0;
     RD0_bit = 0;
     ADCON1 = 0x00; // All channels are config as analog I/p.
     
     ADC_Init();
     Lcd_Init(); // Initialize LCD
     Lcd_cmd(_lcd_clear);
     Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
     Lcd_out(1,3,"Temperature:");
     LCD_Out(2,1,"Eff:    Ref:");

     while(1)
     {
             read_value = Adc_Get_Sample(2)>>2;
             ref_value  = ADC_Get_Sample(4)>>2;
             
             if (read_value < ref_value)
                RD0_bit = 1; //turns LED ON
             else
                RD0_bit = 0; //turns LED OFF
                
             value = (unsigned int) read_value * (500./255); // Resolution for 5 volts, 8 bits
             ByteToStr(value,Ltrim(aux));
             LCD_Out(2,5, aux); //shows reading temperature
             
             value = (unsigned int) ref_value * (500./255);
             ByteToStr(value,Ltrim(aux));
             LCD_Out(2,13, aux); //shows reference temperature

             delay_ms(50);
     }
}
Try to understand what was done.

I hope this help you.
BR

Re: PIC16F877A, LM35 and LCD

Posted: 30 May 2012 14:31
by Nass
@MARIO I went through your code and it was very helpful. After studying it I finally understood and I tried to write my own..and after linking it to proteus I still get an error:

Code: Select all

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections

float adc1, temp;
char t1[13];

float adc2, ref_temp;
char t2[13];

void main()
{
Lcd_init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms (100);
ADCON1= 0;
TRISD = 0;
PORTE = 0;

while(1)
{
adc1 = ADC_read(2);
temp = adc1*5000/1023;
floatToStr(temp,t1);
lcd_out (1, 1, "Temp:");
lcd_out(1, 6, t1);
lcd_out(1, 15, "C");

adc2 = ADC_read(5);
ref_temp = adc2*5000/1023;
floatToStr(ref_temp,t2);
lcd_out (2, 2, "Ref:");
lcd_out(2, 7, t2);
lcd_out(2, 16, "C");

if (temp > ref_temp)
{
PORTD = 1;
}
else
PORTD = 0;

}
}
The error says: No model specified for RV1. Simulation failed due to partition analysis errors.

This is so frustrating when I built the program and there were no errors I was so relieved but there's still an error in the simulation. Thanks..

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 11:40
by jtemples
The error says: No model specified for RV1. Simulation failed due to partition analysis errors.

This is so frustrating ...
Instead of getting frustrated, maybe you should try to figure out what that error means and then deal with it?

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 20:34
by Nass
:!: Are you serious?? Do you think I haven't tried? You're not really helping you know.. :|
I gave it a try and I still get an error and since I still can't figure out what's wrong I posted it here. So if you can't help, instead of acting all smart don't bother answering. :|

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 20:43
by MARIO
Nass wrote:The error says: No model specified for RV1. Simulation failed due to partition analysis errors.
There is another var resistor called POT-HG. Replace it and see.
Not all components of Proteus is capable of simulation.

BR.

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 20:54
by p.erasmus
Typical post again.if the code does not work in Proteus it must be the compiler that has a bug
never heard of bugs in Proteus or the user that does not know what he is doing in Proteus :P
The error says: No model specified for RV1. Simulation failed due to partition analysis errors.
Did you code this RV1 in your code if not then how can it be your code may be you should think in Proteus what is RV1 :lol:

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 22:50
by Mince-n-Tatties
working proteus project with hex.
test1.zip
(23.04 KiB) Downloaded 721 times

Re: PIC16F877A, LM35 and LCD

Posted: 31 May 2012 23:33
by Nass
@Mince: Thank you so much i really appreciate your help! :D

but..how can i see the code? Because I just need to add the code for the LED..

Thanx again.

Re: PIC16F877A, LM35 and LCD

Posted: 01 Jun 2012 09:28
by Mince-n-Tatties
Nass wrote:but..how can i see the code?
the code is exactly that code which MARIO posted for you.