做個(gè)15路輸出的彩燈
//想做個(gè)15路輸出的彩燈,使用P0.0~P0.7和P2.0~P2.6端口輸出;預(yù)置數(shù)為15個(gè)1(10進(jìn)制32767)。
//設(shè)置+1(P3.3)和-1(P3.4)兩個(gè)按鈕。
//可以減到十進(jìn)制32047,到32047后如果繼續(xù)按-1按鈕,可以回到32767重新進(jìn)行減法。
//加法也一樣,加到32767后,再按+1按鈕可以到32047重新+1.
//再設(shè)置一個(gè)按鈕(P2.7),當(dāng)P2.7接地時(shí),在當(dāng)前顯示的數(shù)字上減去十進(jìn)制數(shù)1712進(jìn)行顯示。
//求單片機(jī)彩燈程序。
最佳答案:
//============================
#include
sbit K_1 = P3^3; //+
sbit K_2 = P3^4; //-
sbit D_1 = P2^7; //顯示方式選擇
//----------------------------
void delay_ms(unsigned int n)
{
unsigned char j;
while(n--) for(j = 0; j < 230; j++); //230是實(shí)驗(yàn)測(cè)試所得!
}
void main()
{
unsigned int m;
m = 32767;
while(1) {
if (D_1 == 1) {
P2 = ~(m / 256);
P0 = ~(m % 256);
}
else {
P2 = ~((m - 1712) / 256);
P0 = ~((m - 1712) % 256);
}
if (K_1 == 0) {
delay_ms(10);
if (K_1 == 0) {
m++;
if (m > 32767) m = 32047;
while(K_1 == 0);
} }
if (K_2 == 0) {
delay_ms(10);
if (K_2 == 0) {
m--;
if (m < 32047) m = 32767;
while(K_2 == 0);
} } }
}
//============================
本程序用PROTEUS仿真畫面如下: