본문 바로가기

[Microchip]/dsPIC33-SM

dsPIC33 ADC 테스트

dsPIC33 ADC 테스트




ADC 변환 비트 모드
AD1CON1bits.AD12B =1;  //AD변환 비트 값 설정: 12-bit, 1-channel ADC operation

AD1CON1bits.ASAM = 1; //자동 AD변환 시작
AD1CON1bits.SiMSAM = 1 //CH0, CH1 .. 동시에 샘플링
AD1CON1bits.FORM<1:0>
 11 : signed factional
 10 : fractional
 01 : signed integer
 00 : integer


ADC 채널 선택
CHPS<1:0>: ADC 채널 선택
When AD12B = 1, CHPS<1:0> is: U-0, Unimplemented, Read as ‘0’
1x =Converts CH0, CH1, CH2 and CH3
01 =Converts CH0 and CH1
00 =Converts CH0

ex)  AD1CON2bits.CHPS=0;   //AD 변환 채널 설정 : CH0


ADC Ref선택


ex)
AD1CON2bits.VCFG = 0;  //AD변환 기준 전압 설정: Vreg+ = Vdd, Vreg- = Vss

AD1CHS0: ADC1 입력 채널 0 선택

CH0NB: Channel 0 Negative Input Select for Sample B bit
1 = Channel 0 negative input is AN1
0 = Channel 0 negative input is VREF


CH0SB<4:0>: Channel 0 Positive Input Select for Sample B bits
dsPIC33FJ32MC304, dsPIC33FJ64MC204/804, and dsPIC33FJ128MC204/804 devices only:
01000 = Channel 0 positive input is AN8

00010 = Channel 0 positive input is AN2
00001 = Channel 0 positive input is AN1
00000 = Channel 0 positive input is AN0
dsPIC33FJ32MC302, dsPIC33FJ64MC202/802, and dsPIC33FJ128MC202/802 devices only:
00101 = Channel 0 positive input is AN5

00010 = Channel 0 positive input is AN2
00001 = Channel 0 positive input is AN1
00000 = Channel 0 positive input is AN0.


CH0NA: Channel 0 Negative Input Select for Sample A bit
1 = Channel 0 negative input is AN1
0 = Channel 0 negative input is VREF

CH0SA<4:0>: Channel 0 Positive Input Select for Sample A bits
dsPIC33FJ32MC304, dsPIC33FJ64MC204/804, and dsPIC33FJ128MC204/804 devices only:
01000 = Channel 0 positive input is AN8

00010 = Channel 0 positive input is AN2
00001 = Channel 0 positive input is AN1
00000 = Channel 0 positive input is AN0
dsPIC33FJ32MC302, dsPIC33FJ64MC202/802, and dsPIC33FJ128MC202/802 devices only:
00101 = Channel 0 positive input is AN5

00010 = Channel 0 positive input is AN2
00001 = Channel 0 positive input is AN1
00000 = Channel 0 positive input is AN0



AD1CSSL: ADC1 INPUT SCAN SELECT REGISTER


CSS<8:0>: ADC Input Scan Selection bits
1 = Select ANx for input scan
0 = Skip ANx for input scan


AD1PCFGL: ADC1 PORT CONFIGURATION REGISTER

PCFG<8:0>: ADC Port Configuration Control bits
1 = Port pin in Digital mode, port read input enabled, ADC input multiplexor connected to AVSS
0 = Port pin in Analog mode, port read input disabled, ADC samples pin voltage

ex)
 //AD1CSSL 레지스터 : AN0 ~ AN15 아날로그 입력 핀 설정
 Cbi(AD1CSSL, BIT0|BIT1);  //AN0 ~ AN1 AD 입력 핀 설정

 //AD1PCFGL ADC포트 설정  
 Sbi(AD1PCFGL, BIT0|BIT1);  //AN0 ~ AN1 까지 모든 핀 아날로그 입력 핀 설정


AD1CHS123: ADC1 입력 채널 선택



dsPIC ADC테스트 동영상



dsPIC ADC 테스트 소스코드

#include "system.h"
#include "serial.h"

int main(void)
{
 int flag = 0;
 unsigned int adc_val = 0;
 SystemInit();

 //Init LED
 Led1Init();
 Led2Init();

 DebugInit(BAUD_115200);

 DebugPrint("dsPIC33 ADC test Program\r\n");
 
 Led1On();
 Led2Off();

 //adc init
 AdcInit();

 while(1)
 {
  if(flag)
  {
   adc_val = AdcRead(0);
   printf("%d\r\n", adc_val);
   Delay(10);
  }

  if(DebugIsByte())
  {
   Led2Toggle();

   switch(U0_GetByte())
   {
    case '0':
     Led1Off();
     DebugPrint("Led1 Off\r\n");
     break;

    case '1':
     Led1On();
     DebugPrint("Led1 On\r\n");
     break;

    case '3':
     flag = 1;
     break;

    case '4':
     flag = 0;
     break;
  }
 }
}


 

반응형