打印
[AVR单片机]

DS18B20源程序(CVAVR)

[复制链接]
2779|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
winnerhds|  楼主 | 2008-6-1 21:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <mega16.h>

// 1 Wire Bus functions
#asm
   .equ __w1_port=0x15 ;PORTC
   .equ __w1_bit=5
#endasm
#include <1wire.h>

// DS1820 Temperature Sensor functions
#include <ds1820.h>  
#define uchar unsigned char
flash uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

// Declare your global variables here

void main(void)
{
// Declare your local variables here
  uchar   
  
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// 1 Wire Bus initialization
w1_init();
PORTC=0X04;
DDRC=0X04;
PORTD=0X08;
DDRD=0X08;

while (1)
      {
      // Place your code here

      }
}

相关帖子

沙发
winnerhds|  楼主 | 2008-6-1 21:15 | 只看该作者

DS18B20源程序(CVAVR环境)

#include <mega16.h>
#include <delay.h>

#asm                            // 1 Wire Bus functions
   .equ __w1_port=0x15 ;PORTC
   .equ __w1_bit=5
#endasm
#include <1wire.h>
#include <ds1820.h>            // DS1820 Temperature Sensor functions

#define uchar unsigned char
#define uint unsigned int
flash uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
flash uchar wei_tab[]={0x00,0xf7,0xef,0xdf,0xbf};  //高到低
// Declare your global variables here 


void display(uchar pos,uchar num)
{          
           //uint k;
       PORTA=tab[num];
       PORTC|=0x80;    //开锁存
       PORTD=wei_tab[pos];    //WEI1开 
       delay_ms(4);  //延时       
       PORTC&=0xef;    //锁存  
       delay_ms(2);  //延时
       PORTA=0x00;    //灭灯  
}

void main(void)
{
// Declare your local variables here
uchar teml,temh,a,b;
uint tem; 
float tt;

PORTA=0xff;
DDRA=0xff;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0xff;
PORTD=0x00;
DDRD=0xff;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// 1 Wire Bus initialization
//w1_init();
while (1)
      {
      // Place your code here
      w1_init();
      w1_write(0xcc);     //发转换命令
      w1_write(0x44);     //启动
      delay_ms(750);
      w1_init();
      w1_write(0xcc);    //读
      w1_write(0xbe);
      
      teml=w1_read(); 
      temh=w1_read();
      delay_ms(750);      //延时 
      tem=temh;
      tem=tem*256;
      tem|=teml;
      tt=tem*0.0625;
      tem=tt*10+0.5;
      
      ///tem=ds1820_read_spd(0xbe); 
      ///tem=0x16;     
      a=tem/100;
      b=tem%100/10;
      //a=4;b=5;
      display(1,a);
      delay_ms(2);  //延时
      display(2,b);
      }
}

使用特权

评论回复
板凳
winnerhds|  楼主 | 2008-6-1 21:16 | 只看该作者

huifu

不好意思,第一篇发错了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