給孫子做的
2.JPG (547.9 KB, 下載次數: 37)
下載附件
2022-8-18 14:53 上傳
7.PNG (47.78 KB, 下載次數: 39)
下載附件
2022-8-18 14:53 上傳
功能這樣的
一 紅綠燈轉換與倒計時
以下三步,不斷循環。
1.紅燈亮,9秒倒計時,小于3秒時紅燈閃。
2.綠燈亮,9秒倒計時,小于3秒時綠燈閃。
3.黃燈亮,3秒倒計時,燈不閃。
二 按鍵有三個
黃燈鍵:按黃燈鍵開機(STC89C52),亮黃燈,長按轉夜間模式,黃燈和數碼管的小數點閃爍,數碼管不顯示數字。
紅燈鍵:短按亮紅燈,長按關機。
綠燈鍵:按綠燈鍵,亮綠燈。
三 定時控制
如果沒有按鍵操作,6分鐘后轉為夜間模式,10分鐘后自動關機(實際上是進入掉電模式,耗電小于1微安)。
代碼如下
#include "reg51.h" //STC89C52RC@12MHz
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//---開關
sbit S_hong = P3^0; //短按亮紅燈,長按關
sbit S_lv = P3^1; //綠燈
sbit S_huang = P3^2; //短按亮黃燈,長按夜間模式,黃燈閃爍。
//----LED燈
sbit LED_hong = P1^2;
sbit LED_lv = P1^1;
sbit LED_huang = P1^0;
//---數碼管
sbit SMG_dp =P0^0;
sbit SMG_c =P0^1;
sbit SMG_d =P0^2;
sbit SMG_e =P0^3;
sbit SMG_g =P0^4;
sbit SMG_f =P0^5;
sbit SMG_a =P0^6;
sbit SMG_b =P0^7;
//----
uint JS_1mS =0;
uchar JS_1S =9;
uint JS_1000mS=0;
uchar DengYanSe =1; //1:紅色 3:黃色 2:綠色
bit BZ_hong =0;
bit BZ_huang = 0;
bit BZ_lv = 0;
uchar SJ_xianshi;
bit BZ_shanshuo =0;
bit BZ_ye =0; //夜間模式,黃燈閃
//共陽數碼管段碼
uchar code DuanMa_SMG[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
//
void chushihua_t0(void) //
{
TMOD=0x01;
TH0=0xfc; //(65536-1000)/256
TL0=0x18; //
TF0 =0;
ET0=1;
TR0=1; //
EA=1;
}
//==========================================
void DiaoDian()
{
P0 = 0xff;
P1 = 0xff;
P3 = 0xff;
P2 = 0xff;
EX0 =1;
PCON = 0x02;
_nop_();_nop_();_nop_();_nop_();
}
//=========按鍵及相關=============----
void anjian_shaumiao() //1mS
{
static uint JS_s=0;
static uchar SJ_s=0;
P3=0xff;
if(S_hong==0||S_lv==0||S_huang==0)
{
JS_s++;
JS_1000mS =0;
if(JS_s>=1000)
{
if(JS_s==1000)
{
if(S_huang==0)
{
DengYanSe = 3;
BZ_ye =1;
}
if(S_lv==0)
{
DengYanSe = 2;
JS_1S = 0;
BZ_ye = 0;
}
if(S_hong==0)
DiaoDian();
}
else
JS_s = 1200;
SJ_s =0;
}
else
{
if(JS_s>10)
{
if(S_lv==0) SJ_s=2;
if(S_huang==0) SJ_s=3;
if(S_hong==0) SJ_s=1;
}
}
}
else
{
JS_s =0;
if(SJ_s)
{
JS_1mS =0;
BZ_ye =0;
DengYanSe = SJ_s;
if(DengYanSe==3) //
JS_1S =3;
else
JS_1S = 9;
}
SJ_s =0;
}
}
//===================================================
void main()
{
uchar i =200;
while(i--);
P1 =0xff;
P0 =0xff;
DengYanSe =1;
JS_1S =9;
BZ_ye =0;
chushihua_t0();
while(1);
}
//==========================================
void timer0(void) interrupt 1 //
{
TR0 =0;
TF0 = 0;
TH0 = 0xfc;
TL0 = 0x18;
TR0 =1;
//--------------------------
JS_1mS++;
if(JS_1mS>=1000)
{
JS_1mS = 0;
if(!BZ_ye) //白天模式
{
JS_1S --;
if(JS_1S==0)
{
DengYanSe ++;
if(DengYanSe==4)
DengYanSe =1;
if(DengYanSe==3)
JS_1S = 3;
else
JS_1S =9;
}
}
JS_1000mS++;
if(JS_1000mS>=600)
{
DiaoDian(); //休眠模式
}
else
{
if(JS_1000mS>=480)//6分鐘
BZ_ye =1; //夜間模式
}
}
if(JS_1mS>=500)
BZ_shanshuo = 1;
else
BZ_shanshuo = 0;
//---數碼管顯示部分-----LED驅動--------
if((JS_1mS&0x000f)==0) //16毫秒刷新一次
{
P0 = 0xff;
P1 = 0xff;
}
if((JS_1mS&0x000f)==1)
{
SJ_xianshi = DuanMa_SMG[JS_1S];
if(!BZ_ye) //白天模式 顯示倒計時
{
SMG_a = SJ_xianshi&0x01;
SMG_b = SJ_xianshi&0x02;
SMG_c = SJ_xianshi&0x04;
SMG_d = SJ_xianshi&0x08;
SMG_e = SJ_xianshi&0x10;
SMG_f = SJ_xianshi&0x20;
SMG_g = SJ_xianshi&0x40;
}
else
if(!BZ_shanshuo)
SMG_dp = 0;
//-------------------
if(BZ_ye)
{
if(!BZ_shanshuo)
LED_huang = 0;
}
else
{
if(BZ_shanshuo&&JS_1S<4&&DengYanSe<3);
else
{
if(DengYanSe==1)
LED_hong = 0;
if(DengYanSe==2)
LED_lv = 0;
if(DengYanSe==3)
LED_huang = 0;
}
}
}
//----------------------------------------
anjian_shaumiao();
}
//===============================================
void INT0_HuanXing(void) interrupt 0 //
{
EX0 =0;
DengYanSe=3;
JS_1S = 3;
}
|