1、能够理解清楚单片机IO口的结构。
2)t1相当于PnP三级管,t2相当于npn三极管
3) 强推挽io具有较强的驱动能力,电流输出能力很强。
2、能够看懂上下拉电阻的电路应用,并且熟练使用上下拉电阻。
3、理解28BYJ-48减速步进电机的工作原理。
4、能够熟练编写步进电机正反转任意角度的程序。
#include <REGX52.H>
sbit MC0 = P1^0;
sbit MC1 = P1^1;
sbit MC2 = P1^2;
sbit MC3 = P1^3;
sbit MC4 = P1^4;sbit KeyIn1 = P2^4;
sbit KeyIn2 = P2^5;
sbit KeyIn3 = P2^6;
sbit KeyIn4 = P2^7;sbit KeyOut1 = P2^3;
sbit KeyOut2 = P2^2;
sbit KeyOut3 = P2^1;
sbit KeyOut4 = P2^0;static unsigned long a;
unsigned char KeySta[4][4] = { {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
unsigned char code KeyCodeMap[4][4] = { { 0x31, 0x32, 0x33, 0x26 }, { 0x34, 0x35, 0x36, 0x25 }, { 0x37, 0x38, 0x39, 0x28 }, { 0x30, 0x1B, 0x0D, 0x27 }
};
void KeyDriver();
void main()
{EA = 1; TMOD = 0x01; TH0 = 0xFC; TL0 = 0x67;ET0 = 1; TR0 = 1; while(1)KeyDriver();
}
void StartMotor(signed long angle)
{EA = 0;a = angle*4076/360;EA = 1;
}
void StopMotor()
{EA = 0;a = 0;EA = 1;}
void KeyAction(unsigned char keycode)
{bit arr;if(keycode >= 0x30 && keycode <= 0x39){if(arr == 0) StartMotor(360*(int)(keycode-0x30));elseStartMotor(-360*(int)(keycode-0x30));}else if(keycode == 0x26){arr = 0;}else if(keycode == 0x28){arr = 1;}else if (keycode == 0x25) {StartMotor(90);}else if (keycode == 0x27) {StartMotor(-90);}else if (keycode == 0x1B) {StopMotor();}}
void KeyDriver()
{unsigned char i, j;static unsigned char backup[4][4] = { {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};for (i=0; i<4; i++) {for (j=0; j<4; j++){if (backup[i][j] != KeySta[i][j]) {if (backup[i][j] != 0) {KeyAction(KeyCodeMap[i][j]); }backup[i][j] = KeySta[i][j]; }}}
}void KeyScan()
{static unsigned char add = 0;unsigned char j ;static unsigned char keybuf[4][4] = { {0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}};keybuf[add][0] = (keybuf[add][0] << 1) | KeyIn1;keybuf[add][1] = (keybuf[add][1] << 1) | KeyIn2;keybuf[add][2] = (keybuf[add][2] << 1) | KeyIn3;keybuf[add][3] = (keybuf[add][3] << 1) | KeyIn4;for(j = 0;j <4;j++){if( (keybuf[add][j] & 0x0F) == 0x00 ){KeySta[add][j] = 0;}else if((keybuf[add][j] & 0x0F) == 0x0f){KeySta[add][j] = 1;}}add++;add = add & 0x03;switch(add){case 0:KeyOut4 = 1;KeyOut1 = 0;break;case 1:KeyOut1 = 1;KeyOut2 = 0;break;case 2:KeyOut2 = 1;KeyOut3 = 0;break;case 3:KeyOut3 = 1;KeyOut4 = 0;break;default:break;}
}void TurnMotor()
{static unsigned char index = 0; static unsigned char tmp = 0; unsigned char code BeatCode[8] = { 0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6};if(a != 0){if(a > 0){index++;index = index & 0x07;a--; }else{index--;index = index & 0x07;a++; }tmp = P1;tmp = tmp & 0xF0;tmp = tmp | BeatCode[index];P1 = tmp;}else {P1 = P1 | 0x0F;}
}
void InterruptTimer0() interrupt 1
{bit biv = 0;TH0 = 0xFC; TL0 = 0x67; KeyScan(); biv = ~biv;if (biv == 1){TurnMotor();}
}
5、学会蜂鸣器发声的方法。
#include <REGX52.H>
sbit BUZZ = P1^6;
void openbuzz(unsigned char arr);
void stopbuzz();
unsigned int TORH;
unsigned int TORL;
void main()
{unsigned int i;EA = 1;TMOD = 0x01;while(1){openbuzz(4000);for(i = 0;i <= 40000;i++);stopbuzz();for(i = 0;i <= 20000;i++);openbuzz(1000);for(i = 0;i <= 40000;i++);stopbuzz();for(i = 0;i <= 20000;i++);}
}
void openbuzz(unsigned char arr)
{unsigned int reload;reload = (65536)- (11059200/12)/(arr*2);TORH = (unsigned int)(reload >> 8);TORL = (unsigned int)reload;TH0 = 0xFF; TL0 = 0xFE;ET0 = 1;TR0 = 1;
}
void stopbuzz()
{ET0 = 0;TR0 = 0;
}
void InterruptTimer0() interrupt 1
{TH0 = TORH; TL0 = TORL;BUZZ = ~BUZZ;
}