I recently finished a program that uses DS1820 temperature sensor with PIC16F877 to measure its whole range -ve and +ve.
But I don't know how to read a temp. in the code itself, as I just (copy & paste it) slightly modified it from the internet to be able to run on PIC16F877 (I tried the code in MikroC program's Help, but didn't work on Proteus ISIS) .
I want (porta.b5 = 1) when the temp. is +5°C, else porta.b5=0
The problem is I don't understand how the code is running, I tried (text[2] == 5) porta.b5=1, and it didn't work (as text is an array of char)
Here is the code:
But I don't know how to read a temp. in the code itself, as I just (copy & paste it) slightly modified it from the internet to be able to run on PIC16F877 (I tried the code in MikroC program's Help, but didn't work on Proteus ISIS) .
I want (porta.b5 = 1) when the temp. is +5°C, else porta.b5=0
The problem is I don't understand how the code is running, I tried (text[2] == 5) porta.b5=1, and it didn't work (as text is an array of char)
Here is the code:
Rich (BB code):
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
unsigned short C_Neg=0;
char *text = "000.0000";
unsigned temp, templ, temph;
unsigned int temp2write, tempinc, temp_fraction;
char temp_whole;
void main()
{
trisc = 0;
portc = 0;
trisa = 1;
porta.b0 = 0;
ADCON1 = 7; // Configure AN pins as digital I/O
Lcd_Init(); // Lcd_Init_EP4, see Autocomplete
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, " Temperature: ");
// Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223);
Lcd_Chr(2,14,'C');
OPTION_REG = 0x00; // Clear INTEDG, External Interrupt on falling edge
INTCON.INTF = 0; // Clear interrupt flag prior to enable
INTCON.INTE = 1; // enable INT interrupt
INTCON.GIE = 1; // enable Global interrupts
//--- main loop
while (1)
{
//--- perform temperature reading
Ow_Reset(&PORTa,0); // Onewire reset signal
Ow_Write(&porta,0,0xCC); // Issue command SKIP_ROM
Ow_Write(&porta,0,0x44); // Issue command CONVERT_T
INTCON.GIE = 1; // 1-wire library disables interrpts
Delay_ms(750);
Ow_Reset(&porta,0);
Ow_Write(&porta,0,0xCC); // Issue command SKIP_ROM
Ow_Write(&porta,0,0xBE); // Issue command READ_SCRATCHPAD
templ = Ow_Read(&porta,0);
TempH = Ow_Read(&PORTA, 0);
temp = (temph << 8) + templ; //temp(temp_value)
// check if temperature is negative
if (temp & 0x8000)
{
C_Neg = 1;
text[0] = '-'; //text(tempc)
temp = ~temp + 1; //temp(temp_value)
}
else C_Neg = 0;
// extract temp_whole
temp_whole = temp >> 1;
if (temp & 0x0001)
{ // LSB is 0.5C
temp_fraction = 5;
}
else temp_fraction = 0;
tempinc = temp_whole*10+temp_fraction;
// convert temp_whole to characters
if (!C_Neg)
{
if (tempinc/1000) text[0] = tempinc/1000 +48;
else text[0] = ' ';
}
text[1] = (tempinc/100)%10 + 48; // extract tens digit
text[2] = (tempinc/10)%10 + 48; // extract ones digit
text[4] = tempinC%10 + 48;
Lcd_Out(2, 5, text);
}
}
Attachments
-
2.4 KB Views: 11
Last edited by a moderator: