Transcript 18-ADC_Test
UNIT
20 AD Converter Test
로봇 SW 교육원
조용수
학습 목표
• 조도 센서 값 읽기
• 가변 저항 값 읽기
• 가변 저항 값 읽어서 LED에 적용
2
조도 센서 값 읽기
3
APDS-9007 :
Ambient Light Photo Sensor with Logarithmic Current
Output
ADC Init
4
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable IP clock */
CLK->APBCLK |= CLK_APBCLK_UART0_EN_Msk;
CLK->APBCLK |= CLK_APBCLK_ADC_EN_Msk;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock
and cyclesPerUs automatically. */
SystemCoreClockUpdate();
/* Set P3 multi-function pins for UART0 RXD and TXD */
SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
/* Set P1.0 to AIN0 function */
SYS->P1_MFP = SYS_MFP_P10_AIN0 ;
/* Lock protected registers */
SYS_LockReg();
}
ADC Init
• ADC Channel 0 Settings
void ADC_Init()
{
ADC->ADCR &= ~(0x2 << 2); // Sing Mode
ADC->ADCR |= 0x1 ; // ADEN Set
}
5
ADC Init
• Read ADC Channel 0
int value0 = 0;
ADC->ADCHER = (0x1 << 0) ; // ADC Channel 0 Set
ADC->ADCR |= 0x1 << 11 ; // Start Convert
while(ADC->ADSR & (0x1 << 3));
value0 = ADC->ADDR[0] & 0xFFFF;
printf("Photo Sensor Value 11 : ");
printf("%d \n", value0);
6
ADC Init
• Set Interrupt Handler
void ADC_IRQHandler()
{
int value0 = 0;
int value1 = 0;
NVIC->ICPR[0] = (0x1 << 29);
ADC->ADSR &= ~0x1; // Clear Interrupt
ADC->ADCR &= ~(0x1 <<1) ; // Disable Interrupt
value0 = ADC->ADDR[0] & 0xFFFF;
printf("ADC(Interrupt) : ");
printf("%d \n", value0);
}
7
ADC Init
• Start ADC using Interrupt
Int main()
{
…
//NVIC_EnableIRQ(ADC_IRQn);
NVIC->ISER[0] = (0x1 << 29);
…
printf("Stat ADC\n");
ADC->ADCHER = 0x1 ; // ADC Channel 0 Set
ADC->ADCR |= (0x1 <<1) ; // Enable Interrupt
ADC->ADCR |= 0x1 << 11 ; // Start Convert
}
8
가변 저항 값 읽기
• VCC , GND , ADC1 연결
9
가변 저항 값 읽기
• ADC 0, 1 선택
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
…
/* Set P1.0 to AIN0 function */
SYS->P1_MFP = SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1;
/* Lock protected registers */
SYS_LockReg();
}
10
가변 저항 값 읽기
• ADC 0, 1 선택
void ADC_IRQHandler()
{
int value0 = 0;
int value1 = 0;
NVIC->ICPR[0] = (0x1 << 29);
ADC->ADSR &= ~0x1; // Clear Interrupt
ADC->ADCR &= ~(0x1 <<1) ; // Disable Interrupt
value0 = ADC->ADDR[0] & 0xFFFF;
value1 = ADC->ADDR[1] & 0xFFFF;
printf("ADC(Interrupt) : ");
printf("%d , %d\n", value0, value1);
}
11
가변 저항 값 읽기
• ADC 0, 1 선택
printf("Stat ADC\n");
ADC->ADCHER = (0x3 << 0) ; // ADC Channel 0,1 Set
ADC->ADCR |= (0x1 <<1) ; // Enable Interrupt
ADC->ADCR |= 0x1 << 11 ; // Start Convert
12
가변 저항 값 읽기
• Single Cycle Scan Mode
void ADC_Init()
{
ADC->ADCR &= ~(0x2 << 2); // Sing Mode
ADC->ADCR |= 0x2; // Single Cycle Scan Mode
ADC->ADCR |= 0x1 ; // ADEN Set
}
13
가변 저항 값 읽어서 LED에 적용
• 가변 저항 값 읽어서 구간 별로 4개의 LED On
–
0 ~ 10 : All OFF
– 10 ~ 1000 : LED1
– 1001 ~ 2000 : LED2
– 2001 ~ 3000 : LED3
– 3001 ~
: LED4
14