文件下载地址:
https://download.csdn.net/download/weixin_43130546/12260891
main.c
#include
extern bit enBuzzer;
extern bit enLedflow;
extern bit enMatrix;
extern void ConfigTimer0(void);
extern void ConfigUART(void);
extern void UartDriver(void);
extern void UartText(unsigned char *buf, unsigned char len);
extern void InitLedflow(void);
extern void MatrixShow(void);
extern void InitMatrix(void);
extern void InitLcd1602(void);
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len);
extern void InitDigi(void);
void main(void)
{
ConfigTimer0();
ConfigUART();
InitMatrix();
InitDigi();
InitLcd1602();
while(1)
{
UartDriver();
MatrixShow();
}
}
bit CmpMemory(unsigned char *ptr1, unsigned char *ptr2, unsigned char len)
{
while(len--)
{
if(*ptr1++ != *ptr2++)
{
return 0;
}
}
return 1;
}
void UartAction(unsigned char *buf, unsigned char len)
{
unsigned char i;
unsigned char code cmd0[] = "buzzer on";
unsigned char code cmd1[] = "buzzer off";
unsigned char code cmd2[] = "led on";
unsigned char code cmd3[] = "led off";
unsigned char code cmd4[] = "matrix on";
unsigned char code cmd5[] = "matrix off";
unsigned char code cmd6[] = "digishowstr ";
unsigned char code cmd7[] = "lcdshowstr ";
unsigned char code cmdLen[] =
{
sizeof(cmd0)-1,sizeof(cmd1)-1,sizeof(cmd2)-1,sizeof(cmd3)-1,
sizeof(cmd4)-1,sizeof(cmd5)-1,sizeof(cmd6)-1,sizeof(cmd7)-1
};
unsigned char code *cmdPtr[] =
{
&cmd0[0],&cmd1[0],&cmd2[0],&cmd3[0],
&cmd4[0],&cmd5[0],&cmd6[0],&cmd7[0]
};
for(i=0; i<sizeof(cmdLen); i++)
{
if(len >= cmdLen[i])
{
if(CmpMemory(buf, cmdPtr[i], cmdLen[i]))
{
break;
}
}
}
switch(i)
{
case 0 : enBuzzer = 1; break;
case 1 : enBuzzer = 0; break;
case 2 : enLedflow = 1; InitLedflow(); break;
case 3 : enLedflow = 0; InitLedflow(); break;
case 4 : enMatrix = 1; break;
case 5 : enMatrix = 0; InitMatrix(); break;
case 6 : ; break;
case 7 :
buf[len] = '\0';
LcdShowStr(0,0,buf+cmdLen[7]);
i = len - cmdLen[7];
if(i < 16)
{
LcdAreaClear(i, 0, 16-i);
}
break;
default : UartText("bad command.\r\n", sizeof("bad command.\r\n")-1); return;
}
buf[len++] = '\r';
buf[len++] = '\n';
UartText(buf,len);
}
interruptTimer0.c
#include
//#include
const unsigned char TIMER0MS = 1;
unsigned char T0RH, T0RL;
extern void UartMonitor(void);
extern void BuzzerScan(void);
extern void LedflowScan(void);
extern void LedScan(void);
extern void MatrixScan(void);
void ConfigTimer0(void)
{
unsigned long tmp;
tmp = 11059200 / 12;
tmp = tmp * TIMER0MS / 1000;
tmp = 65536 - tmp;
tmp += 18;
T0RH = (unsigned char)(tmp>>8);
T0RL = (unsigned char)tmp;
TH0 = T0RH;
TL0 = T0RL;
TMOD &= 0xF0;
TMOD |= 0x01;
EA = 1;
ET0 = 1;
TF0 = 0;
TR0 = 1;
//P1 = ~0x01;
}
void InterruptTimer0(void) interrupt 1
{
static unsigned int flag200ms = 0;
static unsigned int flag1s = 0;
TH0 = T0RH;
TL0 = T0RL;
UartMonitor();
BuzzerScan();
//LedScan();
flag200ms++;
flag1s++;
if(flag200ms >= 200)
{
flag200ms = 0;
MatrixScan();
}
if(flag1s >= 1000)
{
flag1s = 0;
//P1 = _crol_(P1,1);
LedflowScan();
}
}
interruptUART.c
#include
const unsigned long BAUD = 9600;
unsigned char cntRxd = 0;
unsigned char pdata bufRxd[64];
bit flagTxd = 0;
bit flagFrame = 0;
extern const unsigned char TIMER0MS;
extern void UartAction(unsigned char *buf, unsigned char len);
void ConfigUART(void)
{
TH1 = 256 - (11059200/12/32) / BAUD;
TL1 = TH1;
TMOD &= 0x0F;
TMOD |= 0x20;
SCON = 0x50;
EA = 1;
ET1 = 0;
ES = 1;
TF1 = 0;
TR1 = 1;
}
void UartText(unsigned char *buf, unsigned char len)
{
while(len--)
{
flagTxd = 0;
SBUF = *buf++;
while(!flagTxd);
}
}
unsigned char UartRead(unsigned char *buf, unsigned char len)
{
unsigned char i;
if(len > cntRxd)
{
len = cntRxd;
}
for(i=0; i<len; i++)
{
*buf++ = bufRxd[i];
}
cntRxd = 0;
return len;
}
//void UartAction(unsigned char *buf, unsigned char len)
//{
// buf[len++] = '\r';
// buf[len++] = '\n';
// UartText(buf,len);
//}
void UartDriver(void)
{
unsigned char len;
unsigned char pdata buf[40];
if(flagFrame)
{
flagFrame = 0;
len = UartRead(buf,sizeof(buf));
UartAction(buf,len);
}
}
void UartMonitor(void)
{
static unsigned char cntbkp = 0;
static unsigned char idletmr = 0;
if(cntRxd > 0)
{
if(cntRxd != cntbkp)
{
cntbkp = cntRxd;
idletmr = 0;
}
else
{
idletmr += TIMER0MS;
if(idletmr >= 30)
{
idletmr = 0;
flagFrame = 1;
}
}
}
else
{
cntbkp = 0;
idletmr = 0;
}
}
void InterruptUART(void) interrupt 4
{
if(RI)
{
RI = 0;
if(cntRxd < sizeof(bufRxd))
{
bufRxd[cntRxd++] = SBUF;
//SBUF = SBUF + 1;
}
}
if(TI)
{
TI = 0;
flagTxd = 1;
}
}
buzzer.c
#include
sbit BUZZER = P2^3;
bit enBuzzer = 0;
void BuzzerScan(void)
{
if(enBuzzer)
{
BUZZER = ~BUZZER;
}
else
{
BUZZER = 1;
}
}
LedFlow.c
#include
#include
bit enLedflow = 0;
void InitLedflow(void)
{
if(enLedflow)
{
P1 = ~0x01;
}
else
{
P1 = 0xFF;
}
}
void LedflowScan(void)
{
if(enLedflow)
{
P1 = _crol_(P1,1);
}
else
{
P1 = 0xFF;
}
}
matrix.c
#include
sbit DIO = P3^4;//串口发送引脚
sbit S_CLK = P3^5;//发送时钟
sbit R_CLK = P3^6;//显示时钟
bit enMatrix = 0;
unsigned char MatrixIndex = 0;//移屏索引
unsigned char code MatrixBuff[40] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xC3,0xC3,0xFF,0xFF,0xC3,0xC3,0x00,
0x38,0x7C,0x7E,0x3F,0x3F,0x7E,0x7C,0x38,
0x00,0xFE,0xFF,0x03,0x03,0xFF,0xFE,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};//阴码给1为点亮,列码
void MatrixSendByte(unsigned char byte)
{
unsigned char i;
S_CLK = 0;
for(i=0; i<8; i++)
{
if(byte&0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
byte >>= 1;
S_CLK = 1;
S_CLK = 0;
}
}
void MatrixShow(void)
{
if(enMatrix)
{
unsigned char i;
unsigned char row = ~0x01;//行row,列column
R_CLK = 0;
for(i=0; i<8; i++)
{
MatrixSendByte(row);//row:0111-1111
MatrixSendByte(MatrixBuff[MatrixIndex+i]);//col
row = ~(~row<<1);
R_CLK = 1;
R_CLK = 0;
}
}
}
void MatrixScan(void)
{
if(enMatrix)
{
MatrixIndex++;
if(MatrixIndex >= 32)
{
MatrixIndex = 0;
}
}
}
void InitMatrix(void)
{
R_CLK = 0;
MatrixSendByte(0xFF);
MatrixSendByte(0x00);
R_CLK = 1;
}
digi.c
#include
sbit DULA = P2^6;//段锁存
sbit WELA = P2^7;//位锁存
unsigned char code DUTable[16] =
{
0x3F,0x06,0x5B,0x4F,
0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,
0x39,0x5E,0x79,0x71
};
unsigned char code WETable[8] =
{
~0x80,~0x40,~0x20,~0x10,
~0x08,~0x04,~0x02,~0x01
};
unsigned char LedBuff[8] =
{
//0x00,0x00,0x00,0X00,
//0X00,0X00,0X00,0X00
0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3F
};
void LedScan(void)//定时器1ms刷新
{
static unsigned char i = 0;
P0 = 0x00;
DULA = 1;
DULA = 0;
P0 = WETable[i];
WELA = 1;
WELA = 0;
P0 = LedBuff[i];
DULA = 1;
DULA = 0;
i++;
i &= 0x07;
}
void InitDigi(void)
{
P0 = 0x00;
DULA = 1;
DULA = 0;
P0 = 0xFF;
WELA = 1;
WELA = 0;
}
LCD1602.c
#include
#define LCD1602_DB P0//鏁版嵁鎬荤嚎
sbit LCD1602_RS = P3^5;//鏁版嵁1&鍛戒护0
sbit LCD1602_RW = P3^6;//璇?&鍐?
sbit LCD1602_E = P3^4;//鑴夊啿
void LcdWaitReady(void)
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
}
while(sta & 0x80);
}
void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if(0==y)
{
addr = 0x00 + x;
}
else
{
addr = 0x40 + x;
}
LcdWriteCmd(addr | 0x80);
}
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
LcdSetCursor(x,y);
while(*str != '\0')
{
LcdWriteDat(*str++);
}
}
void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
LcdSetCursor(x,y);
while(len--)
{
LcdWriteDat(' ');
}
}
void LcdFullClear(void)
{
LcdWriteCmd(0x01);
}
void InitLcd1602(void)
{
LCD1602_E = 0;
LcdWriteCmd(0x38);//璁剧疆鏄剧ず妯″紡0x38锛?6*2鏄剧ず锛?*7鐐归樀锛?浣嶆暟鎹帴鍙?
LcdWriteCmd(0x0F);//0b00001DCB锛欴-寮€鏄剧ず1&鍏虫樉绀?锛汣-寮€鍏夋爣1&鍏冲厜鏍?锛汢-寮€鍏夋爣闂儊1&鍏冲厜鏍囬棯鐑?
LcdWriteCmd(0x06);//0b000001NS锛歂-鎸囬拡鑷姞1&鎸囬拡鑷噺0锛汼-寮€绉诲睆1&鍏崇Щ灞?
LcdWriteCmd(0x01);//娓呭睆
}