/************************************************************************************** 龙丘MC9S12(DG128)多功能开发板 Designed by Chiu Sir E-mail:chiusir@163.com 软件版本:V1.1 最后更新:2008年11月20日 ------------------------------------ Code Warrior 4.7 Target : MC9S12DG128 Crystal: 16.000Mhz busclock: 8.000MHz pllclock:16.000MHz 本程序主要包括以下功能: 1.设置锁相环和总线频率; 2.IO口使用; 3.共四路ATD使用及显示方法。 LED计数,根据灯亮可以读取系统循环了多少次 *****************************************************************************************/ #i nclude <hidef.h> /* common defines and macros */ #i nclude <mc9s12dg128.h> /* derivative information */ #i nclude "LQprintp.h" #pragma LINK_INFO DERIVATIVE "mc9s12dg128b" //----------------------------------------------------- void setbusclock(void) { CLKSEL=0X00; //disengage PLL to system PLLCTL_PLLON=1; //turn on PLL SYNR=1; REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz; _asm(nop); //BUS CLOCK=16M _asm(nop); while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it; CLKSEL_PLLSEL =1; //engage PLL to system; } //----------------------------------------------------- static void SCI_Init(void) { SCI0CR2=0x2c; //enable Receive Full Interrupt,RX enable,Tx enable SCI0BDH=0x00; //出口波特率为9600 SCI0BDL=0x68; //SCI0BDL=busclk/(16*SCI0BDL) //busclk 8MHz, 9600bps,SCI0BDL=0x34 //busclk 16MHz, 9600bps,SCI0BDL=0x68 //busclk 24MHz, 9600bps,SCI0BDL=0x9C } //busclk 32MHz, 9600bps,SCI0BDL=0xD0 //----------------------------------------------------- void AD_Init(void) { ATD0CTL2=0xC0; //AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止 ATD0CTL3=0x20; //每次转换4个序列, No FIFO, Freeze模式下继续转 ATD0CTL4=0x81; //8位精度, 采样时间为2个AD时钟周期,ATDClock=[BusClock*0.5]/[PRS+1] ATD0CTL5=0xB0; //右对齐无符号,,连续转换 ,8轮流通道采样 ATD0DIEN=0x00; //禁止数字输入 } //----------------------------------------------------- void Dly_ms(int ms) { int ii,jj; if (ms<1) ms=1; for(ii=0;ii<ms;ii++) for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms } U8 ReadATD(U8 ch) { U8 ad=0; while(!ATD0STAT0_SCF); switch(ch) { default: case 0: ad= ATD0DR0L; break; case 1: ad= ATD0DR1L; break; case 2: ad= ATD0DR2L; break; case 3: ad= ATD0DR3L; break; case 4: ad= ATD0DR4L; break; case 5: ad= ATD0DR5L; break; case 6: ad= ATD0DR6L; break; case 7: ad= ATD0DR7L; break; } return ad; } void main(void){ unsigned char LedCnt=0; int fenwei=0,gewei=0; setbusclock(); SCI_Init(); AD_Init(); DDRB=0xFF; PUCR_PUPBE=1; EnableInterrupts; for(;;) { LedCnt=(LedCnt>0XFE?0:++LedCnt); Dly_ms(1000); //修改延时以修改数据发送频率 //低电平灯亮用这句,注释掉下面那句 PORTB=~LedCnt; //高电平灯亮用这句,注释掉上面那句 //PORTB=LedCnt; putstr("\nWeb: shop36265907.taobao.com"); fenwei=ReadATD(0) *100/51;//参考电压5V, 500/255 gewei=fenwei/100; fenwei=fenwei%100; printp("\nAD0:%d.%02dV,",gewei,fenwei); fenwei=ReadATD(1) *100/51;//参考电压5V, 500/255 gewei=fenwei/100; fenwei=fenwei%100; printp("\nAD1:%d.%02dV,",gewei,fenwei); fenwei=ReadATD(2) *100/51;//参考电压5V, 500/255 gewei=fenwei/100; fenwei=fenwei%100; printp("\nAD2:%d.%02dV,",gewei,fenwei); fenwei=ReadATD(3) *100/51;//参考电压5V, 500/255 gewei=fenwei/100; fenwei=fenwei%100; printp("\nAD3:%d.%02dV;",gewei,fenwei); } } 下面是完整工程,需要的请下载:demoATD.rar |
欢迎光临 智能车制作 (http://111.231.132.190/) | Powered by Discuz! X3.2 |