|
dspf2812实现电机检测霍尔传感器,实现自动加速和正反转
- #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.
- //===========================================================================
复制代码
|
评分
-
查看全部评分
|