蓝桥杯单片机第三届省赛自动售水机

   日期:2021-04-13     浏览:112    评论:0    
核心提示:#蓝桥杯单片机省三自动售水机提示:能力有限仅供参考提示:本人能力有限仅供新手使用,第一次发表博客难免有错误仅供参考。提示:以下是本篇文章正文内容,下面案例可供参考一、main.c代码如下:#include “main.h”bit s7;bit s6;bit flag;bit Vp_flag;uint Vp;uint water,water_value;void Timer0Init(void) //2毫秒@11.0592MHz{AUXR |= 0x80; //定

#蓝桥杯单片机省三自动售水机
提示:能力有限仅供参考

提示:本人能力有限仅供新手使用,第一次发表博客难免有错误仅供参考。

提示:以下是本篇文章正文内容,下面案例可供参考

一、main.c

代码如下:

#include “main.h”
bit s7;
bit s6;
bit flag;
bit Vp_flag;
uint Vp;
uint water,water_value;
void Timer0Init(void) //2毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x9A; //设置定时初值
TH0 = 0xA9; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA = 1;
ET0 = 1;
}
void key()
{
if(P30 == 0)
{
Delay5ms();
if(P30 == 0)
{s7 = 1;}
// while(!P30);
}
if(P31 == 0)
{
Delay5ms();
if(P31 == 0)
{s6 = 1;}
// while(!P30);
}
}
void main()
{
Y4; P0 = 0xff;
Y5; P0 = 0x00;
Timer0Init();
while(1)
{
key();
if(Vp_flag == 1)
{
Vp = AD_read(0x01);
Vp_flag = 0;
}
if((s7 == 0)&(s6 == 0)){smg_display(0,0,5,0,0,1,0,0);}
else if((s7 == 1)&(s6 == 0)){smg_display(0,0,5,0,water/1000,water%1000/100,water%100/10,water%10);}
if(s6 == 1)
{
smg_display(0,0,5,0,water_value/1000,water_value%1000/100,water_value%100/10,water_value%10)
}
}
}
void Timer0() interrupt 1
{
uint ms;
uchar ss;
smg();
ss++;
if(ss == 10)
{
Vp_flag = 1;
ss = 0;
if(Vp<125)
{
Y4; P0 = 0xfe;
}
else
{
Y4; P0 = 0xff;
}
}
if((s7 == 1)&(s6 == 0)&(flag == 0))
{
ms++;
Y5; P0 = 0x50;
if(ms == 50)
{
water++;
ms = 0;
if(water == 9999)
{
flag = 1;
s6 = 1;
}
}
}
else if(s6 == 1)
{
Y5; P0 = 0x00;
water_value = water/2;
}
}

main.h

#ifndef _MAIN_H
#define _MAIN_H
#include “common.h”
#include “smg.h”
#include “iic.h”
#endif

二、common.c 就是公共使用的库

代码如下(示例):
#include “common.h”
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (–j);
} while (–i);
}

common.h

#ifndef _COMMON_H
#define _COMMON_H
#include “stc15f2k60s2.h”
#include “intrins.h”
#define uchar unsigned char
#define uint unsigned int
#define Y4 P2 = (P2&0x1f)|0x9f
#define Y5 P2 = (P2&0x1f)|0xaf
#define Y6 P2 = (P2&0x1f)|0xcf
#define Y7 P2 = (P2&0x1f)|0xef
#define Y0 P2 = (P2&0x1f)|0x1f
void Delay5ms();
#endif

三、smg.c

#include “smg.h”
uchar code smg_dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0xc0&0x7f,0xf9&0x7f,0xa4&0x7f,0xb0&0x7f,0x99&0x7f,0x92&0x7f,0x82&0x7f,0xf8&0x7f,0x80&0x7f,0x90&0x7f,0xbf,0xff};
uchar code smg_wela[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar smg_display_buff[] = {1,2,3,4,5,6,7,8};
void smg_display(uchar d0,d1,d2,d3,d4,d5,d6,d7)
{
smg_display_buff[0] = d0;
smg_display_buff[1] = d1+10;
smg_display_buff[2] = d2;
smg_display_buff[3] = d3;
smg_display_buff[4] = d4;
smg_display_buff[5] = d5+10;
smg_display_buff[6] = d6;
smg_display_buff[7] = d7;
}
void smg()
{
uchar num;
Y6; P0 = smg_wela[num];
Y7; P0 = smg_dula[smg_display_buff[num]];
Y0; P0 = 0xff;
num++;
if(num == 8)
{
num = 0;
}
}

smg.h

#ifndef _SMG_H
#define _SMG_H
#include “common.h”
void smg();
void smg_display(uchar d0,d1,d2,d3,d4,d5,d6,d7);
#endif

四、iic.c

#include “iic.h”
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 /
sbit SCL = P2^0; /
时钟线 /
void IIC_Delay(unsigned char i)
{
do{ 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; // 0:应答,1:非应答
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;
}
//通过I2C总线发送数据
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;
}
//从I2C总线上接收数据
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;
}
uint AD_read(uchar add)
{
uint dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
dat = (dat
5)/2.55;
return dat;
}

ii.h

#ifndef _IIC_H
#define _IIC_H
#include “common.h”
uint AD_read(uchar add);
#endif

小结

后面的省赛陆续更新,我已经写过一遍了,正在写第二遍,代码优化不少,很少甚至没有,本人能力有限,也希望各位能人志士,提出宝贵的指导和建议。
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服