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 } void Delay_us(uint us)/////6us { unsigned int i,j; for(i=0;i } void Delay(uint ms)//////60us { unsigned int i,j; for(i=0;i } void Delay40(uint ms)//////60us { unsigned int i,j; for(i=0;i } 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< } /**************************************************************************** * 名称: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< } 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);
Previous article:avr relay +1602 program
Next article:AVR I2C Communication Program
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Introduction to low-power Bluetooth technology solutions for lithium battery intelligent protection boards
- Sony employee stole $154 million in public funds and exchanged them for Bitcoin: when it was recovered, it appreciated to over $180 million
- 8-bit, 16-bit and 32-bit MCUs, are more bits better?
- [Evaluation of EVAL-M3-TS6-665PN development board] 3. Power-on test
- Get a gift for grabbing the post: Watch the live broadcast and gain an in-depth understanding of the principle, operation, and waterproof structure design of ST's latest MEMS barometer
- CPAFC Carrier Synchronization
- When should I use a PWM controller?
- Goodbye 2019, hello 2020!
- SensorTile.box unpacking issues
- Are the EX0 bit in the IE register and the IE0 bit in the TCON register in the 51 microcontroller duplicated?