C语言驱动程序 3.docx

上传人:b****0 文档编号:9847842 上传时间:2023-05-21 格式:DOCX 页数:8 大小:15.53KB
下载 相关 举报
C语言驱动程序 3.docx_第1页
第1页 / 共8页
C语言驱动程序 3.docx_第2页
第2页 / 共8页
C语言驱动程序 3.docx_第3页
第3页 / 共8页
C语言驱动程序 3.docx_第4页
第4页 / 共8页
C语言驱动程序 3.docx_第5页
第5页 / 共8页
C语言驱动程序 3.docx_第6页
第6页 / 共8页
C语言驱动程序 3.docx_第7页
第7页 / 共8页
C语言驱动程序 3.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言驱动程序 3.docx

《C语言驱动程序 3.docx》由会员分享,可在线阅读,更多相关《C语言驱动程序 3.docx(8页珍藏版)》请在冰点文库上搜索。

C语言驱动程序 3.docx

C语言驱动程序3

#include

#defineuintunsignedint

#defineucharunsignedchar

#definems*77

//f=12M

#defineLEDLen4

#defineDj_star(){IE=0x81;pri_dj=0;}

#defineDj_stop(){IE=0x00;pri_dj=1;P1=0xff;shache="0";delay(800ms);delay(800ms);delay(400ms);shache=1;}

#defineChilun_Num8

/*齿轮数8个*/

#defineset_display_num(){LEDBuf[0]=tmp/1000;LEDBuf[1]=tmp/100%10;\

LEDBuf[2]=tmp/10%10;LEDBuf[3]=tmp%10;}

ucharLEDBuf[LEDLen]={0,0,0,0};

voidread_num();/*读播码盘到set_round_num*8*/

voiddisplay();

voiddelay(uintdelay_time){uinti;for(i=0;i

voidrun();

voidfx_run();

uintround_num=0;/*记录已转的齿轮数,中断1次加1*/

uintset_round_num=0;/*播码盘设置圈数*/

uintset_pwm_width=0;/*播码盘设置步进电机正向速度*/

bitone_round_flg=0;

sbitled_1000=P0^7;//usefordisplay

sbitled_100=P0^6;//usefordisplay

sbitled_10=P0^5;//usefordisplay

sbitled_1=P0^4;//usefordisplay

sbitkey_start=P3^0;

sbitkey_puse=P3^0;

sbitkey_clear=P3^1;

/*P3^2接齿轮传感器中断*/

sbitbujin_zx_stop=P3^3;/*接步进电机,正向到位传感器,为0停机*/

sbitbujin_fx_stop=P3^4;/*接步进电机,反向到位传感器,为0停机*/

sbitshache=P3^5;/*接刹车控制继电器0电位有效*/

sbitpri_dj=P3^6;/*接主电机控制继电器0电位有效*/

voidmain(){

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();

}

}

}

voidrun(){

#defineDelay_time180

/*转一圈50次循环,每循环4步,50*4=200,200*1。

8=360*/

uchari;

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_dj;i++){

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=0xff;i=0;one_round_flg=0;while(!

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);

//nextpreekey

while(!

shache);

while

(1){

while(key_puse&key_clear);

delay(8ms);

if(!

key_clear){round_num=0;display();}

if(!

key_puse)break;

}

while(!

key_puse);

delay(8ms);

while(!

key_puse);

}

}

}

}

voidext_int0(void)interrupt0{/*主电机齿轮中断*/

uinttmp;

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;

}

voiddisplay(){

uchari;

uinttmp=0;

tmp=round_num/Chilun_Num;

set_display_num();

for(i=0;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;

}

voidread_num(){

/*读播码盘到set_round_num,set_pwm_width*/

uchartmp;

P2=0xFF;

P2=0xEF;//11101111

delay(1ms);

tmp=~(P2|0xF0);

P2=0xDF;//11011111

delay(1ms);

tmp=(~(P2|0xF0))*10+tmp;

set_round_num=tmp;

P2=0xBF;//10111111

delay(1ms);

tmp=(~(P2|0xF0));

P2=0x7F;//01111111

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;//01111111

delay(1ms);

tmp=~(P2|0xF0);

P1=0xFF;

P2=0xFF;

P1&=0x7F;//10111111

delay(1ms);

tmp=(~(P2|0xF0))*10+tmp;

set_pwm_width=tmp;

P1=0xFF;

P2=0xFF;

}

voidfx_run(){

#definef_Delay_time180

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;

}

 

参考链接:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2