STM8 Microcontrollers – the Final Chapters

One Wire (OW) – DS18B20

One Wire (OW) communication is not as much as popular as I2C, SPI or serial communication. However, there are a number of devices that use one-wire communication method. DS18B20 digital temperature sensor from Dallas is one such example. For OW communication, we do not need any special hardware. Only GPIO bit-banging is all that is needed. Additionally, we can use a timer if we do not want to use software-based delays.

Hardware Connection

OW-DS18B20

Code Example

one_wire.h

#include "STM8S.h"


#define DS18B20_port      GPIOD       
#define DS18B20_pin       GPIO_PIN_2

#define DS18B20_INPUT()   do{GPIO_DeInit(DS18B20_port); GPIO_Init(DS18B20_port, DS18B20_pin, GPIO_MODE_IN_FL_NO_IT);}while(0)
 
#define DS18B20_OUTPUT()  do{GPIO_DeInit(DS18B20_port); GPIO_Init(DS18B20_port, DS18B20_pin, GPIO_MODE_OUT_PP_HIGH_FAST);}while(0)

#define DS18B20_IN()      GPIO_ReadInputPin(DS18B20_port, DS18B20_pin)

#define DS18B20_OUT_LOW() GPIO_WriteLow(DS18B20_port, DS18B20_pin)
#define DS18B20_OUT_HIGH() GPIO_WriteHigh(DS18B20_port, DS18B20_pin)     


bool onewire_reset(void);
void onewire_write_bit(bool bit_value);
bool onewire_read_bit(void);
void onewire_write(unsigned char value);   
unsigned char onewire_read(void);

one_wire.c

#include "one_wire.h"


bool onewire_reset(void
{                                        
        bool res = FALSE;

        DS18B20_OUTPUT();                
        DS18B20_OUT_LOW();
        delay_us(480);       
        DS18B20_OUT_HIGH();
        delay_us(60);       

        DS18B20_INPUT();
        res = DS18B20_IN();
        delay_us(480);      

        return res;
}


void onewire_write_bit(bool bit_value)
{
       DS18B20_OUTPUT();
       DS18B20_OUT_LOW();

       if(bit_value)
       {    
              delay_us(104);
              DS18B20_OUT_HIGH();  
       }             
}    


bool onewire_read_bit(void)       
{    
       DS18B20_OUTPUT();
       DS18B20_OUT_LOW(); 
       DS18B20_OUT_HIGH(); 
       delay_us(15);     
       DS18B20_INPUT();

       return(DS18B20_IN());   
}


void onewire_write(unsigned char value)
{                   
        unsigned char s = 0;

        DS18B20_OUTPUT();

        while(s < 8)   
        {                             
                if((value & (1 << s)))
                {
                       DS18B20_OUT_LOW();
                       delay_cycles(1);  
                       DS18B20_OUT_HIGH(); 
                       delay_us(60);  
                }      

                else
                {
                      DS18B20_OUT_LOW();          
                       delay_us(60);          
                       DS18B20_OUT_HIGH();  
                       delay_cycles(1); 
                }
                s++;
        }
}                                     


unsigned char onewire_read(void)
{
        unsigned char s = 0x00;
        unsigned char value = 0x00;

        while(s < 8)
        {
                DS18B20_OUTPUT();

                DS18B20_OUT_LOW();
                delay_cycles(1);                 
                DS18B20_OUT_HIGH(); 

                DS18B20_INPUT();
                if(DS18B20_IN()) 
                {                                     
                    value |=  (1 << s);                        
                }       

                delay_us(60);     
                s++;
        }    

        return value;
}   

DS18B20.h

#include "STM8S.h"
#include "one_wire.h" 


#define convert_T          0x44
#define read_scratchpad   0xBE           
#define write_scratchpad  0x4E
#define copy_scratchpad   0x48  
#define recall_E2          0xB8
#define read_power_supply 0xB4   
#define skip_ROM           0xCC

#define resolution         12


void DS18B20_init(void);
float DS18B20_get_temperature(void);

DS18B20.c

#include "DS18B20.h"


void DS18B20_init(void)                            
{                                      
       onewire_reset();
       delay_ms(100);
}             


float DS18B20_get_temperature(void)
{                                              
       unsigned char msb = 0x00;
       unsigned char lsb = 0x00;
       register float temp = 0.0; 

       onewire_reset();    
       onewire_write(skip_ROM);       
       onewire_write(convert_T);

       switch(resolution)  
       {                                                 
              case 12:
              {                                              
                     delay_ms(750);
                     break;
              }               
              case 11:                                    
              {             
                     delay_ms(375);
                     break;
              }          
              case 10:                            
              {                                
                     delay_ms(188);  
                     break;
              }                                       
              case 9:                                  
              {                                               
                     delay_ms(94);                
                     break;                           
              }                       
       }                 

       onewire_reset();

       onewire_write(skip_ROM);                
       onewire_write(read_scratchpad);

       lsb = onewire_read();
       msb = onewire_read();

       temp = msb;                          
       temp *= 256.0;
       temp += lsb;


       switch(resolution)  
       {                                 
              case 12:           
              {                                               
                     temp *= 0.0625;                
                     break;                           
              }            
              case 11:
              {             
                     temp *= 0.125;     
                     break;
              }               
              case 10:
              {           
                     temp *= 0.25;      
                     break;
              } 
              case 9:                                
              {                                
                     temp *= 0.5;       
                     break;     
              }                         
       } 

       delay_ms(40);      

       return (temp);      
}

main.c

#include "STM8S.h"
#include "DS18B20.h"
#include "lcd.h"


unsigned char bl_state;
unsigned char data_value;

const unsigned char symbol[8] =
{
   0x00, 0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00
};


void clock_setup(void);
void GPIO_setup(void);
void lcd_symbol(void);
void print_C(unsigned char x_pos, unsigned char y_pos, signed int value);
void print_I(unsigned char x_pos, unsigned char y_pos, signed long value);
void print_D(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char points);
void print_F(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points);


void main(void)
{            
       float temp = 0.0;

       clock_setup();
       GPIO_setup();

       DS18B20_init();

       LCD_init(); 
       LCD_clear_home();
       lcd_symbol();

       LCD_goto(0, 0);
       LCD_putstr("STM8 DS18B20 Ex.");

       LCD_goto(0, 1);
    LCD_putstr("T/ C");
    LCD_goto(2, 1);
    LCD_send(0, DAT);

       while(TRUE)
       {
              temp = DS18B20_get_temperature();
              print_F(9, 1, temp, 3);
              delay_ms(600);
       };
}


void clock_setup(void)
{
       CLK_DeInit();

       CLK_HSECmd(DISABLE);
       CLK_LSICmd(DISABLE);
       CLK_HSICmd(ENABLE);
       while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

       CLK_ClockSwitchCmd(ENABLE);
       CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
                                                                            CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

       CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,
                             CLK_SOURCE_HSI,
                            DISABLE,
                            CLK_CURRENTCLOCKSTATE_ENABLE);

       CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, ENABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}


