2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Table of contents
1.1 Software and hardware environment information
1.2 Development Board Information
2.1 Hardware interface circuit
2.3 Generate Keil project files
3.2 DS18B20 driver implementation
Renesas R7FA8D1BH (Cortex®-M85) controls DS18B20 and ADC to implement the jump function between two pages
This article mainly introduces a comprehensive application case designed by Renesas R7FA8D1BH (Cortex®-M85): using the IO of R7FA8D1BH to implement a single bus protocol and realize the function of driving ds18b20, which mainly completes the reading of temperature values and formats the values and displays them on the OLED screen. It also sends the temperature data to the serial terminal through the serial terminal.
Software and hardware information | Version Information |
---|---|
Renesas MCU | R7FA8D1BH |
Keil | MDK ARM 5.38 |
FSP version | 5.3.0 |
Debug tool: N32G45XVL-STB | DAP-LINK |
The author chose to use the WildFire Yaoyang Development Board_Renesas RA8. The main control MCU of this board is R7FA8D1BHECBD, and the core of 7FA8D1BHECBD is ARM Contex-M85.
For the R7FA8D1BHECBD chip, which uses the Cortex®-M85 Core, ST-LINK-V2 or J-LINK-V9 does not support download and debug functions. After many attempts, I foundN32G45XVL-STBThe DAP-LINK on the board can download and debug R7FA8D1BHECBD.
The following figure is a physical picture of the N32G45XVL-STB development board:
The interface circuit of DS18B20 has been designed on Yaoyang development board_Renesas RA8, which uses P809 interface as the DQ control signal of DS18B20.
Configure P809 as a normal IO interface, and then dynamically configure the output or output status of the IO in the code
After completing the FSP parameter configuration, you can Generate Project. Open the project file, and its structure is as follows:
Create the ds18b20.c file to implement the driver code
In my previous article, I have analyzed the timing and implementation logic of DS18B20 in detail, so I will not introduce it here.
DS18B20 Application Notes_ds18b20 reads data waveform -CSDN blog
Line 14 of the code: Define the us step delay function
Line 15 of the code: Define the delay function with a step length of ms
Code line 18: Define the IO PIN of DS18B20
Code line 21: Input port configuration
Code line 22: Output port configuration
Code line 24: Set IO low level
Code line 25: Set IO high level
Code line 47: Read the IO status in input mode
Function: ds18b20Init, detect whether DS18B20 is online
Function: ds18b20BlockModeProcess. Read the value of DS18B20
- /**
- * @brief reset DS18B20
- * @note if reset ds18b20 sucess, the return value is TRUE
- * @param None
- * @retval True or Flalse
- */
- static uint8_t ds18b20Init( void )
- {
- uint16_t tempCnt = 0;
- bsp_io_level_t status;
-
- // Set PIN mode output
- DS_Mode_Out_PP();
-
- // Master pin is high
- DQ_SET_HIGH;
- timeDelayUS(10);
-
- // Master pin is low
- DQ_SET_LOW;
- // wait for 600 us
- timeDelayUS(750);
-
- // Set PIN mode input
- DS_Mode_IN_PUT();
-
- while(1)
- {
- status = DQ_RAD_PIN();
- if( status == 0)
- {
- tempCnt = 0;
- return TRUE;
- }
- else
- {
- timeDelayUS(1);
- tempCnt++;
- if( tempCnt > 480 )
- return FALSE;
- }
- }
- }
-
-
- static uint8_t readBit( void )
- {
- uint8_t readCnt = 2;
- uint8_t bitVal = 1;
-
- DQ_SET_LOW;
- timeDelayUS(3);
- DQ_SET_HIGH;
-
- timeDelayUS(5); // 15 us
-
- while(readCnt-- )
- {
- //read DQ value
- if( DQ_RAD_PIN() == 0)
- {
- bitVal = 0;
- }
- timeDelayUS(2); // 15 us
- }
-
- timeDelayUS(30); // 15 us
-
- return bitVal;
- }
-
- static uint8_t ds18b20ReadByte( void )
- {
- uint8_t byteVal = 0;
-
- for ( uint8_t i = 0; i < 8; i++ )
- {
- byteVal >>= 1;
-
- uint8_t bitVal = readBit();
- if( bitVal > 0)
- {
- byteVal |= 0x80;
- }
- }
-
- return byteVal;
- }
-
-
- /**
- * @brief write one byte to DS18B20
- * @note
- * @param byte: the data that is sended to ds18b20
- * @retval None
- */
- void ds18b20WriteByte( uint8_t byte)
- {
- unsigned char k;
-
- // Set PIN mode output
- DS_Mode_Out_PP();
-
- for ( k = 0; k < 8; k++ )
- {
- if (byte & (1<<k))
- {
- DQ_SET_LOW;
- timeDelayUS(2);
-
- DQ_SET_HIGH;
- timeDelayUS(65);
- }
- else
- {
- DQ_SET_LOW;
- timeDelayUS(65);
-
- DQ_SET_HIGH;
- timeDelayUS(2);
- }
- }
- }
-
- uint8_t ds18b20BlockModeProcess( void )
- {
- uint16_t tempValue;
- uint8_t tempL, tempH;
-
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
-
- // wait for 600 us
- timeDelayUS(600);
-
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0x44); // start convert temperature
-
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- // wait for 600 us
- timeDelayUS(600);
-
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0xbe); // read temperature data register
-
- tempL = ds18b20ReadByte();
- tempH = ds18b20ReadByte();
-
- if (tempH > 0x7f)
- {
- tempL = ~tempL;
- tempH = ~tempH+1;
- st_ds1b20val.sign = 1;
- }
-
- tempValue = (uint16_t)((tempH << 8) | tempL);
-
- st_ds1b20val.temperatureVal = (float)(tempValue * 0.0625);
-
- return TRUE;
- }
-
-
- // NO blocking mode operate ds18b20
- uint8_t ds18b20NoBlockingProcess( void )
- {
- uint16_t tempValue;
- static uint16_t waitCnt = 0;
- uint8_t tempL, tempH;
- static uint8_t runState = 0;
-
- switch( runState )
- {
- default:
- case INIT_DQ:
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- runState = WAIT_READY;
- break;
-
- case WAIT_READY:
- timeDelayUS(2); // IDEL
- runState = SKIDROM_CMD;
- break;
-
- case SKIDROM_CMD:
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0x44); // begin to convert temperature data
- waitCnt = 0;
- runState = WAIT_CONVERT;
- break;
-
- case WAIT_CONVERT:
- waitCnt++;
- if( waitCnt > WAIT_CNT_CONVERT)
- {
- waitCnt = 0;
- runState = RESET_CMD;
- }
- break;
-
- case RESET_CMD:
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- runState = WAIT_DATA_READY;
- break;
-
- case WAIT_DATA_READY:
- timeDelayUS(2); // IDEL
- runState = READ_CMD;
- break;
-
- case READ_CMD:
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0xbe); // read temperature data register
- runState = GET_VALUE;
- break;
-
- case GET_VALUE:
-
- tempL = ds18b20ReadByte();
- tempH = ds18b20ReadByte();
-
- if (tempH > 0x7f)
- {
- tempL = ~tempL;
- tempH = ~tempH+1;
- st_ds1b20val.sign = 1;
- }
-
- tempValue = (uint16_t)((tempH << 8) | tempL);
-
- st_ds1b20val.temperatureVal = (float)(tempValue * 0.0625);
- runState = INIT_DQ;
- return TRUE;
- }
-
- return FALSE;
- }
Code line 113: Read the value of ds18b20
Code line 130: Get the result data of ds18b20
Code line 131: Formatting display data
Code line 132: Display data on OLED
Compile the code and download it to the board. The running results are as follows:
DS18B20 driver code
1) Create the ds18b20.c file and write the following code
- /*
- FILE NAME : ds18b20.c
- Description: user ds18b20 interface
- Author : [email protected]
- Date : 2024/06/03
- */
- #include "ds18b20.h"
- #include "hal_data.h"
-
- typedef enum{
- INPUT = 0,
- OUTPUT = 1,
- }IO_TYPE;
-
- typedef enum{
- FALSE = 0,
- TRUE = 1,
- }RETURN_RESULT;
-
- typedef enum{
- INIT_DQ = 0,
- WAIT_READY,
- SKIDROM_CMD,
-
- WAIT_CONVERT,
- RESET_CMD,
- READ_CMD,
-
- WAIT_DATA_READY,
- GET_VALUE,
- IDLE_NULL
- }RUN_STATE;
-
- ds18b20Struc st_ds1b20val;
-
-
- ds18b20Struc get_ds18b20_value( void )
- {
- return st_ds1b20val;
- }
-
- static bsp_io_level_t DQ_RAD_PIN(void)
- {
- bsp_io_level_t state;
-
- // READ io
- R_IOPORT_PinRead(&g_ioport_ctrl, DS_IO_PORT_PIN, &state);
-
- return state;
- }
-
- /**
- * @brief reset DS18B20
- * @note if reset ds18b20 sucess, the return value is TRUE
- * @param None
- * @retval True or Flalse
- */
- static uint8_t ds18b20Init( void )
- {
- uint16_t tempCnt = 0;
- bsp_io_level_t status;
-
- // Set PIN mode output
- DS_Mode_Out_PP();
-
- // Master pin is high
- DQ_SET_HIGH;
- timeDelayUS(10);
-
- // Master pin is low
- DQ_SET_LOW;
- // wait for 600 us
- timeDelayUS(750);
-
- // Set PIN mode input
- DS_Mode_IN_PUT();
-
- while(1)
- {
- status = DQ_RAD_PIN();
- if( status == 0)
- {
- tempCnt = 0;
- return TRUE;
- }
- else
- {
- timeDelayUS(1);
- tempCnt++;
- if( tempCnt > 480 )
- return FALSE;
- }
- }
- }
-
-
- static uint8_t readBit( void )
- {
- uint8_t readCnt = 2;
- uint8_t bitVal = 1;
-
- DQ_SET_LOW;
- timeDelayUS(3);
- DQ_SET_HIGH;
-
- timeDelayUS(5); // 15 us
-
- while(readCnt-- )
- {
- //read DQ value
- if( DQ_RAD_PIN() == 0)
- {
- bitVal = 0;
- }
- timeDelayUS(2); // 15 us
- }
-
- timeDelayUS(30); // 15 us
-
- return bitVal;
- }
-
- static uint8_t ds18b20ReadByte( void )
- {
- uint8_t byteVal = 0;
-
- for ( uint8_t i = 0; i < 8; i++ )
- {
- byteVal >>= 1;
-
- uint8_t bitVal = readBit();
- if( bitVal > 0)
- {
- byteVal |= 0x80;
- }
- }
-
- return byteVal;
- }
-
-
- /**
- * @brief write one byte to DS18B20
- * @note
- * @param byte: the data that is sended to ds18b20
- * @retval None
- */
- void ds18b20WriteByte( uint8_t byte)
- {
- unsigned char k;
-
- // Set PIN mode output
- DS_Mode_Out_PP();
-
- for ( k = 0; k < 8; k++ )
- {
- if (byte & (1<<k))
- {
- DQ_SET_LOW;
- timeDelayUS(2);
-
- DQ_SET_HIGH;
- timeDelayUS(65);
- }
- else
- {
- DQ_SET_LOW;
- timeDelayUS(65);
-
- DQ_SET_HIGH;
- timeDelayUS(2);
- }
- }
- }
-
- uint8_t ds18b20BlockModeProcess( void )
- {
- uint16_t tempValue;
- uint8_t tempL, tempH;
-
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
-
- // wait for 600 us
- timeDelayUS(600);
-
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0x44); // start convert temperature
-
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- // wait for 600 us
- timeDelayUS(600);
-
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0xbe); // read temperature data register
-
- tempL = ds18b20ReadByte();
- tempH = ds18b20ReadByte();
-
- if (tempH > 0x7f)
- {
- tempL = ~tempL;
- tempH = ~tempH+1;
- st_ds1b20val.sign = 1;
- }
-
- tempValue = (uint16_t)((tempH << 8) | tempL);
-
- st_ds1b20val.temperatureVal = (float)(tempValue * 0.0625);
-
- return TRUE;
- }
-
-
- // NO blocking mode operate ds18b20
- uint8_t ds18b20NoBlockingProcess( void )
- {
- uint16_t tempValue;
- static uint16_t waitCnt = 0;
- uint8_t tempL, tempH;
- static uint8_t runState = 0;
-
- switch( runState )
- {
- default:
- case INIT_DQ:
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- runState = WAIT_READY;
- break;
-
- case WAIT_READY:
- timeDelayUS(2); // IDEL
- runState = SKIDROM_CMD;
- break;
-
- case SKIDROM_CMD:
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0x44); // begin to convert temperature data
- waitCnt = 0;
- runState = WAIT_CONVERT;
- break;
-
- case WAIT_CONVERT:
- waitCnt++;
- if( waitCnt > WAIT_CNT_CONVERT)
- {
- waitCnt = 0;
- runState = RESET_CMD;
- }
- break;
-
- case RESET_CMD:
- if (ds18b20Init() == FALSE)
- {
- return FALSE;
- }
- runState = WAIT_DATA_READY;
- break;
-
- case WAIT_DATA_READY:
- timeDelayUS(2); // IDEL
- runState = READ_CMD;
- break;
-
- case READ_CMD:
- ds18b20WriteByte(0xcc);
- ds18b20WriteByte(0xbe); // read temperature data register
- runState = GET_VALUE;
- break;
-
- case GET_VALUE:
-
- tempL = ds18b20ReadByte();
- tempH = ds18b20ReadByte();
-
- if (tempH > 0x7f)
- {
- tempL = ~tempL;
- tempH = ~tempH+1;
- st_ds1b20val.sign = 1;
- }
-
- tempValue = (uint16_t)((tempH << 8) | tempL);
-
- st_ds1b20val.temperatureVal = (float)(tempValue * 0.0625);
- runState = INIT_DQ;
- return TRUE;
- }
-
- return FALSE;
- }
-
-
-
- /* End of this file */
2) Create the ds18b20.h file and write the following code
- /*
- FILE NAME : ds18b20.h
- Description: user ds18b20 interface
- Author : [email protected]
- Date : 2024/06/03
- */
- #ifndef DS18B20_H
- #define DS18B20_H
- #include "hal_data.h"
-
-
- #define WAIT_CNT_CONVERT 500
-
- #define timeDelayUS(us) R_BSP_SoftwareDelay(us, BSP_DELAY_UNITS_MICROSECONDS);
- #define DS_DELAY_MS(ms) R_BSP_SoftwareDelay(ms, BSP_DELAY_UNITS_MILLISECONDS);
-
-
- #define DS_IO_PORT_PIN BSP_IO_PORT_08_PIN_09
-
-
- #define DS_Mode_IN_PUT() R_IOPORT_PinCfg(&g_ioport_ctrl, DS_IO_PORT_PIN, IOPORT_CFG_PORT_DIRECTION_INPUT)
- #define DS_Mode_Out_PP() R_IOPORT_PinCfg(&g_ioport_ctrl, DS_IO_PORT_PIN, IOPORT_CFG_PORT_DIRECTION_OUTPUT)
-
- #define DQ_SET_LOW R_IOPORT_PinWrite(&g_ioport_ctrl, DS_IO_PORT_PIN, BSP_IO_LEVEL_LOW)
- #define DQ_SET_HIGH R_IOPORT_PinWrite(&g_ioport_ctrl, DS_IO_PORT_PIN, BSP_IO_LEVEL_HIGH)
-
-
- typedef struct{
- float temperatureVal;
- bool sign;
- }ds18b20Struc;
-
- uint8_t ds18b20BlockModeProcess( void );
- ds18b20Struc get_ds18b20_value( void );
-
-
- #endif /* DS18B20_H */
-
-