Display One Wire text on GLCD

General discussion on mikroC PRO for PIC.
Author
Message
panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Display One Wire text on GLCD

#1 Post by panaos99 » 14 Aug 2022 10:39

Hi all,

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
Attachments
Display One Wire text on GLCD.JPG
Display One Wire text on GLCD.JPG (49.24 KiB) Viewed 1094 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Display One Wire text on GLCD

#2 Post by filip » 16 Aug 2022 08:34

Hi,

Can you please attach detailed photo of your board ?

Regards,
Filip.

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#3 Post by panaos99 » 16 Aug 2022 08:49

filip wrote:
16 Aug 2022 08:34
Hi,

Can you please attach detailed photo of your board ?

Regards,
Filip.
Sure, I just took some pictures.
Anything specific to look for?
Attachments
IMG_20220816_084345.jpg
IMG_20220816_084345.jpg (5.4 MiB) Viewed 1093 times
IMG_20220816_084336.jpg
IMG_20220816_084336.jpg (5.47 MiB) Viewed 1093 times
IMG_20220816_084309.jpg
IMG_20220816_084309.jpg (5.49 MiB) Viewed 1093 times

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#4 Post by hexreader » 16 Aug 2022 10:18

Try the attached project, with a few changes made:
Attachments
Forum.zip
(96.9 KiB) Downloaded 29 times
Start every day with a smile...... (get it over with) :)

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#5 Post by panaos99 » 16 Aug 2022 10:26

hexreader wrote:
16 Aug 2022 10:18
Try the attached project, with a few changes made:
Thanks hexreader! That did the trick!
GLCD is now showing the temperature, with a bit of flash every 0.5 sec.
Do you mind sharing the changes you made in the code?

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#6 Post by hexreader » 16 Aug 2022 10:31

Changes were:

1 ANSELE = 0; // PORTE digital, not analogue
2 box drawn over old number before new number drawn
3 PLL turned on and frequency box changed 8MHz to 32MHz - why run at 8 MHz when you can go at 32 MHz?
4 formatting improved
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#7 Post by hexreader » 16 Aug 2022 10:41

If the flicker annoys you, then attached is a version that reduces flicker:
Attachments
Forum.zip
(97.65 KiB) Downloaded 24 times
Start every day with a smile...... (get it over with) :)

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#8 Post by panaos99 » 16 Aug 2022 11:03

hexreader wrote:
16 Aug 2022 10:31
Changes were:

1 ANSELE = 0; // PORTE digital, not analogue
2 box drawn over old number before new number drawn
3 PLL turned on and frequency box changed 8MHz to 32MHz - why run at 8 MHz when you can go at 32 MHz?
4 formatting improved
Thank you once more for the updated version below, very kind of you! :)

I was under the impression that if ANSEL is unimplemented, it reads as ‘0’ anyway, but in retrospect, I should have declared it anyway.

Didn't even know about the PLL, just did some reading in the PIC's manual. So, to enable this, we just enable the option in the "edit project" menu and set it at 32MHz? Is that all?
Attachments
PIC18F45K22 - enable PLL.JPG
PIC18F45K22 - enable PLL.JPG (81.87 KiB) Viewed 1086 times

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#9 Post by hexreader » 16 Aug 2022 11:17

Analogue capable pins are analogue at power-on

Datasheet will tell you POR (power-on-reset) values for all registers
we just enable the option in the "edit project" menu and set it at 32MHz? Is that all?
Yes, that is all there is to it

I have noticed that after a while, my GLCD corrupts.
Do not know why that should be
Does your GLCD work OK?
Start every day with a smile...... (get it over with) :)

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#10 Post by panaos99 » 16 Aug 2022 11:23

Thank you for clarifying all these, very helpful!

My board is still running ok, no issues with the GLCD.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#11 Post by hexreader » 16 Aug 2022 12:08

Problem solved

Tried a different GLCD and all is well

Will mark first GLCD as "iffy" to prevent future confusion
Start every day with a smile...... (get it over with) :)

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#12 Post by panaos99 » 16 Aug 2022 17:33

hexreader wrote:
16 Aug 2022 12:08
Problem solved

Tried a different GLCD and all is well

Will mark first GLCD as "iffy" to prevent future confusion
glad u sort it out.

I was looking into DS1820 manual, and it looks like its able to use an ADC with up to 12 bits. PIC18F45K22 has only a 10 bit ADC but I understand that we are only using 9 bits on this code, is that right?
is it possible to use the 10th bit as well?

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#13 Post by hexreader » 16 Aug 2022 19:02

panaos99 wrote:
16 Aug 2022 17:33
I was looking into DS1820 manual, and it looks like its able to use an ADC with up to 12 bits.
No, no, no.
DS1820 gives out a digital signal with nine bits of temperature resolution
DS18B20 can be set for 9, 10, 11 or 12 bits of resolution
Both devices are digital as far as the PIC18F45K22 is concerned. Interface pin must always be digital, never analogue
panaos99 wrote:
16 Aug 2022 17:33
PIC18F45K22 has only a 10 bit ADC
This has no relevance. DS1820 gives out only digital signals
panaos99 wrote:
16 Aug 2022 17:33
but I understand that we are only using 9 bits on this code, is that right?
is it possible to use the 10th bit as well?
DS1820 only sends 9-bit data so, code is adjusted for a 9 bit DS1820
You can adjust code for 10, 11 or twelve bits if you change to a DS18B20.

If analogue temperature sensor is of interest then fit an LM35 to your EasyPIC v7a into the TS2 socket
Note how TS1 socket is marked digital and DS1820, while TS2 is marked analogue and LM35
Start every day with a smile...... (get it over with) :)

panaos99
Posts: 13
Joined: 13 Mar 2015 16:49

Re: Display One Wire text on GLCD

#14 Post by panaos99 » 16 Aug 2022 19:53

Once more, thanks hexreader :)

In my attempt to show more digits after the decimal, I thought I was dealing with an analogue sensor.. Have no idea why I confused the two :?
I already tried the LM35, no issues there.

I will try and source a couple of DS18B20 sensors in the next couple of days, can you please guide me on how to adjust for 10+ bits of temperature resolution using the onewire function?

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Display One Wire text on GLCD

#15 Post by hexreader » 16 Aug 2022 20:42

Only one number in one line of code to change:

Code: Select all

const unsigned short TEMP_RESOLUTION = 12;                                      // select 9 for DS1820, 12 for DS18B20
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”