#include "BAT32G137.h"
#include "adc.h"
#include "cg_tmm.h"
#include "elc.h"
#include "dma.h"
// bref: ADC DMA掃描
// para:
// note:
static void adc_init(void)
{
// 開啓ADC外設
CGC->PER0 |= CGC_PER0_ADCEN_Msk; /* enables input clock supply */
// 停止ADC
ADC->ADM0 = 0x00U; /* disable AD conversion and clear ADM0 register */
// 設置ADC 通道
/* A/D input pin setting: Please modify the following code according to your application needs. */
// 所有的IO設置爲 模擬通道
/* Set ANI0(P20) pin: It is necessary for ADC_VREF_AVREFP_AVREFM, used as AVREFP */
PORT->PMC2 |= 0x01U;
/* Set ANI1(P21) pin: It is necessary for ADC_VREF_AVREFP_AVREFM, used as AVREFM */
PORT->PMC2 |= 0x02U;
/* Set ANI2(P22) pin */
PORT->PMC2 |= 0x04U;
/* Set ANI3(P23) pin */
PORT->PMC2 |= 0x08U;
// 設置ADC時鐘
/* AD operation mode: select or scan mode */
ADC->ADM0 = _28_AD_CONVERSION_CLOCK_1 | _00_AD_COMPARATOR_DISABLE;
/* AD conversion mode setting */
// ADM1.0爲0爲四個通道 爲1 是單個通道
// 掃描模式
ADC->ADM1 |= _80_AD_OPERMODE_SCAN; //
// ADC參考源設定
ADC->ADM2 = _00_AD_POSITIVE_VDD | _00_AD_NEGATIVE_VSS | _00_AD_AREA_MODE_1 ;
// 數據值範圍
/* AD comversion result comprision upper limit setting */
ADC->ADUL = _FF_AD_ADUL_VALUE;
/* AD comversion result comprision lower limit setting */
ADC->ADLL = _00_AD_ADLL_VALUE;
// 採樣率 15.5 ADCLK
ADC->ADNSMP = 0x0F;
// /* adhard power up */
ADC->ADM0 |= ADCE;
// 通道選擇或者通道組選擇
ADC->ADS = 0x00;
// 軟件觸發
ADC->ADTRG = _00_AD_TRIGGER_SOFTWARE;
}
static void gpio_init(void)
{
// P50
PORT->POM5 &= ~(1<<0);
PORT->PM5 &= ~(1<<0);
PORT->P5 |= (1<<0);
//PORT->P5 &= ~(1<<0);
}
static uint16_t adc_buff[8];
static void dma_init(void)
{
const uint8_t ctrl_data_num = 0;
// DMA_VECTOR_ADC DMA中斷向量位置
DMAVEC->VEC[DMA_VECTOR_ADC] = ctrl_data_num;
// CTRL_DMACR_SZ_Pos 數據長度(1表示16位 應爲ADC結果大於8位,所以選16位)
// CTRL_DMACR_RPTINT_Pos 不使用鏈式觸發(所謂的鏈傳輸設置過後執行完會自動提取緊挨的DMA信息塊)
// CTRL_DMACR_DAMOD_Pos 目標地址是否增加 1表示增加
// CTRL_DMACR_SAMOD_Pos 源地址是否增加 0表示不增加
// CTRL_DMACR_RPTSEL_Pos DMA傳輸的方向 0表示爲 從源地址提取到目標地址
// CTRL_DMACR_MODE_Pos 1 表示否則一直執行 循環模式還是單次模式
DMAVEC->CTRL[ctrl_data_num].DMACR = (1 << CTRL_DMACR_SZ_Pos) | (0 << CTRL_DMACR_RPTINT_Pos)|
(1 << CTRL_DMACR_DAMOD_Pos) | (0 << CTRL_DMACR_SAMOD_Pos) |
(0 << CTRL_DMACR_RPTSEL_Pos)| (1 << CTRL_DMACR_MODE_Pos);
// 塊大小
DMAVEC->CTRL[ctrl_data_num].DMBLS = 1;
// 每個DMA循環傳輸次數
DMAVEC->CTRL[ctrl_data_num].DMACT = 4;
// DMA 傳輸重載值
DMAVEC->CTRL[ctrl_data_num].DMRLD = 4;
// DMA傳輸源地址
DMAVEC->CTRL[ctrl_data_num].DMSAR = (uint32_t)&ADC->ADCR;
// DMA傳輸目標地址
DMAVEC->CTRL[ctrl_data_num].DMDAR = (uint32_t)&adc_buff[0];
/* init DMA registers */
// 開啓 DMA時鐘
CGC->PER1 |= CGC_PER1_DMAEN_Msk;
// 指定信息塊內置地址
DMA->DMABAR = DMAVEC_BASE;
// 開啓對應的 DMA
DMA->DMAEN1 |= 1 << 2;
}
int main(void)
{
SystemCoreClockUpdate();
gpio_init();
adc_init();
dma_init();
ADC->ADM0 |= ADCS;
while(1)
{
}
}
ADC速率爲1.2M。
高速模式ADC轉換週期爲31.5 ADCLK。
低電流模式ADC轉換週期爲40.5 ADCLK。
硬件等待是等待ADC電源穩定爲1us.
可以設置外部觸發。
注意每次掃描模式是掃描四個通道。