题目
代码
timer.c
#include <timer.h>
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=ET0=1;
}
timer.h
#ifndef _TIMER_H_
#define _TIEMR_H_
#include <STC15F2K60S2.H>
void Timer0Init(void);
#endif
smg.c
#include <smg.h>
unsigned char code smgduan[11]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XFF};
unsigned char dspbuf[8];
void smg_display(void)
{
static unsigned char i;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
P2&=0X1F;
P2=(P2&0X1F)|0XC0;
P0=1<<i;
P2&=0X1F;
P2=(P2&0X1F)|0XE0;
P0=smgduan[dspbuf[i]];
P2&=0X1F;
if(++i==8)
i=0;
}
smg.h
#ifndef _SMG_H_
#define _SMG_H_
#include <STC15F2K60S2.H>
extern unsigned char dspbuf[8];
void smg_display(void);
#endif
key.c
#include <key.h>
unsigned char key_sum=0;
unsigned char k4_press=0,k5_press=0,k6_press=0,k7_press=0;
void key_read(void)
{
static unsigned char k4_time=0,k5_time=0,k6_time=0,k7_time=0;
key_sum=0;
if(k4==0)
{
k4_time++;
if(k4_time==1)
{
key_sum=4;
}
else if(k4_time==20)
{
key_sum=0;
k4_time=1;
}
}
else
k4_time=0;
if(k5==0)
{
k5_time++;
if(k5_time==1)
{
key_sum=5;
}
else if(k5_time==20)
{
key_sum=0;
k5_time=1;
}
}
else
k5_time=0;
if(k6==0)
{
k6_time++;
if(k6_time==1)
{
key_sum=6;
}
else if(k6_time==20)
{
key_sum=0;
k6_time=1;
}
}
else
k6_time=0;
if(k7==0)
{
k7_time++;
if(k7_time==1)
{
key_sum=7;
}
else if(k7_time==20)
{
key_sum=0;
k7_time=1;
}
}
else
k7_time=0;
}
void key_control(void)
{
if(key_sum==4)
k4_press=1;
else if(key_sum==5)
k5_press=1;
else if(key_sum==6)
k6_press=1;
else if(key_sum==7)
k7_press=1;
}
key.h
#ifndef _KEY_H_
#define _KEY_H
#include <STC15F2K60S2.H>
sbit k7=P3^0;
sbit k6=P3^1;
sbit k5=P3^2;
sbit k4=P3^3;
void key_read(void);
void key_control(void);
extern unsigned char k4_press;
extern unsigned char k5_press;
extern unsigned char k6_press;
extern unsigned char k7_press;
#endif
adc.c
#include <adc.h>
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
//总线启动条件
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;
}
//等待应答
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;
}
unsigned char adc_read(void)
{
unsigned char dat=0;
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(0X03);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0X91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
adc.h
#ifndef _ADC_H_
#define _ADC_H_
#include <STC15F2K60S2.H>
unsigned char adc_read(void);
#endif
other.c
#include <other.h>
void bee_on(void)
{
P2=(P2&0X1F)|0XA0;
P0=0x40;
P2&=0X1F;
}
void bee_off(void)
{
P2=(P2&0X1F)|0XA0;
P0&=0XBF;
P2&=0X1F;
}
void relay_on(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X10;
P2=0X1F;
}
void relay_off(void)
{
P2=(P2&0X1F)|0XA0;
P0&=0XEF;
P2&=0X1F;
}
void led_right(void)
{
static unsigned char i;
P2=(P2&0X1F)|0X80;
P0=~(1<<i);
P2&=0X1F;
if(++i==8)
i=0;
}
void led_left(void)
{
static unsigned char i;
P2=(P2&0X1F)|0X80;
P0=~(0x80>>i);
P2&=0X1F;
if(++i==8)
i=0;
}
void led_off(void)
{
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2&=0X1F;
}
other.h
#ifndef _OTHER_H_
#define _OTHER_H_
#include <STC15F2K60S2.H>
void bee_on(void);
void bee_off(void);
void relay_on(void);
void relay_off(void);
void led_left(void);
void led_right(void);
void led_off(void);
#endif
main.c
#include <STC15F2K60S2.H>
#include <timer.h>
#include <key.h>
#include <other.h>
#include <smg.h>
#include <adc.h>
unsigned char flag_key;
unsigned char flag_led;
unsigned char flag_deng;
unsigned char led;
unsigned char led_mode;
unsigned char flag_adc;
long adc_dat;
unsigned int weight;
unsigned char flag_relay;
unsigned char flag_bee;
unsigned char transporter;
unsigned char flag_alarm;
unsigned char addr=0X00;
void main(void)
{
bee_off();
Timer0Init();
adc_init(0X03);
while(1)
{
/************key************/
if(flag_key)
{
flag_key=0;
key_read();
key_control();
}
if(k4_press)
{
k4_press=0;
transporter=1;
}
else if(k5_press)
{
k5_press=0;
led_mode=1;
}
else if(k6_press)
{
k6_press=0;
led_mode=2;
}
else if(k7_press)
{
k7_press=0;
transporter=0;
}
/************transporter************/
if(transporter)
{
led=1;
flag_relay=1;
flag_alarm=1;
}
else
{
led=0;
flag_relay=0;
flag_alarm=0;
}
/************adc************/
if(flag_adc)
{
flag_adc=0;
adc_dat=adc_read_dat();
}
/************weight************/
weight=adc_dat*1000/255;
if(flag_alarm)
{
if(weight>750)
{
flag_bee=1;
led=0;
flag_relay=0;
}
else
flag_bee=0;
}
else
flag_bee=0;
/************led************/
if(flag_deng) //加了20ms灯就不会一闪一闪的了
{
flag_deng=0;
if(led) //要开灯
{
if(led_mode==1) //右移
{
if(flag_led) //200ms时间间隔
{
flag_led=0;
led_right();
}
}
else if(led_mode==2)
{
if(flag_led)
{
flag_led=0;
led_left();
}
}
}
else
led_off();
}
/************eeprom************/
if(weight>750&&weight != eeprom_read(addr))
{
addr+=0x01;
eeprom_write(addr,adc_dat);
}
/************relay************/
if(flag_relay)
relay_on();
else
relay_off();
/************bee************/
if(flag_bee)
bee_on();
else
bee_off();
/************smg************/
dspbuf[0]=weight/1000;
dspbuf[1]=weight%1000/100;
dspbuf[2]=weight%100/10;
dspbuf[3]=weight%10;
dspbuf[4]=10;
dspbuf[5]=10;
dspbuf[6]=10;
dspbuf[7]=10;
}
}
void timer0_isr() interrupt 1
{
static unsigned char flag_key_time=0;
static unsigned char flag_led_time=0;
static unsigned char flag_deng_time=0;
static unsigned char flag_adc_time=0;
if(++flag_key_time==30)
{
flag_key_time=0;
flag_key=1;
}
if(++flag_deng_time==20)
{
flag_deng_time=0;
flag_deng=1;
}
if(++flag_led_time==200)
{
flag_led_time=0;
flag_led=1;
}
if(++flag_adc_time==100)
{
flag_adc_time=0;
flag_adc=1;
}
smg_display();
}