main.c
#include "sys.h"
#include "iic.h"
uint volt;
uchar k = 0;
uchar led1 = 0,led2 = 0,led3 = 0;
bit flag = 0;
bit l8;
void Function();
void main()
{
InitSystem();
Timer0Init();
while(1)
{
KeyScans();
ReadAIN3();
volt = rb2*1.96+0.2;
DisplaySMG();
Function();
}
}
void Function()
{
if(volt < Vp)
{
TR0 = 1;
k = 1;
if(led1)
{
OperateLed(1,0);
}
if(led2)
{
OperateLed(2,0);
}
if(led3)
{
OperateLed(3,0);
}
if(l8)
OperateLed(8,0);
else
OperateLed(8,1);
}
else
{
k = 0;
TR0 = 0;
count = 0;
flag = 0;
l8 = 0;
t0 = 0;
OperateLed(0xff,1);
led1 = led2 = led3 = 0;
}
}
sys.c
#include "sys.h"
void Select_74HC138(uchar channel)
{
switch(channel)
{
case 0: P2 = P2 & 0X1F; break;
case 4: P2 = (P2 & 0X1F) | 0X80; break;
case 5: P2 = (P2 & 0X1F) | 0XA0; break;
case 6: P2 = (P2 & 0X1F) | 0XC0; break;
case 7: P2 = (P2 & 0X1F) | 0XE0; break;
}
}
void InitSystem()
{
Select_74HC138(4);
P0 = 0xff;
Select_74HC138(5);
P0 = 0x00;
Select_74HC138(0);
}
void Delay(uint ms)
{
uint i;
for(ms; ms > 0; ms--)
for(i = 921; i > 0; i--);
}
void OperateLed(uchar who,status)
{
static uchar temp;
uchar zt;
switch(status)
{
case 0:
temp = temp | _crol_(0x01,who-1);
P0 = ~temp;
break;
case 1:
if(who == 0xff)
{
P0 = 0xff;
temp = 0;
}
else
{
zt = (~temp) | _crol_(0x01,who-1);
if(temp != (~zt))
temp = ~zt;
P0 = zt;
}
break;
case 2:
P0 = ~who;
temp = who;
break;
default: break;
}
Select_74HC138(4);
Select_74HC138(0);
}
uint t0;
uint t;//led8闪烁标志
void Timer0Init(void) //50毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB0; //设置定时初值
TH0 = 0x3C; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void ServiceTimer0() interrupt 1
{
if(k)
t0++;
if(t0 % 20 == 0)
{
count += 1;
}
if(t0 % 40 == 0)
{
led1 = 1;
}
if(t0 % 80 == 0)
{
led2 = 1;
}
if(t0 == 120)
{
t0 = 0;
led3 = 1;
flag = 1;
}
if(flag)
t++;
if(t == 20)
{
t = 0;
l8 = ~l8;
}
if(count == 99)
count = 99;
}
nixie.c
#include "sys.h"
#include "iic.h"
uchar code nixie[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1,0x8c,0x92,0xbf,0xff};
uint Vp = 300;
uchar count = 0;
uchar interface = 0;
void DisplayBit(uchar pos,value,bit k)
{
P0 = 0x00;
Select_74HC138(6);
Select_74HC138(0);
if(k)
P0 = nixie[value] & 0x7f;
else
P0 = nixie[value];
Select_74HC138(7);
Select_74HC138(0);
P0 = _crol_(0x01,pos);
Select_74HC138(6);
Select_74HC138(0);
Delay(1);
}
void DisplayVolt()
{
DisplayBit(0,10,0);
DisplayBit(5,volt / 100,1);
DisplayBit(6,volt / 10 % 10,0);
DisplayBit(7,volt % 10,0);
}
void DisplayVp()
{
DisplayBit(0,11,0);
DisplayBit(5,Vp / 100,1);
DisplayBit(6,Vp / 10 % 10,0);
DisplayBit(7,Vp % 10,0);
}
void DisplayCount()
{
DisplayBit(0,12,0);
DisplayBit(6,count / 10,0);
DisplayBit(7,count % 10,0);
}
void DisplaySMG()
{
switch(interface)
{
case 0: DisplayVolt(); break;
case 1: DisplayVp(); break;
case 2: DisplayCount(); break;
}
}
key.c
#include "sys.h"
void KeyScans()
{
if(s4 == 0)
{
Delay(10);
interface = (interface + 1) % 3;
while(s4 == 0)
{
DisplaySMG();
}
}
if(s5 == 0)
{
Delay(10);
if(interface == 1)
{
Vp -= 50;
if(Vp == -50)
Vp = 500;
}
while(s5 == 0)
{
DisplaySMG();
}
}
if(s6 == 0)
{
Delay(10);
if(interface == 1)
{
Vp += 50;
if(Vp == 550)
Vp = 0;
}
while(s6 == 0)
{
DisplaySMG();
}
}
}
sys.h
#ifndef _SYS_H_
#define _SYS_H_
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit s4 = P3^3;
sbit s5 = P3^2;
sbit s6 = P3^1;
#define uchar unsigned char
#define uint unsigned int
extern unsigned int volt;
extern unsigned char interface;
extern unsigned int Vp;
extern unsigned char k;
extern unsigned char count;
extern unsigned char led1,led2,led3;
extern bit flag;
extern bit l8;
extern unsigned int t0;
//sys.c
void Select_74HC138(unsigned char channel);
void InitSystem();
void Delay(unsigned int ms);
void OperateLed(unsigned char who,status);
void Timer0Init(void);
//nixie.c
void DisplayBit(unsigned char pos,value,bit k);
void DisplayVolt();
void DisplayVp();
void DisplayCount();
void DisplaySMG();
//key.c
void KeyScans();
#endif
可能快比赛了,有点浮躁,就写的有点麻烦,不过功能还是实现了的。