STM8S-Discovery third program - DS18B20

Publisher:BlissfulWhisperLatest update time:2016-10-08 Source: eefocusKeywords:STM8S  Discovery  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/******** STM8S-Discovery DS18B20 Test ********
* Version.........: 1.0
* Target.......: STM8S105C6
* File name.......: main.c
* Compiler.......: IAR for STM8 V1.1
**********************************************/

#include
#include

#define UART_BAUD 9600 //Baud rate
#define F_MASTER 16000000 //Master frequency

#define DS18B20_DQ_OUT PG_DDR_DDR0 = 1 //Output
#define DS18B20_DQ_IN PG_DDR_DDR0 = 0 //Input
#define DS18B20_DQ_HIGH PG_ODR_ODR0 = 1 //Pull high
#define DS18B20_DQ_LOW PG_ODR_ODR0 = 0 //Pull down
#define DS18B20_DQ_PULL_UP PG_CR1_C10 = 1 //Pull up
#define DS18B20_DQ_FLOATING PG_CR1_C10 = 0 //Floating #define
DS18B20_DQ_PUSH_PULL PG_CR1_C10 = 1 //Push-pull
#define DS18B20_DQ_OPEN_DRAIN PG_CR1_C10 = 0 //Open drain
#define DS18B20_DQ_VALUE PG_IDR_IDR0 //DQ value

//Serial port configuration
//Data bits: 8
//Stop bits: 1
//Parity bit: None
void UART_Init(void)
{
    UART2_CR2_TEN = 1;
    UART2_CR2_REN = 1;
    UART2_CR2_RIEN = 1;
    UART2_BRR2 = ((unsigned char)((F_MASTER / UART_BAUD) & 0x0f)) + (((unsigned char)((F_MASTER / UART_BAUD) >> 8)) &0xf0);
    UART2_BRR1 = ((unsigned char)((F_MASTER / UART_BAUD) >> 4));
}

void UART_TxByte (unsigned char _data)
{
    while (UART2_SR_TXE == 0);
    UART2_DR = _data;
}

int putchar(int c)
{
    UART_TxByte(c);
    return c;
}

void _delay_us(unsigned int i)
{
    i *= 3; 
    while(--i);
}

void _delay_ms(unsigned int i)
{
    while(i--)
    {
        _delay_us (1000);
    }
}

void DS18B20_Init(void)
{
    DS18B20_DQ_OUT;   
    DS18B20_DQ_PUSH_PULL;    
    DS18B20_DQ_HIGH;   
    _delay_us(10);
    DS18B20_DQ_LOW;   
    _delay_us(600); //Reset pulse
    
    DS18B20_DQ_IN;   
    DS18B20_ DQ_PULL_UP;    
    _delay_us(100);     
    while(DS18B20_DQ_VALUE == 1);
    _delay_us(400);
}

void DS18B20_WriteByte(unsigned char _data)
{
    unsigned char i = 0;

    DS18B20_DQ_OUT;
    for (i = 0; i < 8; i++)
    {
        DS18B2 0_DQ_LOW;
        _delay_us(2);
        if (_data & 0x01)
        {
            DS18B20_DQ_HIGH;
        }
        _data >>= 1;
        _delay_us(60);
        DS18B20_DQ_HIGH;
    }
}

unsigned char DS18B20_ReadByte(void)
{
    unsigned char i = 0, _data = 0;

    for (i = 0; i < 8; i++)
    {
        DS18B20_DQ_OUT;
        DS18B20_DQ_LOW;
        _delay_us(5);
        _data >>= 1;
        DS18B20_DQ_HIGH;
        DS18B20_DQ_IN;
        if (DS18B20_DQ_VALUE)
        {
            _data |= 0x80;
        }
        DS18B20_DQ_OUT; 
        DS18B20_DQ_HIGH;
        _delay_us(60);
    }

    return _data;
}

float DS18B20_ReadTemperature(void)
{
    unsigned char temp = 0;
    float t = 0;
    
    DS18B20_Init();
    DS18B20_WriteByte(0xcc);
    DS18B20_WriteByte(0x44);

    DS18B20_Init();
    DS18B20_WriteByte(0xcc);
    DS18B20_WriteByte(0xbe);

    temp = DS18B20_ReadByte();
    t = (((temp & 0xf0) >> 4) + (temp & 0x07) * 0.125); 
    temp = DS18B20_ReadByte();
    t += ((temp & 0x0f) << 4);
    
    return t;
}

