⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 这是运行在AVR 单片机 mega48上的一个温度测量程序
💻 C
字号:
                                                                                                                                                                                                                    
#include"main.h"
#include"init.h"
#include"18b20.h"
#include"e2prom.h"

void           usart_transmit(uchar  dat)   ;             //发送函数 
static         uchar    temp_h , temp_l     ;             //要发送温度的高低字节 

			 
//void         usart_receive(void)          ;              //接收函数 
static         uchar     data_usart          ;             //接收到的子节 
//static       uchar     temp_r_h ,temp_r_l   ;            //接收到的温度的高低字节 
//static         uchar     update              ;             //温度调整幅度更新变量 

		       
		  //	 int       temp_adjust           ;               //温度调整幅度 
               signed   char       temp_adjust        ;             //温度调整幅度 
 


int main( void )  
{

	static      uint       temp            		   ;
	static      float      value_temp      		   ;
	static      uint       temperature_send    ;
	static      float      timer1          		   ;
		    uint       jj              		   ;
	//static      uchar      ee_temp                  ;
	static      uint       two                      ;
	static      uint       temp_old                 ;  
	static      uint       temperature = 1          ;  
	//            uchar      rom1  ;
//	static      uchar      jump  ;
	static      uint       temp_1 ,temp_2 ,temp_3 ,temp_4 ,temp_5,temp_6;
	static      float      temperature_freq ;
		    uint       ge    ;
	//static      uchar      first ;
	static      uint       timer2 ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
	_CLI()  ;
  	init()  ;
	for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
 //       _CLI() ;
 //       eeprom_write(0x15 ,15) ; 
  //      NOP10;
   //     eeprom_write(0x16 ,  50 ) ;
        for(ge = 0 ;ge <= 50000 ;ge ++)  ;
//	ee_temp = eeprom_read(0x05)   ;
//	NOP10 ;
//	if(ee_temp == 5)
//	{
//	 	 temp_adjust =  eeprom_read(0x06)   ;
               
 
//	}else
//	temp_adjust = 0 ;
	_SEI()  ;
	//first = 0  ;
	while(1)
	{
	  	_WDR()  ;
	
	    for(two = 30 ; two <= 50 ;two ++)
		{
	
			if(two == 35)
			{
			  
				temp_6  =  getemp() ;
				temp_1  =  temp_6 ;
				temp_1  &= 0x07F8 ;
				
				_WDR() ;
			}
			if(two == 38)
			{
			  	
				temp_2  =  getemp() ;
				temp_2  &= 0x07F8 ;
				_WDR() ;
			}  
			if(two == 41)
			{
			  	
				temp_3  =  getemp() ;
				temp_3  &= 0x07F8 ;
				_WDR() ;
			} 
			if(two == 44)
			{
			  	
				temp_4  =  getemp() ;
				temp_4  &= 0x07F8 ;
				_WDR() ;
			}  
				if(two == 47)
			{
			  	
				temp_5  =  getemp() ;
				temp_5  &= 0x07F8 ;
				_WDR() ;
			}  
                			
		}
		if((temp_1 == temp_2)  && (temp_2  == temp_3) && (temp_3  == temp_4) && (temp_4  == temp_5))
			temp = temp_6 ;
	
		_WDR()  ;
		temp &= 0x07FF ;
		_WDR()  ;
		value_temp = temp * 0.0625           ;         //读出来的实际温度
		temperature_send =  value_temp * 10   ;         //为了便于显示 在这里扩大10倍 (实际温度) 
		temp_old         =  value_temp * 10        ;         //频率输出用的温度  
		
	//	temperature_send =  (uchar)temp_adjust   ;	
		temp_h = (uchar)( temperature_send / 64 + 0x80) ;   
		temp_l = (uchar)( temperature_send  % 64 );     // 低  6  位
		usart_transmit(temp_h) ;   //刷新高字节 
		_WDR()  ;
	        for( jj = 0 ;jj < 6500 ;jj ++)  
		  ;
		for( jj = 0 ;jj < 6500 ;jj ++)  
		  ;
		_WDR()  ;
		usart_transmit(temp_l) ;           //刷新低字节 
		
		
 		
		if(temperature == 1)
		{
		  	temperature = temp_old           ;
		}
		
		if(abs(temp_old - temperature) <= 10)
			temperature = temp_old ;	
			
// 根据温度计算Timer1的重装值 ,公式是:Timer1 = 230400/(x + 25) ;  x----温度 100 
								//		Timer1 = 92160/(x + 10) ;  x----温度   40
	//	temperature = (float)temperature * 0.1 + (int)temp_adjust * 0.1    ;
	//	temperature_freq = temperature* 0.1   ;
		temperature_freq = (temperature  + temp_adjust)* 0.1   ;
		timer1 =  2304.0 / (temperature_freq + 25)  ;
		timer1 *= 100 ;
	/*	
		if(first == 0)
		{
			timer2 = timer1    ;
			 _CLI() ;         //关中断 
			OCR1AH =  timer2 / 256 ;
			OCR1AL =  timer2 % 256 ;
			_SEI() ;
			first = 111 ;
		}
             */
	//	if((uint)timer1 != timer2)
	//	{  
		  	timer2 = (uint)timer1    ;
			 _CLI() ;         //关中断
			OCR1AH =  timer2 / 256 ;
			OCR1AL =  timer2 % 256 ;
			_SEI() ;
//		} 
		
		

	//	OCR1AH = (uint)timer1 / 256   ;
	//	OCR1AL = (uint)timer1 % 256   ;
		_WDR()  ;
/*
		if(update == 111)
		{
//		  	usart_receive()    ;               //更新温度调整变量  
		  	_CLI() ;
			eeprom_write(0x05 ,5) ; 
			NOP10;
			eeprom_write(0x06 ,  data_usart ) ;
                        NOP10;
			_SEI() ;
			update = 0 ; 
		}
	*/
		
	}
	
}


