一、橫向點亮(依次點亮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防止負數(shù),習慣問題
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端全處于低電平,熄滅點亮的流水燈 } 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; } } } 二、縱向點亮(點亮次序由P0.0~P1.0~P2.0~P3.0再到P0.1~P1.1以此類推) include "reg52.h" //此處使用的為89S52的芯片 void delay(unsigned int second) { unsigned int i,j; //定義為unsigned防止負數(shù),習慣問題 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: } } } 三、斜向點亮(從P0~P3口斜向點亮) include "reg52.h" //此處使用的為89S52的芯片 void delay(unsigned int second) { unsigned int i,j; //定義為unsigned防止負數(shù),習慣問題 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); //此處反映了當i為7時;0x01左移0位,亮P0.0的燈 delay(100); P1=0; P2=0x01<<(i<6?i+2:i-6); //此處反映了當i大于6時;0ix01左移i-6位,亮P0.(i-6)的燈 delay(100); P2=0: P3=0x01<<(i<5?i+3:i-5; //同上 delay(100); P3=0: } } }