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

📄 main.c

📁 原创
💻 C
字号:
//ICC-AVR application builder : 2007-8-14 17:19:54
// Target : M16         0x59EF   0xD9DF
// Crystal: 11.059Mhz

#include <iom16v.h>
#include <macros.h>
#include "hal.h"
#include "main.h"
#include "uart.h"
#include "Temp.h"


//AD值转换成客户值的函数

const unsigned int PT100_TAB[410]={
 10000, 10039, 10078, 10117, 10156, 10195, 10234, 10273, 10313, 10351,//0
 10390, 10429, 10468, 10507, 10546, 10585, 10624, 10663, 10702, 10740,//10
 10779, 10818, 10857, 10896, 10935, 10973, 11012, 11051, 11090, 11128,//20
 11167, 11206, 11245, 11283, 11322, 11361, 11399, 11438, 11477, 11515,//30
 11554, 11593, 11631, 11670, 11708, 11747, 11785, 11824, 11862, 11901,//40
 11940, 11978, 12016, 12055, 12093, 12132, 12170, 12209, 12247, 12286,//50
 12324, 12362, 12401, 12439, 12477, 12516, 12554, 12592, 12631, 12669,//60
 12707, 12745, 12784, 12822, 12860, 12898, 12937, 12975, 13013, 13051,//70
 13089, 13127, 13166, 13204, 13242, 13280, 13318, 13356, 13394, 13432,//80
 13470, 13508, 13546, 13584, 13622, 13660, 13698, 13736, 13774, 13812,//90
 13850, 13888, 13926, 13964, 14002, 14039, 14077, 14115, 14153, 14191,//100
 14229, 14266, 14304, 14342, 14380, 14417, 14455, 14493, 14531, 14568,//110
 14606, 14644, 14681, 14719, 14757, 14794, 14832, 14870, 14907, 14945,//120
 14982, 15020, 15057, 15095, 15133, 15170, 15208, 15245, 15283, 15320,//130
 15358, 15395, 15432, 15470, 15507, 15545, 15582, 15619, 15657, 15694,//140
 15731, 15769, 15806, 15843, 15881, 15918, 15955, 15993, 16030, 16067,//150
 16104, 16142, 16179, 16216, 16253, 16290, 16327, 16365, 16402, 16439,//160
 16476, 16513, 16550, 16587, 16624, 16661, 16698, 16735, 16772, 16809,//170
 16846, 16883, 16920, 16957, 16994, 17031, 17068, 17105, 17142, 17179,//180
 17216, 17253, 17290, 17326, 17363, 17400, 17437, 17474, 17510, 17547,//190
 17584, 17621, 17657, 17694, 17731, 17768, 17804, 17841, 17878, 17914,//200
 17951, 17988, 18024, 18061, 18097, 18134, 18171, 18207, 18244, 18280,//210
 18317, 18353, 18390, 18426, 18463, 18499, 18536, 18572, 18609, 18645,//220
 18682, 18718, 18754, 18791, 18827, 18863, 18900, 18936, 18972, 19009,//230
 19045, 19081, 19118, 19154, 19190, 19226, 19263, 19299, 19335, 19371,//240
 19407, 19444, 19480, 19516, 19552, 19588, 19624, 19660, 19696, 19733,//250
 19769, 19805, 19841, 19877, 19913, 19949, 19985, 20021, 20057, 20093,//260
 20129, 20165, 20201, 20236, 20272, 20308, 20344, 20380, 20416, 20452,//270
 20488, 20523, 20559, 20595, 20631, 20667, 20702, 20738, 20774, 20810,//280
 20845, 20881, 20917, 20952, 20988, 21024, 21059, 21095, 21131, 21166,//290
 21202, 21237, 21273, 21309, 21344, 21380, 21415, 21451, 21486, 21522,//300
 21557, 21593, 21628, 21664, 21699, 21735, 21770, 21805, 21841, 21876,//310
 21912, 21947, 21982, 22018, 22053, 22088, 22124, 22159, 22194, 22229,//320
 22265, 22300, 22335, 22370, 22406, 22441, 22476, 22511, 22546, 22581,//330
 22617, 22652, 22687, 22722, 22557, 22792, 22827, 22862, 22897, 22932,//340
 22967, 23002, 23037, 23072, 23107, 23142, 23177, 23212, 23247, 23282,//350
 23317, 23352, 23387, 23422, 23456, 23491, 23526, 23561, 23596, 23631,//360
 23665, 23700, 23735, 23770, 23804, 23839, 23874, 23909, 23943, 23978,//370
 24013, 24047, 24082, 24117, 24151, 24186, 24220, 24255, 24290, 24324,//380
 24359, 24393, 24428, 24462, 24497, 24531, 24566, 24600, 24635, 24669,//390
 24704, 24738, 24773, 24807, 24841, 24876, 24910, 24945, 24979, 25013,//400
};    
 
 
 
