《2022年调试通过的TMSF_AD转换程序 .pdf》由会员分享,可在线阅读,更多相关《2022年调试通过的TMSF_AD转换程序 .pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、TMS320F2812 AD转换程序 (已调试通过 ) /* = File name: F28XADC04U.C Originator: Digital Control Systems Group Texas Instruments Description: This file contains source for the F28X general purpose 4 conversions ADC driver for unipolar signals Joyshang = History: - 27-02-2012 Release Rev 1.0 -*/ #include IQmathL
2、ib.h #include DSP28_Device.h #include f28xadc04u.h #include f28xbmsk.h #define CPU_CLOCK_SPEED 6.6667L / for a 150MHz CPU clock speed #define ADC_usDELAY 5000L #define DELAY_US(A) DSP28x_usDelay(long double) A * 1000.0L) / (long double)CPU_CLOCK_SPEED) - 9.0L) / 5.0L) extern void DSP28x_usDelay(unsi
3、gned long Count); void F28X_adc04u_drv_init(ADCVALS *p) DELAY_US(ADC_usDELAY); AdcRegs.ADCTRL1.all = ADC_RESET_FLAG; /* Reset the ADC Module */ asm( NOP ); asm( NOP ); AdcRegs.ADCTRL3.bit.ADCBGRFDN = 0 x3; /* Power up bandgap/reference circuitry */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - -
4、- - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - DELAY_US(ADC_usDELAY); /* Delay before powering up rest of ADC */ AdcRegs.ADCTRL3.bit.ADCPWDN = 1; /* Power up rest of ADC */ AdcRegs.ADCTRL3.bit.ADCCLKPS = 6; /ADCCLK=HSPCLK/(ADCCTRL17+1)/* Set up ADCTRL3 register */ DELAY_US(ADC_usDELAY); Ad
5、cRegs.ADCTRL1.all = 0 x0110; /* Set up ADCTRL1 register 0 x0110 级联模式*/ AdcRegs.ADCTRL2.all = 0 x0100; /* Set up ADCTRL2 register 0 x0100 EVA启动转换*/ AdcRegs.ADCMAXCONV.bit.MAX_CONV = 3; /* Specify four conversions */ AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0 x0; /IU AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0 x4; /
6、 地AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0 x8; /IV AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0 xC; /VREF1.5V EvaRegs.EXTCON.bit.EVSOCE = 1; /Enable SOC EvaRegs.GPTCONA.bit.T1TOADC = 1; /* Set up EV Trigger with Timer1 UF */ unsigned int tempz1; _iq tempz2; _iq tempz3; _iq tempz4; _iq tempz5; void F28X_adc04u_drv
7、_read(ADCVALS *p) /int offset; /long tmp; /* Wait until ADC conversion is completed */ while (AdcRegs.ADCST.bit.SEQ1_BSY = 1) ; /offset = (int)(p-c1_out - 32767); tempz3 = AdcRegs.ADCRESULT0-AdcRegs.ADCRESULT1; /IU- 地名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页
8、,共 6 页 - - - - - - - - - tempz4 = AdcRegs.ADCRESULT3-AdcRegs.ADCRESULT1; /VREF1.5V-地tempz5 = AdcRegs.ADCRESULT3-tempz4; /IU-tempz4 tempz2 = (AdcRegs.ADCRESULT1 + 0 x8000);/*Ibase=4,IQ14*/ tempz2 = _IQmpy(_IQ(20),tempz2); tempz1 = (int)(tempz2 & 0 x8000)15); if (tempz1=1) p-c1_out = (long)( tempz2 +
9、0 xFFFF0000); p-c1_out = _IQmpy(_IQ(32),p-c1_out); else p-c1_out = (long)( tempz2 + 0 x00000000); p-c1_out = _IQmpy(_IQ(32),p-c1_out);/IQ20 / tempz3 = AdcRegs.ADCRESULT0; /temp3 = _IQmpy(temp2,_IQ(10);/* IQ15 */ /tempz3 = (long)(AdcRegs.ADCRESULT1 - 0 x5652 + 0 x8022)10; /dat_q15 = (1)&0 x7FFF; /* C
10、onvert result to Q15 (unipolar signal) */ /tmp = (long)p-c1_gain*(long)dat_q15; / p-c1_out =AdcRegs.ADCRESULT0 - offset; /(int)(tmp13); /dat_q15 = (AdcRegs.ADCRESULT11)&0 x7FFF; /* Convert result to Q15 (unipolar signal) */ / tmp = (long)p-c2_gain*(long)dat_q15; / p-c2_out = AdcRegs.ADCRESULT1 - off
11、set; /dat_q15 = (AdcRegs.ADCRESULT21)&0 x7FFF; /* Convert result to Q15 (unipolar signal) */ /tmp = (long)p-c3_gain*(long)dat_q15; / p-c3_out = AdcRegs.ADCRESULT2; AdcRegs.ADCTRL2.all |= 0 x4040; /* reset the seq */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,
12、共 6 页 - - - - - - - - - TMS320F2812的 ADC模块用于采样过程的初始化代码void InitAdc(void) / 初始化 ADC模块 unsigned int i; AdcRegs.ADCTRL1.bit.RESET = 1; /ADC 模块软复位 ,3 个周期后 ,该位自动清0 for(i=0;i AdcRegs.ADCTRL1.bit.SUSMOD = 3; / 仿真挂起模式,0 为忽略仿真挂起AdcRegs.ADCTRL1.bit.ACQ_PS = 1; / 采样保持脉冲个数,即SH为 1+1=2 个 ADCCLK AdcRegs.ADCTRL1.bi
13、t.CPS = 0; / 是否再对送给ADC的时钟进行二分频AdcRegs.ADCTRL1.bit.CONT_RUN = 0; /1: 进入连续运行; 0:开始停止模式AdcRegs.ADCTRL1.bit.SEQ_CASC = 0; /0:独立模式 ; 1:级联序列器模式AdcRegs.ADCTRL3.bit.ADCBGRFDN = 3; / 带隙参考电路上电for(i=0;i AdcRegs.ADCTRL3.bit.ADCPWDN = 1; / 除带隙参考电路外的其他ADC模块上电for(i=0;i AdcRegs.ADCTRL3.bit.ADCCLKPS = 3; /设置ADC的转换时钟
14、ADCCLK = HSPCLK/(2*(ADCCLKPS+CPS)=25MHZ AdcRegs.ADCTRL3.bit.SMODE_SEL = 0; /0:顺序采用模式,1:同步采样模式AdcRegs.MAX_CONV.all = 0 x0000; /设置 ADC的转换通道数AdcRegs.CHSELSEQ1.bit.CONV00 = 0; / 设置 ADCINA0为 SEQ1的第一个转换通道AdcRegs.ADC_ST_FLAG.bit.INT_SEQ1_CLR=1; /清除 SEQ1中断标志AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1; /使能 EVASOC 启
15、动 SEQ1 AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; / 使能 SEQ1中断AdcRegs.ADCTRL2.bit.EVB_SOC_SEQ = 0; AdcRegs.ADCTRL2.bit.RST_SEQ1 = 0; AdcRegs.ADCTRL2.bit.INT_MOD_SEQ1 = 0; AdcRegs.ADCTRL2.bit.EXT_SOC_SEQ1 = 0; AdcRegs.ADCTRL2.bit.RST_SEQ2=0; AdcRegs.ADCTRL2.bit.SOC_SEQ2=0; AdcRegs.ADCTRL2.bit.INT_ENA_SEQ2=
16、0; AdcRegs.ADCTRL2.bit.INT_MOD_SEQ2=0; AdcRegs.ADCTRL2.bit.EVB_SOC_SEQ2=0; AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 0; / 为 1 时,软件启动ADC转换 void InitEv(void) EvaRegs.T1CMPR = 0 x0000; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - EvaRegs.T1PR = 75; /
17、2MHZ 的采样频率EvaRegs.GPTCONA.bit.T1TOADC = 2; /使能事情管理器A,周期中断启动ADC EvaRegs.T1CON.all = 0 x1042; interrupt void ADInterrupt(void) IFR = 0 x0000; /- ADTempADTempIndex = (unsigned char)(AdcRegs.RESULT0)8); ADTempIndex = (ADTempIndex+1)&0 x01; Value = ADTemp0 + ADTemp1; if(+aa 1000000) aa = 0; LED3 = LED3;
18、/- AdcRegs.ADC_ST_FLAG.bit.INT_SEQ1_CLR = 1; /清除 INT_SEQ1中断标志AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; /复位 SEQ1序列PieCtrl.PIEACK.all = 0 xFFFF; / 允许再次响应中断 void main(void) InitSysCtrl(); / 初始化系统DINT; /屏蔽全局中断IER = 0 x0000; /禁止所有中断IFR = 0 x0000; /清除所有中断标志InitPieCtrl(); / 初始化 PIE中断InitPieVectTable(); / 初始化 PIE中断
19、矢量表InitGpio(); / 初始化通用IO 口InitSci(); / 初始化 SCIA寄存器 ,即初始化串口EALLOW; /允许操作寄存器PieVectTable.ADCINT = &ADInterrupt; / 设置 ADC中断入口地址EDIS; /禁止操作寄存器InitAdc(); / 初始化 ADC 寄存器IER |= M_INT1; / 使能 CPU中断 1,即第一组PIE中断,共12 组 PIE中断InitEv(); / 初始化事件触发器A,用于定时采样名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - EINT; /使能全局中断ERTM; /Enable Global realtime interrupt DBGM while(1) SendToPC(ASCIIBuffer,100,'U'); Delay(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -
限制150内