Microcontroller › PIC › help interfacing two lm35 pic16f877a and lcd
- This topic has 5 replies, 3 voices, and was last updated 11 years, 9 months ago by Hemraj.
-
AuthorPosts
-
November 18, 2012 at 9:56 am #3905jeffersonParticipant
Good day masters:
I am using mikroC. I have the problem of giving an output of 5V or 0V in portD. I have two sensors connected to AN0 an AN1. It gives 2.5V and -2.5V as exhange for 5V and OV respectively. I think I have the problem in setting VREF+ and VREF-..Could you please help me guys regarding this? Any modification or suggestion in my code below?
Thanks in advance.
Hope to hear you soon.
CODE:
//Target PIC: PIC16F877A
//dual lm35 in one lcd with interrupt to port D
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;
unsigned int Ch0;
unsigned int Ch1;
unsigned long ADRead;
unsigned int vDisp[7];
unsigned char Display[7];
unsigned int ADCRead(unsigned char channel){
unsigned char l_byte, h_byte;
unsigned int ADR;
#define Temp35 71
#define Temp50 102
#define Temp38 77
#define Temp55 112
//ADCON0 = 0x81; //Change channel
//delay_us(50); //Acquisition Delay
GO_DONE_bit = 1; //Set GO_DONE bit to start conversion
while (GO_DONE_bit == 1); //Wait for bit to be cleared
//If bit is cleared, this means conversion is over
l_byte = ADRESL;
h_byte = ADRESH;
ADR = (h_byte<<|l_byte;
return ADR;
}
void main() {
PORTA = 0;
TRISA = 0X03;
CMCON = 7; //Disable comparator
PORTB = 0;
TRISB = 0;
PORTD = 0;
TRISD = 0;
ADCON1 = 0xC4;
ADCON0 = 0x81;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1, 1, "Temp1:");
//Display = "+125 'C";
Display[4] = 39; //'
Display[5]= 'C';
LCD_Out(2,1, "Temp2:");
Display[9] = 39;
Display[10] = 'C';
ADC_Init();
while (1){
ADRead = (ADC_Get_Sample(0) * 500) >> 10;
vDisp[0] = ADRead / 100;
vDisp[1] = (ADRead / 10) % 10;
vDisp[2] = ADRead % 10;
Display[1] = vDisp[0] + 48;
Display[2] = vDisp[1] + 48;
Display[3] = vDisp[2] + 48;
LCD_Chr(1, 8, Display[0]);
LCD_Chr(1, 9, Display[1]);
LCD_Chr(1, 10, Display[2]);
LCD_Chr(1, 11, Display[3]);
LCD_Chr(1, 12, Display[4]);
LCD_Chr(1, 13, Display[5]);
//LCD_Out(1, 8, ); // 'Show temperature
delay_ms(200); //200ms delay for waiting
Ch0 = ADCRead(0); //Gets reading from channel 0
if (Ch0 < Temp35){
RD0_bit = 1; //Turn on
}
else{
RD0_bit = 0; //Turn off
if(Ch0 > Temp50){
RD1_bit = 1; //Turn on
}
else{
RD1_bit = 0; //Turn off
}
ADRead = (ADC_Get_Sample(1) * 500) >> 10;
vDisp[4] = ADRead / 100;
vDisp[5] = (ADRead / 10) % 10;
vDisp[6] = ADRead % 10;
Display[1] = vDisp[4]+ 48;
Display[2] = vDisp[5] + 48;
Display[3] = vDisp[6] + 48;
LCD_Chr(2, 8, Display[0]);
LCD_Chr(2, 9, Display[1]);
LCD_Chr(2, 10, Display[2]);
LCD_Chr(2, 11, Display[3]);
LCD_Chr(2, 12, Display[4]);
LCD_Chr(2, 13, Display[5]);
//LCD_Out(1, 8, ); // 'Show temperature
delay_ms(200); //200ms delay for waiting
Ch1 = ADCRead(1); //Gets reading from channel 0
if (Ch1 < Temp38){
RD2_bit = 1; //Turn on
}
else{
RD2_bit = 0; //Turn off
}
if(Ch1 > Temp55){
RD3_bit = 1; //Turn on
}
else{
RD3_bit = 0; //Turn off
}
}
}November 18, 2012 at 5:43 pm #8757AJISH ALFREDParticipantHi Jefferson,
We are glad to debug your code. But befor going through the code please give us bit more details about the trouble you are facing with it. You haven’t mentioned anything about the purpose of this coding and what happens at the output while running the code. The more details you share about your project, we may get more clues.
November 19, 2012 at 6:02 am #8763jeffersonParticipantsir,
my problem is that I am getting 2.5V and -2.5V in portD instead of 5V and 0V respectively. Is it possible to measure 5V and 0V in that port? Or do I need revision of my code? by the way I am using external reference voltage(+VREF and -VREF). There is a condition:
FOR FIRST SENSOR:
Ch0 = ADCRead(0); //Gets reading from channel 0
if (Ch0 < Temp35){ //if the temperature is less than 35 degrees RD0 gives 5V if not 0V.
RD0_bit = 1; //Turn on
}
else{
RD0_bit = 0; //Turn off
if(Ch0 > Temp50){ // if the temperature is greater than 50 degrees RD1 gives 5V otherwise 0V
RD1_bit = 1; //Turn on
}
else{
RD1_bit = 0; //Turn off
}FOR SECOND SENSOR:
Ch1 = ADCRead(1); //Gets reading from channel 0
if (Ch1 < Temp38){ //whenever second sensor reaches less than 38 degrees RD2 gives 5V and if not 0 V
RD2_bit = 1; //Turn on
}
else{
RD2_bit = 0; //Turn off
}
if(Ch1 > Temp55){ //if temperature is greater than 55 degrees RD3 gives 5v otherwise 0V
RD3_bit = 1; //Turn on
}
else{
RD3_bit = 0; //Turn off
}Likewise the circuit is connected to LCD which displays the amount of temperature,.
hope you understand me..
thank you and more power.
November 19, 2012 at 4:28 pm #8765AJISH ALFREDParticipantI think you forgot to set the direction of PortD. The port will remain as input port until we configure it as output. The lines “RD3_bit = 1” will generate a 5V at the PortD’s pin only if the PortD is set as output port.
November 20, 2012 at 1:03 pm #8770jeffersonParticipantI already configured it as output sir by having my code PORTD = 0; ?thanks
December 18, 2012 at 9:53 am #8850HemrajParticipantUse TRIS registor for direction. Not PORTD.
-
AuthorPosts
- You must be logged in to reply to this topic.