1、直流电机介绍
- 直流电机是一种将电能转换为机械能的装置。一般的直流电机有两个电极,当电极正接时,电机正转,当电极反接时,电机反转
- 直流电机主要由永磁体(定子)、线圈(转子)和换向器组成
- 除直流电机外,常见的电机还有步进电机、舵机、无刷电机、空心杯电机等
2、PWM工作原理
我们来看一下精度=占空比变化步距是啥意思:(总的来说就是在调整PWM的时候每次调整的大小步距)
3、LED呼吸灯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <REGX52.H>
sbit LED=P2^0; unsigned int time; unsigned int i;
void Delayxms(unsigned int xms) { while(xms--); }
void main(){ while(1){ for(time=0;time<100;time++){ for(i=0;i<20;i++){ LED=0; Delayxms(time); LED=1; Delayxms(100-time); } } for(time=100;time>0;time--){ for(i=0;i<20;i++){ LED=0; Delayxms(time); LED=1; Delayxms(100-time); } } } }
|
4、定时器中断实现PWM波
定时器初始化:定时器实现每100微妙中断一次,可以改变中断的频率以改变PWM频率。
1 2 3 4 5 6 7 8 9
| void Timer0Init(void) { TMOD &= 0xF0; TMOD |= 0x01; TL0 = 0x9C; TH0 = 0xFF; TF0 = 0; TR0 = 1; }
|
main.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <REGX52.H> #include "Delay.h" #include "Key.h" #include "Nixie.h" #include "Timer0.h"
sbit Motor=P1^0;
unsigned char Counter,Compare; unsigned char KeyNum,Speed;
void main() { Timer0_Init(); while(1) { KeyNum=Key(); if(KeyNum==1) { Speed++; Speed%=4; if(Speed==0){Compare=0;} if(Speed==1){Compare=50;} if(Speed==2){Compare=75;} if(Speed==3){Compare=100;} } Nixie(1,Speed); } }
void Timer0_Routine() interrupt 1 { TL0 = 0x9C; TH0 = 0xFF; Counter++; Counter%=100; if(Counter<Compare) { Motor=1; } else { Motor=0; } }
|