void GPIO_setup(void)
{
    GPIO_DeInit(DS18B20_port);
}


void lcd_symbol(void
{
   unsigned char s = 0; 

   LCD_send(0x40, CMD);

   for(s = 0; s < 8; s++)
   {
        LCD_send(symbol[s], DAT);
   }

   LCD_send(0x80, CMD);
}


void print_C(unsigned char x_pos, unsigned char y_pos, signed int value)
{
     unsigned char ch[5] = {0x20, 0x20, 0x20, 0x20, '\0'};

     if(value < 0x00)
     {
        ch[0] = 0x2D;
        value = -value;
     }
     else
     {
        ch[0] = 0x20;
     }

     if((value > 99) && (value <= 999))
     {
         ch[1] = ((value / 100) + 0x30);
         ch[2] = (((value % 100) / 10) + 0x30);
         ch[3] = ((value % 10) + 0x30);
     }
     else if((value > 9) && (value <= 99))
     {
         ch[1] = (((value % 100) / 10) + 0x30);
         ch[2] = ((value % 10) + 0x30);
         ch[3] = 0x20;
     }
     else if((value >= 0) && (value <= 9))
     {
         ch[1] = ((value % 10) + 0x30);
         ch[2] = 0x20;
         ch[3] = 0x20;
     }

        LCD_goto(x_pos, y_pos);
     LCD_putstr(ch);
}


void print_I(unsigned char x_pos, unsigned char y_pos, signed long value)
{
    unsigned char ch[7] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, '\0'};

    if(value < 0)
    {
        ch[0] = 0x2D;
        value = -value;
    }
    else
    {
        ch[0] = 0x20;
    }

    if(value > 9999)
    {
        ch[1] = ((value / 10000) + 0x30);
        ch[2] = (((value % 10000)/ 1000) + 0x30);
        ch[3] = (((value % 1000) / 100) + 0x30);
        ch[4] = (((value % 100) / 10) + 0x30);
        ch[5] = ((value % 10) + 0x30);
    }

    else if((value > 999) && (value <= 9999))
    {
        ch[1] = (((value % 10000)/ 1000) + 0x30);
        ch[2] = (((value % 1000) / 100) + 0x30);
        ch[3] = (((value % 100) / 10) + 0x30);
        ch[4] = ((value % 10) + 0x30);
        ch[5] = 0x20;
    }
    else if((value > 99) && (value <= 999))
    {
        ch[1] = (((value % 1000) / 100) + 0x30);
        ch[2] = (((value % 100) / 10) + 0x30);
        ch[3] = ((value % 10) + 0x30);
        ch[4] = 0x20;
        ch[5] = 0x20;
    }
    else if((value > 9) && (value <= 99))
    {
        ch[1] = (((value % 100) / 10) + 0x30);
        ch[2] = ((value % 10) + 0x30);
        ch[3] = 0x20;
        ch[4] = 0x20;
        ch[5] = 0x20;
    }
    else
    {
        ch[1] = ((value % 10) + 0x30);
        ch[2] = 0x20;
        ch[3] = 0x20;
        ch[4] = 0x20;
        ch[5] = 0x20;
    }

    LCD_goto(x_pos, y_pos);
    LCD_putstr(ch);
}


