//ICC-AVR application builder : 2011-1-5 22:10:42
// Target : M8
// Crystal: 8.0000Mhz
// 注意: 编程熔丝时把看门狗的勾去掉,程序未喂狗
#include <iom8v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define LED_N 10 //10位显示
//----------------------------------
#define Bit(n) (1<<n)
//LED_RUN
#define led_on PORTD&=~Bit(PD3)
#define led_off PORTD|=Bit(PD3)
#define led_run PORTD^=Bit(PD3)
//定义74HC595时钟、数据、锁存、使能引脚状态
#define CLK_H PORTB|=Bit(PB6)
#define CLK_L PORTB&=~Bit(PB6)
#define DAT_H PORTB|=Bit(PB7)
#define DAT_L PORTB&=~Bit(PB7)
#define RCK_H PORTB|=Bit(PB2)
#define RCK_L PORTB&=~Bit(PB2)
#define ENA_H PORTB|=Bit(PB1)
#define ENA_L PORTB&=~Bit(PB1)
//定义DS18B20脚 ~~~没用到的~~~
#define CLR_1WIRE_BUS DDRD|=Bit(PD4) //设置为输出
#define SET_1WIRE_BUS DDRD&=~Bit(PD4)//设置为输入,此时由于PORTD4是低所以程高阻,又因为外部有上拉电阻所以相当于设置总线为高
#define GET_1WIRE_BUS PIND&Bit(PD4)
#define DS18B20_READ_ROM 0x33
#define DS18B20_MATCH_ROM 0X55
#define DS18B20_SKIP_ROM 0XCC
#define DS18B20_SEARCH_ROM 0XF0
#define DS18B20_ALARM_SEARCH_ROM 0XEC
#define DS18B20_WRITE_RAM 0X40
#define DS18B20_READ_RAM 0XBE
#define DS18B20_COPY_RAM 0X48
#define DS18B20_CONVERT_TEM 0X44
#define DS18B20_EECALL_EEPROM 0XB8
#define DS18B20_READ_POWER_SUPPLY 0XB4
//-------------------------------------------------
#define noACK 0
#define ACK 1
#define STATUS_REG_W 0x06
#define STATUS_REG_R 0x07
#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05
#define RESET 0x1e
#define setREAD DDRC |=(1<<PC4); PORTC|=(1<<PC5);DDRC &= ~(1<<PC5)
#define setWRITE DDRC |=(1<<PC4); DDRC |=(1<<PC5)
#define SCK(i) if (i) PORTC|=(1<<PC4); else PORTC &= ~(1<<PC4)
#define DATA(i) if (i) PORTC|=(1<<PC5); else PORTC &= ~(1<<PC5)
//=============================================================================================================
//0, 1, 2, 3, 4, 5 , 6, 7, 8, 9, A, B, C, D, E, F
const uchar led_array[27]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,
//- dark S H Y E n , P , L , ,r25 ,o26
0xbf,0xff,0x92,0x89,0x91,0x86,0xab,0x8c,0xc7,0xaf,0xa3}; //共阳管
//=============================================================================================================
uchar Led_Set_buf[LED_N];
uint count;
uint Temp;
uchar Flag;
uint Flagsing;
uint WiatCount;
ulong WiatTime;
/****************************************************************
//函数名称:delay_1ms(void)
//功 能:延时1ms
//入口参数:无
//出口参数:无
//说明:for(i=1;i<(unsigned int)(xtal*143-2_;i++)xtal为晶振频率,单位为MHz。
*****************************************************************/
void delay_1ms()//1ms
{
uint i;
for(i=1;i<(uint)(8*143-2);i++);
}
/****************************************************************
//函数名称:delay_ms(uint time)
//功 能:延时time ms
//入口参数:time
//出口参数:无
*****************************************************************/
void delay_Nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
{
delay_1ms();
}
}
//************************************************************
void Port_init(void)
{
PORTB = 0xFF;
DDRB = 0xff;//设为输出
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xff;//设为输出
}
/****************************************************************
//函数名称:init_devices(void)
//功 能:初始化器件
//入口参数:无
//出口参数:无
*****************************************************************/
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
//Port_init();
MCUCR = 0x00; //
GICR = 0x00; //
TCCR0|=(1<<CS02);//8M晶振,256预分频
TCNT0 = 0xE1;
TIMSK|= (1<<TOIE0);//开Timer0中断
SEI(); //re-enable interrupts
}
/****************************************************************
//函数名称:timer0_ovf_isr(void)
//功 能:Timer0中断处理函数
//入口参数:无
//出口参数:无
*****************************************************************/
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TIMSK&=(1<<TOIE0);//关Timer0中断
TCNT0 = 0xE1; //reload counter value
count++;
if(count>1000)
{
led_run;
count=0;
}
WiatCount++;
/*
if(WiatCount>1000)
{
WiatTime++;
WiatCount=0;
}
*/
TIMSK|=(1<<TOIE0);//开Timer0中断
}
//***************************************************************
void delay(uint x) //0.8us左右
{
while(x)
{
x--;
}
}
//**************************************************************
void init_18B20()
{
Flag=0;
// CLR_1WIRE_BUS;
DDRD|=Bit(PD4);
PORTD&=~Bit(PD4);
delay(800); //480us~960us
//SET_1WIRE_BUS;
PORTD|=Bit(PD4);
delay(40); //15~60us
// if((GET_1WIRE_BUS)==0)//检测到DS18B20把总线拉低
DDRD&=~Bit(PD4);
if((PIND&Bit(PD4))==0)
{
Flag=1; //复位成功
}
delay(300); //60~240us //等待器件释放总线
// return(Flag);
}
//单总线写一字节
void Write_18B20(uchar data)
{
uchar i=0;
for(i=0;i<8;i++)
{
// CLR_1WIRE_BUS;
DDRD|=Bit(PD4);
PORTD&=~Bit(PD4);
delay(10);//8us
if(data&0x01)
{
// SET_1WIRE_BUS;
PORTD|=Bit(PD4);
}
else
{
// CLR_1WIRE_BUS;
PORTD&=~Bit(PD4);
}
data>>=1;
delay(50);//15~60us
// SET_1WIRE_BUS;
PORTD|=Bit(PD4);
}
}
//单总线读一字节
uchar Read_18B20(void)
{
uchar data=0;
uchar i=0;
for(i=0;i<8;i++)
{
data>>=1;
// CLR_1WIRE_BUS;
DDRD|=Bit(PD4);
PORTD&=~Bit(PD4);
delay(2);//此时>1us
//SET_1WIRE_BUS;
PORTD|=Bit(PD4);
delay(4);//此时<15us
// if(GET_1WIRE_BUS)
DDRD&=~Bit(PD4);
if(PIND&Bit(PD4))
data|=0x80;
delay(80);//此时>60us
}
return(data);
}
//******************************************
void Temp_deal()
{
uchar temp1,temp2,Decimal;
uint Value ;
temp1=Read_18B20();
temp2=Read_18B20();
Temp = (((uint)temp2<<8) | temp1);
if(Temp>0x0FFF)
{
Temp=~Temp+1;
// Si_flag=0;
Led_Set_buf[4]=16;
}
else
{
//Si_flag=1;
Led_Set_buf[4]=17;
}
Decimal= Temp&0x0F;
Led_Set_buf[0]=(((uint)Decimal*625)+500)/1000;
Value=(Temp&0x07F0)>>4 ;
Led_Set_buf[3]=Value/100;
Led_Set_buf[2]=Value%100/10;
Led_Set_buf[1]=Value%10;
}
/****************************************************************
//函数名称:LED_disp(void)
//功 能:74HC595送数
//入口参数:无
//出口参数:无
*****************************************************************/
void LED_disp(void)
{
uchar i,a, b,c,led_bit;
uchar disbuf[LED_N];
for( i=0;i<LED_N;i++)
{
disbuf=Led_Set_buf;
}
if(Led_Set_buf[9]==0)
{
disbuf[9]=17;
if(Led_Set_buf[8]==0)
{
disbuf[8]=17;
if(Led_Set_buf[7]==0)
{
disbuf[7]=17;
if(Led_Set_buf[6]==0)
{
disbuf[6]=17;
}
}
}
}
if(Led_Set_buf[3]==0)
{
disbuf[3]=17;
if(Led_Set_buf[2]==0)
{
disbuf[2]=17;
}
}
ENA_L;
RCK_L;
for(a=0;a<LED_N;a++)
{
c = disbuf[a];
led_bit = led_array[c];
if(a==1&&Flag==1)led_bit&=0x7F;
for(b=0;b<8;b++)
{
CLK_L;
if(led_bit & 0x80)
DAT_H;
else
DAT_L;
led_bit <<= 1;
CLK_H;
CLK_L;
}
}
RCK_H;
RCK_L;
}
//---------------------------------
void main(void)
{
uchar i;
Port_init();
init_devices();
// DDRD&=~Bit(PD4); //初始化为输入,用外部上拉电阻保持总线的高电平
// PORTD&=~Bit(PD4);//此口总保持高
for(i=0;i<LED_N;i++)
{
Led_Set_buf=16; //装载 ----------显示
}
LED_disp();
delay_Nms(1000);
while(1)
{
init_18B20(); //复位18b20
Write_18B20(0xcc); // 发出转换命令
Write_18B20(0x44);
delay_Nms(1000); //延时1S钟,18B20转换需要1S钟时间
init_18B20();
Write_18B20(0xcc); //发出读命令
Write_18B20(0xbe);
Temp_deal();
if(Flag==0)//
{
for(i=0;i<LED_N;i++)
{
Led_Set_buf = 16;
}
}
WiatTime++; //验证定时器,1秒钟跳一次
Led_Set_buf[9]=WiatTime/10000;
Led_Set_buf[8]=WiatTime%10000/1000;
Led_Set_buf[7]=WiatTime%1000/100;
Led_Set_buf[6]=WiatTime%100/10;
Led_Set_buf[5]=WiatTime%10;
LED_disp();
}
}
用户243959 2011-3-16 21:10
xucun915_925777961 2011-3-16 08:58