文章目录
- 题目
- 要点
- 文件树
- 代码
-
- init.c
- main.c
- ds1302.c
- iic.c
- init.h
- type.h
题目
要点
- 选取数码管闪烁
- NE555初始化和检测检测
- 矩阵按键
其他应该就没有什么难点了,很常规这套题,只不过就是题没说清楚开机的时候应该处于什么状态,这里我默认就是时钟显示状态了
文件树
代码
init.c
#include "init.h"
uchar code table[4][4] = {
0,1,1,1, 1,0,1,1, 1,1,0,1, 1,1,1,0};
void SL(uchar _dev, uchar _data)
{
P0=_data;SEL(_dev);
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x40;
TH1 = 0xA2;
TF1 = 0;
TR1 = 1;
ET1 = 1;
EA = 1;
}
void BF(_0, _1, _2, _3,
_4, _5, _6, _7)
{
buf[0] = _0; buf[1] = _1; buf[2] = _2; buf[3] = _3;
buf[4] = _4; buf[5] = _5; buf[6] = _6; buf[7] = _7;
}
uchar FR(float _data, uchar _dig)
{
uint i = 1;
while(_dig--)
{
i = i * 10;
}
return((uint)_data/i%10);
}
void Run(t_delay* time)
{
if(time->cnt++ < time->max);
else
{
time->cnt = 0;
time->ok = 1;
}
}
void NE555Init()
{
AUXR |= 0x80;
TMOD |= 0x05;
}
void NE555Read()
{
if(delay_500.ok == 0)
{
TH0 = 0; TL0 = 0; TR0 = 1;
delay_500.ok = 2;
}
if(delay_500.ok == 1)
{
delay_500.ok = 0;
delay_500.cnt = 0;
TR0 = 0;
neVal = ((uint)TH0<<8) | TL0;
}
}
uchar GetKey()
{
uchar i;
C1 = 1; C2 = 1;
for(i = 0; i < 4; i++)
{
R1 = table[i][0];R2 = table[i][1];
R3 = table[i][2];R4 = table[i][3];
if(C1 == 0) return(2*i + 1);
if(C2 == 0) return(2*i + 2);
}
return 0;
}
main.c
#include "init.h"
enum{
LED=4,EXT,SEL,CODE};
uchar ledData = 0xff;
uchar buf[8];
uchar code CA[] = {
0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90,
0xff, 0xbf};
uchar curDig = 0;
enum{
KS_GT,KS_AS,KS_WA}keyState = KS_GT;
uchar key = 0, tmpKey = 0, keyCnt = 0;
t_delay delay_200 = {
200, 0, 0};
t_delay delay_500 = {
500, 0, 0};
t_delay delay_250 = {
250, 0, 0};
uint neVal;
uint hlv[] = {
2000,1000};
uchar chlv[] = {
(2000 >> 8), 2000, (1000 >> 8), 1000};
uchar timeDig = 250;
uchar volDig = 250;
uchar freDig = 0;
uchar leiXing = 0;
uchar cxDig = 0;
uchar storetime[3] = {
12, 12, 12};
enum{
S_TIME, S_VOL, S_FRE, S_CX}gloSta = S_TIME;
void Timer1Handle() interrupt 3
{
SL(CODE, 0xff); SL(SEL, 1<<curDig); SL(CODE, CA[buf[curDig]]);
curDig = (curDig + 1)%8;
switch(keyState)
{
case KS_GT:
keyCnt = 0; tmpKey = GetKey(); keyState = KS_AS;
break;
case KS_AS:
if(keyCnt++ < 10);
else if(tmpKey == GetKey())
{
if(key != tmpKey)
{
key = tmpKey;
keyState = KS_WA;
}
else
keyState = KS_GT;
}
else
{
keyState = KS_GT;
}
break;
}
Run(&delay_200);
if(delay_500.ok == 2)
Run(&delay_500);
Run(&delay_250);
}
void DisProcess()
{
switch(gloSta)
{
case S_TIME:
if(timeDig == 250)
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,FR(time[0], 1),FR(time[0], 0));
else
{
if(delay_250.ok == 1)
{
delay_250.ok = 0;
switch(timeDig)
{
case 0:
if(buf[0] == F_C)
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,FR(time[0], 1),FR(time[0], 0));
else
BF(F_C, F_C,F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,FR(time[0], 1),FR(time[0], 0));
break;
case 1:
if(buf[3] == F_C)
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,FR(time[0], 1),FR(time[0], 0));
else
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
F_C,F_C,F_SEP,FR(time[0], 1),FR(time[0], 0));
break;
case 2:
if(buf[6] == F_C)
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,FR(time[0], 1),FR(time[0], 0));
else
BF(FR(time[2], 1), FR(time[2], 0),F_SEP,
FR(time[1], 1),FR(time[1], 0),F_SEP,F_C,F_C);
break;
}
}
}
break;
case S_VOL:
if(volDig == 250)
BF(F_SEP, 1,F_SEP,
F_C,FR(adcVal, 3),FR(adcVal, 2),FR(adcVal, 1),FR(adcVal, 0));
else
{
if(delay_250.ok == 1)
{
delay_250.ok = 0;
switch(volDig)
{
case 0:
if(buf[0] == F_C)
BF(FR(hlv[0], 3), FR(hlv[0], 2),FR(hlv[0], 1),FR(hlv[0], 0),
FR(hlv[1], 3), FR(hlv[1], 2),FR(hlv[1], 1),FR(hlv[1], 0));
else
BF(F_C, F_C,F_C,F_C,
FR(hlv[1], 3), FR(hlv[1], 2),FR(hlv[1], 1),FR(hlv[1], 0));
break;
case 1:
if(buf[4] == F_C)
BF(FR(hlv[0], 3), FR(hlv[0], 2),FR(hlv[0], 1),FR(hlv[0], 0),
FR(hlv[1], 3), FR(hlv[1], 2),FR(hlv[1], 1),FR(hlv[1], 0));
else
BF(FR(hlv[0], 3), FR(hlv[0], 2),FR(hlv[0], 1),FR(hlv[0], 0),
F_C, F_C,F_C,F_C);
break;
}
}
}
break;
case S_FRE:
if(freDig == 0)
BF(F_SEP, 2, F_SEP,FR(neVal, 4),
FR(neVal, 3), FR(neVal, 2),FR(neVal, 1),FR(neVal, 0));
else if(freDig == 1)
{
float m;
m = 1000000/neVal;
BF(F_SEP, 2, F_SEP,FR(m, 4),
FR(m, 3), FR(m, 2),FR(m, 1),FR(m, 0));
}
break;
case S_CX:
if(cxDig == 0)
BF(F_C, F_C, F_C,F_C,
F_C, F_C,FR(leiXing, 1),FR(leiXing, 0));
else if(cxDig == 1)
{
BF(FR(storetime[2], 1), FR(storetime[2], 0), F_SEP,
FR(storetime[1], 1), FR(storetime[1], 0), F_SEP,
FR(storetime[0], 1), FR(storetime[0], 0));
}
break;
}
}
void SensorProcess()
{
if(delay_200.ok == 1)
{
static uchar chufa = 1;
delay_200.ok = 0;
if(timeDig == 250)
Time_Read();
adcVal = Adc_Read(0x43, 250);
if((adcVal > hlv[0]) && (chufa == 0))
{
chufa = 1;
leiXing = 1;
memcpy(storetime, time, 3);
Code_Write(1, &leiXing, 1);
Code_Write(2, storetime, 3);
Code_Write(1, &leiXing, 1);
Code_Write(2, storetime, 3);
}
else if((adcVal < hlv[1]) && (chufa == 0))
{
chufa = 2;
leiXing = 0;
memcpy(storetime, time, 3);
Code_Write(1, &leiXing, 1);
Code_Write(2, storetime, 3);
Code_Write(1, &leiXing, 1);
Code_Write(2, storetime, 3);
}
if((adcVal < hlv[0]) && ((chufa == 1) || (chufa == 2)) && (adcVal > hlv[1]))
chufa = 0;
}
}
void keyProcess()
{
if(key == 3)
{
gloSta = S_VOL;
}
if(key == 1)
{
gloSta = S_TIME;
}
if(key == 5)
{
gloSta = S_FRE;
freDig = 0;
}
if(key == 6)
{
gloSta = S_CX;
cxDig = 0;
}
switch(gloSta)
{
case S_TIME:
if(key == 7)
{
if(timeDig == 250)
timeDig = 0;
else
timeDig = (timeDig + 1)%3;
}
else if(key == 1)
{
TimeInit(time[2],time[1],time[0]);
timeDig = 250;
}
else if(key == 2)
{
switch(2-timeDig)
{
case 0:
if(time[0] < 59)
time[0] = time[0] + 1;
break;
case 1:
if(time[1] < 59)
time[1] = time[1] + 1;
break;
case 2:
if(time[2] < 23)
time[2] = time[2] + 1;
break;
}
}
else if(key == 4)
{
switch(2-timeDig)
{
case 0:
if(time[0] > 0)
time[0] = time[0] - 1;
break;
case 1:
if(time[1] > 0)
time[1] = time[1] - 1;
break;
case 2:
if(time[2] > 0)
time[2] = time[2] - 1;
break;
}
}
break;
case S_VOL:
if(key == 7)
{
if(volDig == 250)
volDig = 0;
else
volDig = (volDig + 1)%2;
}
else if(key == 3)
{
chlv[0] = (hlv[0] >> 8);
chlv[1] = hlv[0];
chlv[2] = (hlv[1] >> 8);
chlv[3] = hlv[1];
Code_Write(0, chlv, 4);
volDig = 250;
}
else if(key == 2)
{
switch(volDig)
{
case 0:
if(hlv[0] < 5000)
hlv[0] = hlv[0] + 500;
break;
case 1:
if(hlv[1] < 5000)
hlv[1] = hlv[1] + 500;
break;
}
}
else if(key == 4)
{
switch(volDig)
{
case 0:
if(hlv[0] > 0)
hlv[0] = hlv[0] - 500;
break;
case 1:
if(hlv[1] > 0)
hlv[1] = hlv[1] - 500;
break;
}
}
break;
case S_FRE:
if(key == 7)
freDig = (freDig + 1)%2;
break;
case S_CX:
if(key == 7)
cxDig = (cxDig + 1)%2;
break;
}
}
void main()
{
uchar tmpVal[3];
BPOFF;RLOFF;
SL(LED, 0xff);
Timer1Init();
memset(buf, F_C, 8);
NE555Init();
TimeInit(23,59,55);
Code_Read(0, chlv, 4);
hlv[0] = ((uint)chlv[0] << 8) | chlv[1];
hlv[1] = ((uint)chlv[2] << 8) | chlv[3];
Code_Read(1, &tmpVal, 1);
Code_Read(1, &leiXing, 1);
while(tmpVal[0] != leiXing)
{
Code_Read(1, &tmpVal, 1);
Code_Read(1, &leiXing, 1);
}
Code_Read(1, &storetime, 3);
Code_Read(1, &tmpVal, 3);
while(memcmp(storetime, tmpVal, 3) != 0)
{
Code_Read(1, &storetime, 3);
Code_Read(1, &tmpVal, 3);
}
Code_Read(2, storetime, 3);
while(1)
{
NE555Read();
DisProcess();
SensorProcess();
if(keyState == KS_WA)
{
keyProcess();
keyState = KS_GT;
}
}
}
ds1302.c
#include
#include
#include "type.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
uchar time[3];
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
uchar Ds1302_Read()
{
unsigned char i,temp=0x00;
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
return (temp);
}
uchar covert(uchar _data)
{
return(_data = ((_data/10%10) << 4) | (_data%10));
}
uchar covertback(uchar _data)
{
return((_data >> 4)*10 + (_data & 0x0f));
}
void TimeInit(uchar _h, uchar _m, uchar _s)
{
uchar i;
_h = covert(_h);
_m = covert(_m);
_s = covert(_s);
Write_Ds1302_Byte(0x8e, 0);
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(0xbe);
Write_Ds1302(_s);
Write_Ds1302(_m);
Write_Ds1302(_h);
i = 5;
while(i--)
{
Write_Ds1302(0);
}
RST=0;
Write_Ds1302_Byte(0x8e, 0x80);
}
void Time_Read()
{
uchar i;
Write_Ds1302_Byte(0x8e, 0);
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(0xbf);
for(i = 0; i < 3; i++)
{
time[i] = Ds1302_Read();
}
time[2] = covertback(time[2]);
time[1] = covertback(time[1]);
time[0] = covertback(time[0]);
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
Write_Ds1302_Byte(0x8e, 0x80);
}
iic.c
#include "reg52.h"
#include "intrins.h"
#include "type.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
float adcVal;
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;
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;
}
float Adc_Read(uchar _addr, uchar _data)
{
uchar tmpVal;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(_addr);
IIC_WaitAck();
IIC_SendByte(_data);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
tmpVal = IIC_RecByte();
IIC_SendAck(0);
return((float)IIC_RecByte()*1000/51.0);
}
void Code_Write(uchar _addr, uchar* _data, uchar _dig)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(_addr*8);
IIC_WaitAck();
while(_dig--)
{
IIC_SendByte(*_data++);
IIC_WaitAck();
}
IIC_Stop();
}
void Code_Read(uchar _addr, uchar* _data, uchar _dig)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(_addr*8);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
_dig--;
while(_dig--)
{
*_data++ = IIC_RecByte();
IIC_SendAck(0);
}
*_data++ = IIC_RecByte();
IIC_Stop();
}
init.h
#ifndef _INIT_H
#define _INIT_H
#include "stc15f2k60s2.h"
#include "stdio.h"
#include "string.h"
#include "intrins.h"
#include "type.h"
#define SEL(x) P2=P2&0x1f|x<<5; P2=P2&0x1f
sbit BP=P0^6; sbit RL=P0^4;
sbit C1 = P4^4; sbit C2 = P4^2;
sbit R1 = P3^0; sbit R2 = P3^1; sbit R3 = P3^2; sbit R4 = P3^3;
#define BPON BP=1;SEL(5)
#define RLON RL=1;SEL(5)
#define BPOFF BP=0;SEL(5)
#define RLOFF RL=0;SEL(5)
#define F_C 10
#define F_SEP 11
typedef struct delay
{
uint max;
uint cnt;
uchar ok;
}t_delay;
extern uchar buf[8];
extern t_delay delay_500;
extern uint neVal;
extern uchar time[3];
extern float adcVal;
void SL(uchar _dev, uchar _data);
void Timer1Init(void);
void BF(_0, _1, _2, _3,
_4, _5, _6, _7);
uchar FR(float _data, uchar _dig);
void Run(t_delay* time);
void NE555Init();
void NE555Read();
void TimeInit(uchar _h, uchar _m, uchar _s);
void Time_Read();
float Adc_Read(uchar _addr, uchar _data);
uchar GetKey();
void Code_Read(uchar _addr, uchar* _data, uchar _dig);
void Code_Write(uchar _addr, uchar* _data, uchar _dig);
#endif
type.h
#ifndef _TYPE_H
#define _TYPE_H
#define uchar unsigned char
#define uint unsigned int
#endif