單片機(jī)流水燈三個(gè)點(diǎn)亮方向控制程序
一、橫向點(diǎn)亮(依次點(diǎn)亮P0.0~P0.1到P1.0~P1.7到P2.0~P2.7到P3.0~P3.7)
include "reg52.h" //此處使用的為89S52的芯片
void delay(unsigned int second)
{
unsigned int i,j; //定義為unsigned防止負(fù)數(shù),習(xí)慣問(wèn)題
for(i=0;i { for(j=0;j<200000;j++); } } void main(void) { unsigned int i; while(1) { for(i=0;i<8:i++) { P0=0x01< delay(100); //由1us=10E6,故此處100為2秒 P0=0: //使P0端全處于低電平,熄滅點(diǎn)亮的流水燈 } for(i=0;i<8:i++) { P1=0x01< delay(100); P1=0; } for(i=0;i<8:i++) { P2=0x01< delay(100); P2=0; } for(i=0;i<8:i++) { P3=0x01< delay(100); P3=0; } } } 二、縱向點(diǎn)亮(點(diǎn)亮次序由P0.0~P1.0~P2.0~P3.0再到P0.1~P1.1以此類(lèi)推) include "reg52.h" //此處使用的為89S52的芯片 void delay(unsigned int second) { unsigned int i,j; //定義為unsigned防止負(fù)數(shù),習(xí)慣問(wèn)題 for(i=0;i { for(j=0;j<200000;j++); } } void main(void) { unsigned i; while(1) { for(i=0;i<8;i++) { P0=0x01< delay(100); P0=0; P1=0x01< delay(100); P1=0; P2=0x01< delay(100); P2=0; P3=0x01< delay(100); P3=0: } } } 三、斜向點(diǎn)亮(從P0~P3口斜向點(diǎn)亮) include "reg52.h" //此處使用的為89S52的芯片 void delay(unsigned int second) { unsigned int i,j; //定義為unsigned防止負(fù)數(shù),習(xí)慣問(wèn)題 for(i=0;i { for(j=0;j<200000;j++); } } void main(void) { unsigned i; while(1) { for(i=0;i<8;i++) { P0=0x01< delay(100); P0=0; P1=0x01<<(i<7?i+1:i-7); //此處反映了當(dāng)i為7時(shí);0x01左移0位,亮P0.0的燈 delay(100); P1=0; P2=0x01<<(i<6?i+2:i-6); //此處反映了當(dāng)i大于6時(shí);0ix01左移i-6位,亮P0.(i-6)的燈 delay(100); P2=0: P3=0x01<<(i<5?i+3:i-5; //同上 delay(100); P3=0: } } }