51单片机(普中板) 90g360°舵机控制
主函数 main
#include <REGX52.H> #include "Delay.h" #include "Timer0.h" unsigned char count = 0; unsigned char pwm; void Timer0() interrupt 1 { // 每隔0.1ms=100us进入 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 if (count <= pwm) //5=0.5ms=500us 顺时针 //15=1.5ms=1500us 逆时针 {P0_0 = 1;} //高电平 else {P0_0 = 0;} //低电平 count ++; if (count >= 200) {count = 0;} } void main() { Timer_Init(); while(1) { pwm = 5; Delay(2000); count = 0; pwm = 15; Delay(2000); count = 0; } }
Delay.h
#ifndef __Delay_H__ #define __Delay_H__ void Delay(unsigned int xms); #endif
Delay.c
#include <REGX52.H> #include <intrins.h> // 100us // 1ms = 1000us = Delay(10) // 20ms = 20000us = Delay(200) void Delay(unsigned int xms) //@11.0592MHz { unsigned char i; while (xms--) { _nop_(); i = 43; while (--i); } }
Timer0.h
#ifndef __Timer0_H__ #define __Timer0_H__ void Timer_Init(); //1毫秒@11.0592MHz #endif
Timer.c
#include <REGX52.H> void Timer_Init() //@11.0592MHz { //设置定时器模式 TMOD &= 0xF0; TMOD |= 0x01; TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 // 65535us // 100us = 0.1ms 65435 TH0=0xff=65435/256,TL0=0x9b=65435%256 // 1000us = 1ms 64535 // 20000us = 20ms 45535 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 ET0 = 1; // 中断 EA = 1; // 中断 PT0 = 0; //中断优先级 } /* // 定时器中断函数 void Timer0() interrupt 1 { static unsigned int count; //static 静态变量,函数结束不清零 TL0 = 0xff; //设置定时初始值 TH0 = 0x9b; //设置定时初始值 count ++; if (count >= 100) { count = 0; P2_1 =~ P2_1; } } */
独立键盘控制(正转,反转,暂停)
问题:有时候点击k1或k2需要按两下,第一下暂停,第二下才正转或反转
#include <REGX52.H> #include "Delay.h" #include "Timer0.h" unsigned char count = 0; unsigned char pwm; void Timer0() interrupt 1 { // 每隔0.1ms=100us进入 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 if (count <= pwm) //5=0.5ms=500us 顺时针 //15=1.5ms=1500us 逆时针 {P0_0 = 1;} //高电平 else {P0_0 = 0;} //低电平 count ++; if (count >= 200) {count = 0;} } void main() { Timer_Init(); while(1) { if (P3_1 == 0) // P3_1=独立键盘k1 { // 顺时针转动 Delay(20); while (P3_1 == 0); Delay(20); count = 0; pwm = 5; } if (P3_0 == 0) // P3_0=独立键盘k2 { // 逆时针转动 Delay(20); while (P3_0 == 0); Delay(20); count = 0; pwm = 15; } if (P3_2 == 0) // P3_2=独立键盘k3 { // 暂停转动 Delay(20); while (P3_0 == 0); Delay(20); count = 0; pwm = 150; } } }
原理可参考:(38条消息) 180度舵机与360度舵机_Xander Yuan的博客-CSDN博客
控制角度(未成功,还需改进,暂做记录)
方法:通过1个单位时间(20ms)舵机转动角度大小来控制整个角度,测试11个单位时间舵机能转一圈。
#include <REGX52.H> #include "Delay.h" #include "Timer0.h" unsigned char count = 0; unsigned char pwm; unsigned char n = 0; void Timer0() interrupt 1 { // 每隔0.1ms=100us进入 TH0 = 65435 / 256; //设置定时初始值 TL0 = 65435 % 256; //设置定时初始值 if (count <= pwm) //5=0.5ms=500us 顺时针 //15=1.5ms=1500us 逆时针 {P0_0 = 1;} //高电平 pwm毫秒的高电平 else {P0_0 = 0;} //低电平 count ++; if (count >= 200) {count = 0; n ++ ;} } void main() { Timer_Init(); while(1) { if (n >= 1) //一个脉冲(20ms)转动角度 { count = 0; pwm = 0; } if (P3_1 == 0) // P3_1=独立键盘k1 { // 顺时针转动 Delay(20); while (P3_1 == 0); Delay(20); n = 0; count = 0; pwm = 5; } if (P3_0 == 0) // P3_0=独立键盘k2 { // 逆时针转动 Delay(20); while (P3_0 == 0); Delay(20); count = 0; pwm = 15; } if (P3_2 == 0) // P3_2=独立键盘k3 { // 暂停转动 Delay(20); while (P3_0 == 0); Delay(20); count = 0; pwm = 150; } if (P3_3 == 0) { Delay(20); while (P3_3 == 0); Delay(20); n ++ ; } } }

浙公网安备 33010602011771号