|
最終效果:
|
|
数据表結構圖:
|
|
數據表結構: CREATE TABLE IF NOT EXISTS `PowerLog` ( `LogID` int(11) NOT NULL, `LogTime` datetime NOT NULL, `LogFlag` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'TEST', `LogTimeZone` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'UTC+8', `ComputerIP` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '192.168.0.4', `Temperature` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO DATA' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
|
我使用的操作系統:Fedora Linux 22 單片機型號:AVR ATMega16
|
|
單片機所用晶振:7.3728MHz 串口線:USB轉串口線(設備地址:/dev/ttyUSB0)
|
|
下面公佈程序源代碼。 【單片機部分】 [文件列表] delay.c delay.h DS18B20.c DS18B20.h IR.c IR.h pce.c UART.c UART.h
|
|
[delay.c] #include "delay.h"
//延时n毫秒 void delay(unsigned int n) { unsigned int i; while (n--) for (i=0;i<1140;i++); //1ms基准延时 }
void delay_us(unsigned int n) { if (n==0) return; while (--n); }
//延时0.5ms void delay500us(void) { unsigned int i; for (i=0;i<570;i++); //1140*0.5=570 }
//延迟0.56ms void delay560us(void) { unsigned int i; for (i=0;i<638;i++); //1140*0.56=638.4 }
|
|
[delay.h] void delay(unsigned int n); void delay_us(unsigned int n); void delay500us(void); void delay560us(void);
|
|
[DS18B20.c] #include <iom16v.h> #include <macros.h> #include "delay.h" #include "DS18B20.h"
unsigned char DSFlags=0xff; unsigned char DSTN,DSTD;
void DS18B20_Init(void) { DQ_OUT; DQ_1; delay_us(6); DQ_0; delay_us(600); DQ_1; delay_us(120); if (DQ) DSFlags|=BIT(DSERR); else DSFlags&=~BIT(DSERR); delay_us(620); }
void DS18B20_Read(unsigned char* dat) { unsigned char i; for (i=0;i<8;i++) { DQ_OUT; DQ_1; delay_us(2); DQ_0; delay_us(4); DQ_1; delay_us(6); *dat>>=1;
if (DQ) *dat|=0x80; else *dat&=0x7f; delay_us(60); } }
void DS18B20_Write(unsigned char dat) { unsigned char i; for (i=0;i<8;i++) { DQ_OUT; DQ_1; delay_us(2); DQ_0; if (dat&0x01) DQ_IN; else DQ_OUT; delay_us(30); DQ_1; delay_us(3); dat>>=1; } delay_us(10); }
void DS18B20_ReadyReadTemp(void) { DS18B20_Init(); DS18B20_Write(0xcc); DS18B20_Write(0x44); delay_us(20);
DS18B20_Init(); DS18B20_Write(0xcc); DS18B20_Write(0xbe); }
void DS18B20_Measure(void) { unsigned char tl,th,tltemp; DS18B20_ReadyReadTemp(); DS18B20_Read(&tl); DS18B20_Read(&th);
if ((th&0xf8)!=0x00) { DSFlags|=BIT(DSNEG); tl=~tl; th=~th; tltemp=tl+1; tl=tltemp; if (tltemp>=255) th++; } else DSFlags&=~BIT(DSNEG);
DSTN=th*16+tl/16; DSTD=(tl&0x0f)*625/100; //*6.25 }
|
|
10樓 巨大八爪鱼
2015-6-28 11:44
[DS18B20.h] #define DSNEG 4 #define DSERR 6
#define DQ (PINB&BIT(1)) #define DQ_0 PORTB&=~BIT(1) #define DQ_1 PORTB|=BIT(1) #define DQ_IN DDRB&=~BIT(1) //write #define DQ_OUT DDRB|=BIT(1) //read
extern unsigned char DSFlags; extern unsigned char DSTN; extern unsigned char DSTD;
void DS18B20_Init(void); void DS18B20_Read(unsigned char* dat); void DS18B20_Write(unsigned char dat); void DS18B20_ReadyReadTemp(void); void DS18B20_Measure(void);
|