#include <reg52h>
void time(unsigned int ucMs);//函数声明
void main(void)
{
unsigned char ucTimes;
#define DELAY_TIME 450
while(1)
{
P1=0xff;
P0=0xff;
for(ucTimes=0;ucTimes<8;ucTimes++){
P0 = P0 - (0x01<<ucTimes);time(DELAY_TIME);}
for(ucTimes=0;ucTimes<8;ucTimes++){
P1 = P1 - (0x01<<ucTimes);time(DELAY_TIME);}
P1=0xff;
P0=0xff;
for(ucTimes=0;ucTimes<8;ucTimes++){
P1 = P1 - (0x80>>ucTimes);time(DELAY_TIME);}
for(ucTimes=0;ucTimes<8;ucTimes++){
P0 = P0 - (0x80>>ucTimes);time(DELAY_TIME);}
}}
void time(unsigned int ucMs)
{
#define DELAYTIMES 239
unsigned char ucCounter;
while(ucMs!=0) {
for(ucCounter=0; ucCounter<DELAYTIMES; ucCounter++) { }
ucMs--;
}
}
#include<reg51h>
#define uchar unsigned char
uchar num=0;
void t0isr() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num>=20)
{
num=0;
P1=~P1;
}
}
main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
P0=0xf0;
while(1);
}
//晶振12M
DSEG AT 30H
timetemp: DS 1
LED: DS 1
CSEG AT 00H
ORG 0000H
LJMP START
ORG 000BH
LJMP Timer0Interrupt
START:
MOV SP,#60H
MOV timetemp,#20
MOV P1,#0FFH;LED低电平点亮
LCALL InitTimer0
MOV LED,#0FEH
LOOP:
MOV P1,LED ;add your code here!
LJMP LOOP
InitTimer0:
MOV TMOD,#01H
MOV TH0,#HIGH(65536-50000)
MOV TL0,#LOW(65536-50000);延时50ms
SETB EA
SETB ET0
SETB TR0
RET
Timer0Interrupt:
PUSH ACC
PUSH PSW
CLR TR1
MOV A,TL1
ADD A,#LOW(65536-50000)+7
MOV TL1,A
CLR A
ADDC A,#HIGH(65536-50000)
SETB TR1
DJNZ timetemp,NEXTT1
MOV timetemp,#20
;========================
MOV A,LED
RL A
MOV LED,A;add your code here!
;========================
POP PSW
POP ACC
RETI
假设按键用P20,有上拉电阻,低电平有效;LED用高电平点亮
1、端口初始化
SETB P20
MOV P1,#0
2、检测按键变化,每按过一次,按键计数器KEYCOUNT就加1,
;查询按键是否按下
;若已按下,则等待按键d起,若未按下则等待按下
;按键未d起就一直等待,否则执行
inc keycount
3、将按键计数器的值送往P1口
mov a,keycount
mov P1,a
以上就是关于单片机PO P1口控制16个LED 的跑马灯程序全部的内容,包括:单片机PO P1口控制16个LED 的跑马灯程序、单片机P1口接8盏LED小灯,试写一个低4盏灯和高4盏灯分别亮灭1s、循环不止的程序。要求采用定时器中断的方、51单片机,用定时器中断,在P1口实现1S移动一次的流水灯,用汇编语言编写的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)