- #define uchar unsigned char
- // 使用8步法对步进电机进行控制的数组
- uchar st[]={0x09, 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01}; //1001, 1000, 1100, 0100, 0110, 0010, 0011, 0001
- #define uint unsigned int
-
- void delay(uint t){ // 模糊延时(不精确的延时)
- uint i=0, j=0;
- for(i=0; i<t; i++){
- for(j=0; j<120; j++);
- }
- }
-
- void stepper(){ // 电机控制函数
- int i=0;
- for(i=0; i<8; i++){ // 从左往右取值是正转
- for(i=7; i>0; i--){ // 从右往左取值是反转
- P2 = st[i];
- delay(v); // 延时越短 电机转动的速度越快
- }
- }
-
-
-
- void main(){ // 主函数
- inittimer();
- while(1){
- stepper(); // 调用电机控制函数
- }
- }
-
- #define uchar unsigned char
- // 使用8步法对步进电机进行控制的数组
- uchar st[]={0x09, 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01}; //1001, 1000, 1100,0100, // 0110, 0010, 0011, 0001
- #define uint unsigned int
- uchar count=0, num=0; // 定义中断中使用的变量
-
- void inittimer(){ // 计时器初始化函数
- TMOD = 0x01;
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- ET0 = 1;
- EA = 1;
- TR0 =1;
- }
-
- void main(){ // 主函数
- inittimer();
- while(1){
- //stepper();
- }
- }
- //***********************************************************************************
- // 中断服务函数每隔50毫秒进来1次
- // 赋初值就是确定下一次计时多长时间。
- ///**********************************************************************************
- void zhongduan() interrupt 1{ // 中断服务函数 每隔50毫秒进来一次
- TH0 = 0x3C ; //(65536-50000)/256; // 3C 高8位赋初值
- TL0 = 0xB0 ;//(65536-50000)%256; // B0 低8位赋初值
- count++; // 每隔50毫秒 count自加1
- if(count==20){ // 如果count等于20了,证明加了20次了,也就是1秒钟了
- P2 = st[num++]; // P2等于数组st的第num个值(从第0个到第8个)
- if(num==8){ // 如果num等于8了,就是st的值从头取到尾了。
- num=0; // 就将num置0,从头取
- }
- count = 0; // 将count置0,从头计算
- }
- }
-
|