从新记录一下自己经常要用到的模块,也是必写的模块。
mian.c
#include <sys.h>
void main()
{
InitSystem();
while(1)
{
KeyScans();
Operate_key_val();
DisplayBit(0,1);
Led(1,0);
BMR(0x50,1);
}
}
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 Led(uchar pos, status)
{
static uchar temp;
uchar zt;
switch(status)
{
case 0:
temp = temp | _crol_(0x01, pos-1);
P0 = ~temp;
break;
case 1:
if(pos == 0xff)
{
P0 = 0xff;
temp = 0;
}
else
{
zt = (~temp) | _crol_(0x01, pos-1);
if(temp != (~zt))
temp = ~zt;
P0 = ~temp;
}
break;
case 2:
P0 = ~pos;
temp = pos;
break;
}
Select_74HC138(4);
Select_74HC138(0);
}
void BMR(uchar num, status)
{
static uchar temp;
if(status)
{
temp = num;
P0 = temp;
}
else if(status == 0xff)
P0 = 0xff;
else
{
temp = temp & (~num);
P0 = temp;
}
Select_74HC138(5);
Select_74HC138(0);
}
nixie.c
#include <sys.h>
uchar code nixie[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf, 0xff};
void DisplayBit(uchar pos, value)
{
P0 = 0X00;
Select_74HC138(6);
Select_74HC138(0);
if(pos == 5)
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);
}
key.c
#include <sys.h>
uchar key_val = 20;
uchar key_flag = 0;
void KeyScans()
{
uchar hang;
if(key_val == 20)
{
for(hang = 0; hang < 4; hang++)
{
P3 = _cror_(0XF7,hang) & 0xff;
if(l1 == 0)
{
Delay(10);
key_val = hang + 4*1;
while(!l1)
{
}
}
if(l2 == 0)
{
Delay(10);
key_val = hang + 4*2;
while(!l2)
{
}
}
if(l3 == 0)
{
Delay(10);
key_val = hang + 4*3;
while(!l3)
{
}
}
if(l4 == 0)
{
Delay(10);
key_val = hang + 4*4;
while(!l4)
{
}
}
}
}
}
void Operate_key_val()
{
if(key_val != 20)
{
switch(key_val)
{
case 12: key_flag = 0; break;
case 13: key_flag = 0; break;
case 16: key_flag = 0; break;
case 17: key_flag = 0; break;
default: key_flag++; break;
}
}
key_val = 20;
}
sys.h
#ifndef _SYS_H_
#define _SYS_H_
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit l1 = P4^4;
sbit l2 = P4^2;
sbit l3 = P3^5;
sbit l4 = P3^4;
#define uchar unsigned char
#define uint unsigned int
//sys.c
void Select_74HC138(unsigned char channel);
void InitSystem();
void Delay(unsigned int ms);
void Led(unsigned char pos, status);
void BMR(unsigned char num, status);
//nixie.c
void DisplayBit(unsigned char pos, value);
//key.c
void KeyScans();
void Operate_key_val();
#endif