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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴֵ<EFBFBD>,<EFBFBD><EFBFBD>ȡƫ<EFBFBD><EFBFBD>ֵ,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ADC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡУֵ
|
|||
|
*
|
|||
|
* @param none
|
|||
|
*
|
|||
|
* @return ƫ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
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 <EFBFBD>ⲿ<EFBFBD>źŵ<EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @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 <EFBFBD>ⲿ<EFBFBD>źŲ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @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 <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>ص<EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @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ִ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param none
|
|||
|
*
|
|||
|
* @return ADCת<EFBFBD><EFBFBD><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ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param charg - Touchkey<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>,5bits<EFBFBD><EFBFBD>Ч, t=charg*Tadc
|
|||
|
* @param disch - Touchkey<EFBFBD>ŵ<EFBFBD>ʱ<EFBFBD><EFBFBD>,3bits<EFBFBD><EFBFBD>Ч, t=disch*Tadc
|
|||
|
*
|
|||
|
* @return <EFBFBD><EFBFBD>ǰTouchKey<EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ADC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param cycle - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><EFBFBD>㷽<EFBFBD><EFBFBD>Ϊ(256-cycle)*16*Tsys
|
|||
|
*
|
|||
|
* @return none
|
|||
|
*/
|
|||
|
void ADC_AutoConverCycle(uint8_t cycle)
|
|||
|
{
|
|||
|
R8_ADC_AUTO_CYCLE = cycle;
|
|||
|
}
|
|||
|
|
|||
|
/*********************************************************************
|
|||
|
* @fn ADC_DMACfg
|
|||
|
*
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>DMA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param s - <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DMA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param startAddr - DMA <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ
|
|||
|
* @param endAddr - DMA <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|||
|
* @param m - <EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>棩
|
|||
|
*
|
|||
|
* @param ts_v - <EFBFBD><EFBFBD>ǰ<EFBFBD>¶ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @return ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ<EFBFBD><EFBFBD><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);
|
|||
|
}
|