/********
**模拟智能传送装置
**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;
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)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0x28;
TF0 = 0;
TR0 = 1;
}
//
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);}
if(P31==0) {model=2;while(P31==0);}
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);
shuju = (shuju*3.906);
P2 = 0xa0;P0 = 0x10;
if(count>=20) {LED = _cror_(LED,1);count=0;}
else {P2 = 0x80;P0 = LED;}
if(P31==0) {model=2;while(P31==0);}
if(P30==0) {model=3;while(P30==0);}
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);
shuju = (shuju*3.906);
P2 = 0xa0;P0 = 0x10;
if(count>=20) {LED = _crol_(LED,1);count=0;}
else {P2 = 0x80;P0 = LED;}
if(P32==0) {model=1;while(P32==0);}
if(P30==0) {model=3;while(P30==0);}
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;
if(P33==0) {model=0;while(P33==0);}
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);}
P2 = 0x80;P0 = 0xff;
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];
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;
}
//***********************************************************************