/******** 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
* 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);
}
}
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]
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]
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]
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Introduction to DSP GPIO
- Examples of embedded system security issues
- The tool is still in Beta state
- EEWORLD University ---- Introduction to Power Electronics
- What does inductance 10t mean?
- How to Improve Heat Dissipation Using PCB Design
- Does anyone know which layers of AltiumDesigner the PCB production file suffixes correspond to?
- Comparison of CC2540 and nRF51822 application development
- The main purpose of immersion gold circuit board and the difference from other processes
- TAxIV interrupts for msp430