void usart_transmit( uchar dat )
{
  	static uchar  value  ;
	value = dat          ;
// 等待发送缓冲器为空
	while ( ( UCSR0A & 0x20) != 0x20  ) 
	;
	UCSR0A &= 0xBF       ; 
// 将数据放入缓冲器,发送数据
	UDR0 = value         ;
	
}


#pragma  vector  =  USART_RX_vect  
__interrupt  void  my_usart_rxc(void)
{
   	uchar   wrong , status ;
	status  =  UCSR0A;
	if (status & 0x1C)             //有错误  
	{
	  	wrong  =  UDR0     ;
               
	}  
	else
	{
            data_usart  =  UDR0 ;  
            temp_adjust = data_usart  ;
  //          update      =  111    ;
 
   //         eeprom_write(0x15 ,15) ; 
    //        NOP10;
     //       eeprom_write(0x16 , data_usart) ;
     
    //        temp_adjust =  data_usart  ;
     //       

	}  
	
}


/*

#pragma  vector  =  USART_RX_vect  
__interrupt  void  my_usart_rxc(void)
{
   	uchar   wrong , status ;
	status  =  UCSR0A;
	if (status & 0x1C)             //有错误  
		wrong  =  UDR0     ;
	else   
	{
	  	data_usart = UDR0 ;  
		if(data_usart >> 7)           //是高字节  
		{
		 	
			temp_r_h = data_usart     ;      //屏蔽高 4  位 
		}else
		{
			temp_r_l = data_usart   ;      //屏蔽高 2 位 
			update = 111               ;  
		}
		
	}  
}


void  usart_receive(void)
{

  	
  	if((temp_r_h >> 6) == 3)              //说明是负数
	{
	  	temp_r_h     &=   0x3F  ;
		temp_adjust  =    temp_r_h   ;
		temp_adjust  <<=  6        ;
		temp_adjust  += (uint)temp_r_l ;
		temp_adjust = 0 - temp_adjust ;
	}else
	{
		temp_r_h     &=   0x3F  ;
		temp_adjust  =    temp_r_h   ;
		temp_adjust  <<=  6        ;
		temp_adjust  += (uint)temp_r_l ;
	}  
} 

*/

⌨️ 快捷键说明

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