uint8 sendpro=0;   	  //时间保护计数器
uint8 test_byte=0;    //测试通信
uint8 sendpro_bk;     //时间保护计数器的后备
 
uint8 t_send_flag = 0;			  //A250用到
uint8 rece_complete_flag = 0;     //A250用到 
 
uint16 T10_send[MAX_SEND]={0};    //发送缓冲
uint16 T10_read[MAX_RECE]={0,0,0,0,0,0,0,0,0,0,15,5};    //接收缓冲

uint8 receFCom[6]={0};            //解包缓冲
uint8 receDComCount = 0;          //解包计数器

uint8 testFlag = 0;

//包头的特殊码表
const uint8 receNFtab[62] = {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,
	                         0x0f,0x1f,0x2f,0x3f,0x4f,0x5f,0x6f,0x7f,0x8f,0x9f,
					         0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,
	                         0x0e,0x1e,0x2e,0x3e,0x4e,0x5e,0x6e,0x7e,0x8e,0x9e,
					         0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,
	                         0x0c,0x1c,0x2c,0x3c,0x4c,0x5c,0x6c,0x7c,0x8c,0x9c,
					         0xd0,0xd1												  	 
};



void port_init(void)
{
 PORTA = 0xe0;
 DDRA  = 0x18;
 PORTB = 0x00;
 DDRB  = 0xFF;
 PORTD = 0x80;
 DDRD  = 0xfA;
 U7(0xff);
 U9(0xff);
 U18(0xff);
 U19(0xff);
 PORTD = 0x00; 
 DDRC = 0b10011111;
 PORTC= 0b11111111;
}

//TIMER1 initialize - prescale:64
// WGM: 4) CTC, TOP=OCRnA
// desired value: 100mSec
// actual value: 99.996mSec (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xBC; //setup
 TCNT1L = 0x82;
 OCR1AH = 0x43;
 OCR1AL = 0x7E;
 OCR1BH = 0x43;
 OCR1BL = 0x7E;
 ICR1H  = 0x43;
 ICR1L  = 0x7E;
 TCCR1A = 0x00;
 TCCR1B = 0x0B; //start Timer
}

//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value:  0.995mSec (0.5%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x54; //set count
 OCR0  = 0xAC;  //set compare
 TCCR0 = 0x03; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 static uint8 a=0;
 TCNT0 = 0x54; //reload counter value
 sendpro++;
 
 a++;
 if(25==a){
   a=0;
   t_send_flag = 1;                                   //发送周期
 }

}


#pragma interrupt_handler time1_compa_isr:7
void time1_compa_isr(void)
{
//  Time_Int++;			  				   			  //温度0.1秒的标志
  PORTA^=0x10;                                        //LED闪烁
  testFlag++;
}

#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 
}


#pragma interrupt_handler adc_isr:15
void adc_isr(void)
{
 //conversion complete, read value (int) using...
 // value=ADCL;            //Read 8 low bits first (important)
 // value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
}

