📄 agc.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"
/** 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 agc_init(void)
{
// init COMP: Mode 001
CMCON = 1;
// init Ureferenz 0 .. 3,125 V
// U = zahl/24 *5 = zahl * 208mV
CVRCON = 0xE5; // 5 * 208mV = 1V an RA2
// init ADC: input ist RA1
// AN0 - Comp,vin- (mit AN2 verbunden)
// AN1 - Pegel
// AN2 - Vref fuer Comparator (mit AN0 verbunden)
ADCON0 = 0x05; // AN1, idle, enabled
ADCON1 = 0x0D; // nur AN0 und AN1 analog
//ADCON0 = 0x01; // AN0, idle, enabled
//ADCON1 = 0x0E; // nur AN0 analog
ADCON2 = 0xFE; // rechts, 20 Tad, Fosc/64
}
// stellt Comparatorpegel neu ein
// auf HF-Pegel +200..400 mV
byte agc_avr(void)
{
word pegel = agc_Pegel(); // HF-Pegel in mV
byte step = pegel/200 + 2; // in Ref-Stfen a 200 mV umrechnen
if (step>15) step=15; // ohoh, das ist viel zu viel
CVRCON = 0xE0 | step; // Spannung einstellen
return step;
}
//misst HF-Pegel
// Ergebnis in mV
word agc_Pegel(void)
{
ADCON0 = 0x05; // Pegel, AN0, idle, enabled
SleepMs(1);
return agc_Adc();
}
//misst Ref-Pegel
// Ergebnis in mV
word agc_Ref(void)
{
ADCON0 = 0x01; // Referenz, AN1, 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 + -