My-CDC-Basic.X変更箇所
(1)main.c MAIN_RETURN main(void)の変更箇所
TRISA = 0b00000111;//AN0とAN1とAN2を入力設定
PORTDbits.RD0 = 0;
PORTDbits.RD1 = 0;
(2)app_device_cdc_basic.cのヘッダ部
void BMode(unsigned short N);
unsigned short MyData[4];//データバッファ
(3)void BMode(unsigned short N)の呼び出し
if(FIN[0][0]=='B' && FIN[0][1]=='B' && FIN[0][2]==0)
{
N=atoi((const char *)FIN[1]);
BMode(N);
sprintf(writeBuffer,"%d\t%d\r\n",MyData[0],MyData[1]);
numBytesRead = strlen(writeBuffer);
putUSBUSART(writeBuffer,numBytesRead);
}
(4)void BMode(unsigned short N)関数
void BMode(unsigned short N)
{
ADCON0bits.GO = 0; // Stop AD conversion
ADCON0bits.CHS = 0b0010; //Analog Channel Select bits=Channel 2 (AN0)
uint16_t result;
long X1=0,X2=0;
int i;
PORTDbits.RD0 = 1;
PORTDbits.RD1 = 0;
__delay_ms( 2 );
for(i=0;i< N;i++)
{
ADCON0bits.GO = 1; // Start AD conversion
while(ADCON0bits.NOT_DONE); // Wait for conversion
result = ADRESH;
result < < = 8;
result |= ADRESL;
X1 = X1 + result;
}
PORTDbits.RD0 = 0;
PORTDbits.RD1 = 0;
X1=X1/N;
MyData[0]=X1;
PORTDbits.RD0 = 0;
PORTDbits.RD1 = 1;
__delay_ms( 1 );
for(i=0;i< N;i++)
{
ADCON0bits.GO = 1; // Start AD conversion
while(ADCON0bits.NOT_DONE); // Wait for conversion
result = ADRESH;
result < < = 8;
result |= ADRESL;
X2 = X2 + result;
}
PORTDbits.RD0 = 0;
PORTDbits.RD1 = 0;
X2=X2/N;
MyData[1]=X2;
}