#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define ms *77
// f = 12 M
#define LEDLen 4
#define Dj_star() {IE=0x81pri_dj=0}
#define Dj_stop() {IE=0x00pri_dj=1P1=0xffshache="0"delay(800ms)delay(800ms)delay(400ms)shache = 1}
#define Chilun_Num 8
/* 齿轮数 8 个*/
#define set_display_num() { LEDBuf[0] = tmp /余神 1000LEDBuf[1] = tmp / 100 % 10 \
LEDBuf[2] = tmp / 10 % 10 LEDBuf[3] = tmp % 10 }
uchar LEDBuf[LEDLen] = {0,0,0,0}
void read_num () /* 读播码盘 到 set_round_num * 8 */
void display ()
void delay(uint delay_time) { uint ifor (i=0i <delay_time i++) }
void run ()
void fx_run()
uint round_num = 0 /* 记录已转的 齿轮数 , 中断1次 加 1*/
uint set_round_num = 0 /* 播码盘设置 圈数 */
uint set_pwm_width = 0 /* 播码盘设置 步进电机 正向速度 */
bit one_round_flg = 0
sbit led_1000 = P0^7 //use for display
sbit led_100 = P0^6 //use for display
sbit led_10= P0^5 //use for display
sbit led_1 = P0^4 //use for display
sbit key_start = P3^0
sbit key_puse = P3^0
sbit key_clear = P3^1
/* P3^2 接齿轮传感器 中断 */
sbit bujin_zx_stop = P3^3 /* 接步进电机 ,正向逗裂到位传感器 ,为 0 停机 */
sbit bujin_fx_stop = P3^4 /* 接步进电机 ,反向到位传感器 ,为 0 停机 */
sbit shache= P3^5 /* 接刹车控制继电器 0 电位有效 */
sbit pri_dj= P3^6 /* 接主电机控竖指亏制继电器 0 电位有效 */
void main(){
TCON = 0x01
display()
while(1) {
IE="0x00"
round_num = 0
display()
if ( bujin_fx_stop ) fx_run()
while ( key_start )
delay ( 8ms )
if(!key_start){
read_num()
//set_round_num = 8
while ( !key_start )
run ()
fx_run()
}
}
}
void run () {
#define Delay_time 180
/* 转一圈 50 次循环,每循环 4 步 ,50 * 4 = 200 , 200 * 1。8 = 360 */
uchar i
P1 = 0xff
set_pwm_width = 15 + set_pwm_width / 10
while ( 1 ) {
while( !shache | !key_start )
Dj_star()
for ( i="0" bujin_zx_stop &!pri_dji++ ){
P1 = 0xf9
delay ( Delay_time ) // bujin_zx_stop = P3^3
P1 = 0xfc // bujin_fx_stop = P3^4
delay ( Delay_time) // key_puse = P3^0
P1 = 0xf6 // key_clear = P3^1
delay ( Delay_time ) // shache= P3^5
P1 = 0xf3 // pri_dj= P3^6
delay ( Delay_time )
if( i == set_pwm_width ) { P1 = 0xffi = 0one_round_flg = 0while ( !one_round_flg &key_puse )}
if(!key_puse) { delay(4ms) if(!key_puse) break }
}
P1 = 0xff
if ( pri_dj ) break
if ( !key_puse ) {
delay ( 8ms )
if ( !key_puse ) {
Dj_stop()
while ( !key_puse )
// next pree key
while( !shache )
while(1){
while ( key_puse & key_clear )
delay ( 8ms )
if ( !key_clear ) { round_num = 0display()}
if ( !key_puse ) break
}
while( !key_puse )
delay(8ms)
while( !key_puse )
}
}
}
}
void ext_int0(void) interrupt 0 { /* 主电机 齿轮 中断 */
uint tmp
EA = 0
if( !pri_dj ){
round_num ++
if (round_num % Chilun_Num == 0 ){
one_round_flg = 1
tmp = round_num / Chilun_Num
set_display_num()
P0 = 0xf0
P0 = P0 | LEDBuf[0]
led_1000 = 0
P0 |= 0xf0
P0 = 0xf0
P0 = P0 | LEDBuf[1]
led_100 = 0
P0 |= 0xf0
P0 = 0xf0
P0 = P0 | LEDBuf[2]
led_10= 0
P0 |= 0xf0
P0 = 0xf0
P0 = P0 | LEDBuf[3]
led_1 = 0
P0 |= 0xf0
P0 = 0xf0
}
if ( round_num >= set_round_num ) Dj_stop()
}
EA = 0x81
}
void display(){
uchar i
uint tmp = 0
tmp = round_num / Chilun_Num
set_display_num()
for(i = 0i <LEDLen i ++){
P0 = 0xf0
P0 = P0 | LEDBuf[i]
if(i==0) led_1000 = 0 //P0^4
if(i==1) led_100 = 0 //P0^5
if(i==2) led_10= 0 //P0^6
if(i==3) led_1 = 0 //P0^7
P0 |= 0xf0
}
P0 = 0xf0
}
void read_num(){
/* 读播码盘 到 set_round_num ,set_pwm_width */
uchar tmp
P2 = 0xFF
P2 = 0xEF // 1110 1111
delay ( 1ms )
tmp = ~(P2 | 0xF0)
P2 = 0xDF // 1101 1111
delay ( 1ms )
tmp = (~(P2 | 0xF0 )) * 10 + tmp
set_round_num = tmp
P2 = 0xBF // 1011 1111
delay ( 1ms )
tmp = (~(P2 | 0xF0))
P2 = 0x7F // 0111 1111
delay ( 1ms )
tmp = (~(P2 | 0xF0)) * 10 + tmp
set_round_num = set_round_num + tmp * 100
set_round_num = set_round_num * Chilun_Num
P2 = 0xFF
P1 = 0xbF // 0111 1111
delay ( 1ms )
tmp = ~(P2 | 0xF0)
P1 = 0xFF
P2 = 0xFF
P1 &= 0x7F // 1011 1111
delay ( 1ms )
tmp = (~(P2 | 0xF0)) * 10 + tmp
set_pwm_width = tmp
P1 = 0xFF
P2 = 0xFF
}
void fx_run(){
#define f_Delay_time 180
while ( bujin_fx_stop ) { /* 反向 回车 直到 传感器 动作*/
P1 = 0xf3 //0011
delay ( f_Delay_time )
P1 = 0xf6 //0110
delay ( f_Delay_time )
P1 = 0xfc //1100
delay ( f_Delay_time )
P1 = 0xf9 //1001
delay ( f_Delay_time )
}
P1 = 0xff
}
内容:1、本程序用于测试4相步进电机常规驱动2、需要用跳帽或者杜邦线把信号输出端和对应的步进电机信号输入端连接起来
3、速度不可以调节的过快,不然就没有力矩转动了
4、按s4(设睁哪置成独立按键模式)可以控制正反转
------------------------------------------------*/
#include
bit Flag//定义正反转标志位
unsigned char code F_Rotation[4]={0xf1,0xf2,0xf4,0xf8}//正转表格
unsigned char code B_Rotation[4]={0xf8,0xf4,0xf2,0xf1}//反转表格
/******************************************************************/
/坦嫌*延时函数*/
/******************************************************************/
void Delay(unsigned int i)//延时
{
while(--i)
}
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
unsigned char i
EX1=1//外部中断0开
IT1=1//边沿触发
EA=1 //全局中断开
while(!Flag)
{
P0=0x71//显示 F 标示正转
for(i=0i<4i++) //4相
{
P1=F_Rotation[i] //输出对应的相 可以自行换成反转表格
Delay(500) //改变这个参数可以调整电机转速 ,数字越小,转速越大
}
}
while(Flag)
{
P0=0x7C//显示 b 标示反转
for(i=0i<4i++) //4相
{
P1=B_Rotation[i] //输出对应的相让早手
Delay(500) //改变这个参数可以调整电机转速 ,数字越小,转速越大
}
}
}
/******************************************************************/
/* 中断入口函数 */
/******************************************************************/
void ISR_Key(void) interrupt 2 using 1
{
Delay(300)
Flag=!Flag//s3按下触发一次,标志位取反
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)