论坛里大神用pic18f4520写的8路AD采样,没给注释,新手有些地方看不太懂,请教大神!
请问红字部分是什么意思,实现的什么功能?
谢谢!
//******Multi_ADChannel converter**********//
#include <pic18.h>
__PROG_CONFIG(1,0xc200);
__PROG_CONFIG(2,0x0a16);
__PROG_CONFIG(3,0x8100);
__PROG_CONFIG(4,0x0081);
__PROG_CONFIG(5,0xc00f);
__PROG_CONFIG(6,0xe00f);
__PROG_CONFIG(7,0x400f);
#define uchar unsigned char
#define uint unsigned int
uchar temp;
void multi_convertor(void);
void port_check(void);
uint ADbuf[10];
void delay_ms(uint t)
{
uint m,n;
for (m=0;m<t; m++)
for (n=0; n<1140;n++);
}
void GPIO_Init(void)
{
TRISA=0xff;
TRISB=0xff;
TRISC=0xff;
TRISD=0xff;
TRISE=0xff;
PSPMODE=0;
PSPIE=0;
PSPIP=0;
TRISBbits.RB2 =1; //set direction of RB2 to be An input;
TRISBbits.RB3 =1; //set direction of RB3 to be An input;
}
void ADC_Init(void)
{
ADCON1 = 0x05; //seting AN0~AN9 to be analog input;
ADCON2 = 0xBE; //20TAD,FOSC/64 and right justified;
}
void timer0_init(void)
{
INTCON =0xE0;//GIE,PEIE,TMR0IE to set;
T0CON = 0x1F;// timer0 to be 16-bit;
TMR0H = (65536-1000)/256;
TMR0L = (65536-1000)%256;
T0CON |=0x80;
TMR0ON=1;
}
void EUART_init(void)
{
TRISC7=1;
TRISC6=1;
SPBRG=0x40;
TXSTA=0x24;
RCSTA=0x90;
RCIE=0;
TXIE=0;
PEIE=1;
GIE=1;
}
void transmit_udata(uchar udata)
{
TXREG=udata;
while(!TRMT);
}
uchar receive_udata(void)
{
uchar temp;
if (RCIF==1)
{
temp=RCREG;
}
return temp;
}
uint data_average(uint buffer[30])
{
uchar i,j;
uint temp;
float temp1;
for(i=1; i<30; i++)
for(j=29; j>=i; --j)
{
if(buffer[j-1] > buffer[j])
{
temp = buffer[j-1];
buffer[j-1] = buffer[j];
buffer[j] = temp;
}
}
temp1 = 0;
for(i=5; i<25; i++)
{
temp1 += buffer[ i];
}
temp = (uint)(((float)temp1) / 20 + 0.5);
return(temp);
}
void main(void)
{
EUART_init();
GPIO_Init();
ADC_Init();
timer0_init();
while(1)
{
temp=receive_udata();
if (temp==0x01)
{
temp=0;
port_check();
multi_convertor();
}
}
}
void port_check(void)
{
uchar num1, num2;
num1=PORTD;
num2=PORTB&0X03;
transmit_udata(num2);
transmit_udata(num1);
}
void multi_convertor(void)
{
uchar data1,data2,i;
static uchar ADC_channel=0;
uint adc[30]={0};
// ADC_channel++;
for (ADC_channel=0; ADC_channel<10; ADC_channel++)
{
switch(ADC_channel)
{
case 0: ADCON0=0x01;break;//AN0 channel;
case 1: ADCON0=0x05;break;//AN1 channel;
case 2: ADCON0=0x09;break;//AN2 channel;
case 3: ADCON0=0x0D;break;//AN3 channel;
case 4: ADCON0=0x11;break;//AN4 channel;
case 5: ADCON0=0x15;break;//AN5 channel;
case 6: ADCON0=0x19;break;//AN6 channel;
case 7: ADCON0=0x1D;break;//AN7 channel;
case 8: ADCON0=0x21;break;//AN8 channel;
case 9: ADCON0=0x25;break;//AN9 channel;
default:break;
}
for(i=0;i<30;i++)
{
ADCON0 |=0x01;
asm("NOP");
asm("NOP");
GODONE=1;
while(GODONE);
adc[ i]=(ADRESH*256)+ADRESL;
}
ADbuf[ADC_channel]=data_average(adc);
data1=ADbuf[ADC_channel]/255;
data2=ADbuf[ADC_channel]%255;
transmit_udata(data1);
transmit_udata(data2);
}
}
|