//ICC-AVR application builder : 2009-10-25 10:43:39
// Target : M16
// Crystal: 16.000Mhz
#include <iom16v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#include "xianshi.c"
#include "delay.h"
#define CLR_DIR_1WIRE DDRD&=~BIT(4)//只要修改这里的参数就可以了
#define SET_DIR_1WIRE DDRD|=BIT(4) //里面什么都不用该!
#define CLR_OP_1WIRE PORTD&=~BIT(4)
#define SET_OP_1WIRE PORTD|=BIT(4)
#define CHECK_IP_1WIRE (PIND&0x10)//检测
unsigned char wmh,wml
void init_1820()
{
SET_DIR_1WIRE //设置PD4 为输出
SET_OP_1WIRE
CLR_OP_1WIRE
delay_nus(480) //480us以上
SET_OP_1WIRE
CLR_DIR_1WIRE
delay_nus(20) //15~60us
while(CHECK_IP_1WIRE)
SET_DIR_1WIRE
SET_OP_1WIRE
delay_nus(140) //60~240us
}
void write_1820(unsigned char x)
{
unsigned char m
for(m=0m<乎敏8m++)
{
CLR_OP_1WIRE
if(x&(1<<m))//写数巧森据了,先写低位的!
SET_OP_1WIRE
else
{CLR_OP_1WIRE}
delay_nus(40) //15~60us
SET_OP_1WIRE
}
SET_OP_1WIRE
}
unsigned char read_1820()
{
unsigned char temp,k,n
temp=0
for(n=0n<8n++)
{
CLR_OP_1WIRE
SET_OP_1WIRE
CLR_DIR_1WIRE
k=(CHECK_IP_1WIRE) //读数据,从低位开始
if(k)
temp|=(1<<n)
else
temp&=~(1<<n)
delay_nus(50)//60~120us
SET_DIR_1WIRE
}
return (temp)
}
unsigned int gettemp() //岁宽枝读取温度值
{
unsigned char temh,teml,wm0,wm1,wm2,wm3
init_1820() //复位18b20
write_1820(0xcc) // 发出转换命令
write_1820(0x44)
// delay_nms(800) //不延时也好使,不知道怎么回事!
init_1820()
write_1820(0xcc) //发出读命令
write_1820(0xbe)
teml=read_1820() //读数据
temh=read_1820()
wm0=teml>>4 //只要高8位的低四位和低8位的高四位,温度范围0~99啦!
wm1=temh<<4
wm2=wm1+wm0 //16进制转10进制
return wm2
}
void main()
{
uint tem,ad[4],i
port_init()
while(1)
{
tem = gettemp()
for(i=0i<4i++)
{
ad[3-i]=tem%10
tem=tem/10
}
for(i=0i<4i++)
{
show1(ad[i],i)
delay(5)
}
}
}
显示函数:
#include <iom16v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#pragma data:code
const uint tab1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x00}//共阳数码管代码表
const uint tab2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X00}//共阴数码管代码表
void port_init(void)
{
DDRA = 0xFF
DDRB = 0xFF
DDRC = 0xFF
DDRD = 0x00
}
void delay(uint ms)
{
uint i,j
for(i=0i<msi++)
{
for(j=0j<1141j++)
}
}
void show1(uchar j,uchar k)//显示函数
{
PORTB = ~BIT(k)
PORTA = tab2[j]
delay(1)
}
void show(uint ada)
{
uint i,ad[4]
for(i=0i<4i++)
{
ad[3-i]=ada%10
ada = ada/10
}
while(1)
{
for(i=0i<4i++)
{
show1((ad[i]),i)
delay(100)
}
}
}
程序我都调试过的,都是好的,有不会再问我。可以给分了吗?嘿嘿
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)