static void time_tick(unsigned long data)
{
static int i=0;
switch(status)
{
case off: break;
case clockwise:
if(++i==num){
i=0;
if( row == 4 ) row = 0;
(*(char *)0xd3000006)=pulse_table[row++];
}
ttimer.expires=jiffies+1;
add_timer(&ttimer);
break;
case anticlockwise:
if(++i==num){
i=0;
if( row == -1 ) row = 3;
(*(char *)0xd3000006)=pulse_table[row--];
}
ttimer.expires=jiffies+1;
add_timer(&ttimer);
break;
case default: break;
}
}
在time_tick()函數中判斷步進電機的狀態,是停止、正轉還是反轉。若是正轉,則按正向順序發送脈沖,并添加定時器ttimer;若是反轉,則按反向順序發送脈沖,并添加定時器ttimer;若是停止則不再發送脈沖,也不再添加定時器。
在stepper_module_init()函數中申請I/O端口,并初始化定時器ttimer:
if(check_region(0x28000006, 1)) //看該I/O端口是否已經被占用
{
printk("The stepper port is used by another module.\n");
return -1;
}
request_region(0x28000006, 1, DEVICE_NAME); //申請該I/O端口
init_timer(&ttimer); //初始化定時器ttimer
ttimer.function=time_tick; //填寫定時器處理函數為time_tick()
編寫ioctl函數用來接收應用程序對于步進電機的控制。
int device_ioctl( struct inode *inode, struct file *file, unsigned int ioctl_num,
unsigned long ioctl_param)
{
struct stepper * s;
/* 根據實際程序中的不同需求更改ioctl函數的調用*/
switch (ioctl_num)
{
case IOCTL_SET_MSG:
s = (struct stepper*) ioctl_param;
switch (s->CmdID)
{
case 0: /*開始*/
status=clockwise;
ttimer.expires=jiffies+1; //開啟定時器
add_timer(&ttimer);
break;
case 1: status=off; break; /*停止*/
case 2: /*反轉*/
if(status==clockwise){ status=anticlockwise; }
if(status==anticlockwise){ status=clockwise; }
break;
case 3: if(num!=1)num--; break; /*加速*/
case 4: num++; break; /*減速*/
}
}
return 0;
};
通過s指針得到stepper結構中的表示命令類型的參數,根據該參數判斷命令類型,0是start起動,1是stop停止,2是reverse反向,3是up電機加速,4是down電機減速,通過改變全局變量num和status來控制電機。電機的起動是通過在start分支中起動一個定時器ttimer,然后在定時器處理函數time_tick中發送步進電機脈沖,并重新添加定時器,從而實現步進電機的轉動。
?
電子發燒友App












評論