int main(void)
{  
    CLK_SWCR_SWEN = 1;
    CLK_SWR = 0xB4;    //HSE selected as master clock source
    
    UART_Init();
    printf("********* STM8S-Discovery DS18B20 Test *********\r\n");
    printf("Build: %s  %s\r\n", __DATE__, __TIME__);
    
    asm("rim");
    
    while(1)
    {
        _delay_ms(1000);
        printf("%.3f ", DS18B20_ReadTemperature());
    }
}

#pragma vector = UART2_R_RXNE_vector
__interrupt void UART2_IRQHandler(void)
{
    if (UART2_SR_RXNE == 1)
    {
        UART_TxByte(UART2_DR);
    }
}
Keywords:STM8S  Discovery  DS18B20 Reference address:STM8S-Discovery third program - DS18B20

Previous article:STM8S105S4 I2C
Next article:STC12C5410AD AD conversion sample program

Recommended ReadingLatest update time:2024-11-16 14:28

Detailed explanation of STM8S touch button firmware library
Since there is a small project that needs to use touch buttons, stm8s can save a touch button chip, and the cost is relatively low. It is still valuable in some cost-sensitive applications. Now I write down the information I found on the Internet and my own learning experience and analyze it with you. ST's underlying
[Microcontroller]
Detailed explanation of STM8S touch button firmware library
1602 LCD display DS18B20 temperature
//LCD1602 driver #include stc89c52.h #include intrins.h #define uchar unsigned char #define uint unsigned int  #define LCD1602_RS P2_5 //Define pins #define LCD1602_RW P2_6 #define LCD1602_E P2_7 #define LCD1602_IO P0 #define Busy 0x80 //Used to detect the Busy flag in the LCD1602 status word const uchar num ="01234
[Microcontroller]
Internal eeprom programming of STM8S MCU
Introduction: STM8S microcontroller chip also integrates EEPROM, with a capacity ranging from 640 bytes to 2K bytes. The most unique feature is that in STM8 microcontroller, the access to EEPROM is just like regular RAM, which is very convenient. The address space of EEPROM is uniformly addressed with the memory, star
[Microcontroller]
Configuration of three clock sources of STM8S HSE\HSI\LSI configuration
1. About the HSE clock configuration as the main clock static void CLK_Config(void) {     CLK_DeInit(); // Initialization     CLK_HSECmd(ENABLE); //Enable HSE    CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE,                                 CLK_CURRENTCLOCKSTATE_DISABLE); //Switch HSE and turn o
[Microcontroller]
Configuration of three clock sources of STM8S HSE\HSI\LSI configuration
Understanding of STM8S MCU GPIO.C
1. Composition of STM8S MCU GPIO.C In fact, the stm8s_gpio.c required by the STM8S series microcontroller consists of the following parts: void GPIO_DeInit(GPIO_TypeDef* GPIOx) void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) void GPIO_Write(GPIO_TypeDef* GPIOx, uint8_t P
[Microcontroller]
How to see the RAM ROM size when compiling STM8S using stvd
I just installed the STVD compiler, but it doesn't show how much RAM and ROM are used when compiling? There are two ways to solve this problem: one is to look at the .map file, and the other is to add a patch. The specific steps are as follows. You can download the corresponding file in My Resources. http://download.
[Microcontroller]
STM8S - DS18B20
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/******** STM8S-Discovery DS18B20 Test ******** * Version.........: 1.0 * Target.........: STM8S105C6T6 * File name.......: main.c * Development environment.....: IAR for STM8 V1.1 ********************************
[Microcontroller]
Use STM8S built-in BootLoader_2
Read ST support document UM0560 carefully and follow the steps. Procedure preparation:  1. Open the serial port receive interrupt and send. /* ******************************************** UART2 configured as follow:   - BaudRate = 115200 baud     - Word Length = 8 Bits   -One Stop Bit   - No parity   - Receive and
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号