/*********************************************************************************
Title: DS18X20-Functions via One-Wire-Bus
Author: Martin Thomas <eversmith@heizung-thomas.de>
http://www.siwawi.arubi.uni-kl.de/avr-projects
Software: avr-gcc 3.4.1 / avr-libc 1.0.4
Hardware: any AVR - tested with ATmega16/ATmega32 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)
**********************************************************************************/
//#include <avr/io.h>
#include "ds18x20.h"
#include "onewire.h"
#include "crc8.h"
#ifdef DS18X20_EEPROMSUPPORT
// for 10ms delay in copy scratchpad
#include "delay.h"
#endif
/*----------- start of "debug-functions" ---------------*/
#ifdef DS18X20_VERBOSE
/* functions for debugging-output - undef DS18X20_VERBOSE in .h
if you run out of program-memory */
#include <string.h>
//#include "uart.h"
#include "5200_board.h"
#if 0
void DS18X20_uart_put_temp(const u_int8 subzero,
const u_int8 cel, const u_int8 cel_frac_bits)
{
u_int8 buffer[sizeof(int)*8+1];
int i;
uart_putc((subzero)?'-':'+');
uart_puti((int)cel);
uart_puts_P(".");
// itoa((int)(cel_frac_bits*DS18X20_FRACCONV),buffer,10);
for (i=0;i<4-strlen(buffer);i++) uart_puts_P("0");
uart_puts(buffer);
uart_puts_P("蚓");
}
#endif
void DS18X20_show_id_uart( u_int8 *id, size_t n )
{
size_t i;
for( i = 0; i < n; i++ ) {
if ( i == 0 ) uart_puts_P( "FC:" );
else if ( i == n-1 ) uart_puts_P( "CRC:" );
if ( i == 1 ) uart_puts_P( "SN: " );
uart_puthex_byte(id[i]);
uart_puts_P(" ");
if ( i == 0 ) {
if ( id[0] == DS18S20_ID ) uart_puts_P ("(18S)");
else if ( id[0] == DS18B20_ID ) uart_puts_P ("(18B)");
else uart_puts_P ("( ? )");
}
}
if ( crc8( id, OW_ROMCODE_SIZE) )
uart_puts_P( " CRC FAIL " );
else
uart_puts_P( " CRC O.K. " );
}
void show_sp_uart( u_int8 *sp, size_t n )
{
size_t i;
uart_puts_P( "SP:" );
for( i = 0; i < n; i++ ) {
if ( i == n-1 ) uart_puts_P( "CRC:" );
uart_puthex_byte(sp[i]);
uart_puts_P(" ");
}
}
# if 0
/* verbose output rom-search follows read-scratchpad in one loop */
u_int8 DS18X20_read_meas_all_verbose( void )
{
u_int8 id[OW_ROMCODE_SIZE], sp[DS18X20_SP_SIZE], diff;
u_int8 i;
u_int16 meas;
u_int8 subzero, cel, cel_frac_bits;
for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
{
diff = ow_rom_search( diff, &id[0] );
if( diff == OW_PRESENCE_ERR ) {
uart_puts_P( "No Sensor found\r" );
return OW_PRESENCE_ERR;
}
if( diff == OW_DATA_ERR ) {
uart_puts_P( "Bus Error\r" );
return OW_DATA_ERR;
}
DS18X20_show_id_uart( id, OW_ROMCODE_SIZE );
if( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) { // temperature sensor
uart_putc ('\r');
ow_byte_wr( DS18X20_READ ); // read command
for ( i=0 ; i< DS18X20_SP_SIZE; i++ )
sp[i]=ow_byte_rd();
show_sp_uart( sp, DS18X20_SP_SIZE );
if ( crc8( &sp[0], DS18X20_SP_SIZE ) )
uart_puts_P( " CRC FAIL " );
else
uart_puts_P( " CRC O.K. " );
uart_putc ('\r');
meas = sp[0]; // LSB Temp. from Scrachpad-Data
meas |= (u_int16) (sp[1] << 8); // MSB
uart_puts_P(" T_raw=");
uart_puthex_byte((u_int8)(meas>>8));
uart_puthex_byte((u_int8)meas);
uart_puts_P(" ");
if( id[0] == DS18S20_ID ) { // 18S20
uart_puts_P( "S20/09" );
}
else if ( id[0] == DS18B20_ID ) { // 18B20
i=sp[DS18B20_CONF_REG];
if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) {
uart_puts_P( "B20/12" );
}
else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) {
uart_puts_P( "B20/11" );
}
else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) {
uart_puts_P( " B20/10 " );
}
else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) {
uart_puts_P( "B20/09" );
}
}
uart_puts_P(" ");
DS18X20_meas_to_cel(id[0], sp, &subzero, &cel, &cel_frac_bits);
DS18X20_uart_put_temp(subzero, cel, cel_frac_bits);
uart_puts("\r");
} // if meas-sensor
} // loop all sensors
uart_puts_P( "\r" );
return DS18X20_OK;
}
#endif
#endif
/*----------- end of "debug-functions" ---------------*/
/*
convert raw value from DS18x20 to Celsius
input is:
- familycode fc (0x10/0x28 see header)
- scratchpad-buffer
output is:
- cel full celsius
- fractions of celsius in millicelsius*(10^-1)/625 (the 4 LS-Bits)
- subzero =0 positiv / 1 negativ
always returns DS18X20_OK
TODO invalid-values detection (but should be covered by CRC)
*/
u_int8 DS18X20_meas_to_cel( u_int8 fc, u_int8 *sp,
u_int8* subzero, u_int8* cel, u_int8* cel_frac_bits)
{
u_int16 meas;
u_int8 i;
meas = sp[0]; // LSB
meas |= ((u_int16)sp[1])<<8; // MSB
//meas = 0xff5e; meas = 0xfe6f;
// only work on 12bit-base
if( fc == DS18S20_ID ) { // 9 -> 12 bit if 18S20
/* Extended measurements for DS18S20 contributed by Carsten Foss */
meas &= (u_int16) 0xfffe; // Discard LSB , needed for later extended precicion calc
meas <<= 3; // Convert to 12-bit , now degrees are in 1/16 degrees units
meas += (16 - sp[6]) - 4; // Add the compensation , and remember to subtract 0.25 degree (4/16)
}
// check for negative
if ( meas & 0x8000 ) {
*subzero=1; // mark negative
meas ^= 0xffff; // convert to positive => (twos complement)++
meas++;
}
else *subzero=0;
// clear undefined bits for B != 12bit
if ( fc == DS18B20_ID ) { // check resolution 18B20
i = sp[DS18B20_CONF_REG];
if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) ;
else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT )
meas &= ~(DS18B20_11_BIT_UNDF);
else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT )
meas &= ~(DS18B20_10_BIT_UNDF);
else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) {
meas &= ~(DS18B20_9_BIT_UNDF);
}
}
*cel = (u_int8)(meas >> 4);
*cel_frac_bits = (u_int8)(meas & 0x000F);
return DS18X20_OK;
}
/* converts to decicelsius
input is ouput from meas_to_cel
returns absolute value of temperatur in decicelsius
i.e.: sz=0, c=28, frac=15 returns 289 (=28.9蚓)
0 0 0
1 625 625 1
2 1250 250
3 1875 875 3
4 2500 500 4
5 3125 125
6 3750 750 6
7 4375 375
8 5000 0
9 5625 625 9
10 6250 250
11 6875 875 11
12 7500 500 12
13 8125 125
14 8750 750 14
15 9375 375 */
u_int16 DS18X20_temp_to_decicel(u_int8 subzero, u_int8 cel,
u_int8 cel_frac_bits)
{
u_int16 h;
u_int8 i;
u_int8 need_rounding[] = { 1, 3, 4, 6, 9, 11, 12, 14 };
h = cel_frac_bits*DS18X20_FRACCONV/1000;
h += cel*10;
if (!subzero) {
for (i=0; i<sizeof(need_rounding); i++) {
if ( cel_frac_bits == need_rounding[i] ) {
h++;
break;
}
}
}
return h;
}
/* compare temperature values (full celsius only)
returns -1 if param-pair1 < param-pair2
0 if ==
1 if > */
short DS18X20_temp_cmp(u_int8 subzero1, u_int16 cel1,
u_int8 subzero2, u_int16 cel2)
{
short t1 = (subzero1) ? (cel1*(-1)) : (cel1);
short t2 = (subzero2) ? (cel2*(-1)) : (cel2);
if (t1<t2) return -1;
if (t1>t2) return 1;
return 0;
}
/* find DS18X20 Sensors on 1-Wire-Bus
input/ouput: diff is the result of the last rom-search
output: id is the rom-code of the sensor found */
void DS18X20_find_sensor(u_int8 *diff, u_int8 id[])
{
for (;;) {
*diff = ow_rom_search( *diff, &id[0] );
//send_data2(id[0]);
if ( *diff==OW_PRESENCE_ERR || *diff==OW_DATA_ERR ||
*diff == OW_LAST_DEVICE ) return;
if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID
没有合适的资源?快使用搜索试试~ 我知道了~
avr.rar_AVR spi_SPI atmega8_atmega8_atmega8 spi
共93个文件
c:10个
lis:9个
dp2:9个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 130 浏览量
2022-09-21
07:17:15
上传
评论
收藏 224KB RAR 举报
温馨提示
avr单片机 atmega8 spi和485的数据转发
资源推荐
资源详情
资源评论
收起资源包目录
avr.rar (93个子文件)
avr
server.c 2KB
ds18x20.h 3KB
avr_app.IMD 408B
avr_app.PRI 46KB
server.h 663B
misc.c 502B
databus.c 1KB
main.c 1KB
spi.c 6KB
avr_app.IMB 8KB
databus.h 496B
misc.h 369B
avr_app.PO 776B
5200_board._c 9KB
crc8.c 2KB
avr_app.IAD 528B
onewire.h 2KB
spi.h 2KB
avr_app.WK3 45KB
main._c 2KB
avr_app.PS 60KB
avr_app.PFI 80B
5200_board.c 10KB
crc8.h 1KB
avr_app.IAB 20KB
avr_app.SearchResults 208B
mib.c 740B
mib.h 420B
main.c_t 906B
6151proj
crc8.s 2KB
6151proj.hex 19KB
5200_board.dp2 785B
misc.o 1KB
6151proj.prj 889B
server.dp2 348B
5200_board.lis 101KB
modbus.i 0B
modbus.lis 48KB
mib.lis 7KB
spi.o 15KB
modbus.dp2 551B
6151proj.prj.bak 889B
mib.s 3KB
crc8.dp2 37B
misc.dp2 37B
server.s 7KB
misc.lis 5KB
spi.dp2 429B
5200_board.s 42KB
main.dp2 464B
misc.s 2KB
server.lis 20KB
mib.o 2KB
main.lis 11KB
main.s 5KB
databus.o 2KB
mib.dp2 91B
6151PROJ.SRC 216B
6151proj.dbg 19KB
6151proj.lst 161KB
databus.s 3KB
6151proj.mak 4KB
6151proj.lk 67B
crc8.lis 5KB
5200_board.o 26KB
databus.lis 7KB
spi.s 22KB
main.o 4KB
server.o 5KB
6151proj.cof 27KB
modbus.s 20KB
6151proj.mp 7KB
databus.dp2 403B
spi.lis 56KB
crc8.o 1KB
ds18x20.c 12KB
6151v01
6151v01.dsw 537B
6151v01.plg 4KB
6151v01.dsp 5KB
6151v01.ncb 73KB
Debug
mib.obj 4KB
crc8.obj 2KB
misc.obj 4KB
6151v01.pch 199KB
vc60.idb 33KB
vc60.pdb 52KB
spi.obj 1KB
6151v01.opt 57KB
spi._c 4KB
onewire.c 5KB
5200_board.h 5KB
5200_board._h 4KB
avr_app.PR 8KB
共 93 条
- 1
资源评论
JaniceLu
- 粉丝: 78
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功