模拟智能传送装置,未打开蜂鸣器提示。
/********
**模拟智能传送装置
**main.c
******/
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "iic.h"
#define uint unsigned int
#define uchar unsigned char
uint shuju,zhong;
uchar model,set,count,shuju00,i;
uchar yi,er,san,si,wu,liu,qi,ba,LED;
uchar shuma[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
//函数体的声明
void keyscan();
void display();
//
void allinit()
{
P2=0x80;P0=0xff; //关闭所有LED灯
P2=0xa0;P0=0x00; //关闭继电器,蜂鸣器。。。
P2=0xc0;P0=0xff; //打开所有位选
P2=0xff;P0=0xff; //关闭所有段选
}
//
void Delay_Ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=845;y>0;y--);
}
//
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
//*************************************************
//主程序入口
//*************************************************
void main()
{
i = 0;
set = 0;
model = 3; //上电工作在就绪状态
allinit(); //初始化单片机
Timer0Init(); //打开定时计数器
while(1)
{
keyscan(); //按键扫描
display();
}
}
//
void exter0() interrupt 1 //5ms 进入一次
{
count++;
}
//*************************************************
//按键检测函数
//*************************************************
void keyscan()
{
if(set==0) //未超重
{
if(model==0) //等待模式
{
LED = 0xfe;
if(P32==0) {model=1;while(P32==0);} //如果S5被按下,开始正向传送
if(P31==0) {model=2;while(P31==0);} //如果S6被按下,开始反向传送
P2 = 0xa0;P0 = 0x10; //打开继电器
yi=11;er=11;san=11;
si=11;wu=11;liu=11;
qi=11;ba=11;
}
//
else if(model==1) //正向传送模式
{
EA = 1; //打开中断
ET0 = 1;
shuju = iic_read(0x90,0x03); //获取当前的AD转换量。
shuju = (shuju*3.906); //转换为重量。
P2 = 0xa0;P0 = 0x10; //打开继电器
if(count>=20) {LED = _cror_(LED,1);count=0;} //选择那个LED灯点亮 //每0.1s循环移位
else {P2 = 0x80;P0 = LED;} //保持常亮
if(P31==0) {model=2;while(P31==0);} //如果S6被按下,从正向变成反向
if(P30==0) {model=3;while(P30==0);} //如果S7被按下,停止传送
yi=shuju/1000;er=shuju%1000/100;
san=shuju%100/10;si=shuju%10;
wu=11;liu=11;qi=11;ba=11;
if(shuju>=750) {set=1;zhong=751;i++;} //判断是否超重。
}
else if(model==2) //反向传送模式
{
EA = 1; //打开中断
ET0 = 1;
shuju = iic_read(0x90,0x03); //获取当前的AD转换量。
shuju = (shuju*3.906); //转换为重量。
P2 = 0xa0;P0 = 0x10; //打开继电器
if(count>=20) {LED = _crol_(LED,1);count=0;} //选择那个LED灯点亮 //每0.1s循环移位
else {P2 = 0x80;P0 = LED;} //保持常亮
if(P32==0) {model=1;while(P32==0);} //如果S6被按下,从反向变成正向
if(P30==0) {model=3;while(P30==0);} //如果S7被按下,停止传送
yi=shuju/1000;er=shuju%1000/100;
san=shuju%100/10;si=shuju%10;
wu=11;liu=11;qi=11;ba=11;
if(shuju>=750) {set=1;zhong=751;i++;} //判断是否超重。
}
else if(model==3) //停止模式
{
EA = 0; //关闭中断
ET0 = 0;
P2 = 0xa0;P0 = 0x00; //关闭继电器
P2 = 0x80;P0 = 0xff; //关闭LED灯
if(P33==0) {model=0;while(P33==0);} //s4按下进入准备状态
yi=11;er=11;san=11;
si=11;wu=11;liu=11;
qi=11;ba=11;
}
}
/
if(set==1) //已超重
{
EA = 0; //关闭中断
ET0 = 0;
shuju00 = iic_read(0x90,0x03); //获取重量
shuju = (shuju00*3.906);
if(i>255) {i=0;}
if(shuju > zhong) {zhong = shuju; iic_write(0xa0,0x00+i,shuju00);} //只存储最大值,把AD之后的结果存入EEPROM。
P2 = 0x80;P0 = 0xff; //关闭LED灯。
P2 = 0xa0;P0 = 0x00; //未打开蜂鸣器。
if(shuju<750) {set=0;} //判断是否未超重。
// zhong00 = iic_read(0xa0,0x00+i)*3.906; //查看是否正确存储。可在后四位显示查看。
yi=shuju/1000;er=shuju%1000/100;
san=shuju%100/10;si=shuju%10;
wu=11;liu=11;qi=11;ba=11;
}
}
//
void display()
{
P2=0xc0;
P0=0x01; //选择第一个数码管。
P2=0xff;
P0=shuma[yi]; //显示yi的值。
Delay_Ms(1);
P2=0xc0;
P0=0x02;
P2=0xff;
P0=shuma[er];
Delay_Ms(1);
P2=0xc0;
P0=0x04;
P2=0xff;
P0=shuma[san];
Delay_Ms(1);
P2=0xc0;
P0=0x08;
P2=0xff;
P0=shuma[si];
Delay_Ms(1);
P2=0xc0;
P0=0x10;
P2=0xff;
P0=shuma[wu];
Delay_Ms(1);
P2=0xc0;
P0=0x20;
P2=0xff;
P0=shuma[liu];
Delay_Ms(1);
P2=0xc0;
P0=0x40;
P2=0xff;
P0=shuma[qi];
Delay_Ms(1);
P2=0xc0;
P0=0x80;
P2=0xff;
P0=shuma[ba];
Delay_Ms(1);
}
#ifndef _IIC_H
#define _IIC_H
#include "stc15f2k60s2.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//函数声明
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(unsigned char ackbit);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
//***********************************************************************
unsigned int iic_read(unsigned char addr,unsigned char cmd);
void iic_write(unsigned char addr,unsigned char cmd,unsigned char date);
//***********************************************************************
#endif
/*
程序说明: IIC总线驱动程序 iic.c
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(12MHz)
日 期: 2011-8-9
*/
#include "iic.h"
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//应答位控制
//void IIC_Ack(unsigned char ackbit)
//{
// if(ackbit)
// {
// SDA = 0;
// }
// else
// {
// SDA = 1;
// }
// somenop;
// SCL = 1;
// somenop;
// SCL = 0;
// SDA = 1;
// somenop;
//}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
//***********************************************************************
void iic_write(unsigned char addr,unsigned char cmd,unsigned char date)
{
IIC_Start();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(cmd);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
//
unsigned int iic_read(unsigned char addr,unsigned char cmd)
{
unsigned int date;
IIC_Start();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(cmd);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(addr+1);
IIC_WaitAck();
date = IIC_RecByte();
IIC_Stop();
return date;
}
//***********************************************************************
个人见解,感谢阅读。