📄 config.#1
字号:
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "c8051F020.h"
#include <math.h>
#include <absacc.h>
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Reset_Sources_Init()
{
WDTCN = 0xFF;
}
void Timer_Init()
{
TMOD = 0x10;
T2CON = 0x04;
RCAP2L = 0xAF;
RCAP2H = 0x3C;
TL2 = 0xAF;
TH2 = 0x3C;
}
void ADC_Init()
{
ADC0CN = 0xCC;
ADC0LTH = 0x08;
ADC0LTL = 0x59;
ADC0GTH = 0x07;
ADC0GTL = 0x90;
}
void DAC_Init()
{
DAC0CN = 0x80;
}
void Comparator_Init()
{
int i = 0;
CPT0CN = 0x85;
for (i = 0; i < 60; i++); // Wait 20us for initialization
CPT0CN &= ~0x30;
}
void Voltage_Reference_Init()
{
REF0CN = 0x03;
}
void Port_IO_Init()
{
P0MDOUT = 0xFF;
P1MDOUT = 0xFF;
P2MDOUT = 0xFF;
P3MDOUT = 0xFF;
XBR2 = 0x40;
}
void Oscillator_Init()
{
int i = 0;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x0C;
}
void Interrupts_Init()
{
IE = 0xA0;
IP = 0x20;
EIE1 = 0x04;
EIE2 = 0x02;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Oscillator_Init();
Reset_Sources_Init();
Timer_Init();
ADC_Init();
DAC_Init();
Comparator_Init();
Voltage_Reference_Init();
Port_IO_Init();
Interrupts_Init();
}
void timer2int(void);
void adc0int(void);
void adc0wint(void);
void bootdsp(void);
void wait(void);
sbit hint=P0^0;
sbit hwr=P0^1;
sbit hrdy=P0^2;
sbit hcnt0=P0^3;
sbit hcnt1=P0^4;
sbit hcs=P0^5;
sbit hds2=P0^6;
sbit hds1=P0^7;
sbit has=P3^0;
sbit txen=P3^3;
sbit rxen=P3^4;
sbit bf1=P3^5;
sbit bf2=P3^6;
sbit bf3=P3^7;
unsigned int flag1=0;
unsigned int flag2=0;
unsigned int flag3=0;
unsigned int i,j,a;
void main (void) {
WDTCN = 0xde; //禁止看门狗
WDTCN = 0xad;
Init_Device(); //初始化片内设备
has=1;
hcs=1;
hds2=1;
hint=1;
a=0;
wait();
while(bf2==1);
while(hrdy==0);
bootdsp(); //DSP加载
WDTCN = 0x7f;
bf1=0;
txen=1;
rxen=0;
while (1)
{WDTCN = 0xa5;
}
}
void timer2int(void) interrupt 5 //定时器2溢出中断
{
TF2=0;
WDTCN = 0xa5;
}
void adc0int(void) interrupt 15 //ADC采集完毕中断
{
a=ADC0H;
a=(a<<8)+ADC0L;
if(flag2==0)
{flag1=0;
flag1=0;}
else if(flag2>=16)
{ if((flag2-flag1)<1)
txen=0;
else txen=1;
flag1=0;
flag2=0;
flag3=0;
}
if((a<0x790)||(a>0x859))
flag3++;
if(flag3>=2)
{
txen=1;}
flag2++;
AD0INT=0;
WDTCN = 0xa5;
}
void adc0wint(void) interrupt 8 //ADC窗口比较器中断
{
flag1++;
AD0WINT=0;
WDTCN = 0xa5;
}
void wait() //延时
{for(j=0;j<10;j++)
{WDTCN = 0xa5;}}
void bootdsp(void) //DSP加载程序
{
hwr=1;
has=1;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
hcs=0; //设置加载程序区首地址
hwr=0;
hcnt0=0;
hcnt1=1;
P1=0x01;
P2=0x00;
has=0;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
for(i=8192;i<24320;i++) //自动地址增加方式加载程序,DSP程序在片内0X4000开始的空间
{
hwr=0;
hcnt0=1;
hcnt1=0;
P1=CBYTE[i*2];
P2=CBYTE[i*2+1];
has=1;
hds1=0;
has=1;
hwr=0;
has=1;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
}
hcs=0; //设置启动入口暂存区字地址1
hcs=0;
hwr=0;
hcnt0=0;
hcnt1=1;
P1=0x00;
P2=0x61;
has=0;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
hwr=0; //写入程序入口字节地址低16位
hcnt0=1;
hcnt1=1;
P1=0x2b;
P2=0xde;
has=1;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
hwr=0; //设置启动入口暂存区字地址2
hcnt0=0;
hcnt1=1;
P1=0x00;
P2=0x60;
has=0;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
hwr=0; //写入程序入口字节地址高8位并指示可以运行
hcnt0=1;
hcnt1=1;
P1=0xff;
P2=0x00;
has=1;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
wait();
hwr=0; //清除复位标志
hcnt0=0;
hcnt1=0;
P1=0x00;
P2=0x01;
has=0;
hds1=0;
has=1;
hwr=0;
hds1=1;
while(hrdy==0)
{WDTCN = 0xa5;}
hwr=1;
hcs=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -