main.c

来自「ds18b20+1602液晶显示 基于atmega16l单片机的编程」· C语言 代码 · 共 95 行

C
95
字号
//ICC-AVR application builder : 2008-8-2 15:50:57
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
#include "lm1602.h"
#include "ds18b20.h"

byte flag10ms = 0;

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xE0;
 PORTB = 0x00;
 DDRB  = 0xFF;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}


//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 10mSec
// actual value: 10.000mSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFB; //setup
 TCNT1L = 0x1E;
 TCCR1A = 0x00;
 TCCR1B = 0x03; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 static unsigned char cnt =0;
 //TIMER1 has overflowed
 TCNT1H = 0xFB; //reload counter high value
 TCNT1L = 0x1E; //reload counter low value
 cnt++;
 if(cnt>=50)
 {
  cnt = 0;
  flag10ms = 1;
  }
}


//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer1_init();
 
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x04; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
 unsigned int temp_data;
 init_devices();
 LcdInit();
 DisString(0, 1, "DS18B20 test");
 //insert your functional code here...
 while(1)
 {
    if(flag10ms)
	{
	   flag10ms =0;
	    temp_data = ReadTemperature(); 
	  if( temp_data!=0xffff)
	  {
	      LCD_Dis_Temperature(1,1,temp_data);	//温度数据转换和显示函数
	  }
	  else
	   {
	      DisString(1,1,"DS18B20 Error!");
	   }
	}
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?