mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
233 lines
5.9 KiB
C
233 lines
5.9 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : CH58x_adc.c
|
||
* Author : WCH
|
||
* Version : V1.2
|
||
* Date : 2021/11/17
|
||
* Description
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*******************************************************************************/
|
||
|
||
#include "CH58x_common.h"
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_DataCalib_Rough
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴֵ<DDB4>,<2C><>ȡƫ<C8A1><C6AB>ֵ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ADC<44><43><EFBFBD><EFBFBD><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD>ȡУֵ
|
||
*
|
||
* @param none
|
||
*
|
||
* @return ƫ<><C6AB>
|
||
*/
|
||
signed short ADC_DataCalib_Rough(void) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴֵ<DDB4>,<2C><>ȡƫ<C8A1><C6AB>ֵ
|
||
{
|
||
uint16_t i;
|
||
uint32_t sum = 0;
|
||
uint8_t ch = 0; // <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>
|
||
uint8_t ctrl = 0; // <20><><EFBFBD>ݿ<EFBFBD><DDBF>ƼĴ<C6BC><C4B4><EFBFBD>
|
||
|
||
ch = R8_ADC_CHANNEL;
|
||
ctrl = R8_ADC_CFG;
|
||
R8_ADC_CFG = 0;
|
||
|
||
ADC_ChannelCfg(1); // ADCУͨ<D7BC><CDA8><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>ͨ<EFBFBD><CDA8>1
|
||
R8_ADC_CFG |= RB_ADC_OFS_TEST | RB_ADC_POWER_ON | (2 << 4); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||
R8_ADC_CONVERT = RB_ADC_START;
|
||
while(R8_ADC_CONVERT & RB_ADC_START);
|
||
for(i = 0; i < 16; i++)
|
||
{
|
||
R8_ADC_CONVERT = RB_ADC_START;
|
||
while(R8_ADC_CONVERT & RB_ADC_START);
|
||
sum += (~R16_ADC_DATA) & RB_ADC_DATA;
|
||
}
|
||
sum = (sum + 8) >> 4;
|
||
R8_ADC_CFG &= ~RB_ADC_OFS_TEST; // <20>رղ<D8B1><D5B2><EFBFBD>ģʽ
|
||
|
||
R8_ADC_CHANNEL = ch;
|
||
R8_ADC_CFG = ctrl;
|
||
return (2048 - sum);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_ExtSingleChSampInit
|
||
*
|
||
* @brief <20>ⲿ<EFBFBD>źŵ<C5BA>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||
*
|
||
* @param sp - refer to ADC_SampClkTypeDef
|
||
* @param ga - refer to ADC_SignalPGATypeDef
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_ExtSingleChSampInit(ADC_SampClkTypeDef sp, ADC_SignalPGATypeDef ga)
|
||
{
|
||
R8_TKEY_CFG &= ~RB_TKEY_PWR_ON;
|
||
R8_ADC_CFG = RB_ADC_POWER_ON | RB_ADC_BUF_EN | (sp << 6) | (ga << 4);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_ExtDiffChSampInit
|
||
*
|
||
* @brief <20>ⲿ<EFBFBD>źŲ<C5BA><C5B2><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||
*
|
||
* @param sp - refer to ADC_SampClkTypeDef
|
||
* @param ga - refer to ADC_SignalPGATypeDef
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_ExtDiffChSampInit(ADC_SampClkTypeDef sp, ADC_SignalPGATypeDef ga)
|
||
{
|
||
R8_TKEY_CFG &= ~RB_TKEY_PWR_ON;
|
||
R8_ADC_CFG = RB_ADC_POWER_ON | RB_ADC_DIFF_EN | (sp << 6) | (ga << 4);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_InterTSSampInit
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||
*
|
||
* @param none
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_InterTSSampInit(void)
|
||
{
|
||
R8_TKEY_CFG &= ~RB_TKEY_PWR_ON;
|
||
R8_TEM_SENSOR = RB_TEM_SEN_PWR_ON;
|
||
R8_ADC_CHANNEL = CH_INTE_VTEMP;
|
||
R8_ADC_CFG = RB_ADC_BUF_EN | RB_ADC_POWER_ON | RB_ADC_DIFF_EN | (3 << 4);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_InterBATSampInit
|
||
*
|
||
* @brief <20><><EFBFBD>õ<EFBFBD><C3B5>ص<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||
*
|
||
* @param none
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_InterBATSampInit(void)
|
||
{
|
||
R8_TKEY_CFG &= ~RB_TKEY_PWR_ON;
|
||
R8_ADC_CHANNEL = CH_INTE_VBAT;
|
||
R8_ADC_CFG = RB_ADC_POWER_ON | RB_ADC_BUF_EN | (0 << 4); // ʹ<><CAB9>-12dBģʽ<C4A3><CABD>
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn TouchKey_ChSampInit
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||
*
|
||
* @param none
|
||
*
|
||
* @return none
|
||
*/
|
||
void TouchKey_ChSampInit(void)
|
||
{
|
||
R8_ADC_CFG = RB_ADC_POWER_ON | RB_ADC_BUF_EN | (2 << 4);
|
||
R8_TKEY_CFG |= RB_TKEY_PWR_ON;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_ExcutSingleConver
|
||
*
|
||
* @brief ADCִ<43>е<EFBFBD><D0B5><EFBFBD>ת<EFBFBD><D7AA>
|
||
*
|
||
* @param none
|
||
*
|
||
* @return ADCת<43><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*/
|
||
uint16_t ADC_ExcutSingleConver(void)
|
||
{
|
||
R8_ADC_CONVERT = RB_ADC_START;
|
||
while(R8_ADC_CONVERT & RB_ADC_START);
|
||
|
||
return (R16_ADC_DATA & RB_ADC_DATA);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn TouchKey_ExcutSingleConver
|
||
*
|
||
* @brief TouchKeyת<79><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param charg - Touchkey<65><79><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,5bits<74><73>Ч, t=charg*Tadc
|
||
* @param disch - Touchkey<65>ŵ<EFBFBD>ʱ<EFBFBD><CAB1>,3bits<74><73>Ч, t=disch*Tadc
|
||
*
|
||
* @return <20><>ǰTouchKey<65><79>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
|
||
*/
|
||
uint16_t TouchKey_ExcutSingleConver(uint8_t charg, uint8_t disch)
|
||
{
|
||
R8_TKEY_COUNT = (disch << 5) | (charg & 0x1f);
|
||
R8_TKEY_CONVERT = RB_TKEY_START;
|
||
while(R8_TKEY_CONVERT & RB_TKEY_START);
|
||
return (R16_ADC_DATA & RB_ADC_DATA);
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_AutoConverCycle
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ADC<44><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param cycle - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC>㷽<EFBFBD><E3B7BD>Ϊ(256-cycle)*16*Tsys
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_AutoConverCycle(uint8_t cycle)
|
||
{
|
||
R8_ADC_AUTO_CYCLE = cycle;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_DMACfg
|
||
*
|
||
* @brief <20><><EFBFBD><EFBFBD>DMA<4D><41><EFBFBD><EFBFBD>
|
||
*
|
||
* @param s - <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>DMA<4D><41><EFBFBD><EFBFBD>
|
||
* @param startAddr - DMA <20><>ʼ<EFBFBD><CABC>ַ
|
||
* @param endAddr - DMA <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
* @param m - <20><><EFBFBD><EFBFBD>DMAģʽ
|
||
*
|
||
* @return none
|
||
*/
|
||
void ADC_DMACfg(uint8_t s, uint16_t startAddr, uint16_t endAddr, ADC_DMAModeTypeDef m)
|
||
{
|
||
if(s == DISABLE)
|
||
{
|
||
R8_ADC_CTRL_DMA &= ~RB_ADC_DMA_ENABLE;
|
||
}
|
||
else
|
||
{
|
||
R16_ADC_DMA_BEG = startAddr;
|
||
R16_ADC_DMA_END = endAddr;
|
||
if(m)
|
||
{
|
||
R8_ADC_CTRL_DMA |= RB_ADC_DMA_LOOP | RB_ADC_IE_DMA_END | RB_ADC_DMA_ENABLE;
|
||
}
|
||
else
|
||
{
|
||
R8_ADC_CTRL_DMA &= ~RB_ADC_DMA_LOOP;
|
||
R8_ADC_CTRL_DMA |= RB_ADC_IE_DMA_END | RB_ADC_DMA_ENABLE;
|
||
}
|
||
}
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn ADC_GetCurrentTS
|
||
*
|
||
* @brief <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>棩
|
||
*
|
||
* @param ts_v - <20><>ǰ<EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>棩
|
||
*/
|
||
int ADC_GetCurrentTS(uint16_t ts_v)
|
||
{
|
||
uint32_t C25;
|
||
int cal;
|
||
|
||
C25 = (*((PUINT32)ROM_CFG_TMP_25C));
|
||
cal = (ts_v * 2100) >> 12;
|
||
cal = (((C25 >> 16) & 0xFFFF) ? ((C25 >> 16) & 0xFFFF) : 25) + ((cal - ((int)(C25 & 0xFFFF) - 1050 / 2) * 2) * 10 / 14);
|
||
return (cal);
|
||
}
|