重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
难度不算大,直接上代码
main.c
#include#include "IIC.H"
bit L1,L2,L3,L4,L5,L6,L7,L8,relay,buzzer,key_line,relay_switch,led_open;
unsigned char led_switch,time_8ms,time_20ms,SMG[8];
unsigned int time_200ms,kg;
unsigned char code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
unsigned char code t_com[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
void smg(unsigned char wei,duan) //数码管显示函数
{
P0=0xFF;
P2=0xC0;P0=t_com[wei];P2=0x00;
P0=0xFF;
P2=0xE0;P0=~t_display[duan];P2=0x00;
P0=0xFF;
}
void Kg_Display(void) //货物重量数码管显示函数
{
SMG[0]=16;
SMG[1]=16;
SMG[2]=16;
SMG[3]=16;
if(kg==1000){SMG[4]=kg/1000;}
else{SMG[4]=16;}
SMG[5]=kg/100%10;
SMG[6]=kg/10%10;
SMG[7]=kg%10;
}
void clock_set(void) //初始化led、继电器、蜂鸣器函数
{
P0=0xFF;P2=0x80;
if(L1==1){P00=0;}else{P00=1;}
if(L2==1){P01=0;}else{P01=1;}
if(L3==1){P02=0;}else{P02=1;}
if(L4==1){P03=0;}else{P03=1;}
if(L5==1){P04=0;}else{P04=1;}
if(L6==1){P05=0;}else{P05=1;}
if(L7==1){P06=0;}else{P06=1;}
if(L8==1){P07=0;}else{P07=1;}
P2=0xA0;P0=0x00;
if(relay==1){P04=1;}else{P04=0;}
if(buzzer==1){P06=1;}else{P06=0;}
P2=0x00;
}
void key_work(void) //独立键盘操作函数
{
if(key_line==0) //按键扫描
{
P44=0;P30=1;P31=1;P32=1;P33=1;
if(P30==0) //S7 关闭传送带(关闭继电器、led)
{
while(!P30);
relay_switch=0;buzzer=0;
led_open=0;led_switch=0;
time_200ms=0;
}
if(P31==0) //S6 led向右流水
{
while(!P31);
if(led_open==1){led_switch=2;}
}
if(P32==0) //S5 led向左流水
{
while(!P32);
if(led_open==1){led_switch=1;}
}
if(P33==0) //S4 打开传送带(打开继电器、led)
{
while(!P33);
relay_switch=1;
led_open=1;
}
}
}
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x20; //设置定时初始值
TH1 = 0xD1; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
int main()
{
P2=0x80;P0=0xFF;P2=0x00;
P2=0xA0;P0=0x00;P2=0x00;
Timer1Init(); //定时器1初始化
while(1)
{
key_work(); //独立按键操作
}
}
void led_right(void) //led向右0.2s流水
{
time_200ms++;
if(time_200ms==200){L1=1;L2=0;L3=0;L4=0;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==400){L1=0;L2=1;L3=0;L4=0;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==600){L1=0;L2=0;L3=1;L4=0;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==800){L1=0;L2=0;L3=0;L4=1;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==1000){L1=0;L2=0;L3=0;L4=0;L5=1;L6=0;L7=0;L8=0;}
if(time_200ms==1200){L1=0;L2=0;L3=0;L4=0;L5=0;L6=1;L7=0;L8=0;}
if(time_200ms==1400){L1=0;L2=0;L3=0;L4=0;L5=0;L6=0;L7=1;L8=0;}
if(time_200ms==1600){L1=0;L2=0;L3=0;L4=0;L5=0;L6=0;L7=0;L8=1;time_200ms=0;}
}
void led_left(void) //led向左0.2s流水
{
time_200ms++;
if(time_200ms==200){L1=0;L2=0;L3=0;L4=0;L5=0;L6=0;L7=0;L8=1;}
if(time_200ms==400){L1=0;L2=0;L3=0;L4=0;L5=0;L6=0;L7=1;L8=0;}
if(time_200ms==600){L1=0;L2=0;L3=0;L4=0;L5=0;L6=1;L7=0;L8=0;}
if(time_200ms==800){L1=0;L2=0;L3=0;L4=0;L5=1;L6=0;L7=0;L8=0;}
if(time_200ms==1000){L1=0;L2=0;L3=0;L4=1;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==1200){L1=0;L2=0;L3=1;L4=0;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==1400){L1=0;L2=1;L3=0;L4=0;L5=0;L6=0;L7=0;L8=0;}
if(time_200ms==1600){L1=1;L2=0;L3=0;L4=0;L5=0;L6=0;L7=0;L8=0;time_200ms=0;}
}
void led_off(void) //led熄灭
{
L1=0;L2=0;L3=0;L4=0;
L5=0;L6=0;L7=0;L8=0;
}
void task_work(void) //模块操作函数
{
Kg_Display(); //货物重量显示
if(relay_switch==1)
{
relay=1; //打开继电器
if(kg>750){relay=0;led_open=0;buzzer=1;} //重量大于750kg关闭传送带,打开蜂鸣器
else{led_open=1;buzzer=0;} //关闭蜂鸣器,传送带正常运转
}
else{relay=0;} //关闭继电器
if(led_open==0){led_off();} //打开led
else
{
if(led_switch==1){led_right();} //led右流转
if(led_switch==2){led_left();} //led左流转
}
}
void server() interrupt 3 //定时器1中断函数
{
if(++time_8ms==8)time_8ms=0;
smg(time_8ms,SMG[time_8ms]); //数码管1ms刷新
if(++time_20ms==20){time_20ms=0;key_line=~key_line;kg=adc(0x03)*200/51;}
//每20ms扫描一次按键,adc读取可调电压
task_work();
clock_set();
}
iic.c
#include "iic.h"
#include "reg52.h"
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
#define DELAY_TIME 5
//
void IIC_Delay(unsigned char i)
{
do{_nop_();_nop_();_nop_();}
while(i--);
}
//
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt<<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da<<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
//以上为官方比赛时会提供的代码
//以下代码需自行添加
unsigned char adc (unsigned char addr)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return temp;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
//以上为官方比赛时提供代码
//以下代码需自行添加
unsigned char adc (unsigned char addr);
#endif
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