changeset 9:7da9a3f23592

Import ds18x20 code
author Matt Johnston <matt@ucc.asn.au>
date Fri, 18 May 2012 23:57:08 +0800
parents c55321727d02
children 1bfe28c348dd
files Makefile crc8.c crc8.h crc8.o diskio.h ds18x20.c ds18x20.h main.c onewire.h uart_addon.c uart_addon.h
diffstat 11 files changed, 1359 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri May 18 20:38:40 2012 +0800
+++ b/Makefile	Fri May 18 23:57:08 2012 +0800
@@ -19,12 +19,18 @@
 
 DEVICE     = atmega328
 PROGDEVICE     = atmega328p
-CLOCK      = 1000000
+CLOCK      = 2000000
 PROGRAMMER = #-c stk500v2 -P avrdoper
 PROGRAMMER = -c stk500 -P ~/dev/stk500 -p $(PROGDEVICE) 
-OBJECTS    = main.o #ff.o mmc.o onewire.o
+OBJS_1WIRE = onewire.o ds18x20.o uart_addon.o crc8.o
+OBJS_SD = ff.o mmc.o
+OBJECTS    = main.o
+OBJECTS += $(OBJS_1WIRE)
+#OBJECTS += OBJS_SD
 LIBS       = -lm
-FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m
+
+# default but 2mhz
+FUSES      = -U hfuse:w:0xd9:m -U lfuse:w:0x62:m
 
 # ATMega8 fuse bits used above (fuse bits for other devices are different!):
 # Example for 8 MHz internal oscillator
@@ -33,17 +39,17 @@
 #        ^ ^ ^ ^   ^ ^ ^------ BOOTSZ0
 #        | | | |   | +-------- BOOTSZ1
 #        | | | |   +---------- EESAVE (set to 0 to preserve EEPROM over chip erase)
-#        | | | +-------------- CKOPT (clock option, depends on oscillator type)
+#        | | | +-------------- WDTON
 #        | | +---------------- SPIEN (if set to 1, serial programming is disabled)
-#        | +------------------ WDTON (if set to 0, watchdog is always on)
+#        | +------------------ DWEN
 #        +-------------------- RSTDISBL (if set to 0, RESET pin is disabled)
 # Fuse low byte:
-# 0x24 = 0 0 1 0   0 1 0 0
+# 0x62 = 0 1 1 0   0 0 1 0
 #        ^ ^ \ /   \--+--/
 #        | |  |       +------- CKSEL 3..0 (8M internal RC)
 #        | |  +--------------- SUT 1..0 (slowly rising power)
-#        | +------------------ BODEN (if 0, brown-out detector is enabled)
-#        +-------------------- BODLEVEL (if 0: 4V, if 1: 2.7V)
+#        | +------------------ CKOUT
+#        +-------------------- CLKDIV8
 #
 # For computing fuse byte values for other devices and options see
 # the fuse bit calculator at http://www.engbedded.com/fusecalc/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc8.c	Fri May 18 23:57:08 2012 +0800
