?? upsd33_adc.c
字號:
/*------------------------------------------------------------------------------
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使能,設置P1口通道為ADC輸入,選擇ADC時鐘頻率
*參數:無
******************************************************************************/
void ADC_EnableAll(void)
{
ACON = 0; // 關閉ADC
P1SFS0 = 0xFF;
P1SFS1 = 0xFF; // 選擇P1口相應位為ADC輸入
ADCPS =(0x08 + 1); //使能ADC時鐘,設置時鐘頻率
_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初始化,選擇模擬通道輸入,初始化時鐘,關閉ADC中斷
*參 數:channel - uchar 選擇ADC通道
*注 意:當ADC通道改變得時候,此子程序一定要被調用
*****************************************************************************/
void ADC_Init (unsigned char channel)
{
unsigned char temp;
ACON = 0;
temp = (0x01) << channel; // 選擇通道
P1SFS0 |= temp;
P1SFS1 |= temp; // 設置P1口相應位為ADC輸入
ADCPS =(0x08 + ADC_CLOCK_DIVIDER); // 使能ADC時鐘,設置時鐘頻率
_nop_();
ACON = 0x20; // 允許ADC轉換
// NOTE: USER CODE MUST WAIT AT LEAST 20 MS before calling ADC_Read (Using the ADC)
}
/******************************************************************************
* uint ADC_Read(channel)
*功能描述:讀A/D轉換后的數據
*參 數:channel,初始化程序中選擇的通道
*返 回 值:ADC轉換結果,12位
*注 意:該子程序調用前,ADC_Init()必須先被調用
*******************************************************************************/
unsigned int ADC_Read( unsigned char channel )
{
unsigned int temp_ADC_result;
ACON &= 0xE0; //清除輸入通路 ~(00101110B) = (11010001B)
ACON |= (channel<<2); //選擇通路
_nop_ ();
_nop_ ();
ACON |= 0x02; //開始ADC轉換
_nop_ (); //延時一個機器周期: ADST: 1->0
while( (ACON & 0x01) != 1 ); //等待轉換結束
// 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轉換值
*參 數:channel,選擇讀取通道
*返 回 值:uint ADC轉換結果
*****************************************************/
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; //小數點后1位
vv[0] = temp/100; // 個位
printstr(0,0,str_v);
printchar(5,1,vv[0]);
printchar(6,1,0x2e);
printchar(7,1,vv[0]);
printchar(8,1,vv[0]);
}
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -