?? upsd33_adc.c
字號(hào):
/*------------------------------------------------------------------------------
upsd3300_adc.c
Description:
The uPSD3300 ADC device driver is intended to provide functions to initialize
and read the analog to digital converter for the uPSD family.
See uPSD3300_adc.h for function proto types.
------------------------------------------------------------------------------*/
/*#include "upsd3300.h"
#include "upsd3300_hardware.h"
#include "upsd33_adc.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#if (FREQ_OSC > 20000) // Set up ADC Divider
#define ADC_CLOCK_DIVIDER 1
#else
#define ADC_CLOCK_DIVIDER 0
#endif
*/
/*****************************************************************************
* ADC_EnableAll()
*功能:ADC使能,設(shè)置P1口通道為ADC輸入,選擇ADC時(shí)鐘頻率
*參數(shù):無
******************************************************************************/
void ADC_EnableAll(void)
{
ACON = 0; // 關(guān)閉ADC
P1SFS0 = 0xFF;
P1SFS1 = 0xFF; // 選擇P1口相應(yīng)位為ADC輸入
ADCPS =(0x08 + 1); //使能ADC時(shí)鐘,設(shè)置時(shí)鐘頻率
_nop_();
_nop_();
ACON = 0x20; // 使能ADC;Enable ADC
// NOTE: USER CODE MUST WAIT AT LEAST 20 MS before calling ADC_Read (Using the ADC)
}
/******************************************************************************
* ADC_Init(channel)
*功能描述:ADC初始化,選擇模擬通道輸入,初始化時(shí)鐘,關(guān)閉ADC中斷
*參 數(shù):channel - uchar 選擇ADC通道
*注 意:當(dāng)ADC通道改變得時(shí)候,此子程序一定要被調(diào)用
*****************************************************************************/
void ADC_Init (unsigned char channel)
{
unsigned char temp;
ACON = 0;
temp = (0x01) << channel; // 選擇通道
P1SFS0 |= temp;
P1SFS1 |= temp; // 設(shè)置P1口相應(yīng)位為ADC輸入
ADCPS =(0x08 + ADC_CLOCK_DIVIDER); // 使能ADC時(shí)鐘,設(shè)置時(shí)鐘頻率
_nop_();
ACON = 0x20; // 允許ADC轉(zhuǎn)換
// NOTE: USER CODE MUST WAIT AT LEAST 20 MS before calling ADC_Read (Using the ADC)
}
/******************************************************************************
* uint ADC_Read(channel)
*功能描述:讀A/D轉(zhuǎn)換后的數(shù)據(jù)
*參 數(shù):channel,初始化程序中選擇的通道
*返 回 值:ADC轉(zhuǎn)換結(jié)果,12位
*注 意:該子程序調(diào)用前,ADC_Init()必須先被調(diào)用
*******************************************************************************/
unsigned int ADC_Read( unsigned char channel )
{
unsigned int temp_ADC_result;
ACON &= 0xE0; //清除輸入通路 ~(00101110B) = (11010001B)
ACON |= (channel<<2); //選擇通路
_nop_ ();
_nop_ ();
ACON |= 0x02; //開始ADC轉(zhuǎn)換
_nop_ (); //延時(shí)一個(gè)機(jī)器周期: ADST: 1->0
while( (ACON & 0x01) != 1 ); //等待轉(zhuǎn)換結(jié)束
// Note: For increased ADC accuracy, the while loop above should be
// replaced with code that puts the MCU into Idle mode via PCON
// and makes use of the ADC interrupt to exit the Idle mode.
// The user would need to enable the ADC int and define the ADC ISR.
temp_ADC_result = (ADAT1<<8)+ADAT0; //Calculate ADC conversion result
return (temp_ADC_result);
}
/******************************************************************************
* uint ADC_Get(channel)
*功能描述:直接得到AD轉(zhuǎn)換值
*參 數(shù):channel,選擇讀取通道
*返 回 值:uint ADC轉(zhuǎn)換結(jié)果
*****************************************************/
uint ADC_Get (uchar channel)
{ uint i,j;
ADC_EnableAll();
ADC_Init(channel);
for(j=250;j>0;j--);
i = ADC_Read(channel);
return(i);
}
void getVA (uchar channel)
{
uchar str_v[] = {"The Voltage is:"};
uint temp;
uint value;
uchar vv[3];
uchar templ;
value = ADC_Get(channel);
value = value*100;
temp = (float)(value/1023)*5;
temp1 = value % 51;
if(temp1 >= 26)temp++;
vv[2] = temp%10;
vv[1] = (temp/10%)10; //小數(shù)點(diǎn)后1位
vv[0] = temp/100; // 個(gè)位
printstr(0,0,str_v);
printchar(5,1,vv[0]);
printchar(6,1,0x2e);
printchar(7,1,vv[0]);
printchar(8,1,vv[0]);
}
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -