motorcontroldemo_028/Vsrc/V_adc.c
2021-08-09 16:22:08 +03:00

193 lines
7.5 KiB
C

/*!
Copyright 2017 ÀÎ "ÍÈÈÝÒ" è ÎÎÎ "ÍÏÔ ÂÅÊÒÎÐ"
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
\file V_adc.c
\brief Ìîäóëü îáðàáîòêè ÀÖÏ (ñì. TAdcDrv) MCB_028
\author ÎÎÎ "ÍÏÔ Âåêòîð". http://motorcontrol.ru
\version v 2.0 22/03/2019
\addtogroup V_adc
@{*/
/* Ðàñïèñàíèå êàíàëîâ
* ADC13 Ia
* ADC19 Ic
* ADC1 Udc
* ADC31 0.75V
* ADC18 0.03V
* ADC20 Pot
* ADC15 AIN1
* ADC27 AIN2
* ADC29 TempSens
*/
#include "main.h"
//! \memberof TAdcDrv
//!
void AdcDrv_init(TAdcDrv *p) {
//! Èíèöèàëèçàöèÿ ÀÖÏ ïðè ïðîãðàììíîì çàïóñêå áåç èñïîëüçîâàíèè ïðåðûâàíèé è ôèôî
Uint32 trash;
// Çàïðåò âñåõ ñåêâåíñîðîâ
ADC->SEQSYNC = 0;
// Ñáðîñ Ñ÷åò÷èêîâ ïðåðûâíèé è èçìåðåíèé
ADC->SEQ[0].SCVAL = 0;
ADC->SEQ[1].SCVAL = 0;
ADC->SEQ[2].SCVAL = 0;
ADC->SEQ[3].SCVAL = 0;
ADC->SEQ[4].SCVAL = 0;
ADC->SEQ[5].SCVAL = 0;
ADC->SEQ[6].SCVAL = 0;
ADC->SEQ[7].SCVAL = 0;
//Âêëþ÷àåì ñåêâåíñîð
ADC->SEQSYNC_bit.SYNC0 = 1;
//Ñòàâèì ìàñêó ïðåðûâàíèÿ
ADC->IM_bit.SEQIM0 = 1;
//Íàñòðàèâàåì èñòî÷íèê çàïóñêà ïðîãðàììíûé
ADC->EMUX_bit.EM0 = ADC_EMUX_EM0_SwReq;
// Ðàçðåøèòü ñåêâåíñîðîâ
ADC->SEQEN_bit.SEQEN0 = 1;
//Íàñòðàèâàåì êîëè÷åñòâî ïåðåçàïóñêîâ ñåêâåíñîðà RCNT + 1
ADC->SEQ[0].SCCTL_bit.RCNT = 3;
ADC->SEQ[0].SCCTL_bit.RAVGEN = 1; // Ðàçðåøåíî óñðåäíåíèå
// Íåò ïðåðûâàíèÿ
ADC->SEQ[0].SCCTL_bit.ICNT = 0;
ADC->SEQ[0].SRQCTL_bit.RQMAX = 0x6;
ADC->ACTL[0].ACTL_bit.ADCEN = 0x1; // Ðàçðåøàåì ðàáîòó ÀÖÏ
ADC->ACTL[0].ACTL_bit.SELRES = ADC_ACTL_ACTL_SELRES_12bit;
ADC->ACTL[1].ACTL_bit.ADCEN = 0x1; // Ðàçðåøàåì ðàáîòó ÀÖÏ
ADC->ACTL[1].ACTL_bit.SELRES = ADC_ACTL_ACTL_SELRES_12bit;
ADC->ACTL[2].ACTL_bit.ADCEN = 0x1; // Ðàçðåøàåì ðàáîòó ÀÖÏ
ADC->ACTL[2].ACTL_bit.SELRES = ADC_ACTL_ACTL_SELRES_12bit;
ADC->ACTL[3].ACTL_bit.ADCEN = 0x1; // Ðàçðåøàåì ðàáîòó ÀÖÏ
ADC->ACTL[3].ACTL_bit.SELRES = ADC_ACTL_ACTL_SELRES_12bit;
ADC->SEQ[0].SRQSEL0_bit.RQ0 = 13; // Êàêîé âõîä ÀÖÏ êîãäà îïðàøèâàåòñÿ
ADC->SEQ[0].SRQSEL0_bit.RQ1 = 19; // --//--
ADC->SEQ[0].SRQSEL0_bit.RQ2 = 1; // --//--
ADC->SEQ[0].SRQSEL0_bit.RQ3 = 20; // --//--
ADC->SEQ[0].SRQSEL1_bit.RQ4 = 15; // --//--
ADC->SEQ[0].SRQSEL1_bit.RQ5 = 27; // --//--
ADC->SEQ[0].SRQSEL1_bit.RQ6 = 29; // --//--
// Æä¸ì, ïîêà ÀÖÏ âûñòàâèò ôëàã "ÃÎÒÎÂ" (ìîæíî âñòàâèòü ýòîò öèêë ïîñëå "ADCEN = 1")
while (!ADC->ACTL[0].ACTL_bit.ADCRDY) {};
while (!ADC->ACTL[1].ACTL_bit.ADCRDY) {};
while (!ADC->ACTL[2].ACTL_bit.ADCRDY) {};
while (!ADC->ACTL[3].ACTL_bit.ADCRDY) {};
}
//!Ðàñ÷åò ÀÖÏ ñ ÷àñòîòîé îñíîâíîãî ðàñ÷åòà âñåé ñèñòåìû óïðàâëåíèÿ (îáû÷íî 10êÃö).
//! Çàíèìàåòñÿ îáðàáîòêîé èçìåðåííûõ ÀÖÏ çíà÷åíèé è ïðåîáðàçóåò â ôîðìàò IQ24.
//! Òîêè ôàç äëÿ ïîâûøåíèÿ òî÷íîñòè óñðåäíÿþòñÿ çà íåñêîëüêî èçìåðåíèé
//! \memberof TAdcDrv
void AdcDrv_fast_calc(TAdcDrv *p) {
Uint32 trash;
Uint16 pot, ain1, ain2, temper;
p->IA_temp = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
p->IC_temp = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
p->Udc_temp = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
pot = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
ain1 = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
ain2 = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
p->T_temp = (int16) ADC->SEQ[0].SFIFO_bit.DATA;
// Ïðåîáðàçîâàíèå
p->Imeas_a = p->IaGainNom * (((p->IA_temp<< 4) + p->Imeas_a_offset));
p->Imeas_c = p->IcGainNom * (((p->IC_temp<< 4) + p->Imeas_c_offset));
p->Udc_meas = p->UdcGainNom * (p->Udc_temp + p->Udc_meas_offset);
p->Imeas_b = -p->Imeas_a - p->Imeas_c;
p->T_meas = p->TGainNom*(p->T_temp);
// Î÷èñòêà ÔÈÔÎ
while (ADC->SEQ[0].SFLOAD)
trash = ADC->SEQ[0].SFIFO;
}
//!Ìåäëåííûé ðàñ÷åò.
//!Çàíèìàåòñÿ ïåðåñ÷åòîì êîýôôèöèåíòîâ, èñïîëüçóåìûõ â ñêîðîñòíîé ôóíêöèè ðàñ÷åòà,
//!÷òîáû íå çàíèìàòü ïðîöåññîðíîå âðåìÿ òàì. Íàõîäèò êîýôôèöèåíòû,
//!íà êîòîðûå íàäî óìíîæèòü ïîëó÷åííîå ñ ÀÖÏ çíà÷åíèå, ÷òîáû ïîëó÷èòü
//!îòíîñèòåëüíûå åäèíèöû ñ çàäàííîé áàçîé.
//! Ïðèìåð p->PvalveGainNom=_IQ16mpy(p->Pvalve_gain,_IQ(1.0/100));
//! Pvalve_gain - çíà÷åíèå â ôîðìàòå int. çàäàåòñÿ ïîëüçîâàòåëåì â UniCON.
//!Îïðåäåëÿåò, ñêîëüêèì ïðîöåíòàì ñîîòâåòñòâóåò ïîëíûé äèàïàçîí ÀÖÏ.  ïðîñòåéøåì ñëó÷àå
//!ðàâåí 100. Ò.å. êîãäà íà ÀÖÏ ìàêñèìàëüíûé ñèãíàë, ýòî ñîîòâåòñòâóåò 100%.
//!_IQ(1.0/100) îáðàòíîå çíà÷åíèå äëÿ ìàñøòàáèðóþùåãî êîýôôèöèåíòà. Òàê êàê âåëè÷èíó ïðîöåíòîâ
//!åñòåñòâåííåå âñåãî ïåðåâåñòè â îòíîñèòåëüíûå åäèíèöû òàê, ÷òî 100% ñîîòâåòñòâóò 1.0,
//!òî ìàñøòàáèðóþùèé êîýôôèöèåíò (áàçà) ðàâåí 100. Ò.å. UniCON, ïðî÷èòàâ èç ñèñòåìû óïðàâëåíèÿ
//! ÷èñëî 1.0 â ôîðìàòå 8.24 äîëæåí óìíîæèòü åãî íà 100, ÷òîáû îòîáðàçèëèñü ïðîöåíòû.
//! Çäåñü êîýôôèöèåíò çàäàí ÿâíî êàê 1.0/100, íî äëÿ ðÿäà ñëó÷àåâ áàçîâîå çíà÷åíèå íóæíî ìåíÿòü.
//!Òàê, äëÿ òîêîâ ôàç èñïîëüçóåòñÿ çíà÷åíèå _1_I_nom, â ôîðìàòå 8.24, ñîîòâåòñòâóþùåå åäèíèöå äåëåííîé íà
//áàçîâîå çíà÷åíèå òîêà, íàïðèìåð, 200 À. Òàê êàê â çàâèñèìîñòè îò ìîùíîñòè ïðåîáðàçîâàòåëÿ áàçîâûé òîê ìîæåò ìåíÿòüñÿ,
//òî ýòî çíà÷åíèå, â îòëè÷èå îò ïðîöåíòîâ, ñäåëàíî íàñòðàèâàåìûì. Ðàñ÷åò _1_I_nom èäåò â äðóãîì ìåñòå, òàê êàê
//çàíèìàåò ìíîãî òàêòîâ ðàñ÷åòà.
//Äëÿ áåççíàêîâîãî çíà÷åíèÿ ÀÖÏ èçìåðÿåò ÷èñëî îò 0 äî 65535. (16 ðàçðÿäîâ, ãäå çàïîëíåíû âåðõíèå).
//Äëÿ ïðèìåðà ñ ïðîöåíòàìè íåîáõîäèìî ñäåëàòü òàê, ÷òîáû ïîëó÷èëîñü ðåçóëüòèðóþùåå çíà÷åíèå â ôîðìàòå 8.24,
//ãäå 1.0 ýòî 65535. Òàêèì îáðàçîì, íóæíî ñäâèíóòü ÷èñëî 65535 íà 24-16=8 ðàçðÿäîâ.
//Ñäâèã íà 8 ðàçðÿäîâ - ýòî óìíîæåíèå íà ÷èñëî 255. ×èñëî 255 - ýòî 1.0 â ôîðìàòå 24.8.
//Òàêèì îáðàçîì, PvalveGainNom - ýòî êîýôôèöèåíò â ôîðìàòå 24.8. îí ïîëó÷àåòñÿ â ðåçóëüòàòå èñïîëüçîâàíèÿ ôóíêöèè
//_IQ16mpy, àðãóìåíû êîòîðîé Pvalve_gain (int) è 1.0/100 â ôîðìàòå 8.24. Ôóíêöèÿ IQ ìíîæåíèÿ
//ïî ñóòè ïðåäñòàâëÿåò ñîáîé îáû÷íîå îóìîæåíèå â 64 ðàçðÿäàõ ñî ñäâèãîì ðåçóëüòàòà âïðàâî íà Q ðàçðÿäîâ.
//Ò.å. _IQ16mpy óìíîæàåò ÷èñëî â ôîðìàòå IQ24 9âòîðîé àðãóìåíò) íà öåëî÷èñëåííûé êîýôôèöèåíò (ïåðâûé àðãóìåíò),
//à ïîòîì ñäâèãàòå ðåçóëüòàò íà 16 ðàçðÿäîâ âïðàâî.
//Òàê, â ðåçóëüòàòå _IQ16mpy(p->Pvalve_gain,_IQ(1.0/100)); ïîëó÷àåòñÿ öåëî÷èñëåííîå ÷èñëî 255, ÿâëÿþùåéñÿ
//1.0 â ôîðìàòå 24.8 èç-çà ñäâèãà íà 16 ðàçðÿäîâ âïðàâî.
//Âñ¸ âûøåïðèâåäåííîå ìóòåâî ñäåëàíî ñ îäíîé öåëüþ - óâåëè÷èòü ïðîèçâîäèòëåüíîñòü îáðàáîòêè ÀÖÏ.
//! \memberof TAdcDrv
void AdcDrv_slow_calc(TAdcDrv *p) {
p->IaGainNom = _IQ16mpy(p->Imeas_a_gain, drv_params._1_I_nom) << 1;
p->IbGainNom = _IQ16mpy(p->Imeas_b_gain, drv_params._1_I_nom) << 1;
p->IcGainNom = _IQ16mpy(p->Imeas_c_gain, drv_params._1_I_nom) << 1;
p->UdcGainNom = _IQ16mpy(p->Udc_meas_gain, drv_params._1_Udc_nom) << 4;
p->TGainNom = _IQ16mpy(p->T_meas_gain, _IQ(0.05)) << 4; //0.05 = 1/20
}
//! Ìèëëèñåêóíäíûé ðàñ÷åò
//! \memberof TAdcDrv
void AdcDrv_ms_calc(TAdcDrv *p) {
}
/*@}*/