单片机20道复习题参考答案.doc
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.用P0 口驱动LED灯,要求循环左移,从右向左每次两个LED灯亮,程序重复
执行。【一个排线。排1(PO-LED)]
#include
^include
void DelaylOms(unsigned int c); if (P0 == OxCO) for (;c>O;c-)
main() { {
{ LED = 0x03; for (b=38;b>0;b-) unsigned char LED; (
LED = 0x03; ) for (a=130;a>0;a~);
while (1) } )
{ } }
P0 = LED; void DelaylOm$(unsigned int c) }
2.用P0 口驱动LED灯做左右跑马灯程序,先向左侧跑马,每次左移2位,初始
时是2位LED灯亮,以后每次2个灯亮。待4次过后,再向右侧跑马,每次右移1位,初始时是1位LED灯亮,以后每次1个灯亮,执行8次。程序重
复执行。【一个排线。排1 (PO-LED)】
存include
ftinclude
void DelaylOms(unsigned int c); 〃延时Delayl0ms(50); for (;c>0;c・・)
10ms LED = 0x80;P0 = LED; (
main。for(i=0;i<7;i++) for (b=38;b>0;b-)
( (Delayl0ms(50); ( unsigned char LED; LED = LED » l;P0 = LED; } for (a=130;a>0;a~);
unsigned char i; //Oxfe = llll 1110 }
LED = 0x03; Delayl0ms(50); }
while (1)
/
LED = OxO3;PO = LED; }
\
P0= LED; }
for(i=0;i<3;i*+) }
{Delayl0ms(50); void DelaylOms(unsigned int c)
flinclude
unsigned char Key_Scan()
i = 0;
include
unsigned char keyValue = 0, i;
(K3==0))
sb it K2 = P1A 1; if (K1==O) K3 = P1A 2;
DelaylOms(l);
sb it K4 = P1A 3; DelaylOms(l); Adefi ne GPIO LED PO if (K1==O) void
main(void)
i = 0;
if
(K4==0)
while((i<50) && (K1==O))
unsigned char ledValue, keyNum; DelaylOms(l); ledValue = 0x01; DelaylOms(l); if
(K4==0)
while (1)
if
keyValue = 4; keyNum = Key_Scan(); i = 0;
if (keyNum == 1)
while((i<50)&& (K4==0))
ledVal ue
if
(K2==0)
DelaylOms(l); -CrorJIedValue,
DelaylOms(l);
if
keyValue = 1; ledVal ue
i = 0;
return keyValue;
_crolJledValue 1);
while((i<50)&& (K2==0))
void DelaylOms(unsigned int c)
else if (keyNum == 3)
DelaylOms(l);
unsigned char a, b; ledValue = Oxff;
for
(;c>O;c-)
else if (keyNum == 4)
for
(b=38;b>0;b-)
if
3. 用按键选择LED 灯做左右移动实验,3个按钮,K1对应P1.0, K2对应P1.1, K3
对应Pl.2, K1按下让LED 灯左移1位循环,K2按下让LED 灯右移1位循 环,K3按下,8个灯全亮。【两个排线。排1(PO-LED )排2(P1-独立按键)】
I
void DelaylOms(unsigned int c); unsigned char Key_Scan();
if (K2==0)
else if (keyNum == 2)
keyValue = 2;
unsigned char a, b; for (;c>0;c ・・) (
for (b=38;b>0;b-) (
for (a=130;a>0;a-);
4. 单个静态数码管流动显示HELLO-LACo 【一个排线。排1(PO-・JP3(右上角))】
#lnclude
Wdefine GPIO_DIG PO void DelaylOms(unsigned int c); unsigned
char
code
DIG_CODE[10)={0x76,0x79,0x38,0x38,0x3f,0x4 0,0x38,0x77,0x39}; void main(void) (
unsigned char i = 0;
while(l)
GPIO_DIG = ~DIG_CODE[i]; i++; if(i == 10)
{
i = 0; }
Delayl0ms(50);
}
}
void Delayl0m$(unsigned int c)
5. 8位动态数码管显示BAD-FOODo 【两个排线。排1 (P0-LED)排2(P1-J16)
ttinclude
unsigned char code DIG_PLACE[8] = { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char code DIG_CODE[8] {0x7c,0x77,0x5e,0x40,0x7L0x3f,0x3f,0x5e}; unsigned char Display Data (8); void DigDisplayO; void main(void) (
unsigned char i; for(i=0; i<8; i++)
(
DisplayData(i) = DIG_CODE(i];
} while(l) (
DigDisplayO;
}
}
void DigDisplay()
unsigned chari; unsigned int j; for(i=0; i<8; i++) (
GPIO_PLACE = DIG_PLACE[i] GPIO_DIG = DisplayData[i]; j = 10;
whlle(j-); GPIO_DIG = 0x00;
}
}