STC89C52RC型号的51单片机中触摸开关控制9g舵机,亲测可用,有疑问可以找我呦
制作出来的实物图如下:
0.5ms--------------0度;
1.0ms------------45度;
1.5ms------------90度;
2.0ms-----------135度;
2.5ms-----------180度;
单片机源程序如下:
- #include<reg52.h>
- #define unchar unsigned char
- unsigned char count; //0.5ms次数标识
- unsigned char jd=0;
- sbit pwm1 = P1^0;
- unsigned char flag=1;
- unsigned char flag1=1;
- sbit key1=P2^0;
- void Time0_Init() //定时器初始化
- {
- TMOD = 0x01; //定时器0工作在方式1
- TH0 = (65535-500)/256;
- TL0 = (65535-500)%256; //12MZ晶振,0.5ms
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关打开
- }
- void Time0_Int() interrupt 1
- {
- static int t=0;
- t++;
- TH0 = (65535-500)/256; //重新赋值
- TL0 = (65535-500)%256;
- if(count< jd) //判断0.5ms次数是否小于角度标识
- {
- pwm1=1; //确实小于,PWM输出高电平
- }
- else
- {
- pwm1=0; //大于则输出低电平
- }
- count++; //0.5ms次数加1
- if(count>=40)
- count=0;
-
-
- }
- unsigned char count; //0.5ms次数标识
- void keyscan() //按键扫描
- {
- if(key1==0&&flag1)
- {
-
- flag=!flag;
- flag1=0;
- }else if(key1) flag1=1;
-
- }
- void main()
- {
-
- count=0;
- Time0_Init();
- while(1)
- {
- keyscan();
- if(flag)
- {
- jd=4;
-
- }
- else
- jd=1;
- }
- }
复制代码
hex文件下载(uv2版本的工程):
51单片机控制舵机.zip
(3.9 MB, 下载次数: 20)
|