ICC-AVR DS1821S temperature alarm program

Publisher:painterLatest update time:2019-11-14 Source: 51heiKeywords:ICC-AVR  DS1821S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The temperature measurement alarm output function of DS1821 has been tested and is usable, and has been used in batches of products.

//ICC-AVR application builder : 2015-10-8 14:15:46

// Target : M8

// Crystal: 8.0000Mhz

/* New chip burning program, capacitor heating temperature is set to 0x14, 0x13; snowboard is set to 0X02, 0X00*/

#include

#include



#define uchar         unsigned char 

#define uint         unsigned int

#define sint         short int

#define uint8 fly

#define uint16        unsigned short 

#define        uint32        unsigned int   

#define bool uchar


#define DS1620 0      

#define CLK           1          

#define        RST           2

#define        LEDL   3

#define LEDH 2

#define TEMPH  0x02             

#define TEMPL  0x00                


void Delay_ms(uint ms)

{

        unsigned int i,j;

        for(i=0;i                for(j=0;j<1141;j++); //1141 is the value obtained through repeated experiments with software simulation under 8MHz crystal oscillator

}


void Delay_us(uint us)/////6us

{

        unsigned int i,j;

        

        for(i=0;i                //for(j=0;j<1;j++); //114 is the value obtained through repeated experiments with software simulation under 8MHz crystal oscillator


}

void Delay(uint ms)//////60us

{

        unsigned int i,j;

        for(i=0;i                for(j=0;j<60;j++); //1141 is the value obtained by repeated experiments under 8MHz crystal oscillator through software simulation

}

void Delay40(uint ms)//////60us

{

        unsigned int i,j;

        for(i=0;i                for(j=0;j<40;j++); //1141 is the value obtained by repeated experiments under 8MHz crystal oscillator through software simulation

}

void Delay_1slot(unsigned int x)/////5us

{

                unsigned int i;

                i=x;

                while(i>0)i--;

}

void Delay_1us(uint ms)

{

        unsigned int i;

        i=0;

        i=0;

        i=0;

        i=0;

        

}


void port_init(void)

{

PORTB = 0x07;

DDRB  = 0x07;

PORTC = 0x00; //m103 output only

DDRC  = 0x00;

PORTD = 0x0f;

DDRD  = 0x0f;

}

void led_rotation(void)

{

    flying j;

        for(j=0;j<4;j++)//Loop light up

        {

                PORTD&=~BIT(3-j);

                Delay_ms(60);

                PORTD|=BIT(3-j);

        }

}


//UART0 initialize

// desired baud rate: 9600

// actual: baud rate:9615 (0.2%)

// char size: 8 bit

// parity: Disabled

void uart0_init(void)

{

UCSRB = 0x00; //disable while setting baud rate

UCSRA = 0x00;

UCSRC = BIT(URSEL) | 0x06;

UBRRL = 0x33; //set baud rate lo

UBRRH = 0x00; //set baud rate hi

UCSRB = 0x18;

}


//call this routine to initialize all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

port_init();

// uart0_init();

MCUCR = 0x00;

GICR  = 0x00;

TIMSK = 0x00; //timer interrupt sources

// SEI(); //re-enable interrupts

//all peripherals are now initialized

}


/****************************************************************************

* 名称:UartSendByte()

* Function: Send byte data to the serial port.

* Input parameter: data data to be sent

* Export parameters: None

****************************************************************************/

void  uart0SendByte(uint8 data)

{  

        while( !( UCSRA & (1<        UDR = data; //Send data

}

/****************************************************************************

* 名称:UartSendString()

* Function: Send a string to the serial port.

* Input parameter: data data to be sent

* Export parameters: None

****************************************************************************/

void  uart0SendString(uint8 *ptr)

{

        while(*ptr)

        {

                uart0SendByte(*ptr++);

        }

        uart0SendByte(0x0D);

        uart0SendByte(0x0A); //Send carriage return and line feed at the end

}

/****************************************************************************

* Name: UartRcvByte()

* Function: Receive byte data from the serial port.

* Entry parameters: None

* Export parameter: data received

****************************************************************************/

uint8 uart0RcvByte(void) //Receive using query method

{

        while( !( UCSRA & (1<        return UDR; //Get and return data

}


void ds1821_reset()

{

//uchar ack;

DDRB|=BIT(DS1620);

PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

Delay_us(600);

PORTB|=BIT(DS1620); //If it is high, pull the single bus high

//Delay(1);

DDRB&=~BIT(DS1620);

while((PINB&BIT(DS1620)));

while(!(PINB&BIT(DS1620)));

//  Delay_us(150);

// ack=PINB;

//  ack&=0x01;

  Delay_us(50);

// return ack;

}


void DS1821_WriteByte(uint8 dat)

{

uint8 i;

DDRB|=BIT(DS1620);

for(i=0;i<8;i++)

{

        PORTB&=~BIT(DS1620);

        Delay_1slot(0);

        Delay_1slot(0);

    if (dat&0x01)

        { // bit = 1 LSB first

           PORTB|=BIT(DS1620);

    } 

        else 

        { // bit = 0

      PORTB&=~BIT(DS1620);

    }

        Delay40(1);

        PORTB|=BIT(DS1620);

        Delay_1slot(0);

    that >>= 1;

        

        

  }

DDRB&=~BIT(DS1620); 

}


uint8 DS1821_ReadByte(void)

{

uint8 that,i; 

that=0;

for(i=0;i<8;i++)

{

  that >>= 1; 

  DDRB|=BIT(DS1620); //Set p01 to output state

  PORTB&=~BIT(DS1620);

  PORTB|=BIT(DS1620);

  DDRB&=~BIT(DS1620);;//Set p01 to input state

  PORTB&=~BIT(RST);

  PORTB|=BIT(RST);

  if(PINB&BIT(DS1620))   

  {

    dat|=0x80; // msb 優先

  }

   Delay40(1);//等40us, 加上其它程式碼, 每個bit有80us

}     

return that;

}


void DS1821_switchmode(void)

{

  uint8 i;

  DDRB|=BIT(DS1620);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

//  Delay_ms(3000);

        Delay_1slot(1);

  PORTB&=~BIT(CLK);

  Delay_1slot(2);

  

  for(i=0;i<16;i++)

  {

   PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  //Delay_1slot(1);

          PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  //Delay_1slot(1);

  

  

  }

  /*

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);


  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);


  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

      

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);  


  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);


  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  

  PORTB&=~BIT(DS1620); //If it is low, pull the single bus low

  Delay_1slot(1);

  PORTB|=BIT(DS1620); //If it is high, pull the single bus high

  Delay_1slot(1);

  */

  Delay_1slot(2);

  PORTB|=BIT(CLK);

}


/*/Watchdog start function

void WDT_ON()

{

        WDTCR=0x0f; //WDE=1-watchdog enable, WDP0:1:2=1:1:1-2 seconds to feed the watchdog.                

}


//Watchdog shutdown function

void WDT_OFF()

{

        WDTCR|=BIT(WDTOE)|BIT(WDE); //Create 4 cycles of shutdown time

        WDTCR&=~BIT(WDE); //turn off watchdog

}*/



int main()

{

uint8 data;

char a;

int d,j,i,cntl=0,cnth=0,cntm=0;

init_devices(); 

led_rotation();

//uart0SendByte(0x80);

ds1821_reset();

DS1821_switchmode();

Delay_us(2);


// uart0SendByte(0x81);

/* DS1821_switchmode(); //Mode conversion

  ds1821_reset();

  DS1821_WriteByte(0x0c); //Write status register value

  DS1821_WriteByte(0x02);

  Delay_us(4); 

// data=DS1821_ReadByte(); */ //Mode conversion, the chip does not need

  

//  uart0SendByte(data); 

// uart0SendByte(0x82);

//  data=&0x04;

// if(data==0x00) PORTD&=~BIT(2); //Work in 1-line mode, turn on light 3

ds1821_reset();

// Delay_us(4);

// uart0SendByte(0x83);

DS1821_WriteByte(0x01); //Write the upper temperature limit

DS1821_WriteByte(TEMPH);

  Delay_us(4);

ds1821_reset();

DS1821_WriteByte(0x02); //Write lower limit of temperature 

DS1821_WriteByte(TEMPL);

  Delay_us(4);

ds1821_reset();

DS1821_WriteByte(0xa1); //Read temperature upper limit

data=DS1821_ReadByte();

if(data==TEMPH)

    PORTD&=~BIT(LEDH);        

else

    PORTD|=BIT(LEDH);

ds1821_reset();

DS1821_WriteByte(0xa2); //Read temperature lower limit

data=DS1821_ReadByte();

if(data==TEMPL)

        PORTD&=~BIT(LEDL);        

  else        

        PORTD|=BIT(LEDL);

        

ds1821_reset();        

DS1821_WriteByte(0x0c); //Write status register value

DS1821_WriteByte(0x46);

Delay_us(4); 

//return 0;   

while(1);

//  uart0SendByte(0x84);

  

// ds1821_reset();

// DS1821_WriteByte(0xee); //Start temperature conversion instruction

//Delay_us(4);



//WDT_ON();

// while(1)

// {

//WDR();


// uart0SendByte(0x55);

   

// ds1821_reset();

/*  DS1821_WriteByte(0xac);

  data=DS1821_ReadByte();


  if((data&0x04)!=0)

  {

           cntm++;

          if(cntm%2==0)

                   PORTD&=~BIT(CLK);        

          else        

                PORTD|=BIT(CLK);

        

}


uart0SendByte(data);

Delay_us(4);

   

  ds1821_reset();

DS1821_WriteByte(0xa1);

[1] [2]
Keywords:ICC-AVR  DS1821S Reference address:ICC-AVR DS1821S temperature alarm program

Previous article:avr relay +1602 program
Next article:AVR I2C Communication Program

Latest Microcontroller Articles
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号