|
飛思卡爾單片機(jī)AD采集初始化函數(shù),各頻率PWM輸出設(shè)置,延時(shí)函數(shù)。
- void SecondDelay(unsigned int x) //define delay time program about xs
- {
- unsigned char i,j,k,m;
- while(x--)
- {
- for(i=0;i<10;i++)
- for(j=0;j<12;j++)
- for(k=0;k<100;k++)
- for(m=0;m<100;m++)
- {;}
- }
- }
- void mSecondDelay(unsigned int x) //define delay time program about xs
- {
- unsigned char i,j,k,m;
- while(x--)
- {
- for(i=0;i<10;i++)
- for(j=0;j<12;j++)
- for(k=0;k<10;k++)
- for(m=0;m<1;m++)
- {;}
- }
- }
- void ATD0Initial(void)
- {
-
- ATD0CTL0=0x00; /*多通道轉(zhuǎn)換時(shí),確定ANO轉(zhuǎn)換完成后的下一個(gè)要轉(zhuǎn)換的通道號*/
- /*ATD Control Register 0
- * 0b00000000
- * ||||||||__ WRAP0
- * |||||||___ WRAP1
- * ||||||____ WRAP2
- * |||||_____ 保留
- */
- ATD0CTL1=0x87; /*設(shè)不使用外部觸發(fā)*/
- /* ATD Control Register 1
- *
- * 0b10000111
- * ||||||||__ ETRIGCH2
- * |||||||___ ETRIGCH2
- * ||||||____ ETRIGCH2
- * |||||_____|
- * ||||______|
- * |||_______|保留
- * ||________|
- * |_________ ETRIGSEL(0-開放;1-禁止)
- */
- ATD0CTL2=0x80; /*設(shè)8通道ATD0模塊為正常工作模式,使能A/D,正常清除標(biāo)志,不使用外部觸發(fā),禁止中斷*/
- /* ATD Control Register 2
- *
- * 0b10000000
- * ||||||||__ ATD Sequence Complete Interrupt Flag
- * |||||||___ ATD Sequence Complete Interrupt Enable
- * ||||||____ External Trigger Mode Enable
- * |||||_____ External Trigger Polarity
- * ||||______ External Trigger Level/Edge Control
- * |||_______ ATD Power Down inWait Mode
- * ||________ ATD Fast Flag Clear All(0 ATD flag clearing operates normally)
- * |_________ ATD Power Up(0 Power down ATD;1 Normal ATD functionality)
- */
- ATD0CTL3=0x08; /*隊(duì)列長度為1,即只轉(zhuǎn)換1個(gè)通道*/
- /* ATD Control Register 3
- *
- * 0b00001000
- * ||||||||__ FRZ0
- * |||||||___ FRZ1
- * ||||||____ FIFO
- * |||||_____ S1C
- * ||||______ S2C
- * |||_______ S4C
- * ||________ S8C
- * |_________ 保留
- */
-
- ATD0CTL4=0x21; /*10位精度,A/D轉(zhuǎn)換的第二階段為4個(gè)A/D轉(zhuǎn)換周期,4分頻*/
- /* ATD Control Register 4
- *
- * 0b00100000
- * ||||||||__ PRS0
- * |||||||___ PRS1
- * ||||||____ PRS2
- * |||||_____ PRS3
- * ||||______ PRS4
- * |||_______ SMP0
- * ||________ SMP1
- * |_________ SRES8(0-10位精度;1-8位精度)
- */
-
- ATD0CTL5=0x80; /*右對齊,無符號,單通道轉(zhuǎn)換模式,選擇模擬通道0*/
- /* ATD Control Register 5
- *
- * 0b10000000
- * ||||||||__ CA采樣通道0
- * |||||||___ CB
- * ||||||____ CC
- * |||||_____ 保留
- * ||||______ MULT(0-單通道采樣;1-多通道采樣)
- * |||_______ SCAN(0-單次轉(zhuǎn)換隊(duì)列;1-連續(xù)轉(zhuǎn)換隊(duì)列)
- * ||________ DSGN(0-無符號;1-有符號)
- * |_________ DJM(0-左對齊;1-右對齊)
- */
- ATD0DIEN=0x00; /*數(shù)字輸入允許寄存器,禁止數(shù)字輸入*/
-
- }
- void ATD1Initial(void)
- {
- /*AD轉(zhuǎn)換器2的設(shè)置*/
-
- ATD1CTL0=0x00; /*多通道轉(zhuǎn)換時(shí),確定ANO轉(zhuǎn)換完成后的下一個(gè)要轉(zhuǎn)換的通道號*/
- /*ATD Control Register 0
- * 0b00000000
- * ||||||||__ WRAP0
- * |||||||___ WRAP1
- * ||||||____ WRAP2
- * |||||_____ WRAP3
- * ||||______ 保留
- */
- ATD1CTL1=0x87; /*外部觸發(fā)源設(shè)置寄存器,在此設(shè)不使用外部觸發(fā)*/
- /* ATD Control Register 1
- *
- * 0b10000111
- * ||||||||__ ETRIGCH2
- * |||||||___ ETRIGCH2
- * ||||||____ ETRIGCH2
- * |||||_____|
- * ||||______|
- * |||_______|保留
- * ||________|
- * |_________ ETRIGSEL(0-開放外部觸發(fā);1-禁止)
- */
-
- ATD1CTL2=0x80; /*設(shè)16通道ATD1模塊為使能A/D,正常清除標(biāo)志,不使用外部觸發(fā)*/
- /* ATD Control Register 2
- *
- * 0b10000000
- * ||||||||__ ATD Sequence Complete Interrupt Flag(1-有序列轉(zhuǎn)換完成中斷發(fā)生,0-無)
- * |||||||___ ATD Sequence Complete Interrupt Enable(1-使能序列轉(zhuǎn)換完成中斷)
- * ||||||____ External Trigger Mode Enable(0-禁止外部觸發(fā);1-使能外部觸發(fā))
- * |||||_____ External Trigger Polarity
- * ||||______ ETRIGLE(External Trigger Level/Edge Control)外部觸發(fā)邊沿控制
- * |||_______ AWAI(ATD Power Down inWait Mode)0-在等待模式正常運(yùn)行,1-省電模式
- * ||________ AFFC(ATD Fast Flag Clear All)(0 ATD flag clearing operates normally)
- * |_________ ADPU(ATD Power Up)(0 Power down ATD;1 Normal ATD functionality)
- */
- ATD1CTL3=0x08; /*探制轉(zhuǎn)換序列的長度,此處設(shè)隊(duì)列長度為1,即只轉(zhuǎn)換一個(gè)通道,最大長度為16*/
- /* ATD Control Register 3
- *
- * 0b00001000
- * ||||||||__ FRZ0
- * |||||||___ FRZ1
- * ||||||____ FIFO
- * |||||_____ S1C|1
- * ||||______ S2C|0
- * |||_______ S4C|0
- * ||________ S8C|0長度為1
- * |_________ 保留
- */
- ATD1CTL4=0x21; /*設(shè)置轉(zhuǎn)換精度,采樣 時(shí)間,ATD時(shí)鐘分頻,在此設(shè)10位精度,A/D轉(zhuǎn)換的第二階段為4個(gè)A/D轉(zhuǎn)換周期,4分頻*/
- /* ATD Control Register 4
- *
- * 0b00100000
- * ||||||||__ PRS0
- * |||||||___ PRS1
- * ||||||____ PRS2
- * |||||_____ PRS3
- * ||||______ PRS4 00000即2分頻
- * |||_______ SMP0=1
- * ||________ SMP1=0 4個(gè)ATD時(shí)鐘周期
- * |_________ SRES8(0-10位精度;1-8位精度)
- */
- ATD1CTL5=0x80; /*設(shè)置數(shù)據(jù)格式,掃瞄方式,采樣方式,采樣通道,在此設(shè)右對齊,無符號,單次轉(zhuǎn)換隊(duì)列模式,單通道采樣,選擇模擬通道0*/
- /* ATD Control Register 5
- *
- * 0b10000000
- * ||||||||__ CA=0
- * |||||||___ CB=0
- * ||||||____ CC=0
- * |||||_____ CD=0 采樣通道0
- * ||||______ MULT(0-單通道采樣;1-多通道采樣)
- * |||_______ SCAN(0-單次轉(zhuǎn)換隊(duì)列;1-連續(xù)轉(zhuǎn)換隊(duì)列)
- * ||________ DSGN(0-無符號;1-有符號)
- * |_________ DJM(0-左對齊;1-右對齊)
- */
- ATD1DIEN0=0x00; /*數(shù)字輸入允許寄存器,禁止數(shù)字輸入*/
- ATD1DIEN1=0x00; /*數(shù)字輸入允許寄存器,禁止數(shù)字輸入*/
-
- }
- unsigned int ATD0Sample(unsigned char ADChannel)
- {
- ATD0CTL5=0x80;
- ATD0CTL5+=ADChannel; /*ADChannel可設(shè)為0-7,則可選擇0-7中的任一通道*/
- while(ATD0STAT0_SCF==0){}; /*等待當(dāng)前隊(duì)列轉(zhuǎn)換完成,當(dāng)轉(zhuǎn)換正在進(jìn)行SCF=0,則為真,一直空循環(huán)*/
- ATD0STAT0_SCF=1; /*當(dāng)置SCF為1時(shí),將使該標(biāo)志位清零*/
- return ATD0DR0;
- }
- unsigned int ATD1Sample(unsigned char ADChannel)
- {
- ATD1CTL5=0x80;
- ATD1CTL5+=ADChannel;
- while(ATD1STAT0_SCF==0){}; /*等待當(dāng)前隊(duì)列轉(zhuǎn)換完成,!邏輯非,當(dāng)轉(zhuǎn)換正在進(jìn)行SCF=0,則為真,一直循環(huán)*/
- ATD1STAT0_SCF=1; /*當(dāng)置SCF為1時(shí),將使該標(biāo)志位清零*/
- return ATD1DR0;
-
- }
- /*平均值濾波函數(shù)*/
- float ResultsMeanAverageFilter(unsigned char SelectATDConverter,unsigned char i)
- {
- unsigned char count,j,k;
- unsigned int valueBuf[N];
- unsigned int temp;
- unsigned int sum=0;
-
- if(SelectATDConverter==0) /*當(dāng)使用轉(zhuǎn)換器0進(jìn)行采樣時(shí)的處理函數(shù)*/
-
- {
-
- for (count=0;count<N;count++)
- {
- valueBuf[count]=ATD0Sample(i);
- }
- for (count=1;count<N-1;count++)
- {
- sum+=valueBuf[count];
- }
- return ((float)sum/(N-2));
-
- }
-
- if(SelectATDConverter==1) /*當(dāng)使用轉(zhuǎn)換器1進(jìn)行采樣時(shí)的處理函數(shù)*/
-
- {
-
- for (count=0;count<N;count++)
- {
- valueBuf[count]=ATD1Sample(i);
- }
- for (count=1;count<N-1;count++)
- {
- sum+=valueBuf[count];
- }
- return ((float)sum/(N-2));
- }
-
- }
-
- /*限幅濾波函數(shù)*/
- unsigned int ResultsLimiteFilter(unsigned char SelectATDConverter,unsigned char i,unsigned char A)
- {
- unsigned int a[2];
- if(SelectATDConverter==0) /*當(dāng)使用轉(zhuǎn)換器0進(jìn)行采樣時(shí)的處理函數(shù)*/
- {
- a[0]=ATD0Sample(i);
- a[1]=ATD0Sample(i);
- if ((a[1]-a[0]>A)||(a[0]-a[1]>A))
- {return a[0];}
- else
- {return a[1];}
- }
-
- if(SelectATDConverter==1) /*當(dāng)使用轉(zhuǎn)換器1進(jìn)行采樣時(shí)的處理函數(shù)*/
- {
- a[0]=ATD1Sample(i);
- a[1]=ATD1Sample(i);
- if ((a[1]-a[0]>A)||(a[0]-a[1]>A))
- {return a[0];}
- else
- {return a[1];}
- }
-
- }
- void PWM_Initial(void)
- {
- PWMPOL=0x00; /*PWM脈寬極性選擇,初始輸出為低電平*/
- PWMCLK=0xff; /*選擇SA,SB時(shí)鐘*/
- PWMPRCLK=0x22; /*A,B時(shí)鐘預(yù)分頻為4總線時(shí)鐘頻率*/
- PWMCAE=0x00; /*PWM波形采用左對齊模式*/
- PWMCTL=0x00; /*設(shè)置通道為獨(dú)立工作模式*/
- PWMSCLA=60; /*A的時(shí)間常數(shù)為60*/
- PWMSCLB=60;
- PWMPER0=100; /*0-7通道250hz*/
- PWMPER1=0x63; /*0x63對應(yīng)數(shù)值為99*/
- PWMPER2=0x63;
- PWMPER3=0x63;
- PWMPER4=0x63;
- PWMPER5=0x63;
- PWMPER6=0x63;
- PWMPER7=0x63;
- PWME=0xff; /*PWM使能寄存器,使能8路PWM控制*/
- PWMDTY0=100; /*各通道占空比初始化為0*/
- PWMDTY1=100;
- PWMDTY2=100;
- PWMDTY3=100;
- PWMDTY4=100;
- PWMDTY5=100;
- PWMDTY6=100;
- PWMDTY7=100;
- }
- void PWM_Initial0_5khz(void)
- {
- PWMPOL=0x00; /*PWM脈寬極性選擇,初始輸出為低電平*/
- PWMCLK=0xff; /*選擇SA,SB時(shí)鐘*/
- PWMPRCLK=0x00; /*A,B時(shí)鐘預(yù)分頻為0總線時(shí)鐘頻率*/
- PWMCAE=0x00; /*PWM波形采用左對齊模式*/
- PWMCTL=0x00; /*設(shè)置通道為獨(dú)立工作模式*/
- PWMSCLA=60; /*A的時(shí)間常數(shù)為60*/
- PWMSCLB=60;
- PWMPER0=100; /*0-7通道1khz*/
- PWMPER1=100; /*0x63對應(yīng)數(shù)值為99*/
- PWMPER2=100;
- PWMPER3=100;
- PWMPER4=100;
- PWMPER5=100;
- PWMPER6=100;
- PWMPER7=100;
- PWME=0xff; /*PWM使能寄存器,使能8路PWM控制*/
- PWMDTY0=100; /*各通道占空比初始化為0*/
- PWMDTY1=100;
- PWMDTY2=100;
- PWMDTY3=100;
- PWMDTY4=100;
- PWMDTY5=100;
- PWMDTY6=100;
- PWMDTY7=100;
- }
- void PWM_Initial2khz(void)
- {
- PWMPOL=0x00; /*PWM脈寬極性選擇,初始輸出為低電平*/
- PWMCLK=0xff; /*選擇SA,SB時(shí)鐘*/
- PWMPRCLK=0x00; /*A,B時(shí)鐘預(yù)分頻為0總線時(shí)鐘頻率*/
- PWMCAE=0x00; /*PWM波形采用左對齊模式*/
- PWMCTL=0x00; /*設(shè)置通道為獨(dú)立工作模式*/
- PWMSCLA=30; /*A的時(shí)間常數(shù)為30*/
- PWMSCLB=30;
- PWMPER0=100; /*0-7通道1khz*/
- PWMPER1=100; /*0x63對應(yīng)數(shù)值為99*/
- PWMPER2=100;
- PWMPER3=100;
- PWMPER4=100;
- PWMPER5=100;
- PWMPER6=100;
- PWMPER7=100;
- PWME=0xff; /*PWM使能寄存器,使能8路PWM控制*/
- PWMDTY0=100; /*各通道占空比初始化為0*/
- PWMDTY1=100;
- PWMDTY2=100;
- PWMDTY3=100;
- PWMDTY4=100;
- PWMDTY5=100;
- PWMDTY6=100;
- PWMDTY7=100;
- }
- void PWM_Initial3khz(void)
- {
- PWMPOL=0x00; /*PWM脈寬極性選擇,初始輸出為低電平*/
- PWMCLK=0xff; /*選擇SA,SB時(shí)鐘*/
- PWMPRCLK=0x00; /*A,B時(shí)鐘預(yù)分頻為0總線時(shí)鐘頻率*/
- PWMCAE=0x00; /*PWM波形采用左對齊模式*/
- PWMCTL=0x00; /*設(shè)置通道為獨(dú)立工作模式*/
- PWMSCLA=20; /*A的時(shí)間常數(shù)為60*/
- PWMSCLB=20;
- PWMPER0=100; /*0-7通道1khz*/
- PWMPER1=100; /*0x63對應(yīng)數(shù)值為99*/
- PWMPER2=100;
- PWMPER3=100;
- PWMPER4=100;
- PWMPER5=100;
- PWMPER6=100;
- PWMPER7=100;
- PWME=0xff; /*PWM使能寄存器,使能8路PWM控制*/
- PWMDTY0=100; /*各通道占空比初始化為0*/
- PWMDTY1=100;
- PWMDTY2=100;
- PWMDTY3=100;
- PWMDTY4=100;
- PWMDTY5=100;
- PWMDTY6=100;
- PWMDTY7=100;
- }
- void PWM_Initial4khz(void)
- {
- PWMPOL=0x00; /*PWM脈寬極性選擇,初始輸出為低電平*/
- PWMCLK=0xff; /*選擇SA,SB時(shí)鐘*/
- PWMPRCLK=0x00; /*A,B時(shí)鐘預(yù)分頻為0總線時(shí)鐘頻率*/
- PWMCAE=0x00; /*PWM波形采用左對齊模式*/
- PWMCTL=0x00; /*設(shè)置通道為獨(dú)立工作模式*/
- PWMSCLA=15; /*A的時(shí)間常數(shù)為60*/
- PWMSCLB=15;
- PWMPER0=100; /*0-7通道1khz*/
- PWMPER1=100; /*0x63對應(yīng)數(shù)值為99*/
- PWMPER2=100;
- PWMPER3=100;
- PWMPER4=100;
- PWMPER5=100;
- PWMPER6=100;
- PWMPER7=100;
- PWME=0xff; /*PWM使能寄存器,使能8路PWM控制*/
- PWMDTY0=100; /*各通道占空比初始化為0*/
- PWMDTY1=100;
- PWMDTY2=100;
- PWMDTY3=100;
- PWMDTY4=100;
- PWMDTY5=100;
- PWMDTY6=100;
- PWMDTY7=100;
- }
復(fù)制代碼
51hei.png (4.47 KB, 下載次數(shù): 46)
下載附件
2021-1-4 14:56 上傳
頭文件51hei下載地址:
SubFunctions.7z
(2.88 KB, 下載次數(shù): 2)
2021-1-4 14:57 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|