C语言程序源代码.docx

上传人:b****6 文档编号:5577081 上传时间:2022-12-28 格式:DOCX 页数:12 大小:21.38KB
下载 相关 举报
C语言程序源代码.docx_第1页
第1页 / 共12页
C语言程序源代码.docx_第2页
第2页 / 共12页
C语言程序源代码.docx_第3页
第3页 / 共12页
C语言程序源代码.docx_第4页
第4页 / 共12页
C语言程序源代码.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

C语言程序源代码.docx

《C语言程序源代码.docx》由会员分享,可在线阅读,更多相关《C语言程序源代码.docx(12页珍藏版)》请在冰豆网上搜索。

C语言程序源代码.docx

C语言程序源代码

C语言程序源代码

C语言程序源代码

#include//51寄存器定义

#include"intrins.h"

#definecontrolP1//P1_0:

A相,P1_1:

B相,P1_2:

C相,P1_3:

D相

#definediscodeP0//显示代码控制端口

#defineucharunsignedchar//定义无符号型变量

#defineuintunsignedint

sbiten_dm=P3^0;//显示代码锁存控制

sbiten_wk=P3^1;//位控锁存控制

ucharcodecorotation[4]={0x03,0x06,0x0c,0x09};//电机正转

ucharcoderollback[4]={0x0c,0x06,0x03,0x09};//电机反转

ucharcodetab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//显示字段

uintcodeLevspeed[5]={500,400,300,200,100};//电机速度等级

ucharHscan,speedcount;//Hscan行扫描,speedcount速度等级计数

uintuu;//频率计数

uintstep,setstep;//step:

电机步伐计数,setstep:

手动设置电机步伐

uintspeed=500;//电机初始速度

ucharcount;

ucharflag[5];

ucharbutcount;//按键次数

//****************************************

//flag[0]正转标志

//flag[1]反转标志

//flag[2]加速标志

//flag[3]减速标志

//flag[4]设置标志

//****************************************

Delay1mS(unsignedinttt)//延时1ms“Delay1mS”延时子程序,用循环语句延时。

{

unsignedchari;

while(tt--)

for(i=113;i>0;i--);

}

keyscan()//键盘扫描“keyscan”键盘扫描子程序,用于寻找是否有按键按下。

{

P2=0xf0;//把oxfe赋值给P2口//将按键控制口置于未按键的状态.单片机输出//口假设不是按键按下状态.

if(P2!

=0xf0)//如果P2口的值不等于0xfe//检查是否有按键按下.如果有,处理{}内的事

{//有按键按下.

Delay1mS(150);//调用延时函数//有按键按下,避开无效读码值的时间.,或者是脉冲长度

Hscan=0xfe;//将Hscan赋值,初始遥控值是要置高电平的.

P2=Hscan;//

while((Hscan&0x10)!

=0)//检查X10口是否有键按下。

未按下是1,

//在这显示出你的按键口是P2_4.即检查P2_4是高电平,无//键按下,低电平有键按下。

{

P2=Hscan;

if((P2&0xf0)!

=0xf0)//检查按键脉冲是否是变化,

returnP2;

elseHscan=(Hscan<<1)|0x01;//这里在存码值,应该是遥控输入。

将码值存入Hscan

//这里是读取码值的关键,如果来的脉冲不管是高电平//还是低电平,靠左移一位保存脉冲的状态值.

}

}

elsereturn0;

}

key_val()//按键处理函数//这里是读取将存好的码值进行处理.看是什么代码值

