基于ATmega128的DHT11温湿度传感器的使用2
- UID
- 864567
|
基于ATmega128的DHT11温湿度传感器的使用2
#define LCK_1 PORTB|=(1<
#define SDI_0 PORTB&=~(1<
#define SDI_1 PORTB|=(1<
void init(void);
void send_595(unsigned char dat);
void digitron_show(unsigned int int_part,unsigned int float_part);
#endif
================================================
//这里是xianshi.c
#include"xianshi.h"
#ifndef __DELAY_H
#include"delay.h"
#endif
#ifndef __DHT11_H
#include"dht11.h"
#endif
//数码管显示数组定义
const unsigned char table[]=
{
0x3F,// 0
0x06,// 1
0x5B,// 2
0x4F,// 3
0x66,// 4
0x6D,// 5
0x7D,// 6
0x07,// 7
0x7F,// 8
0x6F,// 9
0x3F+0x80,// 0.
0x06+0x80,// 1.
0x5B+0x80,// 2.
0x4F+0x80,// 3.
0x66+0x80,// 4.
0x6D+0x80,// 5.
0x7D+0x80,// 6.
0x07+0x80,// 7.
0x7F+0x80,// 8.
0x6F+0x80// 9.
};
unsigned int s,t,st,int_part,float_part,temp,SH;
//发送一字节数据到595
void send_595(unsigned char dat)
{
unsigned char i;
LCK_0;
SDI_1;
SCK_0;
//上面的三条语句为了初始化端口状态
for(i=0;i<8;i++)
{
LCK_0;//时钟线拉低
if(dat&0x80)
SDI_1;
else SDI_0;
dat=dat<<1;
delay_us(100);
LCK_1; //时钟线拉高将数据读入595的移位寄存器
delay_us(100);
}
SCK_1; //发送数据到并行端口
SCK_0;
}
void show(void)
{
unsigned char temp_shi,temp_ge,SH_shi,SH_ge,x,y;
unsigned int i;
st=dht();
t=st&0x0000ffff;
s=st&0xffff0000;
s=s>>16;
//下面为把温度和湿度换算成十进制并且四舍五入
temp=(t>>8);
temp_shi=temp/10;
temp_ge=temp%10;
SH=(s>>8);
SH_shi=SH/10;
SH_ge=SH%10;
int_part=SH_shi*10+SH_ge;
float_part=0;
for(i=0;i<50;i++)
{
digitron_show(int_part,float_part);
}
}
void digitron_show(unsigned int int_part,unsigned int float_part)
{
PORTA=0x01;
send_595(table[float_part/10]);
send_595(0x00);
delay_ms(5);
PORTA=0x02;
send_595(table[(int_part%10)+10]);
send_595(0x00);
delay_ms(5);
PORTA=0x04;
send_595(table[int_part/10]);
send_595(0x00);
delay_ms(5);
}
================================================
//这里是MAIN.C
#include
#include
#include"delay.h"
#include"dht11.h"
#include"xianshi.h"
#pragma interrupt_handler Timer0_COMP:16
#define uchar unsigned char
uchar k=0;
void init(void);
void main()
{
init();//初始化
TCCR0=0X0F;
DDRA=0XFF;
TCCR0=0X0f;//CTC模式
OCR0=145;//10ms
TIMSK=0X02;
SEI();
while(1);
}
//初始化子函数
void init(void)
{
DDRA=0XFF;
DDRB=0XFF;
}
void Timer0_COMP(void)
{
TCCR0=0X08;
CLI();
k++;
if(k==255)
{
k=0;
show();
}
TCCR0=0X0f;//重置初值
SEI();
} |
|
|
|
|
|