void print_D(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char points)
{
    char ch[5] = {0x2E, 0x20, 0x20, '\0'};

    ch[1] = ((value / 100) + 0x30);

    if(points > 1)
    {
        ch[2] = (((value / 10) % 10) + 0x30);

              if(points > 1)
              {
                     ch[3] = ((value % 10) + 0x30);
              }
    }

    LCD_goto(x_pos, y_pos);
    LCD_putstr(ch);
}


void print_F(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points)
{
    signed long tmp = 0x0000;

    tmp = value;
    print_I(x_pos, y_pos, tmp);
    tmp = ((value - tmp) * 1000);

    if(tmp < 0)
    {
       tmp = -tmp;
    }

    if(value < 0)
    {
        value = -value;
              LCD_goto(x_pos, y_pos);
        LCD_putchar(0x2D);
    }
    else
    {
        LCD_goto(x_pos, y_pos);
        LCD_putchar(0x20);
    }

    if((value >= 10000) && (value < 100000))
    {
        print_D((x_pos + 6), y_pos, tmp, points);
    }
    else if((value >= 1000) && (value < 10000))
    {
        print_D((x_pos + 5), y_pos, tmp, points);
    }
    else if((value >= 100) && (value < 1000))
    {
        print_D((x_pos + 4), y_pos, tmp, points);
    }
    else if((value >= 10) && (value < 100))
    {
        print_D((x_pos + 3), y_pos, tmp, points);
    }
    else if(value < 10)
    {
        print_D((x_pos + 2), y_pos, tmp, points);
    }
}

Explanation

One wire communication is detailed in these application notes from Maxim:

https://www.maximintegrated.com/en/app-notes/index.mvp/id/126

https://www.maximintegrated.com/en/app-notes/index.mvp/id/162

These notes are all that are needed for implementing the one wire communication interface for DS18B20. Please go through these notes. The codes are self-explanatory and are implemented from the code examples in these app notes.

Demo

DS18B20

Pages: 1 2 3 4 5 6 7 8 9 10

Continue Reading ...

Related Posts

