main.c
******************************************************************************
* 文件名:17电赛试题
* 描 述:
* 作 者:思索与猫
* 日 期: 19/5/12
* 备 注:
*
******************************************************************************
#include
#include
void main()
{
CloseFucker();
Timer0Init();
while(1)
{
ShowTable();
KeyDriver();
}
}
sys.c
#include
void Timer0Init() //[email protected]
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x20;
TH0 = 0xD1;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void T0_time() interrupt 1
{
static uint time_count = 0,sec_count = 0,PWM = 0;
KeyScan();
Display();
if(mode == 0&&++time_count >= 3000) //3秒换行
{
time_count = 0;
x++;
if(x >= 4) x = 0;
if(view == 2&&x >= 3) x = 0;
ShowLed(x); //根据行数亮灯
}
if(mode == 1&&sec_flag == 1&&++sec_count >= 1000) //计秒
{
sec_count = 0;
sec++;
if(sec >= 50) sec = 0;
}
if(mode == 2) //序列信号发生器,使用PWM
{
if(++PWM >= 80) PWM = 0;
if(PWM < 10)
{
P10 = 1;
}
else if(PWM < 30)
{
P10 = 0;
}
else if(PWM < 40)
{
P10 = 1;
}
else if(PWM < 50)
{
P10 = 0;
}
else if(PWM < 80)
{
P10 = 1;
}
}
}
void CloseFucker()
{
P2 = P2&0x1f|0xa0;
P0 = 0xaf;
P2 = P2&0x1f;
}
sys.h
#ifndef __SYS_H_
#define __SYS_H_
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;
//sys
void Timer0Init();
void CloseFucker();
//show
void Display();
void ShowTable();
void ShowLed(uchar led_dat);
//key
void KeyDriver();
void KeyAction(uchar keyvalue);
void KeyScan();
extern uchar Array1[4][3];
extern uchar Array2[4][3];
extern uchar Array3[3][3];
extern uchar x;
extern uchar mode,view;
extern bit sec_flag;
extern uchar sec;
#endif
display.c
#include
uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x80};//10black,11-
uchar Table[8];
uchar Array1[4][3] = {0,0,0,
0,0,0,
0,0,0,
0,0,0};
uchar Array2[4][3] = {0,0,0,
0,0,0,
0,0,0,
0,0,0};
uchar Array3[3][3] = {0,0,0,
0,0,0,
0,0,0,};
uchar x = 0;
uchar sec = 0;
void Display()
{
static uchar index = 0;
P2 = P2&0x1f|0xe0;
P0 = 0xff;
P2 = P2&0x1f;
P2 = P2&0x1f|0xc0;
P0 = 1<
key.c
#include
#define add 55 //使用宏定义
#define sort 56
#define conv 57
#define change 58
sbit Key_Out_1 = P3^0;
sbit Key_Out_2 = P3^1;
sbit Key_Out_3 = P3^2;
sbit Key_Out_4 = P3^3;
sbit Key_In_1 = P4^4;
sbit Key_In_2 = P4^2;
sbit Key_In_3 = P3^5;
sbit Key_In_4 = P3^4;
uchar KeySta[4][4] = {{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}};
uchar KeyCodeMap[4][4] = {{0, 1, 2, change}, //键盘图
{10, 11, 12, add},
{20, 21, 22, sort},
{30, 31, 32, conv}};
uchar mode = 0;
uchar view = 0;
bit sec_flag;
void KeyDriver()
{
uchar i = 0,j = 0;
static uchar KeyBack[4][4] = {{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}};
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(KeySta[i][j] != KeyBack[i][j])
{
if(KeySta[i][j] != 0)
{
KeyAction(KeyCodeMap[i][j]);
}
KeyBack[i][j] = KeySta[i][j];
}
}
}
}
void KeyAction(uchar keyvalue)
{
if(keyvalue == change) //切换按钮
{
mode++;
if(mode == 3) mode = 0;
}
if(mode == 0)
{
if(keyvalue == sort) //排序按钮
{
ArrayTwoSort();
}
else if(keyvalue == add) //加法按钮
{
view++;
ShowLed(0x08);
if(view >= 2)
{
ArrayAdd();
view = 0;
}
}
else if(keyvalue == conv) //乘法按钮
{
view++;
ShowLed(0x10);
if(view == 2)
{
ArrayConv();
}
if(view >= 3) view = 0;
}
else
{
if(view == 0) //矩阵元素加
{
Array1[keyvalue/10][keyvalue%10]++;
if(Array1[keyvalue/10][keyvalue%10] >= 100)
{
Array1[keyvalue/10][keyvalue%10] = 0;
}
}
else if(view == 1)
{
Array2[keyvalue/10][keyvalue%10]++;
if(Array2[keyvalue/10][keyvalue%10] >= 100)
{
Array2[keyvalue/10][keyvalue%10] = 0;
}
}
}
}
if(mode == 1)
{
if(keyvalue == 21) //计秒开始
{
sec_flag = 1;
sec = 0;
x = 0;
}
else if(keyvalue == 20) //计秒结束
{
sec_flag = 0;
}
}
}
void KeyScan()
{
static uchar KeyBuf[4][4] = {{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff}};
uchar i = 0;
static uchar keyout = 0;
switch(keyout)
{
case 0:Key_Out_1 = 0;Key_Out_4 = 1;break;
case 1:Key_Out_2 = 0;Key_Out_1 = 1;break;
case 2:Key_Out_3 = 0;Key_Out_2 = 1;break;
case 3:Key_Out_4 = 0;Key_Out_3 = 1;break;
}
KeyBuf[keyout][0] = KeyBuf[keyout][0]<<1|Key_In_1;
KeyBuf[keyout][1] = KeyBuf[keyout][1]<<1|Key_In_2;
KeyBuf[keyout][2] = KeyBuf[keyout][2]<<1|Key_In_3;
KeyBuf[keyout][3] = KeyBuf[keyout][3]<<1|Key_In_4;
for(i=0; i<4; i++)
{
if(KeyBuf[keyout][i] == 0x0f)
{
KeySta[keyout][i] = 1;
}
if(KeyBuf[keyout][i] == 0x00)
{
KeySta[keyout][i] = 0;
}
}
keyout++;
keyout &= 0x03;
}
fun.c
#include
void ArrayTwoSort() //二维矩阵排序(从大到小)
{
uchar i,z = 0,temp=0;
for(z=1; z<12; z++)
{
for(i=0; i<11; i++)
{
if(Array1[i/3][i%3]
fun.h
#ifndef __FUN_H_
#define __FUN_H_
#include
void ArrayTwoSort();
void ArrayAdd();
void ArrayConv();
#endif
这道题并没有很难的算法,即使不会大部分也能在网上找,一次简单练习。
序列信号发生器那部分可以也使用DA输出。