// **********ds1620b.c // Jonathan W. Valvano 1/15/02 // Copyright 2002 by Jonathan W. Valvano, valvano@uts.cc.utexas.edu // You may use, edit, run or distribute this file // as long as the above copyright notice remains // Interface between MC68HC812 and DS1620 // Uses SPI (solution to lab assignment) // See Valvano, Embedded Microcomputer Systems, Chap 7 // bit status Configuration/Status Register meaning // 7 DONE 1=Conversion done, 0=conversion in progress // 6 THF 1=temperature above TH, 0=temperature below TH // 5 TLF 1=temperature below TL, 0=temperature above TL // 1 CPU 1=CPU control, 0=stand alone operation // 0 1SHOT 1=one conversion and stop, 0=continuous conversions // temperature digital value (binary) digital value (hex) // +125.0 C 011111010 $0FA // +64.0 C 010000000 $080 // +1.0 C 000000010 $002 // +0.5 C 000000001 $001 // 0 C 000000000 $000 // -0.5 C 111111111 $1FF // -16.0 C 111100000 $1E0 // -55.0 C 110010010 $192 // DS1620 MC68HC812A4 // 1 DQ PS5 // 2 CLK PS6 // 3 RST PS7 // 4 GND ground // 5 TCOM not connected // 6 TLOW not connected // 7 THIGH not connected // 8 Vcc +5 V with bypass cap to ground void DS1620_Init(void){ // PS7=RST=0 DDRS=0xE0; // PS6=CLK=SPI clock out PORTS=0x60; // PS5=DQ=SPI bidirectional data /* bit SP0CR1 7 SPIE = 0 no interrupts 6 SPE = 1 enable SPI 5 SWOM = 0 regular outputs? 4 MSTR = 1 master 3 CPOL = 1 output changes on fall 2 CPHA = 1 and input clocked in on rise 1 SSOE = 0 PS7 regular output DS1620 RST 0 LSBF = 1 least significant bit first */ SP0CR1=0x5D; /* bit SP0CR2 3 PUPS = 0 no internal pullups 2 RDS = 0 regular drive 0 SPC0 = 1 bidirectional mode */ SP0CR2=0x01; SP0BR=0x02; // 1MHz could be 2Mhz } #define SPIF 0x80 void static out8(char code){ unsigned char dummy; // assumes DDRS bit 5 is 1, output SP0DR=code; while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR; // clear SPIF } void DS1620_Start(void){ PORTS |= 0x80; // PS7=RST=1 out8(0xEE); PORTS &= 0x7F;} // PS7=RST=0 void DS1620_Stop(void){ PORTS |= 0x80; // PS7=RST=1 out8(0x22); PORTS &= 0x7F;} // PS7=RST=0 void DS1620_WriteConfig(char data){ PORTS |= 0x80; // PS7=RST=1 out8(0x0C); out8(data); PORTS &= 0x7F;} // PS7=RST=0 void static out9(int code){ unsigned char dummy; SP0DR=0x00FF & code; // lsbyte while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR; // clear SPIF SP0DR=0x00FF&(code>>8); // msbyte while((SP0SR&SPIF)==0); // gadfly wait for SPIF dummy=SP0DR; // clear SPIF } void DS1620_WriteTH(int data){ PORTS |= 0x80; // PS7=RST=1 out8(0x01); out9(data); PORTS &= 0x7F;} // PS7=RST=0 void DS1620_WriteTL(int data){ PORTS |= 0x80; // PS7=RST=1 out8(0x02); out9(data); PORTS &= 0x7F;} // PS7=RST=0 unsigned char static in8(void){ int n; unsigned char result; DDRS &= 0xDF; // PS5=DQ input SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF result=SP0DR; // get data, clear SPIF DDRS |= 0x20; // PS5=DQ output return result;} unsigned char DS1620_ReadConfig(void){ unsigned char value; PORTS |= 0x80; // PS7=RST=1 out8(0xAC); value=in8(); PORTS &= 0x7F; // PS7=RST=0 return value;} unsigned int static in9(void){ unsigned int result; DDRS &= 0xDF; // PS5=DQ input SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF result=SP0DR; // get LS data, clear SPIF SP0DR=0; // start shift register while((SP0SR&SPIF)==0); // gadfly wait for SPIF if(SP0DR&0x01) // get MS data, clear SPIF result |= 0xFF00; // negative else result &= 0x00FF; // positive DDRS |= 0x20; // PS5=DQ output return result;} unsigned int DS1620_ReadTH(void){ unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xA1); value = in9(); PORTS &= 0x7F; // PS7=RST=0 return value;} unsigned int DS1620_ReadTL(void){ unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xA2); value = in9(); PORTS &= 0x7F; // PS7=RST=0 return value;} unsigned int DS1620_ReadT(void){ unsigned int value; PORTS |= 0x80; // PS7=RST=1 out8(0xAA); value = in9(); PORTS &= 0x7F; // PS7=RST=0 return value;}