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

📄 main.c

📁 Freescale mc9s12dg128 AD 采样程序
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <MC9S12XS128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"

 unsigned char AD[15];   
 
 //LED Display       RTI.h

unsigned char LedCode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char LedData[]={0,0,0,0};            // 数码管显示

unsigned char LedNum = 0;

void InitRTI(void) {
  RTICTL = 0x3F; // 
  CRGINT = 0x80; // enable RTI interrupts  
 }
 
 void interrupt 7 RTI_ISR(void) {                //RTI Interrupt
   
  PORTK = 0x01 << LedNum ;
  if(LedNum==1)
    PORTB = LedCode[LedData[LedNum]] & 0x7f;
  else
    PORTB = LedCode[LedData[LedNum]];
  
  LedNum++;
  if(LedNum >= 4) LedNum = 0;
  
   
  CRGFLG = 0x80;   
}


//--------------------------------------------------------------



 // 总线频率设置    PLL_Init (void)     40M
void  PLL_Init(void)
{
    CLKSEL=0X00;    //disengage PLL to system
    PLLCTL_PLLON=1;   //turn on PLL
    SYNR =0x58;                        
    REFDV=0x09; 
    POSTDIV=0x00;       //pllclock=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
    _asm(nop);          //BUS CLOCK=40M
    _asm(nop);
    while(!(CRGFLG_LOCK==1));   //when pll is steady ,then use it;
    CLKSEL_PLLSEL =1;          //engage PLL to system; 
}
 
 
 //端口初始化   Port_Init (void)   A(Input)  B(Output&display) T(4,5,7)  K(Ledenable) M(enginenable)  P(output PWM)
 
void Port_Init(void)
{
     DDRA=0x00;
     DDRB=0xFF;
     
     DDRK=0x0F;
     PORTK=0xFF;
     DDRP=0xFF;
     DDRM=0x0F;   //engine
     DDRT=0x7F;                    // PT7 作为输入 PT4和PT5作为输出
     PTT=0xFF;       //初始使得PT口输出都为1  //可以使用for循环改变PT4和PT5的输出
     
/*
   for(;;)
   {
       PTT=0x10;
       delay_100ms;      //延时时间待定
       PTT=0x20;
       
   }

*/          
}

 void AD_Init(void) {
         int k=0;
         for(k=0;k<14;k++) {
          AD[k]=0;
         }
         AD[14]=255;  
   
    ATD0CTL2=0xC0;  //AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止
                            
    ATD0CTL3=0x00; // 每个序列8次转换, No FIFO, Freeze模式下继续转huan
                           
    ATD0CTL4=0x69; // 10位精度, 采样时间为2个AD时钟周期, 
                            
                               //ATDClock=[BusClock*0.5]/[PRS+1]  ; PRS=9, divider=12


    ATD0CTL5=0x30;   //右对齐无符号,,连续转换 ,多通道采样,通道零开始


    ATD0DIEN=0x00;   // 禁止数字输入 
 
 
 }
 
 //get AD convert value   GetAD_Values(void)  

 void GetAD_Values(void) {
     while(!ATD0STAT0_SCF);
    AD[0] = ATD0DR0L;
    AD[1] = ATD0DR1L;                     //得到的是对应的十进制数值
    AD[2] = ATD0DR2L;
    AD[3] = ATD0DR3L;
    AD[4] = ATD0DR4L;
    AD[5] = ATD0DR5L;
    AD[6] = ATD0DR6L;
    AD[7] = ATD0DR7L;       
    AD[8] = ATD0DR8L;
    AD[9] = ATD0DR9L;
    AD[10] = ATD0DR10L;
    AD[11] = ATD0DR11L;
    AD[12] = ATD0DR12L;
    AD[13] = ATD0DR13L;
    AD[14] = ATD0DR14L;
    
    LedData[0]=AD[0]/100;       //百位                  //PORTB 显示
    LedData[2]=(AD[0]/10)/10;  //  十位
    LedData[3]=(unsigned long)AD[0]%10;  //个位
    
 }
 

void main(void) {
  /* put your own code here */
  Port_Init();    
   PLL_Init();
   InitRTI();
  EnableInterrupts;
  
   AD_Init();
  for(;;) {
  GetAD_Values();
  } /* wait forever */
  /* please make sure that you never leave this function */
}

⌨️ 快捷键说明

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