📄 adc.c
字号:
#include "def.h"
struct STRADC adc;
extern struct STRWORK work;
extern void savemsg( unsigned char msgtype, unsigned char msgcode);
void Init_ADC(void);
void DealADC(void);
void diandu(void);
//******************************************************************
void Init_ADC(void)
{
// P6SEL = 0xc0; // Pin P6.6 p6.7 used by ADC module
ADC12CTL0 &=~ 0x02; // Disable conversion before changing
// the ADC12 settings!!!
/* selection of reference and input */
// ADC12MCTL0 = 0x13; // Ref = AVss, AVcc; Input = A3
// ADC12MCTL1 = 0x14; // Ref = AVss, AVcc; Input = A4
ADC12MCTL0 = 0x16; // Ref = AVss, AVcc; Input = A6
ADC12MCTL1 = 0x17+0x80; // Ref = AVss, AVcc; Input = A7
ADC12CTL1 = 0x0202; // first conv. result is stored in ADC12MEM0
//0x0272 // ADC12SC bit triggers Sample&Hold
// sample pulse is generated by Sampling Timer
// Clock Source: ADC12 internal oscillator
// Clock divider: 1
// conversion mode: one single sequence
ADC12CTL0 = 0x03f0; // Sample&Hold Time 0
//0x03f0 // Sample&Hold Time 1
// Multiple Sample&Hold
// reference voltage is off
// ADC12 module is switched on
// no interrupts
ADC12IE = 0x0002; // enable all ADC interrupts
ADC12CTL0 |= 0x02; // enable conversion
}
//-----------------------------------------------------------
interrupt [ADC_VECTOR] void ADC12(void) // ADC INTERRUPT HANDLER //50hz
{
unsigned int temp[8];
static unsigned int number;
static unsigned char hivnum;
ADC12IFG=0;//clear interrupt source
temp[0]=ADCMEM[0];//V
temp[1]=ADCMEM[1];//i
adc.Vtemp=temp[0];
adc.Itemp=temp[1];
if(adc.Vtemp>ConInV)
{
if(hivnum<25)hivnum++;
}
else
{
if(hivnum>0)hivnum--;
}
if((hivnum>20)&&(work.invert==OFF))
{
setOUT1;
setOUT2;
work.invert=ON;
}
if((hivnum<2)&&(work.invert==ON))
{
clrOUT1;
clrOUT2;
work.invert=OFF;
}
number++;
if(number>800)
{
savemsg(DEALADC,DEALADC);
number=0;
}
}
void DealADC(void)
{
unsigned long int ltemp;
/// k=0.001245 b=-0.138 vpk=0.0996 vpb=-11.04 ipk=0.0249 ipb=-2.76
ltemp=adc.Vtemp;
ltemp=ltemp*996-110000;
ltemp=ltemp/1000;
adc.Vadc=ltemp;
ltemp=adc.Itemp;
ltemp=ltemp*249-27600;
ltemp=ltemp/1000;
adc.Iadc=ltemp;
ltemp=ltemp*adc.Vadc;
ltemp=ltemp/10000;
adc.gonglv=ltemp;
}
void diandu(void)
{
adc.gongdu=adc.gongdu+(adc.gonglv/60);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -