利用dsp电机测速及详解

摘要

通过按键实现加速,减速,停止功能,利用中断测速,采用软件延时去抖动,具体实现方法看代码。

程序清单

#pragma CODE_SECTION(vect,"vect")
unsigned int *pmem=0;
#define IMR    *(pmem+0x0000)
#define IFR    *(pmem+0x0001)
#define PMST   *(pmem+0x001d)
#define SWCR   *(pmem+0x002b)
#define SWWSR  *(pmem+0x0028)
#define CLKMD  *(pmem+0x0058)
#define ST1    *(pmem+0x007)
#define PRD0  *(pmem+0x0025)  /* timer0 period register */
#define TCR0  *(pmem+0x0026)  /* timer0 control register */
#define uchar unsigned char
#define ulong unsigned long
#define uint unsigned int
uchar flag=0x0;
uchar flag1=0x0;
uint count=0x0;
uint count1=0x0;
uint k=0x0;
ioport    unsigned portc001; //键盘地址
ioport    unsigned port8003; //液晶屏CS1地址读指令
ioport    unsigned port8004; //液晶屏CS1地址写指令
ioport    unsigned port8005; //液晶屏CS1地址写数据 

ioport    unsigned port8000; //液晶屏CS2地址
ioport    unsigned port8001; //液晶屏CS2地址
ioport    unsigned port8002; //液晶屏CS2地址
#define   cs1    port8003
#define   cs1rd  port8004
#define   cs1wd  port8005
#define   cs2    port8000
#define   cs2rd  port8001
#define   cs2wd  port8002

ioport    unsigned portc005; //直流电机地址
#define   djcs    portc005
static const uchar table4[]={
//;--  0  --  **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x80,0x3F,0x00,0xE0,0xFF,0x01,0xE0,0xFF,0x01,0x70,0x80,0x03,0x10,
0x00,0x02,0x10,0x00,0x02,0x70,0x80,0x03,0xE0,0xFF,0x01,0xE0,0xFF,0x01,0x80,0x3F,
0x00,0x00,0x00,0x00,
//;--  1  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x00,0x00,0xE0,
0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  2  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xC0,0x01,0x03,0xE0,0xC1,0x03,0xF0,0xE1,0x03,0x30,0xF0,0x02,0x10,
0x78,0x02,0x30,0x1C,0x02,0xF0,0x0F,0x02,0xE0,0x07,0x02,0xC0,0x03,0x02,0x00,0x00,
0x00,0x00,0x00,0x00,
///;--  3  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xC0,0xE0,0x00,0xE0,0xE0,0x01,0xF0,0xE0,0x03,0x30,0x04,0x03,0x10,
0x04,0x02,0x30,0x0E,0x03,0xF0,0xFF,0x03,0xE0,0xFB,0x01,0xC0,0xF1,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  4  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x3C,0x00,0x00,0x2E,0x00,0x80,0x27,0x00,0xE0,
0x21,0x00,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x00,0x20,0x00,0x00,0x20,
0x00,0x00,0x00,0x00,
//;--  5  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x00,0xC0,0x00,0xF0,0xC7,0x01,0xF0,0xC7,0x03,0xF0,0x07,0x03,0x10,
0x03,0x02,0x10,0x03,0x03,0x10,0xFF,0x03,0x10,0xFF,0x01,0x10,0xFC,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  6  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x80,0x7F,0x00,0xE0,0xFF,0x01,0xE0,0xFF,0x03,0x70,0x06,0x03,0x10,
0x02,0x02,0x30,0x06,0x03,0x70,0xFE,0x03,0x70,0xFC,0x01,0x60,0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  7  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x10,0x00,0x00,0x10,0x00,0x00,0x10,0x00,0x03,0x10,0xE0,0x03,0x10,
0xFC,0x03,0x90,0xFF,0x00,0xF0,0x1F,0x00,0xF0,0x03,0x00,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  8  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xC0,0xF1,0x00,0xE0,0xFB,0x01,0xF0,0xFB,0x03,0x30,0x0E,0x03,0x10,
0x04,0x02,0x10,0x04,0x02,0x30,0x0E,0x03,0xF0,0xFB,0x03,0xE0,0xFB,0x01,0xC0,0xF1,
0x00,0x00,0x00,0x00,
//;--  9  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xC0,0x87,0x01,0xE0,0x8F,0x03,0xF0,0x9F,0x03,0x30,0x18,0x03,0x10,
0x10,0x02,0x30,0x98,0x03,0xF0,0xFF,0x01,0xE0,0xFF,0x00,0x80,0x3F,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,


//;--  A  --  **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x03,0x00,0xE0,0x03,0x00,0xFC,0x03,0x80,0xFF,0x00,0xF0,0x3F,0x00,0xF0,
0x21,0x00,0xF0,0x3F,0x00,0x80,0xFF,0x00,0x00,0xFC,0x03,0x00,0xE0,0x03,0x00,0x00,
0x03,0x00,0x00,0x00,
//;--  B  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x10,0x04,0x02,0x10,
0x04,0x02,0x10,0x04,0x02,0x30,0x0E,0x03,0xF0,0xFF,0x03,0xE0,0xFB,0x01,0xC0,0xF1,
0x00,0x00,0x00,0x00,
//;--  C  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0x00,0x7F,0x00,0xC0,0xFF,0x01,0xE0,0xFF,0x01,0x70,0x80,0x03,0x10,
0x00,0x02,0x10,0x00,0x02,0x30,0x00,0x03,0xF0,0xF1,0x03,0xE0,0xF1,0x01,0xC0,0x71,
0x00,0x00,0x00,0x00,
//;--  D  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x10,0x00,0x02,0x10,
0x00,0x02,0x30,0x00,0x03,0x70,0x80,0x03,0xE0,0xFF,0x01,0xC0,0xFF,0x00,0x80,0x7F,
0x00,0x00,0x00,0x00,
//;--  E  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x10,0x04,0x02,0x10,
0x04,0x02,0x10,0x04,0x02,0x10,0x04,0x02,0x10,0x04,0x02,0x10,0x00,0x02,0x00,0x00,
0x00,0x00,0x00,0x00,
//;--  F  -- **  黑体, 16  **
//; 当前所选字体下一个汉字对应的点阵为:  宽度x高度=12x21,  调整后为: 12x24
0x00,0x00,0x00,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0xF0,0xFF,0x03,0x10,0x08,0x00,0x10,
0x08,0x00,0x10,0x08,0x00,0x10,0x08,0x00,0x10,0x08,0x00,0x10,0x00,0x00,0x10,0x00,
0x00,0x00,0x00,0x00,
};


