DS1821 驱动程序

来源:互联网 发布:怎样快速提升淘宝心 编辑:程序博客网 时间:2024/05/20 17:26

DS1821复位时序


DS1821读写时序




DS1821命令设置



DS1821 工作流程

 


DS1821 驱动


头文件


#ifndef __DS1821DRIVER_H#define __DS1821DRIVER_H/* ------------------------------------------------------------------------- + |   MACROS                                                                   + ------------------------------------------------------------------------- */extern char DS1821DriverStateMachine(void);/* ------------------------------------------------------------------------- + |   END                                                                      + ------------------------------------------------------------------------- */#endif 

源文件

/* ------------------------------------------------------------------------- + |                   Televic N.V.  + ------------------------------------------------------------------------- *//* --------------------------- COPYRIGHT INFORMATION ----------------------- + | This program contains proprietary information that is a trade secret of    | Televic N.V. and also is protected as an         | unpublished  work under applicable Copyright laws. Recipient is to retain  | this program in confidence and is not permitted to use or make copies      | thereof other than as permitted in a prior written agreement with          | Televic N.V.                                     | (c) Copyright, 2005, Televic N.V.                | All rights reserved. Company confidential.                                 + ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- + | Module        : MCC | File name     : ds1821_driver.c | Description   :  | Reference(s)  : none+ ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- + |   HEADER (INCLUDE) SECTION                                                 + ------------------------------------------------------------------------- */#include "ds1821_driver.h"#include "Timer_user.h"#include "hhd_sys.h"#include "hhd_gpio.h"#include <stdio.h>#include <string.h>/* ------------------------------------------------------------------------- + |   EXTERNAL DATA                                                            + ------------------------------------------------------------------------- */                                  /* ------------------------------------------------------------------------- + |   TYPE DEFINITIONS (file scope only)                                  + ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- + |   GLOBAL CONSTANTS                                                         + ------------------------------------------------------------------------- *//* ------------------------------------------------------------------------- + |   GLOBAL VARIABLES                                    + ------------------------------------------------------------------------- *//* - Prototypes in header file --------------------------------------------- *//* - File scope only ------------------------------------------------------- */#define   XTAL   16   //可定义为你所用的晶振频率(单位Mhz)         // #define   Delay_10us(x)   __delay_cycles ( (unsigned long)(x * XTAL)  ) // #define   delay_ms(x)   __delay_cycles ( (unsigned long)(x * XTAL*1000) )// #define   delay_s(x)    __delay_cycles ( (unsigned long)(x * XTAL*1000000) )char g_ds1821_str[DS1821_LEN] = {',','$', 'T', 'E', 'M', 'P', ',' ,' ',' ', ' ', '*','\r', '\n'};;// #define W1_PIN (6)// #define W1_IN  PINF// #define W1_OUT PORTF// #define W1_DDR DDRF// #define Delay_10us(x)    (x)// #define DELAY(x)       delay(x)// #define delay(x)       Delay_10us(x)#define DQ             PIN8#define DQ_DIR_IN      GPIO_ConfigPinsAsInput(GPIOA, DQ)#define DQ_DIR_OUT     GPIO_ConfigPinsAsOutput(GPIOA, DQ)#define DQ_H           GPIO_SetPin(GPIOA,DQ); #define DQ_L           GPIO_ResetPin(GPIOA,DQ);/* ------------------------------------------------------------------------- + |   LOCAL FUNCTION PROTOTYPES                                                + ------------------------------------------------------------------------- *//**----------------------------------------------------------------------------** @Function:   ds1821_reset**** @Descriptions:  Reset ds1821**** @parameters:**                  **                  **                  ** @Returned: presence:0 reset ok, presence:1 reset error.** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/uint8_t ds1821_reset(void){  uint8_t presence, i;  DQ_DIR_OUT;       //Set DQ pin as output  DQ_L;  Delay_10us(48);   //480us~960us  DQ_H;//  Delay_10us(1);   //15~60us wait for DQ  to Low  DQ_DIR_IN;        //Set DQ pin as input   for(i=0; i<6; i++) //15~60us wait for DQ  to Low, ds1821 ack.  { Delay_10us(1); //10us presence = GPIO_GetPinState(GPIOA, DQ);  if(presence == 0) {  //printf("The i = %d\r\n", i);break;}}  //presence = GPIO_GetPinState(GPIOA, DQ);      Delay_10us(48-6); // wait for DQ to High  while((GPIO_GetPinState(GPIOA, DQ)) == 0); // wait for DQ to High//  if(GPIO_GetPinState(GPIOA, DQ) == 0x01) // rval = GPIO_GetPinState(GPIOA, DQ);//W1_OUT &= ~(1<<W1_PIN);   // One-wire LOW//W1_DDR |= 1<<W1_PIN;//  //DELAY( Delay_10us( 480 ));  // 480 us//  //W1_DDR &= ~(1<<W1_PIN);   //DELAY( Delay_10us( 65 ));   // Wait 15 + 50 us//uint8_t err = W1_IN & (1 << W1_PIN );   // Sample bus////DELAY( Delay_10us( 480 - 65 ));  // wait until cycle is gone //  //if( (W1_IN & (1<<W1_PIN)) == 0 )  // check bus for HIGH, othwise we have a short circuit//  err = 1;return presence;}/**----------------------------------------------------------------------------** @Function:   ds1821_bit_read**** @Descriptions:  read one bit**** @parameters:**                  **                  **                  ** @Returned: value:The value to be read** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/uint8_t ds1821_bit_read( void ){uint8_t value = 0;DQ_DIR_OUT;DQ_L;Delay_10us(1); //10us(1us< t < ∞)   DQ_H;         // relase 1-wire//Delay_10us(1); //10usDQ_DIR_IN;value = GPIO_GetPinState(GPIOA, DQ); Delay_10us(50); //50us// W1_OUT &= ~(1<<W1_PIN);   // One-wire LOW// W1_DDR |= 1<<W1_PIN;// // DELAY( Delay_10us( 1 ));  // 1 us time // W1_DDR &= ~(1<<W1_PIN);   // One-wire HIGH and use as input// DELAY( Delay_10us( 14 ));   // 14 us // uint8_t value = W1_IN & (1<<W1_PIN);  // sample bus// DELAY( Delay_10us( 45 ));   // 45 us return value;}/**----------------------------------------------------------------------------** @Function:   ds1821_bit_write**** @Descriptions:  write one bit**** @parameters:**                  **                  **                  ** @Returned: none** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/void ds1821_bit_write( uint8_t b ){  DQ_DIR_OUT;  DQ_L;  Delay_10us(2); //10us  if(b){DQ_H;}else{DQ_L;}Delay_10us(5);DQ_H;  //realse 1-wire // W1_OUT &= ~(1<<W1_PIN);   // One-wire LOW// W1_DDR |= 1<<W1_PIN;// DELAY( Delay_10us( 10 ));   // 10 us // if ( b ) // {// W1_DDR &= ~(1<<W1_PIN);   // One-wire input    // DELAY( Delay_10us( 5 + 45 ));   // in total 60 us// }// else// {// DELAY( Delay_10us( 5 + 45 ));   // 60 us in total// W1_DDR &= ~(1<<W1_PIN);   // One-wire HIGH and use as input    // }}/**----------------------------------------------------------------------------** @Function:   ds1821_read**** @Descriptions:  read  x bit**** @parameters:bits: Specifies the number of bits to read**                  **                  **                  ** @Returned: none** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/int ds1821_read( uint8_t bits){uint8_t i = bits;int  value = 1 << (bits-1);int  b = 0;do{   b >>= 1;   if( ds1821_bit_read() )  //first read LSB b |= value;} while( --i );return b;}/**----------------------------------------------------------------------------** @Function:   ds1821_byte_rd**** @Descriptions:  read one byte**** @parameters:bits: Specifies the number of bits to read**                  **                  **                  ** @Returned: ** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/char ds1821_byte_rd( void ){   return (char) ds1821_read(8);}/**----------------------------------------------------------------------------** @Function:   ds1821_byte_wr**** @Descriptions:  read one byte**** @parameters:bits: Specifies the number of bits to read**                  **                  **                  ** @Returned: ** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/void ds1821_byte_wr( uint8_t b ){  uint8_t i = 8;  do  {ds1821_bit_write( b & 0x01 );b >>= 1;  } while( --i );}/**----------------------------------------------------------------------------** @Function:   DS1821DriverStateMachine**** @Descriptions:  Read actual temperature value**** @parameters:**                  **                  **                  ** @Returned: ds1821_tmp: Actual temperature value** ** @Author:  MCU Application Team**** @Date: 2017-08-16**-------------------------------------------------------------------------------*/char DS1821DriverStateMachine(void){   char ds1821_tmp = 0;    char temp;  uint8_t status = 0;  do{temp = ds1821_reset();//printf("The temp = %x \r\n", temp);}while(temp!=0);printf("The temp Sensor have been checked\r\n");  ds1821_byte_wr(0xee); // start conversiondo{temp = ds1821_reset();//printf("The temp = %x \r\n", temp);}while(temp!=0);  ds1821_byte_wr(0xAA);             // Reads last converted temperature valueds1821_tmp = ds1821_byte_rd();   // low byte//ds1821_byte_wr(0xA1);//status = ds1821_read(8);//printf("The status = %x \r\n", status);//do//{//temp = ds1821_reset();////printf("The temp = %x \r\n", temp);//}while(temp!=0);//ds1821_byte_wr(0xA2);//status = ds1821_read(8);//printf("The status = %x \r\n", status);ds1821_byte_wr(0x22);               // stop conversion   return ds1821_tmp;}/* ------------------------------------------------------------------------- + |   END                                                                      + ------------------------------------------------------------------------- */


