原创 跑表程序

2009-8-4 11:23 1151 6 6 分类: MCU/ 嵌入式
#include <c8051F020.h>
unsigned char Key4_4(void) //P3 低4 位输出,P2 高4 位输入
{
unsigned char i="0",j=0;
unsigned char Key="0xff",LINE=0x0e;
unsigned Delay;
P2MDOUT=0x00;
P3MDOUT=0xff;
//Key=P2;7/5/20097/5/2009
while(LINE!=0xff)
{
P3=LINE;
Key=P2;
if((Key&0xf0)!=0xf0)
{
for(Delay=0;Delay<10000;Delay++); //消抖
Key=P2;
if((Key&0xf0)==0xf0)
{
i++;
LINE<<=1;
LINE|=0x01;
continue;
}
switch(Key>>4)
{
case 0x0e: j="0";break;
case 0x0d:j=1;break;
case 0x0b:j=2;break;
case 0x07:j=3;break;
default:break;
}
while((P2&0xf0)!=0xf0); //监测按键弹起,无连击功能
return i*4+j;
}
i++;
LINE<<=1;
LINE|=0x01;
}
return 0xff;
}
主程序
#include <c8051F020.h>
#include <max7219.h>
#include <timer3.h>
#define SYSCLK 22118400
//void Timer3_Init(int counts);
unsigned char Key4_4(void);
//unsigned char xdata Time[4];
#define RESET 0x01
#define START 0x00
void SYSCLK_Init(void)
{
unsigned char i;
WDTCN=0xde;
WDTCN=0xad; //关看门狗,默认使能
OSCXCN=0x67; //
for(i=0;i<255;i++);
while(!(OSCXCN&0x80));
OSCICN=0x88; //22.1184Mhz
}
void PORT_Init(void)
{
//P1MDOUT|=0xff;
// P2MDOUT=
// P3MDOUT=
P0MDOUT&=~0xE0;
XBR2|=0x40;
}
main()
{
unsigned char Key;
bit Status="0";
SYSCLK_Init();
PORT_Init();
Timer3_Init(SYSCLK/12/100); // timer init
wr_max7219(Addr_Shutdowm,0x01); // max7219 init
wr_max7219(Addr_Scan_Limit,0x07);
wr_max7219(Addr_Intensity,0x0f);
wr_max7219(Addr_Decode_Mode,0x00);
wr_max7219(Addr_Digit0,0x7e);
wr_max7219(Addr_Digit1,0x7e);
wr_max7219(Addr_Digit2,0x7e|0x80);
wr_max7219(Addr_Digit3,0x7e);
wr_max7219(Addr_Digit4,0x00);
wr_max7219(Addr_Digit5,0x00);
wr_max7219(Addr_Digit6,0x00);
wr_max7219(Addr_Digit7,0x00);
EA=1;
while(1)
{
Key=Key4_4();
if(Key==RESET)
{
if(!Status)
{
Time[0]=0;
Time[1]=0;
Time[2]=0;
Time[3]=0;
}
wr_max7219(Addr_Digit0,max7219_7led_code[Time[H_MIN]]);
wr_max7219(Addr_Digit1,max7219_7led_code[Time[H_10SEC]]);
wr_max7219(Addr_Digit2,max7219_7led_code[Time[H_SEC]]);
wr_max7219(Addr_Digit3,max7219_7led_code[Time[H_100ms]]);
}
if(Key==START)
{
Status=!Status;
if(Status)
TMR3CN|=0x04;
else
TMR3CN&=~0x04;
}
}
}
MAX7219
#include <c8051F020.h>
typedef unsigned char uchar;
#define Addr_No_Op 0x00 //不工作寄存器地址
#define Addr_Digit0 0x01
#define Addr_Digit1 0x02
#define Addr_Digit2 0x03
#define Addr_Digit3 0x04
#define Addr_Digit4 0x05
#define Addr_Digit5 0x06
#define Addr_Digit6 0x07
#define Addr_Digit7 0x08
#define Addr_Decode_Mode 0x09 //译码模式寄存器地址
#define Addr_Intensity 0x0a //亮度控制寄存器地址(max7221)
#define Addr_Scan_Limit 0x0b //扫描控制寄存器地址
#define Addr_Shutdowm 0x0c //掉电模式寄存器地址
#define Addr_Display_Test 0x0f //显示检测寄存器地址
sbit Max7219_Din=P0^5;
sbit Max7219_Load=P0^6;
sbit Max7219_Clk=P0^7;
unsigned char code max7219_7led_code[18]={0x7e,0x30,0x6d,0x79, //0-1-2-3
0x33,0x5b,0x5f,0x70, //4-5-6-7
0x7f,0x7b,0x01,0x4e, //8-9-"-"-E
0x37,0x0e,0x67,0x00}; //H-L-P-空白
void wr_max7219(uchar addr,uchar dat)
{
uchar i;
Max7219_Load=0;
Max7219_Clk=0;
for(i=0;i<8;i++)
{
Max7219_Din = (bit)(addr&0x80);
addr=addr<<1;
Max7219_Clk=1;
Max7219_Clk=0;
}
for(i=0;i<8;i++)
{
Max7219_Din=(bit)(dat&0x80);
dat=dat<<1;
Max7219_Clk=1;
Max7219_Clk=0;
}
Max7219_Load=1;
//Max7219_Load=0;
}#define Addr_No_Op 0x00 //不工作寄存器地址
#define Addr_Digit0 0x01
#define Addr_Digit1 0x02
#define Addr_Digit2 0x03
#define Addr_Digit3 0x04
#define Addr_Digit4 0x05
#define Addr_Digit5 0x06
#define Addr_Digit6 0x07
#define Addr_Digit7 0x08
#define Addr_Decode_Mode 0x09 //译码模式寄存器地址
#define Addr_Intensity 0x0a //亮度控制寄存器地址(max7221)
#define Addr_Scan_Limit 0x0b //扫描控制寄存器地址
#define Addr_Shutdowm 0x0c //掉电模式寄存器地址
#define Addr_Display_Test 0x0f //显示检测寄存器地址
extern unsigned char code max7219_7led_code[18];
void wr_max7219(unsigned char addr,unsigned char dat);
#include <c8051F020.h>
#include <max7219.h>
sfr16 TMR3RL=0x92;
sfr16 TMR3=0x94;
#define H_100ms 0
#define H_SEC 1
#define H_10SEC 2
#define H_MIN 3
//void wr_max7219(uchar addr,uchar dat);
unsigned char xdata Time[4]={0};
void Timer3_Init(int counts)
{
TMR3CN=0x00;
TMR3RL=-counts;
TMR3=0xffff;
EIE2|=0x01;
// TMR3CN|=0x04;
}
void Timer3_ISR(void) interrupt 14 //0.01S 定时否则溢出
{
static j;
TMR3CN&=0x7f;
//软件清中断标志位
if((++j%10)!=0) return;
Time[H_100ms]++;
if(Time[H_100ms]>9)
{
Time[H_100ms]=0;
Time[H_SEC]++;
if(Time[H_SEC]>9)
{
Time[H_SEC]=0;
Time[H_10SEC]++;
if(Time[H_10SEC]>5)
{
Time[H_10SEC]=0;
Time[H_MIN]++;
}
}
}
wr_max7219(Addr_Digit0,max7219_7led_code[Time[H_MIN]]);
wr_max7219(Addr_Digit1,max7219_7led_code[Time[H_10SEC]]);
wr_max7219(Addr_Digit2,max7219_7led_code[Time[H_SEC]]|0x80);
wr_max7219(Addr_Digit3,max7219_7led_code[Time[H_100ms]]);
}
定时器头文件
#define H_100ms 0
#define H_SEC 1
#define H_10SEC 2
#define H_MIN 3
void Timer3_Init(int counts);
extern unsigned char xdata Time[4];
//
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条