一、题目要求

二、代码展示
#include <STC15F2K60S2.h>
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
uchar code SMG_duanma[18] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f};
uchar code SMG_Dot_AC[10] =
{0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
uint time_001s = 0;
uchar guang = 0;
void Initsys();
void SelectHC138(uchar channel);
void DisplaySMG_Bit(uchar pos, uchar value);
void SMG_Display();
void Delay_one_ms_SMG();
void Alone_Key();
void Delay_five_ms_Key();
void InitTimer0();
void Mod_1_Strat();
void Mod_2_Stop();
uchar IIC_read(uchar add);
void Light_Check();
void main()
{
Initsys();
InitTimer0();
while(1)
{
Light_Check();
Alone_Key();
}
}
void Light_Check()
{
guang = IIC_read(0X01);
if(guang < 64)
{
SelectHC138(4);
P0 = 0XFE;
}
else
{
SelectHC138(4);
P0 = 0XFF;
}
}
void Mod_1_Strat()
{
SelectHC138(5);
P0 = 0X10;
while(1)
{
Light_Check();
SMG_Display();
Alone_Key();
if(time_001s == 99999)
{
Mod_2_Stop();
break;
}
}
}
void Mod_2_Stop()
{
EA = 0;
SelectHC138(5);
P0 = 0X00;
time_001s = time_001s / 2;
while(1)
{
Light_Check();
SMG_Display();
}
}
void InitTimer0()
{
TMOD = 0x01;
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void ServiceTimer0() interrupt 1
{
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
time_001s++;
}
void Delay_five_ms_Key()
{
uint i,j;
for(i = 0; i < 5; i++)
for(j = 845; j > 0; j--);
}
void Alone_Key()
{
if(P30 == 0)
{
Delay_five_ms_Key();
if(P30 == 0)
{
Mod_1_Strat();
}
while(!P30)
{
Light_Check();
SMG_Display();
}
}
else if(P31 == 0)
{
Delay_five_ms_Key();
if(P31 == 0)
{
Light_Check();
Mod_2_Stop();
}
while(!P31)
{
SMG_Display();
}
}
}
void SelectHC138(uchar channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0X1F) | 0X80;
break;
case 5:
P2 = (P2 & 0X1F) | 0Xa0;
break;
case 6:
P2 = (P2 & 0X1F) | 0Xc0;
break;
case 7:
P2 = (P2 & 0X1F) | 0Xe0;
break;
case 0:
P2 = (P2 & 0X1F) | 0X00;
break;
}
}
void Initsys()
{
SelectHC138(5);
P0 = 0X00;
SelectHC138(4);
P0 = 0XFF;
SelectHC138(6);
P0 = 0XFF;
SelectHC138(7);
P0 = 0XFF;
}
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
void SMG_Display()
{
DisplaySMG_Bit(1, SMG_Dot_AC[0]);
Delay_one_ms_SMG();
DisplaySMG_Bit(2, SMG_duanma[5]);
Delay_one_ms_SMG();
DisplaySMG_Bit(3, SMG_duanma[0]);
Delay_one_ms_SMG();
DisplaySMG_Bit(4, SMG_duanma[time_001s / 10000]);
Delay_one_ms_SMG();
DisplaySMG_Bit(5, SMG_Dot_AC[time_001s % 10000 / 1000]);
Delay_one_ms_SMG();
DisplaySMG_Bit(6, SMG_duanma[time_001s % 1000 / 100]);
Delay_one_ms_SMG();
DisplaySMG_Bit(7, SMG_duanma[time_001s % 100 / 10]);
Delay_one_ms_SMG();
}
void Delay_one_ms_SMG()
{
uint j;
for(j = 845; j > 0; j--);
}
uchar IIC_read(uchar add)
{
uchar temp;
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0X91);
IIC_WaitAck();
temp = IIC_RecByte();
IIC_Stop();
return temp;
}
三、注意事项
- define 换行时,要加
\
- 使用我写的模板
DisplaySMG_Bit
第二个输入参数,记得要用开头定义的数组 iic,c
文件中,要8-12倍延时
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理