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

📄 agc.c

📁 ADS-B接收机DIY全套资料
💻 C
字号:
/*********************************************************************
 * FileName:        agc.c
 * Dependencies:    See INCLUDES section below
 * Processor:       PIC18
 * Compiler:        C18 2.30.01+
 * Company:         sprut
 * Copyright:       2007-2010 Joerg Bredendiek (sprut)
 *
 *
 ********************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */



/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#include "io_cfg.h"             // I/O pin mapping

#include "delays.h"
//#include "system\interrupt\interrupt.h"
#include "user\user.h"
#include "user\adsbin.h"
#include "user\agc.h"

#include "timers.h"
#include "pwm.h"


/** V A R I A B L E S ********************************************************/
#pragma udata


/** D E C L A R A T I O N S **************************************************/
#pragma code


void pwm_init(void)
{
	// agc-offset aus dem EEPROM laden
    EECON1 = 0x00;
	EEADR  = 0x00;	//Adresse = 0x00
	EECON1_RD = 1;
	agc_offset = EEDATA;				// ref sollte etwa 100mV ueber pegel sein
    // unsinnigen offset durch 100mV ersetzen
	if (agc_offset<10)  agc_offset = 100; 
	if (agc_offset>250) agc_offset = 100; 

	agc_mode   = 1;					// agc an

	pwm_period = 0xFF;  			// 1024 stufen
    T2_PS = T2_PS_1_1;  			// 47 kHz
	pwm_dutycycle = 140;			// 0,7 / 5 * 1024 = 143
	SetDCPWM2(1);
	OpenTimer2(TIMER_INT_OFF & T2_PS); 
	OpenPWM2(pwm_period); 			// PWM period =[(period ) + 1] x 4 x TOSC x TMR2 prescaler
	SetDCPWM2(pwm_dutycycle);  		// 10 Bit
}


// stellt Comparatorpegel neu ein
// auf HF-Pegel +100 mV
word pwm_avr(void)
{
	int pegel = agc_Pegel();						// HF-Pegel in mV
	int ref   = agc_Ref();							// Uref in mV
	int step  = (pegel + agc_offset - ref)/10;		// in pwm-stufen umrechnen, gebremst
	pwm_dutycycle += step;
	if (pwm_dutycycle > 1000) pwm_dutycycle = 1000;	// begrenzen 240mV .. 4.88V
    if (pwm_dutycycle <   50) pwm_dutycycle =   50;
	SetDCPWM2(pwm_dutycycle);						// Spannung einstellen
	return step;
}


void agc_init(void)
{
	// init COMP: Mode 001
	CMCON = 1;
	ADCON0 = 0x05;				// AN1, idle, enabled
	ADCON1 = 0x0D;				// nur AN0 und AN1 analog
	ADCON2 = 0xFE;				// rechts, 20 Tad, Fosc/64
}


//misst HF-Pegel
// Ergebnis in mV
word agc_Pegel(void)
{
	ADCON0 = 0x05;					// Pegel, AN1, idle, enabled
	SleepMs(1);
	return agc_Adc();
}


//misst Ref-Pegel
// Ergebnis in mV
word agc_Ref(void)
{
	ADCON0 = 0x01;					// Referenz, AN0, idle, enabled
	SleepMs(1);
	return agc_Adc();
}


//misst ADC-Eingangsspannung
// Ergebnis in mV
word agc_Adc(void)
{
	WORD	wert;
	ADCON0bits.GO = 1;              // Start AD conversion
	while(ADCON0bits.NOT_DONE);     // Wait for conversion
    wert.byte0 = ADRESL;
    wert.byte1 = ADRESH;
	wert._word *= 5;				// Wandlung in mV
	return wert._word;
}


/** EOF agc.c ***************************************************************/

⌨️ 快捷键说明

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