本程序是使用16F877A单片机将4-20mA电流信号在LED单元板上显示的程序.单元板为16针接口,32 x 64点象素,1/16扫描方式.由于是第一次接触LED单元板的项目,所以经验有很多不足,在频率计算,程序简化方面做的还很不够.
本程序中包括了AD转换,滤波和LED控制部分.本程序已经运行半年多.后来发现网上这个方面的程序非常的少,现将程序贴上,愿与同行交流.
/***************************************
* LED 显示屏 4-20MA接收板 *
锦州硬盘之家
***************************************/

/**************************************
RD0:A
RD1:B
RD2:C
RD3:D
RC4:G1
RC5:G2
RC6:CK
RC7:HS
RD4:E
RD5:R1

**************************************/

#include
#include
#include

//定义芯片的配置
__CONFIG (HS & PWRTEN & BORDIS & LVPDIS & WDTDIS);


static bit CK @ PORTBIT(PORTC,6); //
static bit OO @ PORTBIT(PORTC,7); //
static bit EN @ PORTBIT(PORTD,4); //
static bit R1 @ PORTBIT(PORTC,4);
static bit R2 @ PORTBIT(PORTC,5);
static bit A @ PORTBIT(PORTD,0);
unsigned char i,j,m,n,l,t1,t2,temp,w,point1;
bank1 unsigned int c,h;
static bit k;
unsigned int wendu;
bank1 unsigned int adbuf[5];
bank1 union adres //A/D转换结果共用体
{
unsigned int adrehl;
unsigned char adre[2];
}adresult;


const unsigned char up[11][32]={{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xFF,0xE6,0x7F,0xCF,0x3F,0xCF,0x3F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F},
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xFF,0xF9,0xFF,0xC1,0xFF,0xF9,0xFF,0xF9,0xFF,0xF9,0xFF,0xF9,0xFF},
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF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0xFB,0xFD,0xF7,0xFE,0xEF,0xFF,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};


void dely6()
{

for(c=0;c<100;c++)
{
}
}
void dely1()
{
NOP();NOP();NOP();NOP();NOP();NOP();
}
void cong()
{
A=1;
NOP();NOP();NOP();NOP();
A=0;
NOP();NOP();NOP();NOP();
PORTD=0x0E+i;
}



/* max data =0 */
void max_data(void)
{
unsigned char i,k;
k=0;
for (i=1;i<5;i++)
{
if (adbuf[k] k=i;
}
adbuf[k]=0;
}

/* min data =0 */
void min_data(void)
{
unsigned char i,k;
k=0;
for (i=1;i<5;i++)
{
if (adbuf[k]>adbuf[i])
k=i;
}
adbuf[k]=0;
}

/*ad转换*/
void adchange()
{
adresult.adre[0]=ADRESL;
adresult.adre[1]=ADRESH;
adbuf[point1]=adresult.adrehl;
point1++;
CHS2=0;CHS1=0;CHS0=0;
if (point1>=5)
{
unsigned int tem;
point1=0;
min_data();
max_data();
wendu=adbuf[0];
wendu=(wendu+adbuf[1]+adbuf[2]+adbuf[3]+adbuf[4])/3+620;
}
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
ADGO=1;
}

void InitLed()
{
for(l=0;l<64;l++)
{
R1=1;
R2=1;
OO=1;
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
OO=0;
}
CK=1;
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
CK=0;
}

void Initdata()
{
OO=0;
CK=0;
point1=0;
}

void InitPIC16F877()
{
TRISC=0B00000000;
PORTB=0xFF;
TRISD=0B00000000;
PORTD=0xFF;


ADCON0=0x81; //选择A/D通导RA0,打开A/D转换器,时钟8tosc
ADCON1=0x8D; //转换结果右移,ADRESH的高6位为"0"
TRISA=0B11111111; //设置A口为输入方式
INTCON=0; //清所有中断标志位使能位
PIE1=0;
PIR1=0;
PIE2=0;
PIR2=0;
ADGO=1;
}

void main(void)
{
InitPIC16F877();
Initdata();
InitLed();
while (1)
{
adchange();
for(i=0;i<16;i++)
{
for(m=0;m<4;m++)
{
switch (m)
{
case 0:
w=wendu/1000;
break;
case 1:
w=(wendu%1000)/100;
break;
case 2:
w=(wendu%100)/10;
break;
case 3:
w=wendu%10;
break;
}
t1=up[w][2*i];
t2=down[w][2*i];
for(j=0;j<8;j++)
{
temp=t1 & 0x80;
if(temp == 0x80) R1=1; else R1=0;
temp=t2 & 0x80;
if(temp == 0x80) R2=1; else R2=0;
t1=t1<<1;
t2=t2<<1;
dely1();
OO=1;
dely1();
OO=0;
dely1();
}
t1=up[w][2*i+1];
t2=down[w][2*i+1];
for(j=0;j<4;j++)
{
temp=t1 & 0x80;
if(temp == 0x80) R1=1; else R1=0;
temp=t2 & 0x80;
if(temp == 0x80) R2=1; else R2=0;
t1=t1<<1;
t2=t2<<1;
dely1();
OO=1;
dely1();
OO=0;
dely1();
}
}
for(l=0;l<2;l++)
{
t1=up[10][2*i+l];
t2=down[10][2*i+l];
for(j=0;j<8;j++)
{
temp=t1 & 0x80;
if(temp == 0x80) R1=1; else R1=0;
temp=t2 & 0x80;
if(temp == 0x80) R2=1; else R2=0;
t1=t1<<1;
t2=t2<<1;
dely1();
OO=1;
dely1();
OO=0;
dely1();
}
}
EN=1;
dely1();
CK=1;
dely1();
CK=0;
PORTD=0xE0+i;
}
}
}