/**
* @file HW_ADC_ATY.c
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2023 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of uart for STC51
*
* @version
* - 1_01_221231 > ATY
* -# Preliminary version, first Release
* -Undone: over with "\r\n" type
********************************************************************************
*/
#ifndef __HW_ADC_ATY_C
#define __HW_ADC_ATY_C
#include "HW_ADC_ATY.h"
/******************************* For user *************************************/
#if defined(__STC51_ATY)
/**
* @brief ADC init
*/
void ADC_Init(void)
{
#ifndef ADCTIM
#define ADCTIM (*(unsigned char volatile xdata *)0xfea8)
#endif
P_SW2 |= 0x80; // change reg sfr
ADCTIM = 0x3F; // set ADC internal timing sequence
P_SW2 &= 0x7F; // stop change reg sfr
ADCCFG = 0x0F; // set SYSCLK/2/16 as ADC timer
// ADCCFG = 0x00; // set SYSCLK/2/1 as ADC timer
ADC_CONTR = 0x80; // enable ADC
}
#elif defined(__STM32_HAL_ATY)
#endif /* PLATFORM */
/******************************************************************************/
#if defined(__STC51_ATY)
/**
* @brief ADC get mcu vref
* @return mcu vref data
*/
float ADC_GetVref(void)
{
uint16_t dataVref = 0;
float resultVref = 0.0;
int* BGV;
BGV = (int __IDATA*)0xEF;
dataVref = ADC_Get(0x0F);
resultVref = (1024L * (*BGV) / dataVref);
return resultVref;
}
/**
* @brief ADC get data at channel
* @param channel channel
* @return adc data
*/
uint16_t ADC_Get(uint8_t channel)
{
uint16_t errCount = 1000;
ADC_CONTR = ((ADC_CONTR & 0xF0) | (channel & 0x0F));
ADC_CONTR |= 0x40; // start ADC
__NOP_ATY;
__NOP_ATY;
while((!(ADC_CONTR & 0x20)) && errCount)
errCount--;
if(errCount)
{
ADC_CONTR &= ~0x20; // clear over flag
return ((ADC_RES << 2) + (ADC_RESL >> 6));
}
return 0x01;
}
#elif defined(__STM32_HAL_ATY)
uint16_t ADC_Get(uint8_t channel)
{
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef * hadc);
}
#endif /* PLATFORM */
#endif /* __HW_ADC_ATY_C */
/******************************** End Of File *********************************/