⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dac.#1

📁 Cygnal公司的著名产品C8051f系列单片机
💻 #1
字号:
/////////DAC调试程序/////////////////
/////////T2定时更新DAC1输出/////////////////////
////////DAC0为直接更新///////////////////////////
/////////DAC.c////////////////////////////////////////
#include "c8051F040.h"
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
sfr16 RCAP2=0xca;
sfr16 RCAP3=0xca;
sfr16 RCAP4=0xca;
sfr16 TMR4=0xcc;
sfr16 TMR3=0xcc;
sfr16 TMR2=0xcc;
sfr16 PCA0CP0=0xfb;
sfr16 PCA0CP1=0xfd;
sfr16 PCA0CP2=0xe9;
sfr16 PCA0CP3=0xeb;
sfr16 PCA0CP4=0xed;
sfr16 PCA0CP5=0xe1;
sfr16 ADC0VAL=0xbe;
sfr16 ADC0GT=0xc4;
sfr16 ADC0LT=0xc6;
sfr16 DAC1VAL=0xD2;
sfr16 DAC0VAL=0xD2;
union tcfint16{ 
	uint myword;
	struct{uchar hi;uchar low;}bytes;
}myint16;//用联合体定义16位操作
void dac0_cfg(uchar cfg);
void dac1_cfg(uchar cfg);
void dac0_updata(uint val);
void dac1_updata(uint val);
void config();
void t2_ini();
void t2_baud(uint t2reload);
void t2_ini(){
	SFRPAGE = 0x00;
	TMR2CF = 0x08;  // T2时钟为系统时钟
	TMR2CN = 0x00;  // 开启T2定时器
	ET2=1;//开启T2定时器中断
}
void t2_baud(uint t2reload){
	SFRPAGE = 0x00;
	RCAP2=65536-t2reload;
	TMR2=RCAP2;
}
void dac0_cfg(uchar cfg){
	SFRPAGE=0x00;
	DAC0CN=cfg;
	REF0CN|=0x03;//DAC参考电平只能由VREFD输入
}
void dac1_cfg(uchar cfg){
	SFRPAGE=0x01;
	DAC0CN=cfg;
	SFRPAGE=0x00;
	REF0CN|=0x03;
}
void dac0_updata(uint val){
	SFRPAGE=0x00;
	myint16.myword=val;//利用联合体进行快速uint型数据操作
	DAC0L=myint16.bytes.low;
	DAC0H=myint16.bytes.hi;//在直接更新模式下,此时开始更新DAC0输出
}
void dac1_updata(uint val){
	SFRPAGE=0x01;
	myint16.myword=val;//利用联合体进行快速uint型数据操作
	DAC1L=myint16.bytes.low;
	DAC1H=myint16.bytes.hi;//在直接更新模式下,此时开始更新DAC1输出
}	
void config (void) {
    WDTCN = 0x07;	// Watchdog Timer Control Register
    WDTCN = 0xDE;   // Disable WDT    
    WDTCN = 0xAD;
    SFRPAGE = 0x0F;
    XBR0 = 0x00;	// XBAR0: Initial Reset Value
    XBR1 = 0x00;	// XBAR1: Initial Reset Value
    XBR2 = 0x40;	// XBAR2: Initial Reset Value
    XBR3 = 0x00;    // XBAR3: Initial Reset Value
    SFRPAGE = 0x0F;
    P0MDOUT = 0x00; // Output configuration for P0 
    P1MDOUT = 0x00; // Output configuration for P1 
    P2MDOUT = 0x00; // Output configuration for P2 
    P3MDOUT = 0x00; // Output configuration for P3 
    P4MDOUT = 0xff; // Output configuration for P4
    P5MDOUT = 0x07; // Output configuration for P5
    P6MDOUT = 0x00; // Output configuration for P6
    P7MDOUT = 0x00; // Output configuration for P7
    P1MDIN = 0xFF;  // Input configuration for P1
    P2MDIN = 0xFF;  // Input configuration for P2
    P3MDIN = 0xFF;  // Input configuration for P3
    SFRPAGE = 0x0F;
    CLKSEL = 0x00;  // Oscillator Clock Selector
    OSCXCN = 0x00;	// EXTERNAL Oscillator Control Register	
    OSCICN = 0x84;	// Internal Oscillator Control Register
    //采用内部晶振,为24.5MHZ8分频
}   //End of config
void main(){
	uchar i;
	config();
	dac0_cfg(0x80);//dac0使能,且为立即更新方式
	dac1_cfg(0x98);//dac1使能,且为t2溢出更新方式
	i=0;
	t2_ini();
	t2_baud(500);//每隔500 T2时钟更新一次DAC1输出,此时T2尚未启动
	EA=1;
	dac0_updata(0x0800);
	
	/*
	DAC0输出为0.5VREFD,若VREFD与ADC0参考电压相同,则可作为HVREF参考电压输入,使得HVDA差动检测负压
	*/
	
	SFRPAGE=0x00;
	TR2=1;//启动定时器T2
	while(1);
}
void T2_ISR() interrupt 5{//T2定时中断,更新DAC1输出,DAC1为锯齿波输出
	TF2=0;
	SFRPAGE=0x01;
	DAC1VAL++;//因为是T2溢出更新DAC1输出,所以可以sfr16操作,因为此时并不立即更新
	if(DAC1VAL>=0x1000)
		DAC1VAL=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -