Halloween Offer 2014
It is currently 30 Oct 2014 14:42

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: PIC16F877A, LM35 and LCD
PostPosted: 29 May 2012 15:24 
Offline

Joined: 29 May 2012 15:03
Posts: 5
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);
}
}



Attachments:
circuit.jpg
circuit.jpg [ 187.22 KiB | Viewed 3176 times ]
Top
 Profile  
 
PostPosted: 29 May 2012 18:23 
Offline

Joined: 18 Aug 2008 22:13
Posts: 920
Location: Brasil
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:
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.
Attachment:
Code_Tags.JPG
Code_Tags.JPG [ 8.92 KiB | Viewed 3166 times ]

BR


Top
 Profile  
 
PostPosted: 29 May 2012 18:55 
Offline

Joined: 22 Jan 2012 05:46
Posts: 258
Quote:
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.


Top
 Profile  
 
PostPosted: 29 May 2012 19:22 
Offline

Joined: 29 May 2012 15:03
Posts: 5
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.


Top
 Profile  
 
PostPosted: 30 May 2012 00:04 
Offline

Joined: 22 Jan 2012 05:46
Posts: 258
Quote:
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.

Quote:
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.

Quote:
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.


Top
 Profile  
 
PostPosted: 30 May 2012 00:39 
Offline

Joined: 18 Aug 2008 22:13
Posts: 920
Location: Brasil
@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.
// 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


Last edited by MARIO on 31 May 2012 21:24, edited 1 time in total.

Top
 Profile  
 
PostPosted: 30 May 2012 14:31 
Offline

Joined: 29 May 2012 15:03
Posts: 5
@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:

// 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..


Top
 Profile  
 
PostPosted: 31 May 2012 11:40 
Offline

Joined: 22 Jan 2012 05:46
Posts: 258
Quote:
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?


Top
 Profile  
 
PostPosted: 31 May 2012 20:34 
Offline

Joined: 29 May 2012 15:03
Posts: 5
:!: 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. :|


Top
 Profile  
 
PostPosted: 31 May 2012 20:43 
Offline

Joined: 18 Aug 2008 22:13
Posts: 920
Location: Brasil
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.


Top
 Profile  
 
PostPosted: 31 May 2012 20:54 
Offline

Joined: 05 Mar 2009 10:28
Posts: 3104
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

Quote:
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:

_________________
P.Erasmus
Saratov,Russia
--------------------------------------------------------------


Top
 Profile  
 
PostPosted: 31 May 2012 22:50 
Offline

Joined: 25 Dec 2008 15:22
Posts: 2655
Location: Scotland
working proteus project with hex.

Attachment:
test1.zip [23.04 KiB]
Downloaded 229 times

_________________
Best Regards

Mince


Top
 Profile  
 
PostPosted: 31 May 2012 23:33 
Offline

Joined: 29 May 2012 15:03
Posts: 5
@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.


Top
 Profile  
 
PostPosted: 01 Jun 2012 09:28 
Offline

Joined: 25 Dec 2008 15:22
Posts: 2655
Location: Scotland
Nass wrote:
but..how can i see the code?


the code is exactly that code which MARIO posted for you.

_________________
Best Regards

Mince


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group