public.h
#ifndef __PUBLIC_H__
#define __PUBLIC_H__
#include "STC12C5A60S2.h"
#include <intrins.h>
#include <string.h>
#define UCHAR unsigned char
#define UINT unsigned int
#define TRUE 1
#define FALSE 0
#define LEDON 0xFF
#define LEDOFF ~LEDON
#define FOSC 11059200L
#define MODE1T
#ifdef MODE1T
#define T1MS (65536-FOSC/1000)
#else
#define T1MS (65536-FOSC/12/1000)
#endif
#define RxdComBuffByteNums 1024
#define LedUnitNums 1
#define LeftMoveSpeed 16
#define DisBankNums 4
#define LedUnitScanRows 16
#define DisHZNums (4*LedUnitNums)
#define DisBufByteNums (32*DisHZNums)
sbit LA=P2^7;
sbit LB=P2^6;
sbit LC=P2^5;
sbit LD=P2^3;
sbit LAT=P3^7;
sbit CLK=P3^6;
sbit OE=P2^4;
sbit R1=P2^2;
extern xdata UCHAR S1RxdComBuff[RxdComBuffByteNums];
extern UINT S1SerialIndex;
extern UCHAR S1ParseFlag;
extern UINT ReceCount;
extern UINT crc;
extern UCHAR crcLo;
extern UCHAR crcHi;
extern UCHAR actSuccess[];
extern UCHAR actFail[];
extern code UCHAR LedPosition[LedUnitScanRows];
extern idata UCHAR LedDisplayBuf[DisBufByteNums];
void delay(UINT times);
void ClearBuffer(UCHAR dat);
void LedInit();
void Driver74HC138(UCHAR dat);
void SendSegmentByte(UCHAR dat,bit direction);
void OutputData();
void LedDisBuf(void);
void LedDisplay(void);
void LedUploadStaticDisHZ(UCHAR x,UCHAR num,const UCHAR* pDotCode);
extern void ComDelay(UINT dlms);
void ClearRxdComBuffer(UCHAR dat);
extern void UART_one_init();
extern void UART_one_send(UCHAR senddata);
extern void S1Print(UCHAR *buffer);
extern unsigned int CRC16(unsigned char *puchMsg, unsigned int usDataLen);
#endif
F3.75Driver_51.c
#include "public.h"
code UCHAR LedPosition[LedUnitScanRows]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
idata UCHAR LedDisplayBuf[DisBufByteNums];
void delay(UINT times)
{
UINT i,j;
for(i=0;i<times;i++)
for(j=0;j<10;j++);
}
void ClearBuffer(UCHAR dat)
{
memset(LedDisplayBuf,dat,DisBufByteNums);
}
void LedInit()
{
P2M1=0x00;
P2M0=0xF8;
P3M1=0x00;
P3M0=0x00;
P0=0xFF;
P1=0xFF;
P2=0xFF;
P3=0xFF;
OE=0;
LAT=0;
CLK=0;
LA=0;
LB=0;
LC=0;
LD=0;
R1=0;
ClearBuffer(LEDOFF);
}
void Driver74HC138(UCHAR dat)
{
LD=dat/8;
LC=dat%8/4;
LB=dat%4/2;
LA=dat%2;
}
void SendSegmentByte(UCHAR dat,bit direction)
{
UCHAR i,temp;
if(direction==0)
temp=0x80;
else
temp=0x01;
for(i=0;i<8;i++)
{
CLK=0;
R1=dat&temp;
if(direction==0)
dat<<=1;
else
dat>>=1;
CLK=1;
}
}
void OutputData()
{
LAT=1;
_nop_();
LAT=0;
}
void LedDisBuf(void)
{
UCHAR row,num,j;
for(row=0;row<LedUnitScanRows;row++)
{
for(num=0;num<DisHZNums;num++)
{
SendSegmentByte(LedDisplayBuf[32*num+row],0);
SendSegmentByte(LedDisplayBuf[32*num+row+16],0);
}
Driver74HC138(LedPosition[row]);
OutputData();
for(j=0;j<DisHZNums*2;j++)
SendSegmentByte(0x00,0);
OutputData();
delay(1);
}
}
void LedDisplay(void)
{
UCHAR times;
for(times=0;times<LeftMoveSpeed;times++)
LedDisBuf();
}
void LedUploadStaticDisHZ(UCHAR x,UCHAR num,const UCHAR* pDotCode)
{
UCHAR i;
if(x+num<=DisHZNums)
{
for(i=0;i<num*32;i++)
LedDisplayBuf[x*32+i]=pDotCode[i];
}
else
{
for(i=0;i<(DisHZNums-x)*32;i++)
LedDisplayBuf[x*32+i]=pDotCode[i];
}
}
Comdef.c
#include "public.h"
UINT count=0;
xdata UCHAR S1RxdComBuff[RxdComBuffByteNums]={0};
UINT S1SerialIndex=0;
UCHAR S1ParseFlag=0;
UCHAR slaveAddr=0x01;
UCHAR ucControlCode=0x06;
UINT ReceCount=0;
UINT crc=0;
UCHAR crcLo=0;
UCHAR crcHi=0;
UCHAR actSuccess[]={0x01,0x06,0xAA,0xDF,0xA3};
UCHAR actFail[]={0x01,0x06,0x55,0x9F,0xE3};
void ComDelay(UINT dlms)
{
while(dlms--);
}
void ClearRxdComBuffer(UCHAR dat)
{
memset(S1RxdComBuff,dat,RxdComBuffByteNums);
}
void UART_one_init()
{
PCON &= 0x7F;
SCON = 0x50;
AUXR |= 0x40;
AUXR &= 0xFE;
TMOD &= 0x0F;
TMOD |= 0x20;
TL1 = 0xDC;
TH1 = 0xDC;
ET1 = 0;
TR1 = 1;
#ifdef MODE1T
AUXR |= 0x80;
#endif
TMOD |= 0x01;
TL0 = T1MS;
TH0 = T1MS >> 8;
TR0 = 0;
ET0 = 1;
count = 0;
ES=1;
EA=1;
PS=1;
ClearRxdComBuffer(LEDOFF);
}
void UART_one_interrupt() interrupt 4
{
EA = 0;
if(RI){
RI = 0;
TR0 = 1;
count = 0;
S1RxdComBuff[S1SerialIndex++]=SBUF;
}
if(TI){
TI = 0;
}
EA = 1;
}
void UART_one_send(UCHAR senddata)
{
ES = 0;
SBUF = senddata;
while(!TI);
TI = 0;
ES = 1;
}
void S1Print(UCHAR *buffer)
{
while(*buffer!='\0'){
UART_one_send(*buffer);
buffer++;
}
}
void timer0_isr() interrupt 1
{
TL0 = T1MS;
TH0 = T1MS >> 8;
count++;
if (count>=5)
{
TR0 = 0;
count = 0;
S1ParseFlag=1;
}
}
code unsigned char auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x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7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
unsigned int CRC16(unsigned char *puchMsg, unsigned int usDataLen)
{
unsigned char uchCRCHi = 0xFF;
unsigned char uchCRCLo = 0xFF;
unsigned int uIndex ;
while (usDataLen--)
{
uIndex = uchCRCHi ^ *puchMsg++ ;
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
uchCRCLo = auchCRCLo[uIndex];
}
return (uchCRCHi << 8 | uchCRCLo);
}
main.c
#include "public.h"
void main()
{
UINT i;
UINT crcLo_index;
UINT crcHi_index;
LedInit();
UART_one_init();
ComDelay(500);
while(1)
{
LedDisplay();
if(1==S1ParseFlag)
{
S1ParseFlag=0;
ReceCount=S1SerialIndex;
S1SerialIndex=0;
crc=CRC16(S1RxdComBuff,ReceCount-2);
crcLo=crc%256;
crcHi=crc/256;
crcLo_index=ReceCount-2;
crcHi_index=ReceCount-1;
if((crcLo==S1RxdComBuff[crcLo_index])&&(crcHi==S1RxdComBuff[crcHi_index]))
{
for(i=0;i<5;i++)
UART_one_send(actSuccess[i]);
S1RxdComBuff[crcLo_index]=S1RxdComBuff[crcHi_index]=LEDOFF;
LedUploadStaticDisHZ(0,4,S1RxdComBuff+2);
ClearRxdComBuffer(LEDOFF);
}
else
{
for(i=0;i<5;i++)
UART_one_send(actFail[i]);
}
}
}
}
获取资料方法:
1.阿里云网盘衔接: https://www.aliyundrive.com/s/dovFtMMYAFi
2.百度网盘链接:https://pan.baidu.com/s/1qyA32LdOvxtDR1nb2a__6Q 提取码:cggp
欢迎关注公众号,获取更多资料,方便学习交流
|