单片机程序源码如下:
#include
__CONFIG(HS&WDTDIS&LVPDIS);
#define U5A RB0
#define U5B RB1
#define U5C RB2
unsigned char temp1;
unsigned char temp2;
const unsigned char LED[28]=
{
0x3F,/*0*/
0x06,/*1*/
0x5B,/*2*/
0x4F,/*3*/
0x66,/*4*/
0x6D,/*5*/
0x7D,/*6*/
0x07,/*7*/
0x7F,/*8*/
0x6F,/*9*/
0x77,/*A*/
0x7C,/*b*/
0x39,/*C*/
0x58,/*c*/
0x5E,/*d*/
0x79,/*E*/
0x71,/*F*/
0x76,/*H*/
0x74,/*h*/
0x38,/*L*/
0x54,/*n*/
0x37,/*N*/
0x5C,/*o*/
0x73,/*P*/
0x50,/*r*/
0x78,/*t*/
0x3E,/*U*/
0x40,/*-*/
};
unsigned int ADbuf;
unsigned int adcbuf[16];
void main(void)
{
unsigned char buf;
unsigned char USARTbuf;
unsigned char c=0;
unsigned char e=0;
unsigned char i;
unsigned char b;
unsigned char d=0;
TRISA=0B11111111;
TRISB=0B11111000;
TRISD=0B00000000;
PORTA=0B00000000;
PORTB=0B00000000;
PORTD=0B00000000;
ADCON1=0B10001111;
ADIE=0; //禁止A/D中断
SPBRG=25; //设置波特率为9600,误差0.16%
BRGH=1; //设置高速波特率
SYNC=0; //SYNC=0为异步模式,SYNC=1为同步模式
SPEN=1; //允许串口操作
CREN=1; //CREN=0禁止连续接收,CREN=1允许连续接收
TXIE=0; //禁止发送中断
RCIE=0; //禁止接收中断
TX9=0; //TX9=0为8位发送,TX9=1为9位发送
RX9=0; //RX9=0为8位接收,RX9=1为9位接收
TXEN=1; //TXEN=0为禁止发送,TXEN=1为允许发送
while(1) //死循环,单片机初始化后,将一直运行这1个死循环
{
for(i=0;i<16;i++) //连续采样16次,然后取平均数
{
asm("NOP"); //略做延时
asm("NOP"); //略做延时
asm("NOP"); //略做延时
asm("NOP"); //略做延时
ADGO=1; //开始进行A/D转换
while(ADGO) continue; //等待A/D转换结束
adcbuf[i]=ADRESH*256+ADRESL;
//*********************
}
ADbuf=0;
for(i=0;i<16;i++)
{
ADbuf+=adcbuf[i];
}
ADbuf=ADbuf/16;
ADbuf=(int)(((float)ADbuf/1023)*1000);
temp1=ADbuf/100;
temp2=ADbuf%100;
if(ADbuf<999)
{
PORTD=0;
if(++d>3) d=0;
if(d==0)
{
U5A=0;
U5B=0;
U5C=0;
PORTD=LED[(ADbuf%10000)/1000];
}
else if(d==1)
{
U5A=1;
U5B=0;
U5C=0;
PORTD=LED[(ADbuf%1000)/100];
}
else if(d==2)
{
U5A=0;
U5B=1;
U5C=0;
PORTD=LED[(ADbuf%100)/10];
}
else if(d==3)
{
U5A=1;
U5B=1;
U5C=0;
PORTD=LED[ADbuf%10];
}
}
if(ADbuf>999)
{
PORTD=0;
if(++d>3) d=0;
if(d==0)
{
U5A=0;
U5B=0;
U5C=0;
PORTD=LED[15];
}
else if(d==1)
{
U5A=1;
U5B=0;
U5C=0;
PORTD=LED[15];
}
else if(d==2)
{
U5A=0;
U5B=1;
U5C=0;
PORTD=LED[0];
}
else if(d==3)
{
U5A=1;
U5B=1;
U5C=0;
PORTD=LED[10];
}
}
if(RCIF==1)
{
USARTbuf=RCREG;
if(OERR==1);
{
CREN=0;
CREN=1;
}
if(USARTbuf==0x89)
{
TXREG=0X90;
while(TRMT==0);
TXREG=temp1;
while(TRMT==0);
TXREG=temp2;
while(TRMT==0);
}
}
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』