STC單片機(jī)AD和EEPROM的驅(qū)動(dòng)C程序設(shè)計(jì)與編寫(xiě)
stc單片機(jī)具有在應(yīng)用編程,調(diào)試起來(lái)比較方便;帶有10位AD;內(nèi)部eeprom;可在1T/機(jī)器周期下工作,速度是傳統(tǒng)51單片機(jī)的12倍;下面是我寫(xiě)的AD和EEPROM的驅(qū)動(dòng)C代碼:
/*----------------------------------------------------------------
*File Name: stc_AD.c -
*Description:A/D轉(zhuǎn)換程序
*Project: -
*MCU type: STC12C5410AD -
-
*Company: WY -
*Compiler: KEIL C51 -
-----------------------------------------------------------------------*/
#include
/*A/D SFR*/
sfr ADC_LOW2 = 0xBE;
sfr ADC_CONTR = 0xC5;
sfr ADC_DATA = 0xC6;
sfr CLK_DIV = 0xC7; ////////
//定義變量
uchar code display_AD_channel_ID[2] = {0x00,0x01};
uchar data AD_channel_result[2][5]; //各通道A/D轉(zhuǎn)換結(jié)果。前是通道號(hào);后是轉(zhuǎn)換的值
//定義引用外部
extern void Delay(uint number);//晶振=11059200,機(jī)器周期=1.085069444us,"加"的機(jī)器周期=1
extern void send_char_com(uchar ch);
extern void send_string_com(uchar *str,uchar strlen);
void Ad_Change(uchar channel);
//------------------------------------------------------
//功能:A/D轉(zhuǎn)換
//入口:channel = 通道號(hào) .0:0通道;1:1通道。。。。。。。
//出口:AD_channel_1_result: 10位的數(shù)據(jù),16進(jìn)制。
//設(shè)計(jì):郭準(zhǔn),偉業(yè),2006/2/7
//------------------------------------------------------
void Ad_Change(uchar channel)
{
uint AD_Result_Temp = 0 ;
//---------------------將P1.0--P1.1設(shè)置成適合AD轉(zhuǎn)換的模式
/// P1 = 0xff; //將P1口置高,為A/D轉(zhuǎn)換作準(zhǔn)備
ADC_CONTR = ADC_CONTR|0x80; //1000,0000打開(kāi)A/D轉(zhuǎn)換電源
P1M0 = 0x03; //0000,0011用于A/D轉(zhuǎn)換的P1.x口,先設(shè)為開(kāi)漏
P1M1 = 0x03; //0000,0011P1.0--P1.1先設(shè)為開(kāi)漏。斷開(kāi)內(nèi)部上拉電阻
Delay(20); //20
ADC_CONTR = ADC_CONTR&0xE0; //1110,0000 清ADC_FLAG,ADC_START位和低3位
ADC_CONTR = ADC_CONTR|(display_AD_channel_ID[channel]&0x07); //設(shè)置當(dāng)前通道號(hào)
Delay(1); //延時(shí)使輸入電壓達(dá)到穩(wěn)定
ADC_DATA = 0; //清A/D轉(zhuǎn)換結(jié)果寄存器
ADC_LOW2 = 0;
ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,啟動(dòng)轉(zhuǎn)換
do { ; }
while((ADC_CONTR & 0x10)==0); //0001,0000等待A/D轉(zhuǎn)換結(jié)束
ADC_CONTR = ADC_CONTR&0xE7; //1110,0111清ADC_FLAG位,停止A/D轉(zhuǎn)換
AD_Result_Temp = ((AD_Result_Temp|ADC_DATA)<<2)|(ADC_LOW2&0x03);
//保存返回AD轉(zhuǎn)換的 結(jié)果
//----------------------------轉(zhuǎn)換成可由串口顯示的字符
AD_channel_result[channel][0] = AD_Result_Temp/1000+0x30;
AD_channel_result[channel][1] = (AD_Result_Temp%1000)/100+0x30;
AD_channel_result[channel][2] = (AD_Result_Temp%100)/10+0x30;
AD_channel_result[channel][3] = AD_Result_Temp%10+0x30;
//------------------------串口監(jiān)視
// send_char_com(ADC_DATA); //////發(fā)送轉(zhuǎn)換 的 到的 值,這里只是 高8位,值的轉(zhuǎn)換需要考慮
// send_char_com(ADC_LOW2); //////發(fā)送轉(zhuǎn)換 的 到的 值,這里只是 低2位,值的轉(zhuǎn)換需要考慮
send_string_com(AD_channel_result[channel],4);
Delay(1); //
}
/*----------------------------------------------------------------
*File Name: STC_EEPROM.c -
*Description: IAP/ISP 功能 -
*Project: -
*MCU type: STC12C5410AD -
-
*Company: WY -
*Compiler: KEIL C51 -
*DESINER: 郭準(zhǔn) 06.2.7 -
-----------------------------------------------------------------------*/
#include
/*IAP有關(guān)功能寄存器*/
sfr ISP_DATA = 0xE2;
sfr ISP_ADDRH = 0xE3;
sfr ISP_ADDRL = 0xE4;
sfr ISP_CMD = 0xE5;
sfr ISP_TRIG = 0xE6;
sfr ISP_CONTR = 0xE7;
//----------------------------定義常量
#define ENABLE_ISP 0x82 //<20MHz
//#define ENABLE_ISP 0x83 //<12MHz
#define DEBUG_DATA 0x5A
//----------------------------flash 存儲(chǔ)的起始地址
#define DATA_FLASH_START_ADDRESS 0x2800 //stc12c2052ad ////////////???????????
uchar tx_buf[3] = {0,0,0};
extern void Delay(uint number);//晶振=11059200,機(jī)器周期=1.085069444us,"加"的機(jī)器周期=1
extern void send_char_com(uchar ch);
extern void send_string_com(uchar *str,uchar strlen);
uchar Byte_Read(uint address);
void Sector_Erase(uint address);
void Byte_Program(uint address,uchar ch);
/*
void Eeprom_Start(void)
{
P1 = 0xf0; //開(kāi)始工作
Delay(2); //22us..原13us
// SP = 0xE0; //堆棧指針指向0E0H單元
}
*/
//------------------------------------------------------
//功能:讀一字節(jié);調(diào)用前需打開(kāi)IAP功能
//入口:uint address=頁(yè)地址0~512,為了提高處理速度,最好用0~256的范圍
//出口:
//設(shè)計(jì):郭準(zhǔn),偉業(yè),2006/2/7
//------------------------------------------------------
uchar Byte_Read(uint address)
{
uchar data ch;
ISP_CONTR = ENABLE_ISP; //打開(kāi)IAP功能,設(shè)置Flash操作等待時(shí)間
ISP_CMD = 0x01; //選擇讀AP模式
//--------------------------
address = DATA_FLASH_START_ADDRESS+address;
ISP_ADDRH = (uchar)(address>>8); //填頁(yè)地址
ISP_ADDRL = (uchar)(address); //填頁(yè)地址
EA