//代码如下
#include <pic.h>
#include <math.h>
//#include <myfuncs1.h>
//#include <seg74.h>
#include <htc.h>
#define SEG_BITSEL_PORT PORTD //位选端口
#define SEG_BITSEL_PORT_DIR TRISD //位选端口的方向寄存器
#define SEG_FONT_PORT PORTC //字形输出端口
#define SEG_FONT_PORT_DIR TRISC //字形输出端口的方向寄存器
#define DLY_MS 67
const char Seg7TableN[]={0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010,0b10000010,0b11111000,0b10000000,0b10010000};
//__CONFIG(FOSC_HS & WDTE_ON & PWRTE_OFF & BOREN_ON & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
//__CONFIG (XT & WDTDIS & LVPDIS);
unsigned int g_ADRTable[12] = {0,0,0,0,0,0,0,0,0,0,0 };
unsigned char g_Count=0;
bit OverFlag = 0;//12位转换次数完成标志位清零;
void delayms (int ms);
void DisplayData (unsigned int iData);
interrupt ISR (void)
{
if ( ADIE && ADIF )
{
ADIF = 0;
g_ADRTable[g_Count]=(((unsigned int)ADRESH)<<8)+ADRESL;
g_Count++;
if(g_Count==12)
{
g_Count =0;
OverFlag=1;//
}
else
{
GO_DONE = 1;//启动AD
}
}
else
{
while(1);
}
}
void main(void)
{
//void DisplayData();
char i=0,j=0;
int tmp = 0;//
int adc_v = 0;
TRISA0 = 1;
SEG_BITSEL_PORT_DIR = 0;//位选端口的方向寄存器,确定输出端口
SEG_FONT_PORT_DIR = 0;//字形输出端口的方向寄存器,确定输出端口
ADFM = 1;
PCFG3:PCFG0 = 0000;
ADCON0 = 0B11000001;
ADIE = 1;
ADIF = 0;
PEIE = 1;
GIE = 1;
for(i=0;i<15;i++)
{
}
GO_DONE = 1;
while(GO_DONE)
{
if(OverFlag==1)
{
for(i=0;i<12;i++)//冒泡法排序
{
for(j=i+1;j<12;j++)
{
if(g_ADRTable > g_ADRTable[j])
{
tmp = g_ADRTable;
g_ADRTable = g_ADRTable[j];
g_ADRTable[j] = tmp;
}
}
}
tmp = 0;
for(i=2;i<10;i++)
{
tmp = tmp + g_ADRTable;
}
tmp = tmp >>3;//将数按照 2进制像右移动.每向右移动一位,相当于 除2的n次幂
adc_v = tmp;
adc_v = ((long)adc_v*5000/1023);
OverFlag = 0;
GO_DONE = 1;
}
DisplayData(adc_v);
}
}
void DisplayData (unsigned int iData )
{
char i=0,j=0;//
char k = 0,NumStr[ 5 ]={0,0,0,0,0};
NumStr[1]=iData / 1000;//取整
NumStr[2]=iData % 1000/100;
NumStr[3]=iData % 100/10;
NumStr[4]=iData % 10;
for(i=0;i<10;i++)
{
SEG_BITSEL_PORT = k =0X01;
for(j=0;j<=4;j++)
{
SEG_BITSEL_PORT = k;
if(j==1)
{
SEG_FONT_PORT = Seg7TableN[NumStr[1]]&0b01111111;
}
else
{
SEG_FONT_PORT = Seg7TableN[NumStr[j]];
}
delayms(5);
k = k <<1;//把1向左移1为显示
}
}
}
void delayms (int ms)
{
int i=0,j =0;
for(i=0;i < ms;i++)
for(j =0; j < DLY_MS;j++)
{;}
}
//仿真结果,单片机数码管显示全是0,与设计的测量电阻电压不对,希望大家来找茬,这原因与结果
|