单片机控制继电器
发布网友
发布时间:2022-04-26 15:09
我来回答
共5个回答
热心网友
时间:2023-10-11 02:13
这个事专为你写的程序。在keil里编译通过。
ORG 00H
LJMP START
ORG 0BH
LJMP TIM
ORG 30H
START:
MOV IE,#10000010B
MOV TMOD,#00000010B
MOV TH0,#05H
SETB TR0
CLR 20H.0
MOV R0,#00H
MOV R1,#00H
MOV R2,#00H
MOV R3,#00H
MOV R4,#00H
MOV SP,#2FH
LOOP:
JBC 20H.0,NEXT1
SJMP LOOP
NEXT1:
CJNE R3,#00H,NEXT2
INC R3
MOV R1,#00H
MOV R2,#00H
MOV R4,#00H
CLR 20H.2
JNB 20H.2,$
CLR 20H.2
MOV P0,#11111101B
AJMP LOOP
NEXT2:
CJNE R3,#01H,NEXT3
INC R3
MOV R1,#00H
MOV R2,#00H
MOV R4,#00H
CLR 20H.1
JNB 20H.1,$
MOV P0,#11111011B
AJMP LOOP
NEXT3:
CJNE R3,#02H,NEXT4
INC R3
MOV P0,#11110111B
AJMP LOOP
NEXT4:
CJNE R3,#03H,NEXT5
INC R3
AJMP LOOP
NEXT5:
CJNE R3,#04H,NEXT6
INC R3
AJMP LOOP
NEXT6:
CJNE R3,#05H,NEXT7
INC R3
AJMP LOOP
NEXT7:
CJNE R3,#06H,NEXT8
INC R3
AJMP LOOP
NEXT8:
CJNE R3,#07H,NEXT9
INC R3
MOV P0,#11101111B
AJMP LOOP
NEXT9:
CJNE R3,#08H,NEXT10
INC R3
AJMP LOOP
NEXT10:
CJNE R3,#09H,NEXT11
INC R3
MOV P0,#11011111B
AJMP LOOP
NEXT11:
CJNE R3,#0AH,NEXT12
INC R3
MOV P0,#11011111B
AJMP LOOP
NEXT12:
CJNE R3,#0BH,NEXT13
INC R3
MOV P0,#11011111B
AJMP LOOP
NEXT13:
CJNE R3,#0CH,NEXT14
INC R3
MOV P0,#11011111B
AJMP LOOP
NEXT14:
CJNE R3,#0DH,NEXT15
INC R3
MOV P0,#11011111B
AJMP LOOP
NEXT15:
CJNE R3,#0EH,NEXT16
INC R3
MOV P0,#10111111B
AJMP LOOP
NEXT16:
CJNE R3,#0EH,NEXT
INC R3
MOV P0,#10111111B
AJMP LOOP
NEXT:
MOV R3,#00H
AJMP NEXT1
TIM:
INC R0
CJNE R0,#40,OVER
MOV R0,#00H
INC R1
CJNE R1,#10,OVER
MOV R1,#00H
SETB 20H.0
INC R2
CJNE R2,#100,OVER
MOV R2,#00H
SETB 20H.1
INC R4
CJNE R4,#18,OVER
MOV R4,#00H
SETB 20H.2
OVER:
RETI
END
热心网友
时间:2023-10-11 02:14
这个程序不难,但是不知道你是用什么型号的单片机,因为51单片机有很多生产厂家,有些51单片机的分频是不一样的,我现在用AT89S51为例在写程序,晶振6MHz,希望你尽快把单片机的具体型号写出来。
热心网友
时间:2023-10-11 02:14
有专用的继电器芯片,你这个程序就是个延时的问题啊。在你实际用时,没必要用继电器,用三极管就可以控制键盘了
热心网友
时间:2023-10-11 02:15
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit Relay=P3^2;
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main(void)
{
while(1)
{
Relay=0;
delay1ms(500);
Relay=1;
delay1ms(500);
}
}
热心网友
时间:2023-10-11 02:15
试试如下程序。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit P0_0 = P0^0;
sbit P0_1 = P0^1;
sbit P0_2 = P0^2;
sbit P0_3 = P0^3;
sbit P0_4 = P0^4;
sbit P0_5 = P0^5;
sbit P0_6 = P0^6;
sbit P0_7 = P0^7;
void delay1ms(uint z)
{
uint x, y;
for(x = z; x > 0; x--)
for(y = 110; y > 0; y--);
}
void main(void)
{
uint i, j;
while(1) {
P0_1 = 0;
for (i = 0; i < 3; i++)
for (j = 0; j < 120; j++)
delay1ms(500);
P0_1 = 1;
P0_2 = 0;
for (j = 0; j < 20; j++)
delay1ms(500);
P0_2 = 1;
P0_3 = 0;
delay1ms(500);
P0_3 = 1;
P0_4 = 0;
delay1ms(200);
P0_4 = 1;
P0_5 = 0;
delay1ms(500);
P0_5 = 1;
P0_6 = 0;
delay1ms(200);
P0_6 = 1;
}
}