过完年了,也不能老玩了,寒假一堆事,开学还得考试,真是都要emo了,先选一个开始做吧,十一届做完了的话,就去该做第十届了,废话不多说,先看原题
原题目
设计思路:
-
这第一眼看上去有4页,可是把我吓了一跳,我以为这个挺难的,后来仔细读了读,按键的切换模式并不多,四个按键,就是用来切换模式的,功能比较简单
-
就是led闪烁的数值范围需要看仔细,因为之前图方便,自己写了一个ledlight(x),随意控制某个灯,但是写的时候,忘了led灯是8位,不能叠加,所以这个函数只适合在需要一个led灯亮的时候起作用。
-
还是老方法,PCF8591和NE555,在写函数的时候需要注意一下,尤其是NE555,用到了定时器T0和T1
-
这个我回去还得钻研一下,因为在家写的话可以上网查资料,真正比赛就不行了。其他也没什么难点,个人觉得比十一届简单一点。
源代码
iic.c
#include "intrins.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;
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;
}
REG51.H
/*--------------------------------------------------------------------------
REG51.H
Header file for generic 80C51 and 80C31 microcontroller.
Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __REG51_H__
#define __REG51_H__
#define uchar unsigned char
#define uint unsigned int
unsigned char code shuzi[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xbf,0xff};
unsigned char code zimu[]={0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0xc7,0x8c,0xc1};
unsigned char code num[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
unsigned char code weizhi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char ledweizhi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};//8ÊÇÈ«Ãð
/* BYTE Register */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr P4 = 0xC0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* BIT Register */
/* PSW */
sbit CY = 0xD7;
sbit AC = 0xD6;
sbit F0 = 0xD5;
sbit RS1 = 0xD4;
sbit RS0 = 0xD3;
sbit OV = 0xD2;
sbit P = 0xD0;
/* TCON */
sbit TF1 = 0x8F;
sbit TR1 = 0x8E;
sbit TF0 = 0x8D;
sbit TR0 = 0x8C;
sbit IE1 = 0x8B;
sbit IT1 = 0x8A;
sbit IE0 = 0x89;
sbit IT0 = 0x88;
/* IE */
sbit EA = 0xAF;
sbit ES = 0xAC;
sbit ET1 = 0xAB;
sbit EX1 = 0xAA;
sbit ET0 = 0xA9;
sbit EX0 = 0xA8;
/* IP */
sbit PS = 0xBC;
sbit PT1 = 0xBB;
sbit PX1 = 0xBA;
sbit PT0 = 0xB9;
sbit PX0 = 0xB8;
/* P3 */
sbit RD = 0xB7;
sbit WR = 0xB6;
sbit T1 = 0xB5;
sbit T0 = 0xB4;
sbit INT1 = 0xB3;
sbit INT0 = 0xB2;
sbit TXD = 0xB1;
sbit RXD = 0xB0;
/* SCON */
sbit SM0 = 0x9F;
sbit SM1 = 0x9E;
sbit SM2 = 0x9D;
sbit REN = 0x9C;
sbit TB8 = 0x9B;
sbit RB8 = 0x9A;
sbit TI = 0x99;
sbit RI = 0x98;
#endif
void delay_ms(int ms){
int q,w;
for(q = 0;q<ms;q++){
for(w=845;w>0;w--);
}
}
void Delay500us() //@12.000MHz
{
unsigned char i, j;
i = 6;
j = 211;
do
{
while (--j);
} while (--i);
}
void Delay100us() //@12.000MHz
{
unsigned char i, j;
i = 2;
j = 39;
do
{
while (--j);
} while (--i);
}
void allinit()
{
P2=(P2&0x1f)|0x80;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xA0;
P0 = P0 & 0XAF;
P2&=0x1f;
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=0xFF;
P2&=0x1f;
}
void shumaguan_bit(unsigned char a[],unsigned char b)//
{
unsigned char code led_p[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0x88,0xc6};
unsigned char i;
for(i=0;i<=7;i++)
{
if((b>>(7-i))&0x01)
{
P2 = (P2 & 0X1F)|(0X07 << 5);
P0 = 0Xff;
P2 = (P2 & 0X1F)|(0X06 << 5);
P0 = (0x01 << i);
delay_ms(1);
P2 = (P2 & 0X1F)|(0X07 << 5);
P0 = led_p[a[i]];
delay_ms(1);
}
else
{
P2 = (P2 & 0X1F)|(0X07 << 5);
P0 = 0Xff;
P2 = (P2 & 0X1F)|(0X06 << 5);
P0 = (0x01 << i);
delay_ms(1);
P2 = (P2 & 0X1F)|(0X07 << 5);
P0 = 0xff;
delay_ms(1);
}
}
}
void shumaguan_shuzi(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=shuzi[b];
delay_ms(1);
}
void shumaguan_zimu(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=zimu[b];
delay_ms(1);
}
void ledlight(uchar x)
{
P2=(P2&0X1f)|0x80;
P0=ledweizhi[x];
}
MAIN.C
#include<reg51.h>
#include<intrins.h>
#include<iic.c>
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
int addata;
long adc_dat;
int voltage;
int dat;
int v;
unsigned int count_f = 0;
unsigned char count_t = 0;
unsigned int dat_f = 0;
void display_v();
void display_voltage_led();
void Read_AIN3();
void Scan_S7();
void Scan_S6();
void Scan_S5();
void Scan_S4();
void Init_Keys()
{
R1=R2=R3=R4=1;
}
void Init_Timer()
{
TH0 = 0xff;
TL0 = 0xff;
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
TMOD = 0x16;
ET0 = 1;
ET1 = 1;
EA = 1;
TR0 = 1;
TR1 = 1;
}
void Service_T0() interrupt 1
{
count_f++;
}
void Service_T1() interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
count_t++;
if(count_t == 20)
{
dat_f = count_f;
count_f = 0;
count_t = 0;
}
}
void display_f()
{
int f;
shumaguan_zimu(0,5);
ledlight(1);
if(dat_f>9999)
shumaguan_shuzi(3,dat_f/1000);
if(dat_f>999)
shumaguan_shuzi(4,dat_f/1000%10);
if(dat_f>99)
shumaguan_shuzi(5,dat_f/100%10);
if(dat_f>9)
shumaguan_shuzi(6,dat_f/10%10);
shumaguan_shuzi(7,dat_f%10);
if(1000<=dat_f&&dat_f<5000)
{
ledlight(3);
}
else if(dat_f>=10000)
{
ledlight(3);
}
}
void display_voltage_led()
{
voltage=dat*100/51;
P2=0x80;
P0=0xee;//L1 and L5
shumaguan_shuzi(6,voltage%100/10);
shumaguan_shuzi(7,voltage%10);
shumaguan_zimu(0,9);
P2=(P2&0X1f)|0xC0;P0=weizhi[5];
P2=(P2&0X1f)|0xFF;P0=shuzi[voltage/100]+0x80;
v=dat*100/51;
}
void Read_AIN3()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
}
void Scan_S7()
{
uchar temp=4;
if(R1==0)
{
delay_ms(50);
if(R1==0)
{
temp++;
while(R1==0);
}
}
if(temp%2==0)
{
}
if(temp%2==1)
{
while(1)
{
shumaguan_shuzi(0,11);
shumaguan_shuzi(1,11);
shumaguan_shuzi(2,11);
shumaguan_shuzi(3,11);
shumaguan_shuzi(4,11);
shumaguan_shuzi(5,11);
shumaguan_shuzi(6,11);
shumaguan_shuzi(7,11);
if(v!=200)
{
P2=0x80;
P0=0xee;//L1 and L5
}
if(R1==0)
{
delay_ms(50);
if(R1==0)
{
break;
while(R1==0);
}
}
}
}
}
void Scan_S6()
{
uchar temp=4;
if(R2==0)
{
delay_ms(50);
if(R2==0)
{
temp++;
while(R2==0);
}
}
if(temp%2==0)
{
Read_AIN3();
display_voltage_led();
}
if(temp%2==1)
{
while(1)
{
ledlight(8);
Read_AIN3();
display_v();
if(R2==0)
{
delay_ms(50);
if(R2==0)
{
break;
while(R2==0);
}
}
}
}
}
void Scan_S4()
{
uchar temp=4;
if(R4==0)
{
delay_ms(50);
if(R4==0)
{
temp++;
while(R4==0);
}
}
if(temp%2==0)
{
Read_AIN3();
display_voltage_led();
}
if(temp%2==1)
{
while(1)
{
display_f();
if(R4==0)
{
delay_ms(50);
if(R4==0)
{
break;
while(R4==0);
}
}
}
}
}
void display_v()
{
voltage=dat*100/51;
shumaguan_shuzi(6,voltage%100/10);
shumaguan_shuzi(7,voltage%10);
shumaguan_zimu(0,9);
P2=(P2&0X1f)|0xC0;P0=weizhi[5];
P2=(P2&0X1f)|0xFF;P0=shuzi[voltage/100]+0x80;
}
void Scan_S5()
{
uchar temp=4;
if(R3==0)
{
delay_ms(50);
if(R3==0)
{
temp++;
while(R3==0);
}
}
if(temp%2==0)
{
Read_AIN3();
display_voltage_led();
}
if(temp%2==1)
{
while(1)
{
dat=102;
display_v();
P2=0x80;
P0=0xfa;//L1 and L3
if(R3==0)
{
delay_ms(50);
if(R3==0)
{
break;
while(R3==0);
}
}
}
}
}
void main()
{
Init_Keys();
Init_Timer();
while(1)
{
Read_AIN3();
display_voltage_led();
Scan_S7();
Scan_S6();
Scan_S5();
Scan_S4();
}
}
把reg51.h放出来主要是因为我自己不会做.h,所以把自己之前用的数组和函数都放进reg51里了,就先做到这里,我去学高频了,过几天再做一套,五个小时那种