ブルーブリッジカップ電子類単片ユニットモジュール——標準プログラムテンプレート
27003 ワード
#include
#include "intrins.h"
#define u8 unsigned char
#define u16 unsigned int
#define NO_KEY 0xF0
#define KEY_STATE0 0
#define KEY_STATE1 1
#define KEY_STATE2 2
#define LED0 0x00
#define LED1 0x01
#define LED2 0x01<<1
#define LED3 0x01<<2
#define LED4 0x01<<3
#define LED5 0x01<<4
#define LED6 0x01<<5
#define LED7 0x01<<6
#define LED8 0x01<<7
#define NO_THING 0x00
#define BUZZER 0x40
#define RELAY 0x10
#define BU_RE 0x50
/*************************************** **********************************/
u8 code smg_du[]={
//
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e};
u8 code smg_point[]={
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};//0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
u8 code smg_wei[]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//
u8 code led_du[]={
LED0,LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8};
u8 code bure_du[]={
NO_THING,RELAY,BUZZER,BU_RE};
void Timer0Init(void) //1 @12.0MHz
{
AUXR |= 0x80; // 1T
TMOD &= 0xF0; //
TL0 = 0x20; //
TH0 = 0xD1; //
TF0 = 0; // TF0
TR0 = 1; // 0
ET0=1;
EA=1;
}
unsigned char key_scan()
{
static u8 key_state=KEY_STATE0;
u8 key1,key_temp,key_val;
P30=1;P31=1;P32=1;P33=1;P44=0;
if(P30==0) key1=0x10;
if(P31==0) key1=0x20;
if(P32==0) key1=0x30;
if(P33==0) key1=0x40;
if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1)) key1=0xf0;
key_temp=key1;
switch(key_state)
{
case KEY_STATE0:
if(key_temp!=NO_KEY)
{
key_state=KEY_STATE1;
}
break;
case KEY_STATE1:
if(key_temp==NO_KEY)
{
key_state=KEY_STATE0;
}
else
{
key_state=KEY_STATE2;
switch(key_temp)
{
case 0x10:key_val=7;break;
case 0x20:key_val=6;break;
case 0x30:key_val=5;break;
case 0x40:key_val=4;break;
}
}
break;
case KEY_STATE2:
if(key_temp==NO_KEY)
{
key_state=KEY_STATE0;
}
break;
}
return key_val;
}
bit key_flag=0;//
u8 key_value;//
u8 led_choose=0;//LED1
u8 bure_choose=0;//ULN2003
void main()
{
Timer0Init();
while(1)
{
if(key_flag)
{
key_flag=0;
key_value=key_scan();
switch(key_value)
{
case 4:
led_choose=1;
bure_choose=0;
break;
case 5:
led_choose=2;
bure_choose=1;
break;
case 6:
led_choose=3;
bure_choose=2;
break;
case 7:
led_choose=4;
bure_choose=3;
break;
}
}
}
}
void timer0() interrupt 1 using 1
{
static u16 key_count=0,display_count=0,i=0;
/*************************************** **********************************/
key_count++;
if(key_count==10)
{
key_count=0;
key_flag=1;
}
/*************************************** **********************************/
display_count++;
if(display_count==1)
{
P0=~led_du[led_choose];P2=0x80;P2=0x00;
}
if(display_count==2)
{
P0=bure_du[bure_choose];P2=0xA0;P2=0x00;
}
if(display_count==3)
{
display_count=0;
P2=0XE0;P0=~0X00;P2=0X00;
P2=0XC0;P0=smg_wei[i];P2=0X00;
P2=0XE0;P0=~smg_du[i];P2=0X00;
i++;
if(i==8) i=0;
}
}
みんながブルーブリッジカップの試合で優れた成績を収めることを祈ります!--南昌大学電子情報工学182班劉昊