//ADC initialize
// Conversion time: 150uS
void adc_init(void)
{
 ADCSR = 0x00; //disable adc
 ADMUX = 0xc0; //select adc input 0
// ACSR  = 0x80;
 ADCSRA = 0x87;
}

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


   

 

}


unsigned int read_adc(void)//查询方式读取ADC单端通道
   {
      ADMUX=(0xc0|0x00); //adc_input:单端通道 0x00~0x07,0x1E,0x1F
      //0xc0:选择内部2.56V参考电压
      ADCSRA|=(1<<ADSC); //启动AD转换
      while ((ADCSRA&(1<<ADIF))==0); 
      ADCSRA|=(1<<ADIF); //写1清除标志位
      return ADC; //ADC=ADCH:ADCL
   }

   #define N (34)

unsigned int AdAverage(void)
{
	unsigned char count,i,j;
	unsigned int value_buf[N];
	unsigned int temp;
	unsigned long sum=0;
	for (count=0;count<N;count++)
	{
		value_buf[count] = read_adc();

	}
	for (j=0;j<N-1;j++)
	{
		for (i=0;i<N-j;i++)
		{
			if ( value_buf[i]>value_buf[i+1] )
			{
				temp = value_buf[i];
				value_buf[i] = value_buf[i+1]; 
				value_buf[i+1] = temp;
			}
		}
	}
	for(count=1;count<N-1;count++)
	sum += value_buf[count];
	return (sum/(N-2));
}	

unsigned int AdAverageT(void)
{
	unsigned char count,i,j;
	unsigned int value_buf[20];
	unsigned int temp;
	unsigned long sum=0;
	for (count=0;count<20;count++)
	{
		value_buf[count] = AdAverage();

	}
	for (j=0;j<20-1;j++)
	{
		for (i=0;i<20-j;i++)
		{
			if ( value_buf[i]>value_buf[i+1] )
			{
				temp = value_buf[i];
				value_buf[i] = value_buf[i+1]; 
				value_buf[i+1] = temp;
			}
		}
	}
	for(count=1;count<20-1;count++)
	sum += value_buf[count];
	return (sum/(20-2));
}





   

//extern uint16 send_s1_data[32];
void main(void){
  unsigned long AD;
  unsigned int  temp,i,j;
  unsigned char k[7];
  unsigned int  temp_A,temp_N,temp_P;
  
//  delay(200);
  init_devices();

  
  while(1){
begin:      
	  AD = AdAverageT();
	  
//	  AD = (AD*330)/125;		 //获得现时放大后的电压值,mv
	  
//	  AD = (AD*201)/1000;		 //电压值  
	  
//	  AD = (AD*2571)/1000 ;      //电阻值

//    100RPT = (AD*100*RA*RH)/(AD的分辨率*RB)	  
	  AD = (AD*257127)/10000;        //真实AD值
	                             
	  temp = 0;
	  while (AD > PT100_TAB[temp]){
	      temp++;
		  if(410==temp){
		      TransmitByte('X');
			  TransmitByte('X');
			  TransmitByte('X');
			  TransmitByte('X'); 
			  TransmitByte('\n'); 
			  goto begin;
		  }
	  }
      temp--;	  				 //实际温度整数部分
	  
	  temp_A = PT100_TAB[temp+1]-PT100_TAB[temp];
	  temp_N = AD               -PT100_TAB[temp];	  
	  temp_P = temp_N*10/temp_A;//实际温度小数部分
	      
	  
	  if((temp%1000)/100){
          TransmitByte( (temp%1000)/100 +'0' );
	  }	
      TransmitByte( ((temp%1000)%100)/10 +'0');
      TransmitByte( ((temp%1000)%100)%10 +'0' );
	  TransmitByte('.' );
      TransmitByte( temp_P%10 +'0' );
	 	   		  	  	  		   	
      TransmitByte( '\n' ); 		    
//	  delay(200);
  }
  
  while(1);
}



⌨️ 快捷键说明

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