《单片机的C语言程序设计与运用(第2版)》期末复习题和答案2 联系客服

发布时间 : 星期一 文章《单片机的C语言程序设计与运用(第2版)》期末复习题和答案2更新完毕开始阅读

第四章

例4-1 P104

假设外部中断0和外部中断1均为下降沿触发,当外部中断0发生时,P0端口的电平反向,当外部中断1发生时,P1端口的电平反向。

中断类

#include

void IS0(void) interrupt 0 {

P0=~P0;} //P0端口反向

void IS1(void) interrupt 2 {

P1=~P1;} //P1端口反向

《单片机的C语言程序设计与运用(第2版)》期末复习题和答案2

void main( ) {

P0=0x00; P1=0xFF;

IT0=1; IT1=1;

EX0=1; EX1=1; EA=1; while(1); }

【例4-9】外部中断示例

在本实例中,首先通过P1.7口点亮发光二极管D1,然后外部输入一脉冲串,则发光二极管D1亮、暗交替。

#include sbit P1_7=P1^7;

void interrupt0( ) interrupt 0 using 2 //外部中断0

2 / 49

《单片机的C语言程序设计与运用(第2版)》期末复习题和答案2

{ P1_7=!P1_7;}

void main( ) {

EA=1;

//开中断

IT0=1; //外部中断0脉冲触发 EX0=1;

//外部中断0

P1_7=0; do{ }while(1); }

如果有3个脉冲,则灯亮、暗交替一次,可如下面编程: #include Sbit P17=P1^7; unsigned char i=3; void main( ) {

EA=1; IT0=1; EX0=1;

P17=0; do{ }while(1); } void interrupt0( ) interrupt 0 { i=i-1; if(i==0) { P17=!P17; } }

3 / 49

i=3;

《单片机的C语言程序设计与运用(第2版)》期末复习题和答案2

【例4-10】如图4-18所示,8只LED阴极接至单片机P0口,两开关S0、S1分别接至单片机引脚P3.2()和P3.3()。编写程序控制LED状态。按下S0后,点亮8只LED;按下S1后,变为闪烁状态。

#include sbit P32=P3^2;

void delay(unsigned int d) //定义延时子函数 { }

void main( ) {

P0=0xFF;

//熄灭LED IT1=1;

//外中断0、1脉冲触发方式

while(--d>0);

IT0=1;

4 / 49