#include "C8051F020.h" //包含C8051F020硬件信息的头文件
#include "absacc.h" //包含绝对地址访问信息的头文件
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit CS=P2^0;
sbit INC=P2^1;
sbit UD=P2^2;
///////////////C8051F的各种初始化/////////////////
//////////////////////////////////////////////////
void Reset_Sources_Init() //禁止看门
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void Port_IO_Init() //交叉开关配置
{
P74OUT = 0x30; //p7推挽
P2MDOUT = 0xff; //p2推挽
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x40; //交叉开关使能
}
void Oscillator_Init() //振荡器初始化,使用片外晶体振荡器
{
uint i = 0;
OSCXCN = 0x67; //使用片外晶体谐振器,f>6.7MHz
for (i = 0; i < 3000; i++); //Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0); //检测外部振荡器是否有效
OSCICN = 0x08; //选择外部振荡器作为系统时钟,禁止内部时钟
}
void Init_Device() // 设备初始化函数,在主程序中调用Init_Device()
{
Reset_Sources_Init(); //复位源初始化
Port_IO_Init(); //端口初始化
Oscillator_Init(); //时钟源初始化
}
//////////////延时us子程序//////////////////////
////////////////////////////////////////////////
void Delay(unsigned int t)
{
unsigned int i;
for(i=0;i<t;i++) ;
}
////////////数字电位器向上调一步/////////////////
/////////////////////////////////////////////////
void X9C103_UP_N_Step(unsigned char N)
{
unsigned char i=0;
CS=0;
Delay(3);
UD=1;
for(i=N;i>0;i--)
{
INC=1;
Delay(2);
INC=0;
Delay(600); // 延时大约500us, Rw等待输出稳定
}
INC=1;//保存模式
CS=1;
}
///////////// 数字电位器向下调一步///////////////////////
/////////////////////////////////////////////////////////
void X9C103_DOWN_N_Step(unsigned char N)
{
unsigned char i=0;
CS=0;
Delay(3);
UD=0;
for(i=N;i>0;i--)
{
INC=1;
Delay(2);
INC=0;
Delay(600); // 延时大约500us, Rw等待输出稳定
}
INC=1;//保存模式
CS=1;
}
void main()
{
Init_Device();
X9C103_DOWN_N_Step(10);
X9C103_UP_N_Step(10);
}