main.c

/** Copyright(C) 2017,HaiWei * ALL rights reserved.** @file    main.c* @author  MCU Application Team* @version V0.1* @date    9-June-2015* @brief   **//* Includes ------------------------------------------------------------------*/#include<string.h>#include<stdio.h>#include "hhd32f1xx.h"#include "hhd_sys.h"#include "hhd_uart.h"#include "hhd_gpio.h"#include "hhd_adc.h"#include "hhd_spi.h"#include "hhd_iic.h"#include "system_user.h"#include "USART_user.h"#include "ADC_user.h"#include "SPI_user.h"#include "I2C_user.h"#include "GPIO_user.h"#include "Timer_user.h"#include "8255.h"#include "task.h"#include "ds1821_driver.h"//#include "delay.h"//#include "sys.h"/*--------------------------Private define-------------------------*///#define GPIO_IN_TEST  0//#define GPIO_OUT_TEST 1//#define UART_TEST 2//#define IO_EXTI   3 #define ADC_TEST  4//#define SPI_TEST    5//#define I2C_TEST    6//#define ADC_TEMP    7#define TIM_5S        500  //10ms x 500 = 5s/*--------------------------Function volitale-------------------------*/uint32_t val = 0;extern uint16_t TimerCnt , gTimerFlag;/*--------------------------Function declare-------------------------*/void NVIC_Init(void);void DeviceInit(void);/**----------------------------------------------------------------------------** @Function:   main**** @Descriptions:   **** @parameters:**                  **                  **                  ** @Returned: none** ** @Author:  MCU Application Team**** @Date: 2017-07-14**-------------------------------------------------------------------------------*/int main(void){  char temp = 0;DeviceInit();TimerCnt = TIM_5S;while(1){    //UART_Test();  //ADC_Test();   // SPI_Test();//I2C_Test();// IO_EXTI_Test(); //GPIO_Test();//ADC_TempSensor();//if(gTimerFlag == 1)//{//TimerCnt = TIM_5S;//gTimerFlag = 0;//} //delay_us(2000000);Delay_10us(100000); //1sGPIO_InvertOutPin(GPIOB, PIN9);//LEDtemp = 0;temp = DS1821DriverStateMachine();printf("The tempretrue is:%d ℃\r\n", temp);//  GPIO_InvertOutPin(GPIOC, PIN6);//LED//delay_us(2000000);// NVIC->IP[0] = 0xff;// val = NVIC->IP[0] ;// printf("The NVIC->IP[0] = %x\r\n", val);// val = NVIC_GetPriority(TIM2_IRQn);// printf("TIM2_IRQn = %x\r\n", val);// val = NVIC_GetPriority(UART1_IRQn);// printf("UART1_IRQn] = %x\r\n", val);  }}/**----------------------------------------------------------------------------** @Function:   DeviceInit**** @Descriptions:   **** @parameters:**                  **                  **                  ** @Returned: none** ** @Author:  MCU Application Team**** @Date: 2017-07-14**-------------------------------------------------------------------------------*/void DeviceInit(void){/* ----------- System initialization -------------------------------------*/SYS_SystemInitial();//SystemCoreClockUpdate();GPIO_Configuration();//IO_EXTI_Configuration();USART_Configuration();//ADC_Configuration();//I2C_Configuration();//ADC_TempSensorConfiguration();//SPI_Configuration();// VariableInit();// D8255_Init(); Timer_Configuration();// delay_init(); NVIC_Init();   NVIC_EnableIRQ(UART1_IRQn);}/**----------------------------------------------------------------------------** @Function:   NVIC_Init**** @Descriptions:   **** @parameters:**                  **                  **                  ** @Returned: none** ** @Author:  MCU Application Team**** @Date: 2017-07-14**-------------------------------------------------------------------------------*/void NVIC_Init(void){NVIC_SetPriority(UART1_IRQn, 0);NVIC_SetPriority(TIM2_IRQn, 1);}