流水灯
#include
sbit LED1 = P2^4;
sbit LED2 = P2^5;
sbit LED3 = P2^6;
sbit LED4 = P2^7;
sbit P1_0 = P1^0;
unsigned long int i;
void fun1()
{
for(i = 0;i<80000;i++);
LED1 = 0;
LED2 = 1;
LED3 = 1;
LED4 = 1;
for(i = 0;i<80000;i++);
LED1 = 1;
LED2 = 0;
LED3 = 1;
LED4 = 1;
for(i = 0;i<80000;i++);
LED1 = 1;
LED2 = 1;
LED3 = 0;
LED4 = 1;
for(i = 0;i<80000;i++);
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 0;
}
void fun2()
{
for(i = 0;i<50000;i++);
LED1 = 0;
LED2 = 1;
LED3 = 1;
LED4 = 1;
for(i = 0;i<50000;i++);
LED1 = 0;
LED2 = 0;
LED3 = 1;
LED4 = 1;
for(i = 0;i<50000;i++);
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 1;
for(i = 0;i<50000;i++);
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
for(i = 0;i<50000;i++);
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 1;
}
void fun3()
{
for(i = 0;i<50000;i++);
P1_0 = 0x01;
LED1 = 1;
LED2 = 0;
LED3 = 0;
LED4 = 0;
for(i = 0;i<50000;i++);
P1_0 = 0x00;
LED1 = 0;
LED2 = 1;
LED3 = 0;
LED4 = 0;
for(i = 0;i<50000;i++);
P1_0 = 0x01;
LED1 = 0;
LED2 = 0;
LED3 = 1;
LED4 = 0;
for(i = 0;i<50000;i++) ;
P1_0 = 0x00;
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 1;
}
void main()
{
P1_0 = 0x00;
while(1)
{
fun1();
}
}
数码管
#include"STC12C5A60S2.h"
sbit KEY1 = P3^2;
sbit KEY2 = P3^4;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit P0_7 = P0^7;
sbit P0_6 = P0^6;
sbit P0_5 = P0^5;
sbit P0_4 = P0^4;
sbit P0_3 = P0^3;
sbit P0_2 = P0^2;
sbit P0_1 = P0^1;
sbit P0_0 = P0^0;
sbit P1_0 = P1^0;
void main()
{
int i;
unsigned long int j;
P1_0 = 0x00;
P0 = 0xFF;
P2_0 = 0;
P2_1 = 0;
P2_2 = 0;
P2_3 = 0;
i = 0;
while(1)
{
if(KEY1==0 && KEY2 == 0)
{
P2_0 = 0;
P2_1 = 1;
P2_2 = 1;
P2_3 = 1;
if(i==0)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 1)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 2)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 1;
P0_4 = 0;
P0_3 = 0;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 3)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 4)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i==5)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 6)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 7)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 8)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 9)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i = 0;
}
}
else if(KEY1==0 && KEY2 == 1)
{
P2_0 = 1;
P2_1 = 0;
P2_2 = 1;
P2_3 = 1;
if(i==0)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 1)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 2)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 1;
P0_4 = 0;
P0_3 = 0;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 3)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 4)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i==5)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 6)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 7)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 8)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 9)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i = 0;
}
}
else if(KEY1==1 && KEY2 == 0)
{
P2_0 = 1;
P2_1 = 1;
P2_2 = 0;
P2_3 = 1;
if(i==0)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 1)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 2)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 1;
P0_4 = 0;
P0_3 = 0;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 3)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 4)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i==5)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 6)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 7)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 8)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 9)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i = 0;
}
}
else if(KEY1==1 && KEY2 == 1)
{
P2_0 = 1;
P2_1 = 1;
P2_2 = 1;
P2_3 = 0;
if(i==0)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 1)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 2)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 1;
P0_4 = 0;
P0_3 = 0;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 3)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}
else if(i == 4)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i==5)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 6)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 7)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 8)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i++;
}else if(i == 9)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
for(j=0;j<60000;j++);
i = 0;
}
}
}
}
按键散转
;***************************************************************
; 引脚定义
;***************************************************************
LED1 BIT P2.4
LED2 BIT P2.5
LED3 BIT P2.6
LED4 BIT P2.7
KEY1 BIT P3^2;
KEY1_STATE EQU 69H
DelayTimes_100MS EQU 6FH
;***************************************************************
; 全灭
;***************************************************************
CLR LED1
CLR LED2
CLR LED3
CLR LED4
;*******************************************************************
;--主程序-----
;*******************************************************************
ORG 0000H ; 定义起始地址,程序将从此处开始运行
LJMP START ; 长跳转,跳转到标号"START"处
;P1M0 = 0x01
ORG 0100H ; 定义START程序的地址
START:
MOV SP,#70H ;设置堆栈,堆栈空间从70H到7FH,共16个字节
MOV A,#00H
MOV KEY1_STATE,A
START1:
LCALL LEDRUN ; 调用跑马灯程序
LJMP START1 ; 跳转回START1,重新执行以上程序
; 相当于C语言中的while(1)的功能
;*******************************************************************
;--子程序-----
;*******************************************************************
LEDRUN:
JNB KEY1,KEY_PROCESS_1
RET ;
;--------------------------------------------------------------------
;
;
;--------------------------------------------------------------------
KEY_PROCESS_1 :
JB KEY1,KEY_PROCESS_2
KEY_PROCESS_2 :
;--散转
MOV A,KEY1_STATE
RL A
ADD A,KEY1_STATE
MOV DPTR,#KEY1_Flash
JMP @A + DPTR
KEY1_Flash :
LJMP KEY1_Flash0
LJMP KEY1_Flash1
LJMP KEY1_Flash2
LJMP KEY1_Flash3
LJMP KEY1_Flash4
KEY1_Flash0:
SETB LED1
CLR LED2
CLR LED3
CLR LED4
MOV A,#01H
ADD A,KEY1_STATE
MOV KEY1_STATE,A
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
LJMP LEDRUN
KEY1_Flash1:
CLR LED1
SETB LED2
CLR LED3
CLR LED4
MOV A,#01H
ADD A,KEY1_STATE
MOV KEY1_STATE,A
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
LJMP LEDRUN
KEY1_Flash2:
CLR LED1
CLR LED2
SETB LED3
CLR LED4
MOV A,#01H
ADD A,KEY1_STATE
MOV KEY1_STATE,A
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
LJMP LEDRUN
KEY1_Flash3:
CLR LED1
CLR LED2
CLR LED3
SETB LED4
MOV A,#01H
ADD A,KEY1_STATE
MOV KEY1_STATE,A
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
LJMP LEDRUN
KEY1_Flash4:
SETB LED1
SETB LED2
CLR LED3
CLR LED4
MOV A,#01H
ADD A,KEY1_STATE
MOV KEY1_STATE,A
MOV KEY1_STATE,00H
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
LJMP LEDRUN
Delay100MS:
MOV R7,DelayTimes_100MS
Delay100MS_1: MOV R6,#10
Delay100MS_2: MOV R5,#150
Delay100MS_3: MOV R4,#200
Delay100MS_4: DJNZ R4, Delay100MS_4
DJNZ R5, Delay100MS_3
DJNZ R6, Delay100MS_2
DJNZ R7, Delay100MS_1
RET ;子程序返回
END
外部中断(C语言)
#include
sbit P1_0 = P1^0;
void configExtInt0();
void configExtInt1();
void main() {
P1_0 = 0;
configExtInt0();
configExtInt1();
while (1);
}
void configExtInt0() {
IT0 = 1;
EA = 1;
EX0 = 1;
}
void configExtInt1() {
IT1 = 1;
EA = 1;
EX1 = 1;
}
void extInt0ISR() interrupt 0 {
unsigned long int i;
P1_0 = 1;
for(i = 0;i<50000;i++);
P1_0 = 0;
for(i = 0;i<50000;i++);
P1_0 = 1;
for(i = 0;i<50000;i++);
P1_0 = 0;
for(i = 0;i<50000;i++);
P1_0 = 1;
for(i = 0;i<50000;i++);
P1_0 = 0;
}
void extInt1ISR() interrupt 2 {
}
外部中断(汇编版)
;***************************************************************
; 引脚定义
;***************************************************************
LED1 BIT P2.4
LED2 BIT P2.5
LED3 BIT P2.6
LED4 BIT P2.7
BEEP BIT P1.0
KEY1 BIT P3^2;
KEY1_STATE EQU 69H
DelayTimes_100MS EQU 6FH
;*******************************************************************
;--主程序-----
;*******************************************************************
ORG 0000H ; 定义起始地址,程序将从此处开始运行
LJMP START ; 长跳转,跳转到标号"START"处
ORG 0003H
LJMP INT0_ISR
ORG 0100H ; 定义START程序的地址
START:
MOV SP,#70H ;设置堆栈,堆栈空间从70H到7FH,共16个字节
MOV A,#00H
MOV KEY1_STATE,A
START1:
LCALL LEDRUN ; 调用跑马灯程序
CLR BEEP
SETB IT0
SETB PX0
SETB EX0
SETB EA
LJMP START1 ; 跳转回START1,重新执行以上程序
LEDRUN:
SETB LED1
CLR LED2
CLR LED3
CLR LED4
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
CLR LED1
SETB LED2
CLR LED3
CLR LED4
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
CLR LED1
CLR LED2
SETB LED3
CLR LED4
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
CLR LED1
CLR LED2
CLR LED3
SETB LED4
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS ; 调用延时100MS子程序
RET ; ; 相当于C语言中的while(1)的功能
INT0_ISR:
PUSH PSW
PUSH ACC
SETB RS0
CLR RS1
SETB BEEP
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
CLR BEEP ; 调用延时100MS子程序
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
SETB BEEP
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
CLR BEEP ; 调用延时100MS子程序
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
SETB BEEP
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
CLR BEEP ; 调用延时100MS子程序
MOV DelayTimes_100MS,#10 ;设置需要延时多少个100MS
LCALL Delay100MS
POP ACC
POP PSW
RETI
Delay100MS:
MOV R7,DelayTimes_100MS
Delay100MS_1: MOV R6,#10
Delay100MS_2: MOV R5,#150
Delay100MS_3: MOV R4,#200
Delay100MS_4: DJNZ R4, Delay100MS_4
DJNZ R5, Delay100MS_3
DJNZ R6, Delay100MS_2
DJNZ R7, Delay100MS_1
RET ;子程序返回
END
定时器
#include
sbit LED1 = P2^4;
sbit LED2 = P2^5;
sbit LED3 = P2^6;
sbit LED4 = P2^7;
sbit P1_0 = P1^0;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit P0_7 = P0^7;
sbit P0_6 = P0^6;
sbit P0_5 = P0^5;
sbit P0_4 = P0^4;
sbit P0_3 = P0^3;
sbit P0_2 = P0^2;
sbit P0_1 = P0^1;
sbit P0_0 = P0^0;
int k = 0;
int one_second_count = 1;
void delay_10ms(unsigned long int n)
{
unsigned long int i;
unsigned long int j;
n = 10*n;
for(i = 0;i<n;k++)
{
for(j=0;j<144;j++);
}
}
void One_Second_Project ()
{
if(k==0)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 1;
P0_0 = 1;
k++;
}
else if(k == 1)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
k++;
}
else if(k == 2)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 1;
P0_4 = 0;
P0_3 = 0;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
k++;
}
else if(k == 3)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 1;
P0_1 = 0;
P0_0 = 1;
k++;
}
else if(k == 4)
{
P0_7 = 1;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
k++;
}else if(k==5)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
k++;
}else if(k == 6)
{
P0_7 = 0;
P0_6 = 1;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
k++;
}else if(k == 7)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 1;
P0_1 = 1;
P0_0 = 1;
k++;
}else if(k == 8)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 0;
P0_3 = 0;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
k++;
}else if(k == 9)
{
P0_7 = 0;
P0_6 = 0;
P0_5 = 0;
P0_4 = 1;
P0_3 = 1;
P0_2 = 0;
P0_1 = 0;
P0_0 = 1;
k = 0;
}
}
void Timer0() interrupt 1
{
if( 1000 == one_second_count)
{
one_second_count = 1;
One_Second_Project ();
}
else
{
one_second_count++;
}
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void main()
{
P1_0 = 0;
Timer0Init();
P2_0 = 0;
while(1)
{
}
}
中断嵌套
#include
sbit LED1 = P2^4;
sbit LED2 = P2^5;
sbit LED3 = P2^6;
sbit LED4 = P2^7;
sbit P0_7 = P0^7;
sbit P0_6 = P0^6;
sbit P0_5 = P0^5;
sbit P0_4 = P0^4;
sbit P0_3 = P0^3;
sbit P0_2 = P0^2;
sbit P0_1 = P0^1;
sbit P0_0 = P0^0;
sbit P1_0 = P1^0;
int k = 0;
int one_second_count = 1;
void One_Second_Project ()
{
switch (k)
{
case 0:
{
LED1 = 0;
LED2 = 1;
LED3 = 1;
LED4 = 1;
k++;
}
break;
case 1:
{
LED1 = 1;
LED2 = 0;
LED3 = 1;
LED4 = 1;
k++;
}
break;
case 2:
{
LED1 = 1;
LED2 = 1;
LED3 = 0;
LED4 = 1;
k++;
}
break;
case 3:
{
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 0;
k = 0;
}
break;
default: k = 0;
break;
}
}
void Timer0() interrupt 1
{
TL0 = 0xCD;
TH0 = 0xD4;
if( 1000 == one_second_count)
{
one_second_count = 1;
One_Second_Project ();
}
else
{
one_second_count++;
}
}
void extInt0ISR() interrupt 0
{
unsigned long int i;
P1_0 = 1;
for(i = 0;i<50000;i++);
P1_0 = 0;
for(i = 0;i<50000;i++);
P1_0 = 1;
for(i = 0;i<50000;i++);
P1_0 = 0;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void ExtInt0Init()
{
IT0 = 0;
EA = 1;
EX0 = 1;
}
void Interrupt_priority()
{
PX0 = 0;
PT0 = 0;
}
void main()
{
P1_0 = 0;
Timer0Init();
ExtInt0Init();
Interrupt_priority();
while(1)
{
}
}
串口单字节发送接收
#include
long int i;
void Init()
{
REN = 1;
SM0 = 0; SM1 = 1;
TR1 = 1;
TMOD = 0X20;
TH1 = 0XFD;
TL1 = 0XFD;
EA = 1;
ES = 1;
}
void UART1_Send_ISR()
{
SBUF = i;
}
void UART1_Recv_ISR()
{
i = SBUF;
}
void main()
{
Init();
while(1);
}
void UART1_ISR()interrupt 4
{
if(TI == 1)
{
TI = 0;
}
else if (RI == 1)
{
UART1_Recv_ISR();
UART1_Send_ISR();
RI = 0;
}
}