#include
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
uchar code beatCode[8] = {
0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6
};
ulong beats = 0;
uchar T0RH = 0, T0RL = 0;
void turnMotor(ulong angle){
EA = 0;
beats = angle * 4076 / 360;
EA = 1;
}
void setTimer0(uint ms){
ulong tmp;
tmp = 11059326 / 12;
tmp = tmp * ms / 1000;
tmp = 65536 - tmp;
tmp += 28;
T0RL = tmp;
T0RH = tmp>>8;
}
void interruptTimer0() interrupt 1{
static uchar index = 0;
TH0 = T0RH;
TL0 = T0RL;
if(beats){
uchar tmp = P1;
tmp &= 0xF0;
tmp |= beatCode[index];
P1 = tmp;
index = ++index & 0x07;
beats--;
}
else{
P1 |= 0x0F;
}
}
void main(){
EA = 1;
TMOD = 0x01;
setTimer0(2);
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
TR0 = 1;
turnMotor(2 * 360 + 180);
while(1);
}
下面是使用库函数循环左移实现的:
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
ulong beats = 0;
uchar T0RH = 0, T0RL = 0;
void turnMotor(ulong angle){
EA = 0;
beats = angle * 4076 / 360;
EA = 1;
}
void setTimer0(uint ms){
ulong tmp;
tmp = 11059326 / 12;
tmp = tmp * ms / 1000;
tmp = 65536 - tmp;
tmp += 28;
T0RL = tmp;
T0RH = tmp>>8;
}
void interruptTimer0() interrupt 1{
static uchar beatCode = 0x33;
TH0 = T0RH;
TL0 = T0RL;
if(beats){
uchar tmp = P1;
tmp &= 0xF0;
beatCode = _crol_(beatCode, 1);
tmp |= beatCode;
P1 = tmp;
beats--;
}
else{
P1 |= 0x0F;
}
}
void main(){
EA = 1;
TMOD = 0x01;
setTimer0(2);
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1;
TR0 = 1;
turnMotor(2 * 360 + 180);
while(1);
}
上一篇:51单片机 使用蜂鸣器播放简单音乐
下一篇:51单片机 矩阵按键的扫描、消抖、动作分离
推荐阅读最新更新时间:2024-03-16 15:45