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

📄 main.c

📁 51单片机C语言常用模块与综合系统设计实例精讲
💻 C
字号:
#include <stdio.h>
#include <absacc.h>
#include <intrins.h>
#include <./Atmel/at89x52.h>
#include "source.h"
struct MY_TIMER TT[TIMER_NUM];
struct volt_detect volt_group;
void time2()interrupt 5 
{
	unsigned char i;
	for(i=0;i<TIMER_NUM;i++)
	{
		if(TT[i].enable==1)
		{
			if(TT[i].count==0)
			{
			  	TT[i].enable=0;	
				TT[i].flag=1;
			}
			else{
				TT[i].count--;
			}
		}
	}
	TF2=0;
}
void start_timer(void)
{
    T2CON=0X00;
	RCAP2L=TIMER2L; //10ms
	TL2=TIMER2L;
	RCAP2H=TIMER2H;
	TH2=TIMER2H;
 	ET2=1;//enable timer2 interrupt
	TR2=1;//start timer
}
void reset_timer(unsigned char i)
{
	TT[i].enable=0;
	TT[i].count=0;
}
void set_timer(unsigned char i,unsigned int count) // time out counter*10ms 
{
	TT[i].count=count;
	TT[i].enable=1;
	TT[i].flag=0;
}
void Adc0801_Start_Cvt(void)
{
	ADC0801_PORT=0;
}
unsigned char Adc0801_Read(void)
{
	return(ADC0801_PORT);
}
#define	DELAY	_nop_();_nop_();_nop_();_nop_();
unsigned int tlc_volt_read(unsigned char  addr) //max system clock  2.1M max i/oclock 1.1M
{
	unsigned char	i;
	unsigned char   tlc_add;
	unsigned int 	dat;
	i=10;
	dat=0;
	tlc_add=addr;
    AD_IOCLK=0;
	AD_CSN=0;_nop_();_nop_(); // set up time
	while(i--)
	{
		AD_IOCLK=0;DELAY;
		if(tlc_add&0x08)
			AD_ADDI=1;
		else
			AD_ADDI=0;
		tlc_add<<=1;

		AD_IOCLK=1;
        dat<<=1;	
		dat |=AD_OUT;
        DELAY;
	}
	AD_CSN=1; //ABOVER IS PREVIOUS BYTE;
  	return dat;
}
#undef DELAY
void Tlc_Ad_Cvt(void)
{
	unsigned int temp_volt;
	temp_volt=tlc_volt_read(volt_group.port);
	temp_volt=temp_volt>>1;//in order to value less than 512
	temp_volt=temp_volt%0x200;//div 512 because the valut of volt is smaller than 512;
	temp_volt=temp_volt*125;
	temp_volt >>=7;//then result is (volt_temp *5*100)/1024 and is is the real volt multiply 100.
	switch(volt_group.preport)
	{
		case VOLT_1_8V_PORT:
        case VOLT_E3_3V_PORT:
		case VOLT_LOCAL_3_3V_PORT:
		case VOLT_VX_AGC_PORT:
		case VOLT_TEST:
			//is the real value multiply 100.
			break;
		case VOLT_N48V_PORT:
			temp_volt=(500-temp_volt)*53;
			temp_volt=temp_volt/2-500;
			break;
		case VOLT_9V_PORT:
			temp_volt=temp_volt*3;
			break;
		case VOLT_N5V_PORT:
			temp_volt=300-temp_volt;
			temp_volt=temp_volt*5;
			temp_volt=temp_volt/2;
			break;
		case VOLT_15V_PORT:
			temp_volt=temp_volt*6;
			break;
		case VOLT_15V_LNB_PORT:
			temp_volt=temp_volt*6;
			break;
		case VOLT_24V_ODU_PORT:
			temp_volt=temp_volt*6;
			break;				
		case VOLT_LOCAL_24V_PORT:
			temp_volt=temp_volt*6;
			break;
		default:break;
	}
	volt_group.volt[volt_group.preport]=temp_volt;
	volt_group.preport=volt_group.port;
	volt_group.port++;
	volt_group.port %=VOLT_TOTAL_PORTS;
}

main()
{
	unsigned char fsm_adc=0;
	unsigned int  V_ADJ=0;
	IE=0X40;
	start_timer();
	EA=1;       /* Enable interrupts */
	volt_group.port=0;
	volt_group.preport=0;
	tlc_volt_read(volt_group.port);
	volt_group.port=1;
	set_timer(VOLT_TIMER,VOLT_TIMER_LEN);
	set_timer(ADC0801_TIMER,ADC0801_TIMER_LEN);
	while(1){
		if(TT[VOLT_TIMER]){
			set_timer(VOLT_TIMER,VOLT_TIMER_LEN);
			Tlc_Ad_Cvt();
		}
		if(TT[ADC0801_TIMER]){
			set_timer(ADC0801_TIMER,ADC0801_TIMER_LEN);
			switch(fsm_adc){
				case FSM_ADC0801_START:
					fsm_adc=FSM_ADC0801_FINISH;
					Adc0801_Start_Cvt();
					break;
				case FSM_ADC0801_FINISH:
					fsm_adc=FSM_ADC0801_START;
					V_ADJ=Adc0801_Read();
					V_ADJ *=5;
					V_ADJ *=100;
					V_ADJ /=256;
					break;
				default:
					fsm_adc=FSM_ADC0801_START;
					break;
			}			
		}
	}
}

⌨️ 快捷键说明

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