I used to do PIC programming back in university and, I recently got myself an EasyPic v7a board to start again
So, I can display temperature ok using a DS1820 on the 16x2 LCD with the example OneWire code, however, when I change the code to show this on the GLCD, i only get zeros "00.00" instead of the actual temperature.
I have also attached a picture of this.
Everything else seems to work ok on the board (ports, switching on LEDs, creating rectangles on the GLCD etc.)
I am using the board's default PIC -PIC18F45K22 @ 8Mhz with a DS1820 temperature sensor with jumper 11 on RE2.
Thanks for the help!
Code: Select all
// PIC18F45K22 @ 8Mhz
// DS1820 Temperature sensor with jumper 11 on RE2
// Glcd module connections
char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at LATB0_bit;
sbit GLCD_CS2 at LATB1_bit;
sbit GLCD_RS at LATB2_bit;
sbit GLCD_RW at LATB3_bit;
sbit GLCD_EN at LATB4_bit;
sbit GLCD_RST at LATB5_bit;
sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections
const unsigned short TEMP_RESOLUTION = 9;
char *text = "000.00 ";
unsigned temp;
void Display_Temperature(unsigned int temp2write)
{
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;
// check if temperature is negative
if (temp2write & 0x8000)
{
text[0] = '-';
temp2write = ~temp2write + 1;
}
// extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;
// convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
text[0] = ' ';
text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
text[2] = temp_whole%10 + 48; // Extract ones digit
// extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// convert temp_fraction to characters
text[4] = temp_fraction/1000 + 48; // Extract thousands digit
text[5] = (temp_fraction/100)%10 + 48; // Extract hundreds digit
//text[6] = (temp_fraction/10)%10 + 48; // Extract tens digit
//text[7] = temp_fraction%10 + 48; // Extract ones digit
// Display temperature on LCD
Glcd_Write_Text_adv("Temperature: C", 3, 1);
// Draw a circle with center in (50,50) and radius=10
Glcd_Circle(92, 4, 1, 1);
Glcd_Write_Text_adv(text, 65, 1);
Delay_ms(1000);
}
int i=0;
void main()
{
TRISA = 0;
ANSELB = 0; // Configure PORTB pins as digital I/O
ANSELC = 0; // Configure PORTC pins as digital I/O
ANSELD = 0; // Configure PORTD pins as digital I/O
Glcd_Init(); // Initialize GLCD
Glcd_Fill(0x00); // Clear GLCD
Delay_ms(200);
Glcd_Rectangle(0, 0, 127, 63, 1);
Glcd_Rectangle(1, 1, 126, 62, 1);
PORTA = 0b00000001;
Delay_ms(200);
//--- main loop
do {
//--- perform temperature reading
Ow_Reset(&PORTE, 2); // Onewire reset signal
Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTE, 2);
Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTE, 2);
temp = (Ow_Read(&PORTE, 2) << 8) + temp;
//--- Format and display result on Lcd
Display_Temperature(temp);
} while (1);
} // end of program