{

ucharkey;

key=keyscan();//这里是读取码值并存放在key里

switch(key)//这里是对比读取的码值

{

case0xee:

//按键‘7’//读取的码值=0xEE,则是按键“7”的代码

//while(P2==0xee);

setstep=setstep*10+7;//步伐数//这里是输入”setstep+7”步伐数对比读取的码值,//setstep原来可能是有数字的。

step=setstep;

butcount++;//计数,看输入拉几个数字

if(butcount>=5)//输入的数字超过5个就置0,无效

{

butcount=0;

setstep=0;

}

break;

case0xde:

//按键‘8’//读取的码值=0xdE,则是按键“8”的代码

//其他同”7”

//while(P2==0xde);

setstep=setstep*10+8;

butcount++;

step=setstep;

if(butcount>=5)

{

butcount=0;

setstep=0;

}

break;

case0xbe:

//按键‘9’//同上”7”

//while(P2==0xbe);

setstep=setstep*10+9;

butcount++;

step=setstep;

if(butcount>=5)

{

butcount=0;

setstep=0;

}

break;

case0x7e:

//按键‘正转//正转按键识别

while(P2==0x7e);//等待按键松开//一值按住电机是不转的,放开后才转.有检查//P2口的状态值

flag[0]=0xff;//开启正转标志,关闭反转//置正转标志.

flag[1]=0x00;//清除反转标志.

butcount=0;

speedcount=0;

speed=500;//置电机的转速.

if(!

flag[4])step=0;

TR0=1;

break;

case0xed:

//按键‘4’//同上”7”

while(P2==0xed);

setstep=setstep*10+4;

butcount++;

step=setstep;

if(butcount>=5)

{

butcount=0;

setstep=0;

}

break;

case0xdd:

//按键‘5’//同上”7”

//while(P2==0xdd);

setstep=setstep*10+5;

step=setstep;

butcount++;

if(butcount>=5)

{

butcount=0;

setstep=0;

}

break;

 

我上周刚做的这个实验成功拉,给你参考一下吧这可是我当时辛辛苦苦编出来的啊,不过我用的是L298驱动的和ULN2003一样,你把它换成2003就行拉

#include

unsignedcharcodetable[]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9,0x00,0xf1,0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0x00};

unsignedchartemp,temp_old;

unsignedcharkey;

unsignedchari,j,k,m,s;

voiddelay(inti)

{

for(m=i;m>0;m--)

for(j=250;j>0;j--)

for(k=10;k>0;k--);

}

voidsaomiao()

{

P3=0xff;

P3_4=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0e:

key=1;

break;

case0x0d:

key=2;

break;

case0x0b:

key=3;

break;

case0x07:

key=4;

break;

}

temp=P3;

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

P3=0xff;

P3_5=0;

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

for(i=50;i>0;i--)

for(j=200;j>0;j--);

temp=P3;

temp=temp&0x0f;

if(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

switch(temp)

{

case0x0d:

key=5;

break;

case0x0b:

key=6;

break;

case0x07:

key=7;

break;

}

temp=P3;

temp=temp&0x0f;

while(temp!

=0x0f)

{

temp=P3;

temp=temp&0x0f;

}

}

}

}

voidmain(void)

{

while

(1)

{

saomiao();

if(key==1)

{P1=0;

P2=0;

saomiao();

}

if(key==2)

{

temp_old=key;

for(s=0;s<8;s++)

{P2=table[s];

P1_4=0;

delay(13);

saomiao();

if(key!

=temp_old)

{

P1_4=1;

break;

}

}

}

if(key==3)

{

temp_old=key;

for(s=0;s<8;s++)

{P2=table[s];

P1_5=0;

delay(5);

saomiao();

if(key!

=temp_old)

{

P1_5=1;

break;

}

}

}

if(key==4)

{

temp_old=key;

for(s=0;s<8;s++)

{P2=table[s];

P1_6=0;

delay(20);

saomiao();

if(key!

=temp_old)

{

P1_6=1;

break;

}

}

}

if(key==5)

{

temp_old=key;

for(s=9;s<17;s++)

{P2=table[s];

P1_7=0;

delay(13);

saomiao();

if(key!

=temp_old)

{

P1_7=1;

break;

}

}

}

if(key==6)

{

temp_old=key;

for(s=9;s<17;s++)

{P2=table[s];

P1_5=0;

delay(5);

saomiao();

if(key!

=temp_old)

{

P1_5=1;

break;

}

}

}

if(key==7)

{

temp_old=key;

for(s=9;s<17;s++)

{P2=table[s];

P1_6=0;

delay(20);

saomiao();

if(key!

=temp_old)

{

P1_6=1;

break;

}

}

}

}

}

 

#include

voiddelay()

{intx,y;

for(x=0;x<40;x++)

for(y=0;y<10;y++);

}

main()

{while

(1)

{P1=0x3f;

delay();

P1=0x6f;

delay();

P1=0xcf;

delay();

P1=0x9f;

delay();

}

}

正反转改P1的值,速度改delay的值。

我默认步进电机是由P1口驱动的。

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

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1