程序
#include<c8051f120.h>
unsigned char Data[5]={0,0,0,0,0};
unsigned char ADCdata;
bit flaglvbo=0;
sbit P00=P0^0;
int number=0;
int dizhi=0x0000;
sfr16 ADC0 = 0xbe;
sfr16 DAC0 = 0xd2;
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
unsigned char Flash_READ(int Addr_read);
void Flash_ERASE(int Addr_erase);
void Flash_WRITE(int addr_write,char ch);
/*****************看门狗初始化******************/
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;//禁用看门狗
}
/**********时钟初始化************************/
void Oscillator_Init()
{
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01; //外部时钟25M
}
/**************IO口配置********************/
void Port_IO_Init()
{
SFRPAGE = CONFIG_PAGE;
P4MDOUT = 0x00;
P5MDOUT = 0x00;
P3MDOUT = 0xff;
P0MDOUT =0xff;
P1MDOUT = 0xff;
P2MDOUT = 0xff;
P7MDOUT = 0x00;
P0=0x01;
XBR2 = 0x40;
}
/***********************ADC初始化*******************/
void ADC0_Init(void)
{
SFRPAGE = ADC0_PAGE;
AMX0CF = 0x60; //独立输入
ADC0CF = 0x00; //增益为1,频率25M。
AMX0SL = 0x03; //通道4
ADC0CN = 0x8d; //定时器2溢出启动ADC0,右对齐。
REF0CN = 0x03; // 启用内部基准源
EIE2 |= 0x02; // 启用 ADC 中断
}
/*******************DAC初始化***********************/
void DAC_Init()
{
SFRPAGE = DAC0_PAGE;
//REF0CN = 0x03;
DAC0CN = 0x94; //数据输出在高8位和低4位
//定时器4溢出更新
DAC0L=0x00;
DAC0H=0x00;
}
/**************定时器2初始化**********************/
void Timer2_Init()
{
SFRPAGE = TMR2_PAGE;
TMR2CF = 0x0A;
RCAP2L = 0xCA; //采样8KHz。
RCAP2H = 0xF3;
TMR2CN = 0x80; //输出4KHz方波
ET2=1;
TR2=1;
}
/******************定时器4初始化******************/
void Timer4_Init()
{
SFRPAGE = TMR4_PAGE;
TMR4CF = 0x08; // T4M = '1'; Timer4 counts SYSCLKs
TMR4CN = 0x00; // STOP timer; set to auto-reload mode
RCAP4L = 0xCA;
RCAP4H = 0xF3;
//T4 = RCAP4;
EIE2 |= 0x04; // enable Timer4 interrupts
TMR4CN |= 0x04; // start Timer4
}
/************************************************/
void delay(int z)
{
while(z--);
//_nop_;
}
/****************************************************************************************************************
*****************************************************************************************************************
**********************************************主函数*************************************************************
*****************************************************************************************************************
****************************************************************************************************************/
void main()
{
Reset_Sources_Init();
Oscillator_Init();
Port_IO_Init();
EA=1;
delay(1000);
P00=1;
while(1)
{
if(flaglvbo==0)
{
TR4=0;
Timer2_Init();
ADC0_Init();
}
else
{
TR2=0;
Timer4_Init();
DAC_Init();
number=0;
}
}
}
void ADC0_ISR (void) interrupt 15
{
SFRPAGE = ADC0_PAGE;
AD0INT = 0; // 清 ADC 中断标志位
ADCdata= ADC0H; // ADC采样数据,八位。
// ADCflag=1;
number++;
P00=~P00;
if(number<65536)
{
Flash_WRITE(dizhi,ADCdata);
dizhi++;
}
else
{
flaglvbo=1;
dizhi=0;
number=0;
}
}
void Timer4_ISR (void) interrupt 16 using 3
{
unsigned char DACdata;
TMR4CN &= ~0x80; // 清T4计数溢出标致位
SFRPAGE=DAC0_PAGE;
if(dizhi<65536)
{
DACdata=Flash_READ(dizhi);
dizhi++;
}
else
{
flaglvbo=0;
dizhi=0;
}
DAC0H=DACdata;
P00=~P00;
}
/*****************************************flash读写程序块******************************************************************/
unsigned char Flash_READ(int Addr_read)
{
unsigned char Data;
unsigned char code *pread; /*程序存储器空间的指针(Flash),指向待读地址 */
PSBANK=0x11; //选择块1
FLSCL=0xC0;
pread=Addr_read;
Data=*pread;
FLSCL=0x00;
return Data;
}
void Flash_ERASE(int Addr_erase) /*程序擦除Addr_erase块,每块1024个字节*/
{
unsigned char xdata *perase;
EA=0;
PSBANK=0x11; //选择块1,
FLSCL =0x01;
PSCTL =0x03;
perase=Addr_erase;
*perase=0;
PSCTL &=0x00;
FLSCL &=0x00;
EA=1;
}
void Flash_WRITE(int addr_write,unsigned char ch)
{
unsigned char xdata *pwrite;
if(addr_write%1024==0)
{
Flash_ERASE(addr_write);
}
/*程序存储器空间的指针(Flash),指向待写地址*/
EA=0; /*关中断*/
PSBANK=0x11; //选择块1
CCH0CN &=0xFE;
FLSCL=0X01; /*允许对Flash进行写*/
PSCTL=0X01;
pwrite=addr_write; /*指向Flash 的首地址*/
*pwrite=ch; /*将数组值写入Flash中*/
FLSCL=0X00; /*禁止对Flash的写操作*/
PSCTL=0X00;
EA=1; /*开中断*/
} |