|
dspf2812實現電機檢測霍爾傳感器,實現自動加速和正反轉
HW1_2019217472_P1.jpg (151.67 KB, 下載次數: 97)
下載附件
2022-4-20 23:44 上傳
HW1_2019217472_P2.jpg (160.38 KB, 下載次數: 94)
下載附件
2022-4-20 23:44 上傳
- #include "DSP281x_Device.h" // DSP281x Headerfile Include File
- #include "DSP281x_Examples.h" // DSP281x Examples Include File
- //GPIOA的宏定義
- //GPIO0-5輸出模式 123
- #define PA0 GpioDataRegs.GPADAT.bit.GPIOA0
- #define PA1 GpioDataRegs.GPADAT.bit.GPIOA1
- #define PA2 GpioDataRegs.GPADAT.bit.GPIOA2
- #define PA3 GpioDataRegs.GPADAT.bit.GPIOA3
- #define PA4 GpioDataRegs.GPADAT.bit.GPIOA4
- #define PA5 GpioDataRegs.GPADAT.bit.GPIOA5
- //GPIO8-10輸入模式
- #define PA8 GpioDataRegs.GPADAT.bit.GPIOA8
- #define PA9 GpioDataRegs.GPADAT.bit.GPIOA9
- #define PA10 GpioDataRegs.GPADAT.bit.GPIOA10
- //函數聲明
- interrupt void cpu_timer0_isr(void); //中斷函數的聲明
- void gpio_init(); //GPIO配置函數聲明
- void fuzhi(int a,int b,int c,int d,int e,int f); //給GPIOA0-5賦值函數聲明
- void mode(int a); //狀態選擇和GPIOA8-10讀取輸出
- int driver_state=1; //中斷查詢確定在狀態多少,driver_state取值1-6個狀態
- int rotor_code; //存放GPIOA8-10的輸出狀態
- void main(void) //主函數
- {
- InitSysCtrl();//初始化系統
- gpio_init(); //GPIOA初始化
- DINT; //關中斷
- IER = 0x0000;
- IFR = 0x0000;
- InitPieCtrl();//初始化PIE
- InitPieVectTable();//初始化PIE中斷矢量表
- EALLOW;
- PieVectTable.TINT0 = & cpu_timer0_isr; //定時器0位中斷0
- EDIS;
- InitCpuTimers(); // cpu初始化
- //定時器0的中斷時間設置 150*1000000/150MHZ=1s
- ConfigCpuTimer(&CpuTimer0, 150, 1000000);
- StartCpuTimer0(); //啟動定時器0
- IER |= M_INT1; //啟動cpu給定時器時鐘
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1; // 中斷0在向量表中的1組第7個
- EINT; // 總中斷 INTM 使能
- ERTM; // 使能總實時中斷 DBGM
- for(;;); //一直等待中斷來
- }
- interrupt void cpu_timer0_isr(void) //中斷服務函數
- {
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //0x0001賦給12組中斷ACKnowledge寄存器,對其全部清除,不接受其他中斷
- CpuTimer0Regs.TCR.bit.TIF=1; // 定時到了指定時間,標志位置位,清除標志
- CpuTimer0Regs.TCR.bit.TRB=1; // 重載Timer0的定時數據
- if(driver_state >= 6)
- driver_state=0; //狀態達到最大值6后復位0
- mode(driver_state); //根據狀態值,選擇模式
- driver_state++;
- }
- //模式選擇和讀取GPIO8-10的值
- void mode(int a) //給 GPIOA 0 2 4 1 3 5賦值
- {
- switch(a)
- {
- case 1:
- fuzhi(1,0,0,1,1,0); //1,0,0,1,1,0
- break; //狀態1
- case 2:
- fuzhi(1,0,0,1,0,1); //1,0,0,1,0,1
- break; //狀態2
- case 3:
- fuzhi(0,1,0,0,1,1); //0,1,0,0,1,1
- break; //狀態3
- case 4:
- fuzhi(0,1,0,1,1,0); //0,1,0,1,1,0
- break; //狀態4
- case 5:
- fuzhi(0,0,1,1,0,1); //0,0,1,1,0,1
- break; //狀態5
- case 6:
- fuzhi(0,0,1,0,1,1); //0,0,1,0,1,1
- break; //轉態6
- default:
- fuzhi(1,0,0,1,1,0);//1,0,0,1,1,0
- break; //默認狀態1
- }
- //為了求取rotor_code的值,把GPIOA8-10設置有權重
- int sum=PA10*4+PA9*2+PA8*1;
- if(sum==0)
- rotor_code=0;
- if(sum==1)
- rotor_code=1;
- if(sum==2)
- rotor_code=10;
- if(sum==3)
- rotor_code=11;
- if(sum==4)
- rotor_code=100;
- if(sum==5)
- rotor_code=101;
- if(sum==6)
- rotor_code=110;
- if(sum==7)
- rotor_code=111;
- }
- //給GPIOA0-5賦值
- void fuzhi(int a,int b,int c,int d,int e,int f)
- {
- PA0=a;
- PA2=b;
- PA4=c;
- PA1=d;
- PA3=e;
- PA5=f;
- }
- //GPIOA的端口模式選擇
- void gpio_init()
- {//設置GPIOA0-5是輸出模式 GPIOA8-10是輸入模式
- EALLOW;
- GpioMuxRegs.GPADIR.bit.GPIOA0 = 1; // GPIOA0設置為輸出
- GpioMuxRegs.GPADIR.bit.GPIOA1 = 1;
- GpioMuxRegs.GPADIR.bit.GPIOA2 = 1;
- GpioMuxRegs.GPADIR.bit.GPIOA3 = 1;
- GpioMuxRegs.GPADIR.bit.GPIOA4 = 1;
- GpioMuxRegs.GPADIR.bit.GPIOA5 = 1;// GPIOA5設置為輸出
- GpioMuxRegs.GPADIR.bit.GPIOA8 = 0; //GPIOA8設置為輸入
- GpioMuxRegs.GPADIR.bit.GPIOA9 = 0; // 設置為輸入
- GpioMuxRegs.GPADIR.bit.GPIOA10 = 0; // GPIOA10 設置為輸入
- }
- //===========================================================================
- // No more.
- //===========================================================================
復制代碼
|
評分
-
查看全部評分
|