PWM_motor.c
资源名称:PWM_motor.rar [点击查看]
上传用户:bamton
上传日期:2015-03-08
资源大小:1k
文件大小:1k
源码类别:
Telnet客户端
开发平台:
Visual C++
- /*用PWM控制电机脉冲频率应控制在25HZ~35HZ之间*/
- /*定时1ms,1个周期30ms,脉冲频率为33HZ */
- #include <reg52.h>
- sbit INT_1=P1^0;
- sbit INT_2=P1^1;
- sbit INT_3=P1^2;
- sbit INT_4=P1^3;
- sbit EN1=P1^4;
- unsigned char time=0;
- unsigned char period=30;
- unsigned char high=10;
- unsigned char th0=0;
- unsigned char tl0=1;
- bit dir;
- void dealy(unsigned char num)
- {
- unsigned char i;
- for(i=0;i<100*num;i++);
- }
- void timer0() interrupt 1 using 1
- {
- TH0=0xfc; /*定时器初值重装载64536*/
- TL0=0x18;
- time++;
- if(dir==1)
- {
- if(time==high)
- {
- INT_1=1;
- INT_2=0;
- }
- else if(time==period)
- {
- time=1;
- INT_1=1;
- INT_2=0;
- }
- }
- else
- {
- if(time==high)
- {
- INT_1=0;
- INT_2=1;
- }
- else if(time==period)
- {
- time=0;
- INT_1=0;
- INT_2=1;
- }
- }
- }
- void main()
- {
- P0=0x00;
- P2=0x00;
- TMOD=0x01; /*定时器0方式1*/
- TH0=0xfc; /*定时器装载初值,设置脉冲信号的占空比为1/5*/
- TL0=0x18;
- ET0=1; /*开定时器0中断*/
- TR0=1; /*启动定时器0*/
- EA=1; //开CPU中断
- dir=1;
- while(1)
- {
- ;
- }
- }