@@ -0,0 +1,63 @@
+/* please read copyright-notice at EOF */
+
+#include <stdint.h>
+
+#define CRC8INIT    0x00
+#define CRC8POLY    0x18              //0X18 = X^8+X^5+X^4+X^0
+
+uint8_t crc8( uint8_t *data, uint16_t number_of_bytes_in_data )
+{
+	uint8_t  crc;
+	uint16_t loop_count;
+	uint8_t  bit_counter;
+	uint8_t  b;
+	uint8_t  feedback_bit;
+	
+	crc = CRC8INIT;
+
+	for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++)
+	{
+		b = data[loop_count];
+		
+		bit_counter = 8;
+		do {
+			feedback_bit = (crc ^ b) & 0x01;
+	
+			if ( feedback_bit == 0x01 ) {
+				crc = crc ^ CRC8POLY;
+			}
+			crc = (crc >> 1) & 0x7F;
+			if ( feedback_bit == 0x01 ) {
+				crc = crc | 0x80;
+			}
+		
+			b = b >> 1;
+			bit_counter--;
+		
+		} while (bit_counter > 0);
+	}
+	
+	return crc;
+}
+
+/*
+This code is from Colin O'Flynn - Copyright (c) 2002 
+only minor changes by M.Thomas 9/2004
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc8.h	Fri May 18 23:57:08 2012 +0800
@@ -0,0 +1,40 @@
+#ifndef CRC8_H_
+#define CRC8_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+uint8_t crc8( uint8_t* data, uint16_t number_of_bytes_in_data );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+/*
+This is based on code from :
+
+Copyright (c) 2002 Colin O'Flynn
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
Binary file crc8.o has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/diskio.h	Fri May 18 23:57:08 2012 +0800
@@ -0,0 +1,74 @@
+/*-----------------------------------------------------------------------
+/  Low level disk interface modlue include file   (C)ChaN, 2010
+/-----------------------------------------------------------------------*/
+
+#ifndef _DISKIO
+
+#define _READONLY	0	/* 1: Remove write functions */
+#define _USE_IOCTL	1	/* 1: Use disk_ioctl fucntion */
+
+#include "integer.h"
+
+
+/* Status of Disk Functions */
+typedef BYTE	DSTATUS;
+
+/* Results of Disk Functions */
+typedef enum {
+	RES_OK = 0,		/* 0: Successful */
+	RES_ERROR,		/* 1: R/W Error */
+	RES_WRPRT,		/* 2: Write Protected */
+	RES_NOTRDY,		/* 3: Not Ready */
+	RES_PARERR		/* 4: Invalid Parameter */
+} DRESULT;
+
+
+/*---------------------------------------*/
+/* Prototypes for disk control functions */
+
+DSTATUS disk_initialize (BYTE);
+DSTATUS disk_status (BYTE);
+DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
+#if	_READONLY == 0
+DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
+#endif
+DRESULT disk_ioctl (BYTE, BYTE, void*);
+void	disk_timerproc (void);
+
+
+
+/* Disk Status Bits (DSTATUS) */
+
+#define STA_NOINIT		0x01	/* Drive not initialized */
+#define STA_NODISK		0x02	/* No medium in the drive */
+#define STA_PROTECT		0x04	/* Write protected */
+
+
+/* Command code for disk_ioctrl fucntion */
+
+/* Generic command (mandatory for FatFs) */
+#define CTRL_SYNC			0	/* Flush disk cache (for write functions) */
+#define GET_SECTOR_COUNT	1	/* Get media size (for only f_mkfs()) */
+#define GET_SECTOR_SIZE		2	/* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */
+#define GET_BLOCK_SIZE		3	/* Get erase block size (for only f_mkfs()) */
+
+/* Generic command */
+#define CTRL_POWER			4	/* Get/Set power status */
+#define CTRL_LOCK			5	/* Lock/Unlock media removal */
+#define CTRL_EJECT			6	/* Eject media */
+
+/* MMC/SDC command */
+#define MMC_GET_TYPE		10	/* Get card type */
+#define MMC_GET_CSD			11	/* Get CSD */
+#define MMC_GET_CID			12	/* Get CID */
+#define MMC_GET_OCR			13	/* Get OCR */
+#define MMC_GET_SDSTAT		14	/* Get SD status */
+
+/* ATA/CF command */
+#define ATA_GET_REV			20	/* Get F/W revision */
+#define ATA_GET_MODEL		21	/* Get model name */
+#define ATA_GET_SN			22	/* Get serial number */
+
+
+#define _DISKIO
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ds18x20.c	Fri May 18 23:57:08 2012 +0800
@@ -0,0 +1,760 @@
+/*********************************************************************************
+Title:    DS18X20-Functions via One-Wire-Bus
+Author:   Martin Thomas <[email protected]>   
+          http://www.siwawi.arubi.uni-kl.de/avr-projects
+Software: avr-gcc 4.3.3 / avr-libc 1.6.7 (WinAVR 3/2010) 
+Hardware: any AVR - tested with ATmega16/ATmega32/ATmega324P and 3 DS18B20
+
+Partly based on code from Peter Dannegger and others.
+
+changelog:
+20041124 - Extended measurements for DS18(S)20 contributed by Carsten Foss (CFO)
+200502xx - function DS18X20_read_meas_single
+20050310 - DS18x20 EEPROM functions (can be disabled to save flash-memory)
+           (DS18X20_EEPROMSUPPORT in ds18x20.h)
+20100625 - removed inner returns, added static function for read scratchpad
+         . replaced full-celcius and fractbit method with decicelsius
+           and maxres (degreeCelsius*10e-4) functions, renamed eeprom-functions,