****************************** Copyright(C) CaKe ************************************
========================================================================================
==============**版權(quán)信息 : CaKen工作室 ==============
==============**文件名 : main.c ==============
==============**作者 : CaKen ==============
==============**版本號(hào) : V1.0 ==============
==============**時(shí)間 : 2014.08.30 ==============
==============**功能描述 : ADC實(shí)驗(yàn)測(cè)試 ==============
========================================================================================
==============**新浪微博 :CaKen ==============
****************************************************************************************
#include"STC12C5A60S2.H"
#include"intrins.h"
sbit SMG1 = P2^4; //數(shù)碼管1
sbit SMG2 = P2^5; //數(shù)碼管2
sbit SMG3 = P2^6; //數(shù)碼管3
sbit SMG4 = P2^7; //數(shù)碼管4
unsigned char code Tab[]=
{0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e}; //陽
****************************************************************************************
函數(shù)名:毫秒級(jí)CPU延時(shí)函數(shù)
調(diào) 用:DELAY_MS (?);
參 數(shù):1~65535(參數(shù)不可為0)
返回值:無
結(jié) 果:占用CPU方式延時(shí)與參數(shù)數(shù)值相同的毫秒時(shí)間
備 注:應(yīng)用于1T單片機(jī)時(shí)i<600,應(yīng)用于12T單片機(jī)時(shí)i<125
****************************************************************************************/
void DELAY_MS (unsigned int a)
{
unsigned int i;
while( --a != 0){
for(i = 0; i < 600; i++);
}
}
***************************************************************************************
函數(shù)名:延時(shí)程序
調(diào) 用:delay();
參 數(shù):
返回值:
結(jié) 果:
備 注: 0.1ms延時(shí)程序(12MHz 10倍于51單片機(jī)速度時(shí))
****************************************************************************************/
void delay (unsigned int a)
{
unsigned char i;
while( --a != 0)
{
for(i = 0; i < 12; i++);
}
}
****************************************************************************************
** 函數(shù)名: Display_smg(?);
** 說明 : 數(shù)碼管顯示程序
** 輸入值:
** 輸出值: 無
** 返回值: 無
*****************************************************************************************/
void Display_smg(unsigned int x)
{
P0 = Tab[x/1000];
SMG1 = 0;
delay(2);
SMG1 = 1;
P0 = Tab[x00/100];
SMG2 = 0;
delay(2);
SMG2 = 1;
P0 = Tab[x0/10];
SMG3 = 0;
delay(2);
SMG3 = 1;
P0 = Tab[x];
SMG4 = 0;
delay(2);
SMG4 = 1;
}
*********************************************************************************************
函數(shù)名:10位A/D轉(zhuǎn)換初始化函數(shù)
調(diào) 用:ADC_Init (?);
參 數(shù):輸入的端口(0000 0XXX 其中XXX是設(shè)置輸入端口號(hào),可用十進(jìn)制0~7表示,0表示P1.0,7表示P1.7)
返回值:無
結(jié) 果:開啟ADC功能并設(shè)置ADC的輸入端口
備 注:使用ADC功能時(shí)需要將對(duì)應(yīng)的IO接口設(shè)置為高阻輸入方式(例如:P1M1 = 0x01;)
**********************************************************************************************/
void ADC_Init (unsigned char CHA)
{
unsigned char AD_FIN=0; //存儲(chǔ)A/D轉(zhuǎn)換標(biāo)志
CHA &= 0x07; //選擇ADC的8個(gè)接口中的一個(gè)(0000 0111 清0高5位)
ADC_CONTR = 0x40; //ADC轉(zhuǎn)換的速度(0XX0 0000 其中XX控制速度,請(qǐng)根據(jù)數(shù)據(jù)手冊(cè)設(shè)置)
_nop_();
ADC_CONTR |= CHA; //選擇A/D當(dāng)前通道
_nop_();
ADC_CONTR |= 0x80; //啟動(dòng)A/D電源
DELAY_MS(1); //使輸入電壓達(dá)到穩(wěn)定(1ms即可)
}
*********************************************************************************************
函數(shù)名:10位A/D轉(zhuǎn)換函數(shù)
調(diào) 用:? = ADC_Read();
參 數(shù):無
返回值:10位ADC數(shù)據(jù)高(從0到1023(十進(jìn)制))
結(jié) 果:讀出指定ADC接口的A/D轉(zhuǎn)換值,并返回?cái)?shù)值
備 注:適用于STC12C5A60S2系列單片機(jī)(必須使用STC12C5A60S2.H頭文件)
**********************************************************************************************/
unsigned int ADC_Read (void)
{
unsigned char AD_FIN=0; //存儲(chǔ)A/D轉(zhuǎn)換標(biāo)志
ADC_CONTR |= 0x08; //啟動(dòng)A/D轉(zhuǎn)換(0000 1000 令A(yù)DCS = 1)
_nop_();
_nop_();
_nop_();
_nop_();
while (AD_FIN ==0) //等待A/D轉(zhuǎn)換結(jié)束
{
AD_FIN = (ADC_CONTR & 0x10); //0001 0000測(cè)試A/D轉(zhuǎn)換結(jié)束否
}
ADC_CONTR &= 0xE7; //1111 0111 清ADC_FLAG位, 關(guān)閉A/D轉(zhuǎn)換,
return (ADC_RES*4+ADC_RESL); //返回A/D轉(zhuǎn)換結(jié)果(10位ADC數(shù)據(jù)高8位在ADC_RES中,低2位在ADC_RESL中)
}
********************************************************************************************
函數(shù)名:主函數(shù)
調(diào) 用:無
參 數(shù):無
返回值:無
結(jié) 果:程序開始處,無限循環(huán)
備 注:
********************************************************************************************/
void main(void)
{
unsigned int m;
ADC_Init (0); //初始化P1^0
P1M1 = 0X01; //設(shè)置為高阻輸入P1^0
P1M0 = 0X00;
while(1)
{
m = ADC_Read(); //把數(shù)模轉(zhuǎn)換的數(shù)值給m
Display_smg(m); //數(shù)碼管顯示值的大小
}
}