|
/*********************************************
Project : LRYFUJIANAVR
Version : 1.0
Date : 2003-10-27
Author : 王向阳制作
Comments:
本程序用于利用DS18B20控制量热
温度的附件的分程序。
援用ATMEGA8 ,4MRC震荡器。CVAVR1。23
。8D编译通过。
Chip type : ATmega8L
Program type : Application
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*********************************************/
#include <mega8.h>
#include <math.h>
#define uchar unsigned char
#define schar signed char
#define ulong unsigned long
#define slong signed long
#define uint unsigned int
#define sint signed int
#define ON 0
#define OFF 1
#define LABA PORTC.5
#define ZhiLengControl PORTD.6
#define JiaReControl PORTD.7
//以下定义键盘键值。
#define NOKEY 0
#define SHEZHI 0x10
#define QUEDING 0x11
#define PIPEI 0x12
#define BEIYONG 0x13
//以下定义显示相关
#define A 10
#define B 11
#define C 12
#define D 13
#define E 14
#define F 15
#define G 16
#define H 17
#define I 18
#define J 19
#define K 20
#define L 21
#define M 22
#define N 23
#define O 24
#define P 25
#define Q 26
#define R 27
#define S 28
#define T 29
#define U 30
#define V 31
#define W 32
#define X 33
#define Y 34
#define Z 35
#define GANG 36
// 1 Wire Bus functions
#asm
.equ __w1_port=0x12
.equ __w1_bit=4
#endasm
#include <1wire.h>
// DS1820 Temperature Sensor functions
#include <ds1820.h>
// maximum number of DS1820 devices
// connected to the 1 Wire bus
#define MAX_DS1820 8
// number of DS1820 devices
// connected to the 1 Wire bus
unsigned char ds1820_devices;
// DS1820 devices ROM code storage area,
// 9 bytes are used for each device
// (see the w1_search function description in the help)
unsigned char ds1820_rom_codes[MAX_DS1820,9];
//给发生温度报警的器件分配ROM码存储空间。
//unsigned char alarm_rom_codes[MAX_DS1820,9];
eeprom unsigned char eep_rom_codes[MAX_DS1820,9];
eeprom int eepSheDingWD[MAX_DS1820];
#define eepZhiLengTongSheDing eepSheDingWD[0]
#define eepJiaReTongSheDing eepSheDingWD[1]
#define eepneitongBSheDing eepSheDingWD[2]
int SheDingWD[MAX_DS1820];
#define ZhiLengTongSheDing SheDingWD[0]
#define JiaReTongSheDing SheDingWD[1]
#define neitongBSheDing SheDingWD[2]
int wendu[MAX_DS1820];
#define ZhiLengTong wendu[0]
#define JiaReTong wendu[1]
#define neitongB wendu[2]
#define waitongB wendu[3]
// unsigned char err;
unsigned int shijian;
bit bitshijian=0;
// Standard Input/Output functions
#include <stdio.h>
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
static uchar a ;
a++;
if (a>30)
{ a=0;
bitshijian=1;
shijian++;
}unsigned char led_buff[4]={0,1,L,R};
uchar DpBit=02;
unsigned char flash DisSeg[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
,
//"0" "1" "2" "3" '4' '5' '6' '7' '8' '9'
'A' 'B' 'C' 'D' 'E' F'
0X3D,0X76,0X0F,0X0E,0X36,0X38,0X37,0X54,0X5C,0X73,0X67,0X50,0X6D,0X78,0X3E,0X1C,
//'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R'
'S' 'T' 'U' 'V'
0X7E,0X00,0X6E,0X48,0X40,0X25};
//'W' 'X' 'Y' 'Z' '-' 'off'
#include <delay.h>
//键盘子程序开始处
/* 毫秒级延时程序 */
void led_display(void)
{unsigned char i=0;
for(i=0;i<4;i++)
{
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
delay_ms(2);
PORTC|=(1<<i);
}
}
unsigned char Scan_key_led_display(void)
{
unsigned char i,key=0;
for(i=0;i<4;i++)
{
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
delay_ms(2);
if((PINC&(1<<4))==0)key=i+0x10; //读取键盘扫描信号有键
执行
PORTC|=(1<<i);
}
return(key);
}
unsigned char Scan_key(void) //键盘扫描函数,注意本函数不作按键释放检查
{ unsigned char key=0;
key=Scan_key_led_display();
if(key)
{
led_display();
led_display();
if(key!=Scan_key_led_display())return 0;
return key;
}
else
return 0;
}
unsigned char keypad(void) //键盘扫描函数,注意本函数作按键释放检查
{ unsigned char temp1,temp2;
temp1=Scan_key();
if (temp1)
{ do
temp2=Scan_key();
while(temp1==temp2);
LABA=ON;
led_display();
led_display();
LABA=OFF;
}
return(temp1);
}
unsigned char ds18b20_select(unsigned char *addr)
{
unsigned char i;
if (w1_init()==0) return 0;
//led_display();
//led_display();
if (addr)
{
w1_write(0x55);
i=0;
do
{
w1_write(*(addr++)); }
while (++i<8);
// led_display();
// led_display();
}
else w1_write(0xcc);
return 1;
}
uchar start_all_ds18b20(void)
{
if (w1_init()==0) return 0;
//for(k=0;k<20;k++)
//led_display();
//Scan_key();
w1_write(0xcc);
w1_write(0x44);
//for(k=0;k<10;k++)
//led_display();
//Scan_key();
//for(k=0;k<70;k++)led_display();
//delay_ms(550);
return 1;
}
int ds1820_temperature_12(unsigned char *addr)
{
if (ds18b20_select(addr)==0) return -9999;
//w1_write(0x44);
//delay_ms(550);
//for(k=0;k<10;k++)
//led_display();
//Scan_key();
if (ds1820_read_spd(addr)==0) return -9999;
//for(k=0;k<10;k++)
//led_display();
w1_init();
return (((int)__ds1820_scratch_pad.temp_msb<<8)|
__ds1820_scratch_pad.temp_lsb)*5;
led_display();
}
union
{
uchar c[2];
int x;
}temp;
int Read_Temperature(void)
{
// Read Scratch Pad
temp.c[0]=w1_read();
temp.c[1]=w1_read(); ;
return temp.x*5;
}
uchar QDwdled_display(void)
{ uchar i=0,j=0;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
if (w1_init()==0)j=0;
// j=start_all_ds18b20();
delay_ms(1);
PORTC|=(1<<i);
i=1;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
delay_ms(2);
w1_write(0xcc);
PORTC|=(1<<i);
i=2;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
w1_write(0x44);
delay_ms(2);
PORTC|=(1<<i);
i=3;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
delay_ms(2);
PORTC|=(1<<i);
return 1 ;
}
int duwdled_display(unsigned char *addr)
{ int lswd;
uchar i=0;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
if (ds18b20_select(addr)==0) lswd=-9999;//return -9999;
//delay_ms(2);
PORTC|=(1<<i);
i=1;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
//if (ds1820_read_spd(addr)==0) return -9999;
delay_ms(2);
w1_write(0xBE);
PORTC|=(1<<i);
i=2;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
lswd= Read_Temperature();
delay_ms(2);
PORTC|=(1<<i);
i=3;
PORTB=DisSeg[led_buff[3-i]]|(0x80&(DpBit<<(i+4)));
PORTC&=~(1<<i);
w1_init();
delay_ms(2);
PORTC|=(1<<i);
return lswd;
//return (((int)__ds1820_scratch_pad.temp_msb<<8)|
// __ds1820_scratch_pad.temp_lsb)*5;
}
void pipei(void)
{
uchar flag=0;
uchar i;
int temp;
led_buff[3]=0;
led_buff[2]=0;
led_buff[1]=0;
led_buff[0]=0;
while (flag==0)
{ if(bitshijian)bitshijian=0;
{
//for(k=0;k<100;k++)led_display();
ds1820_devices=w1_search(0xf0,&ds1820_rom_codes[led_buff[3],0]);
//for(k=0;k<100;k++)led_display();
if(ds1820_devices!=1)
{temp=9999;}
else
{
temp=(ds1820_temperature_12(&ds1820_rom_codes[led_buff[3],0]))>>3;
//for(k=0;k<100;k++)led_display();
};//当匹配时18B20个数不为1个则错误1
}
for(i=0;i<3;i++)
{
led_buff<i>=temp%10;
temp=temp/10;
};
switch(keypad())
{
case NOKEY:;break;
case SHEZHI:if(++led_buff[3]>=MAX_DS1820)led_buff[3]=0;break;
case QUEDING:for(i=0;i<9;i++)eep_rom_codes[led_buff[3],i]
=ds1820_rom_codes[led_buff[3],i];
flag=1;break;
case BEIYONG:flag=1;break;
default : ;
} ;
}
}
void WriteLedBuff(int temp,uchar tongdao)
{uchar i;
if(temp<0)
{
led_buff[3]=GANG;//'-'
}
else led_buff[3]=tongdao;
temp=abs(temp);
for(i=0;i<3;i++)
{
led_buff<i>=(temp%10);
temp=temp/10;
};
}
|
|