78 comments

  • Shawon I’m sorry to bother you, but I can’t run ds18b20 with this code. Lcd shows meaningless character with this clock cpu you were configured, I configured CPUDIV1 and it worked but it only showed “4095.9” and it is not changing

    can you please help me ??
    please send you debug files AT iampruthvi63543@gmail.com

  • hello brother i run your code on stm8s00f0p3. I observed that
    LCD shows meaningless character. it only showed “4095.9” … can u help me? What more I can do to fix it??

    if you don’t mind can you ping me at iampruthvi63543@gmail.com

  • Hey Shawon

    May i know which file that contain this functions :


    void clock_setup(void);
    void GPIO_setup(void);
    void show_value(unsigned char value);

    I didn’t see any of these functions in SPL

    And this functions make an error in my Linker

    Thanks in advance.

  • Hi can you make a tutorial on interfacing mpu6050 with stm8s003f3. The problem I am facing is getting the gyro value only one time when the device is powered on then then nothing received in uart. I hope your tutorial will help me a lot.

  • Hi Shawon
    I have tried multiple times of interfacing gyroscope MPU6050 with stm8s003f3.

    void MPU6050_write(unsigned char cmd)
    {
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(MPU6050,I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(cmd);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTOP(ENABLE);
    }
    the function hangs at while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    i don’t know why this happen. The address of MPU6050 is 0xD0. Please reply me your suggestion.

  • Hi Shawon, thanks a lot for these great tutorial series.

    I’m trying to use the touch library for the STM8S003F3P6. I’m using your code and just editing the controller and the timers.
    Since this one doesn’t have the TIM3, so, I’m going to use the TIM2 instead.
    But after a second of running , the program freezes and the LED doesn’t blink and turns off. I even tried to just keep it on, not blinking, but it still turns off and stays like that forever.
    LED blinks again when commenting the TSL_Action() function in the while(1), but this way, the keys won’t work!
    There is no error or warning during compile!

    Do you have any idea about this?
    Thanks again for helping.

    • Hi again.

      Found out. For timer 2 we must use this:

      #define TIMACQ (TIM2)
      #define TIMACQ_CNTR_ADD (0x530C)

      The TIMACQ_CNTR_ADD’s value should be changed.

      Also, we should make sure some glass material stuff is on the pad, otherwise it won’t work or may freezes.

    • I have this problem too ,its because of TSL_SetStructPointer(); in TSL_Action() , can anyone help us???

  • Hello, I need lcd.h file, please share it and email me. thanks

  • void OLED_write(unsigned char value, unsigned char control_byte)
    {
    while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(SSD1306_I2C_Address, I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(control_byte);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
    I2C_SendData(value);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTOP(ENABLE);
    }

    It gets stuck at “while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));” this line for me, its not stepping beyond this point. Do you think this might be an issue of uC not able to detect the slave device or address.

  • This code is great. but how can i change font size.
    please help me.

  • Hi,what can i write instead of delay_cycle(1).ds18b20 is not functioning in iar,also delay_us& ms are not exact that i checked with logic analyzer.please help!thanks

  • Morteza Jamshidi

    Can you email me the source of the project and its files (One Wire (OW) – DS18B20) ? thank you. my Email address: mortezajamshidi9898@gmail.com

  • Morteza Jamshidi

    Can you email me the source of the project and its files? thank you. my Email address: mortezajamshidi9898@gmail.com

  • Hello i used your code with the IAR compiler but i had problems with delay_cycle, what house to use? I use lcd character without using I2C and HSIDIV = 8 and CPUDIV = 1

  • Hi if you can put sensor SHT10 with STM8s code.
    tnx

  • Hi if you can put sensor SHT10 with STM8s code
    tnx

  • Hi shwon, I’ve been trying to run oled dosplay but I have a problem . The program get stucks in “OLED_init()” function. I think it’s due to “OLED_write”function. I think it get stucks in one of those while loops in this function. Do u have any suggestion? How can I fix this problem?

    • Is it a SSD1306/SSD1309-based OLED display?

      • Yes it is,, just it’s a bit different with your display. I’ve tried these address bytes: “0x78 , 0x3C, 0x3D” they didn’t work… the scl and sda pins were pulled up with 4.7k resistor. I don’t know what should I do??

        • Is it a red PCB OLED? I asked so because some I2C-based OLED displays don’t work properly…. Many are fake one…. I had one such display and if you keep powering on and off our MCU + such OLED board several times, it will work….

  • I am trying to use i2c LCD but I am stuck in one place.

    The code is stuck in an endless loop in below function
    ////////////////
    void PCF8574_write(unsigned char data_byte)
    {
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(data_byte);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTOP(ENABLE);
    }
    ////////////////

    the code is stuck in below line
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    PCF8574_address is 0x4E

  • Hi sir, I use stm8s003f3p6, I want to run oled display, but I have an issue about stack memory. I’ve switched it to long stack memory but it doesn’t change.what should I do? I think this type of stm8s has’nt got as amount as stack memory I need.

    • Switch to chips of higher memory capacities…. This chip can’t be used to drive an OLED screen….

      • Hi shawon; I want to run OLED display but I have an error as follow: “bass size overflow(768)” I try with all type of stm8s microes but it’s not changed… I switched memory model to long stack memory. What should I do?

    • Your comment is awaiting moderation.

      I am trying to use i2c LCD but I am stuck in one place.

      The code is stuck in an endless loop in below function
      ////////////////
      void PCF8574_write(unsigned char data_byte)
      {
      I2C_GenerateSTART(ENABLE);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

      I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_TX);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

      I2C_SendData(data_byte);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

      I2C_GenerateSTOP(ENABLE);
      }
      ////////////////

      the code is stuck in below line
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

      PCF8574_address is 0x4E

      • What about pull-ups on SCL and SDA lines? Are you sure that your PCF8574’s I2C address is 0x4E?

        • I am trying without I2C module but still, nothing is showing on display.

        • I have only followed example code
          In tutorial it is not showing for pull up SDA and sck.
          .
          The address of is 0x27 I have tried with this also.

          I have also tried without i2c but that’s code is also not working .

          • Hmm first thing I guess is either the LCD is broken or you are trying with wrong I2C address… Be sure of the chip embedded in the I2C-LCD module…. Some come shipped with PCF8574 while some are shipped with PCF8574A…. These have different I2C addresses…. Secondly, in the tutorial pull-up is shown in the I2C-LCD module’s schematic and by the nature of I2C communication pull-ups are must…. It should be there no matter if specified or not….

        • Hi,

          I have pulled up SDA and SCL line by 10K and I have checked i2c address 0x27 is correct for 16×2 LCD.

          My LCD is not broken.

          I have checked the same LCD on ARDUINO same address it is working fine but in STM8s it is not showing anything.

          • Are you running the STM8 with 3.3V? If so then the LCD won’t work with 3.3V power…. Either supply the LCD with 5V or use a 3.3V compatible LCD….

          • Hi Shawon Shahryiar,

            Thanks for your replay.

            I am using 5VDC for LCD 16×2 from Stm8s board itself. I have tried with 2 different LCD both not working.
            My stm8s board is working on 3.3vdc only.

            I have debugged the code as per my knowledge the I2C->SR1(shift register) is not updating. it is showing 0 but it should be 1.

            The address is correct 0x3F.

            But my question is why is LCD with or without I2C LCD was not showing any data.
            I know that for I2C I have to use PULLUP both line. But if we use without i2c we don’t want PULL UP but here also it is not showing anything on Display.

            Please help me with this thing.

          • As of all the previous conversation and this one, I have no idea why is it not working…. specially I don’t understand why I2C->SR1 is not properly working…. I would suggest that you slow down your system clock and update the code according to system clock setting…. Most LCDs don’t work when their data update frequency is above 250kHz….

        • I think there is no Address issue. some I2C code issue is here

        • The address of PCF8574A is 0x3F. I tried it also with pull up resistance 10K but no result.

        • Hello Shawon Shahryiar,

          Please send me your mail id for this I2C problem discussion.

  • Hey; thanks a lot for your prefect article, could you send me please the delay libraries I haven’t got the right version of them, I have an error about “delay_cycle()”.

    • STM8S_delay.h


      #include "stm8s.h"

      #define F_CPU 16000000UL
      #define dly_const (F_CPU / 16000000.0F)

      void delay_us(unsigned int value);
      void delay_ms(unsigned int value);

      STM8S_delay.c


      #include "stm8s_delay.h"

      void delay_us(unsigned int value)
      {
      register unsigned int loops = (dly_const * value) ;

      while(loops)
      {
      _asm ("nop");
      loops--;
      };
      }

      void delay_ms(unsigned int value)
      {
      while(value)
      {
      delay_us(1000);
      value--;
      };
      }

      I didn’t use delay cycle in my library…. Are you using some other compiler other than Cosmic + STVD?

      • Look up at “one_wire.c” you used “delay_cycle(1)” three times. When I compile it I get this error: “missing prototype” .I use cosmic + stvd such as you.

      • Shawon I’m sorry to bother you, but I can’t run ds18b20 with this code. Lcd shows meaningless character with this clock cpu you were configured, I configured CPUDIV4 and it worked but it only showed “4095.9” I’m sure about hardware … can u help me? What more I can do to fix it??

        • Have you use pulled up the data pin? Did you try with my code? With my code does it work or not?

          • I’ve just coppied your code , at first complier didn’t find delay_cycle funcion then I replaced it with a while loop such as you said, after that I had to reduce the cpu clock cause LCD didn’t working properly(according to you most LCDs have a maximum working frequency of 250 khz.) at last I gave the wrong temprature from DS. I’ve pulled up the data line with 10 k res.

          • I didn’t mean that you copy my code and retry…. I meant that you check with my hardware connections and my finizalied debugger file “ow_ds18b20.s19” from the debug folder…. If there is still any issue then it is likely a hardware issue or else you are doing something wrong….

  • Really outstanding library! Thanks for helping out. I am using the STM8AF5288 so made some changes and worked like a charm. but the issue i am facing right now is the font size. is there any available font library that could be replaced to get bigger fonts? and If possible can you explain how the font is being created on the ssd1306, I mean the way the pixels are being drawn using the hex code (in the font.h file).
    Thanks in advance.

  • Really outstanding library! Thanks for helping out. I am using the STM8AF5288 so made some changes and worked like a charm. but the issue i am facing right now is the font size. is there any available font library that could be replaced to get bigger fonts ? and If possible can you explain how the font is being created on the ssd1306, I mean the way the pixels are being drawn using the hex code (in the font.h file).
    Thanks in advance.

    • There are several OLED libraries available in the internet with big fonts but I’m happy with this font size to make maximum use of the display…. Thus I didn’t go for development of libraries with big fonts….

      • thanks for the quick reply, But the thing is that i am facing a issue with that of the draw bitmap function.
        i.e.
        void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)

        {

        unsigned int s = 0x0000;

        unsigned char x_pos = 0x00;

        unsigned char y_pos = 0x00;

        for(y_pos = yb; y_pos <= ye; y_pos++)

        {

        OLED_gotoxy(xb, y_pos);

        for(x_pos = xb; x_pos < xe; x_pos++)

        {

        OLED_write(bmp_img[s++], DAT);

        }

        }

        }

        and i am calling it by the following way:
        OLED_draw_bitmap(0,0,128,64,(unsigned char *)&image_data_LesInv);

        and my bitmap file goes like this:
        static const uint8_t image_data_LesInv[128][64] = {
        // 'untitled', 128x64px
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0x7f, 0x3f, 0x3f, 0x3f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x01, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xf8, 0xf8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7f, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x01, 0x07,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0f, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x80, 0xe0,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x80, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
        };

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        but the issue is that my image does gets generated but it gets back to blank screen within a 500ms or so.
        can you please help with the same.

        • void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)
          {
          unsigned int s = 0x0000;
          unsigned char x_pos = 0x00;
          unsigned char y_pos = 0x00;

          for(y_pos = yb; y_pos <= ye; y_pos++)
          {
          OLED_gotoxy(xb, y_pos);
          for(x_pos = xb; x_pos < xe; x_pos++)
          {
          OLED_write(bmp_img[s++], DAT);
          }
          }
          }

          This was the original code but yours seems to be modified in the for loops…. Please recheck unless it is intentional….

          • both the loops are the same. I did not make any changes in the original code but the bitmap image seems to get printed on the screen but it gets instantly blank within 20ms.
            can you help me for the same.
            Thanks in advance.

          • Could you send me the whole code? I think there is an overflow somewhere….

  • Hello,
    How can i change the font size so i can support for example [92][12] ? Saw some libraries on the net but i am not sure if they are compatible with this code… or am i doing something wrong ? I tried manipulating the current one with some cycles within ” OLED_print_char ” also chaning 0x06 to 0x0C with a font library also tried creating fonts but i did not get the expected result. Can you give me some advice please ?
    Thank you for your time!

    • The coordinates of the OLED displays are mapped as multiples of 8-bits or 8 dots in both x and y directions. So just by changing x-coordinate values won’t result in larger fonts…. You must also take care of the y-coordinate too…. Take the example of the bitmap function:

      void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)
      {
      unsigned int s = 0x0000;
      unsigned char x_pos = 0x00;
      unsigned char y_pos = 0x00;

      for(y_pos = yb; y_pos <= ye; y_pos++)
      {
      OLED_gotoxy(xb, y_pos);
      for(x_pos = xb; x_pos < xe; x_pos++)
      {
      OLED_write(bmp_img[s], DAT);
      s++;
      }
      }
      }

      It takes care of the y-coordinate part once the x-coordinate points are filled up….

  • Hi great tutorial!
    Can you post code for interfacing external eeprom using i2c for stm8s003f3??

  • Pingback: .NET i jiné ...

  • Pingback: STM8 Microcontrollers – gStore

  • Outstanding bro. Carry on. Wish your good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *