访问手机版页面
你的位置:老古开发网 > STC单片机的C语言编程 > 正文  
STC单片机AD和EEPROM的程序设计C代码
内容导读:
/*----------------------------------------------------------------*File Name: stc_AD.c -*Description:A/D转换程序*Project: -*MCU type: STC12C5410AD - -*Company: WY -*Compiler: KEIL C51 -*D

/*----------------------------------------------------------------
*File Name: stc_AD.c -
*Description:A/D转换程序
*Project: -
*MCU type: STC12C5410AD -
-
*Company: WY -
*Compiler: KEIL C51 -
*DESINER: 郭准 06.2.7 -
-----------------------------------------------------------------------*/

#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转换结果。前是通道号;后是转换的值

//定义引用外部
extern void Delay(uint number);//晶振=11059200,机器周期=1.085069444us,"加"的机器周期=1
extern void send_char_com(uchar ch);
extern void send_string_com(uchar *str,uchar strlen);
void Ad_Change(uchar channel);


//------------------------------------------------------
//功能:A/D转换
//入口:channel = 通道号 .0:0通道;1:1通道。。。。。。。
//出口:AD_channel_1_result: 10位的数据,16进制。
//设计:郭准,伟业,2006/2/7
//------------------------------------------------------
void Ad_Change(uchar channel)
{
uint AD_Result_Temp = 0 ;
//---------------------将P1.0--P1.1设置成适合AD转换的模式
/// P1 = 0xff; //将P1口置高,为A/D转换作准备
ADC_CONTR = ADC_CONTR|0x80; //1000,0000打开A/D转换电源
P1M0 = 0x03; //0000,0011用于A/D转换的P1.x口,先设为开漏
P1M1 = 0x03; //0000,0011P1.0--P1.1先设为开漏。断开内部上拉电阻
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); //设置当前通道号

Delay(1); //延时使输入电压达到稳定
ADC_DATA = 0; //清A/D转换结果寄存器
ADC_LOW2 = 0;

ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,启动转换

do { ; }
while((ADC_CONTR & 0x10)==0); //0001,0000等待A/D转换结束


ADC_CONTR = ADC_CONTR&0xE7; //1110,0111清ADC_FLAG位,停止A/D转换

AD_Result_Temp = ((AD_Result_Temp|ADC_DATA)<<2)|(ADC_LOW2&0x03);
//保存返回AD转换的 结果
//----------------------------转换成可由串口显示的字符
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;


//------------------------串口监视
// send_char_com(ADC_DATA); //////发送转换 的 到的 值,这里只是 高8位,值的转换需要考虑
// send_char_com(ADC_LOW2); //////发送转换 的 到的 值,这里只是 低2位,值的转换需要考虑

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: 郭准 06.2.7 -
-----------------------------------------------------------------------*/

#include //定义的 系统头文件和全局变量

/*IAP有关功能寄存器*/
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 存储的起始地址
#define DATA_FLASH_START_ADDRESS 0x2800 //stc12c2052ad ////////////???????????

uchar tx_buf[3] = {0,0,0};

extern void Delay(uint number);//晶振=11059200,机器周期=1.085069444us,"加"的机器周期=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; //开始工作
Delay(2); //22us..原13us
// SP = 0xE0; //堆栈指针指向0E0H单元
}
*/
//------------------------------------------------------
//功能:读一字节;调用前需打开IAP功能
//入口:uint address=页地址0~512,为了提高处理速度,最好用0~256的范围
//出口:
//设计:郭准,伟业,2006/2/7
//------------------------------------------------------
uchar Byte_Read(uint address)
{
uchar data ch;

ISP_CONTR = ENABLE_ISP; //打开IAP功能,设置Flash操作等待时间
ISP_CMD = 0x01; //选择读AP模式
//--------------------------
address = DATA_FLASH_START_ADDRESS+address;
ISP_ADDRH = (uchar)(address>>8); //填页地址
ISP_ADDRL = (uchar)(address); //填页地址

标签: ad,eeprom,stc,单片机,
来源:互联网 作者:karen 时间:2018/7/2 10:00:01
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