void cpu_init()             /*初始化DSP*/
{
   asm(" ssbx intm");		//复位指令 禁止总中断
   asm(" ssbx xf");         //总清零
   PMST=0xe8;		//设置处理器工作模式状态寄存器
   SWWSR=0x7fff;		//设置软件等待状态,确保处理器初始化期间CPU和外部慢速器件正					//常通信
   SWCR=0x0001;		//软件等待控制寄存器,等待状态翻倍
   IMR=0x010c; 	//允许int2-3和tint中断
   IFR=0xffff;  	//清除所有中断
   CLKMD=0x000;		//时钟模式恢复
   asm(" rsbx intm"); 	//开总中断
   asm(" rsbx xf");         //打开数据口
}

void zldj()                //直流电机测试程序
{  TCR0=0x10;
   PRD0=0x99;       
   TCR0=0xc20;     
}

void delay100ms()    		//利用for循环软件延时100ms
{int i,j,k;
for (i=0;i<40;i++)
for (j=0;j<12;j++)
for (k=0;k<20;k++);
}

void delay10ms()		//利用for循环软件延时10ms
{int  i,j,k;
for (i=0;i<2;i++)
for (j=0;j<2;j++)
for (k=0;k<2;k++);
}

void dicenga(void)     /*片选1的查询函*/
{
  delay10ms();
}

void dicengb(void)     /*片选2的查询函数*/
{ 
 delay10ms();
}

void lcdwc1(uchar dat) /*片选1的写控制?*/
{
dicenga();
cs1rd=dat;
delay10ms();
}

void lcdwc2(uchar dat)  /*片选2的写控制字函数*/
{
dicengb();
cs2rd=dat;
delay10ms();
}

void lcdwd1(uchar dat)  /*片选1的写数据函数*/
{
dicenga();
cs1wd=dat;
delay10ms();

}

void lcdwd2(uchar dat)  /*片选2的写数据函数*/
{
dicengb();
cs2wd=dat;
delay10ms();

}

void lcdreset(void)   //液晶屏初始化或者说复位
{uchar dat;
 dat=0x3f;
 lcdwc1(dat);
 lcdwc2(dat);
 dat=0xc0;
 lcdwc1(dat);
 lcdwc2(dat);
}

void lcdwd(uchar dat,uchar ypos,uchar xpos)  //判断其在哪个屏
{
uchar bbb;
bbb=dat;
dat=xpos;
if(dat<64)		
   {
dat=bbb;
 lcdwd1(dat);
    }
else
   {
dat=bbb;
  lcdwd2(dat);
   }
}

void lcdpos(uchar ypos,uchar xpos)	//设置显示位置
{
uchar bbb;
bbb=xpos;
if(bbb<64)
  {
bbb=ypos;
bbb=bbb&0x07;
bbb=bbb+0x0B8;
lcdwc1(bbb);
bbb=xpos;
bbb=bbb&0x3f;
bbb=bbb+0x40;
lcdwc1(bbb);
   }
else
    {
bbb=ypos;
bbb=bbb&0x07;
bbb=bbb+0x0b8;
lcdwc2(bbb);
bbb=xpos;
bbb=bbb&0x3F;
bbb=bbb+0x40;
lcdwc2(bbb);
   }
}

void lcdwrite(uchar dat,uchar ypos,uchar xpos)		//LCD写入函数
{
lcdpos( ypos, xpos);
lcdwd(dat,ypos,xpos);
}

void lcdfill()                      /*液晶屏清屏程序*/
{
uchar dat,ypos, xpos;
dat=0x0;
for(ypos=0;ypos<=7;ypos++)
   {
    for(xpos=0;xpos<=127;xpos++)
       {
     lcdwrite(dat,ypos,xpos);
       }
   }
}


void dataplay(uchar vxpos,uchar vypos,uchar dat2) // 数字显示子程序
{
   uchar dat1,ypos,xpos;                          //vxpos是列的起始地址
                                                  //vypos是行的起始地址  
   ulong x;
   x=dat2*36;                                     //确定查表地址
  for(xpos=vxpos+0;xpos

结果图

利用dsp电机测速及详解_第1张图片
利用dsp电机测速及详解_第2张图片

结束语

希望可以帮助更多人,如有误,请各位帮忙斧正,谢谢!
@2

你可能感兴趣的:(DSP)