Bare metal C Ch11 ADC - Hongjayeong-02/Basic_SoC_implementation GitHub Wiki
ํ ์ค ์์ฝ: PA1์ ์๋ ๋ก๊ทธ ์ ๋ ฅ์ ADC1 Channel 1๋ก ์ฐ์ ๋ณํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ UART/printf๋ก ์ถ๋ ฅํ๋ค.
ADC๋ ์ฐ์์ ์ธ ์๋ ๋ก๊ทธ ์ ์์ ๋์งํธ ์ ์๋ก ๋ณํํ๋ค. ์ด ์ค์ต์ ADC ๊ฒฐ๊ณผ๋ 12-bit์ด๋ฏ๋ก ์ถ๋ ฅ ๋ฒ์๋ 0 ~ 4095์ด๋ค.
0 V โ 0 ๋ถ๊ทผ
VREF โ 4095 ๋ถ๊ทผ
Notion ์ ๋ฆฌ์์๋ PA1 โ ADC1_IN1 ๋งคํ๊ณผ ADC1 register ๊ตฌ์ฑ์ด ์์ F411๊ณผ F439ZI์์ ๋์ผํ๋ค๊ณ ์ ๋ฆฌํ๋ค.
Register | ์ญํ -- | -- ADC1->SQR3 | ๋ณํ sequence์ ์ฒซ channel ์ ํ ADC1->SQR1 | sequence ๊ธธ์ด ADC1->CR2 | ADON, CONT, SWSTART ADC1->SR | EOC ๋ณํ ์๋ฃ ADC1->DR | ๋ณํ ๊ฒฐ๊ณผADC1->CR2 |= CR2_CONT;
ADC1->CR2 |= CR2_SWSTART;
CONT=1์ด๋ฏ๋ก SWSTART๋ฅผ ํ ๋ฒ ์ค์ ํ ๋ค ADC๊ฐ ์ฐ์ ๋ณํ์ ์ํํ๋ค.
while (!(ADC1->SR & SR_EOC)) { }
return ADC1->DR;
๋ณํ์ด ๋๋ฌ์์ ํ์ธํ ๋ค ๊ฒฐ๊ณผ register๋ฅผ ์ฝ๋๋ค.
PA1
โ Analog input
โผ
GPIOA MODER = Analog
โ
โผ
ADC1 Channel 1
โ
โผ
Continuous Conversion
โ
โผ
EOC = 1
โ
โผ
ADC1->DR
โ
โผ
printf โ USART3 โ PC
Ch11_adc/
โโโ Inc/adc.h
โโโ Src/adc.c
โโโ Inc/uart.h
โโโ Src/uart.c
โโโ Src/main.c
syscalls.c,sysmem.c, startup file, linker script, CubeIDE metadata/build output์ Ch04์์ ๋ค๋ฃฌ ๊ณตํต ๊ธฐ๋ฐ์ด๋ฏ๋ก ์ด ํ์ด์ง์์๋ ๋ฐ๋ณตํ์ง ์๋๋ค. ์๋์๋ ์ด chapter ๊ธฐ๋ฅ ๊ตฌํ์ ์ง์ ์ฌ์ฉํ source/header๋ฅผ ์ ๋ถ ๊ธฐ๋กํ๋ค.
#ifndef ADC_H__
#define ADC_H__
#include <stdint.h>
#include "stm32f4xx.h"
void pa1_adc_init(void);
void start_conversion(void);
uint32_t adc_read(void);
#endif
#include "adc.h"
#define GPIOAEN (1U << 0)
#define ADC1EN (1U << 8)
#define ADC_CH1 (1U << 0)
#define ADC_SEQ_LEN_1 0x00U
#define CR2_ADON (1U << 0)
#define CR2_CONT (1U << 1)
#define CR2_SWSTART (1U << 30)
#define SR_EOC (1U << 1)
void pa1_adc_init(void)
{
RCC->AHB1ENR |= GPIOAEN;
GPIOA->MODER |= (1U << 2);
GPIOA->MODER |= (1U << 3); /* PA1 -> Analog ๋ชจ๋ */
RCC->APB2ENR |= ADC1EN;
ADC1->SQR3 = ADC_CH1;
ADC1->SQR1 = ADC_SEQ_LEN_1;
ADC1->CR2 |= CR2_ADON;
}
void start_conversion(void)
{
ADC1->CR2 |= CR2_CONT;
ADC1->CR2 |= CR2_SWSTART;
}
uint32_t adc_read(void)
{
while (!(ADC1->SR & SR_EOC)) { }
return ADC1->DR;
}
#ifndef UART_H
#define UART_H
#include "stm32f4xx.h"
void uart_init(void);
void uart_write_string(const char *s);
int __io_putchar(int ch);
#endif
#include <stdint.h>
#include "uart.h"
#define GPIODEN (1U << 3)
#define USART3EN (1U << 18)
#define DBG_UART_BAUDRATE 115200U
#define SYS_FREQ 16000000U
#define APB1_CLK SYS_FREQ
#define CR1_TE (1U << 3)
#define CR1_UE (1U << 13)
#define SR_TXE (1U << 7)
static void uart_write(int ch);
static uint16_t compute_uart_bd(uint32_t periph_clk, uint32_t baudrate);
static void uart_set_baudrate(uint32_t periph_clk, uint32_t baudrate);
int __io_putchar(int ch)
{
uart_write(ch);
return ch;
}
void uart_init(void)
{
RCC->AHB1ENR |= GPIODEN;
GPIOD->MODER &= ~(0x3UL << (8 * 2));
GPIOD->MODER |= (0x2UL << (8 * 2));
GPIOD->MODER &= ~(0x3UL << (9 * 2));
GPIOD->MODER |= (0x2UL << (9 * 2));
GPIOD->AFR[1] &= ~(0xFUL << 0);
GPIOD->AFR[1] |= (0x7UL << 0); /* PD8 -> AF7 (USART3_TX) */
GPIOD->AFR[1] &= ~(0xFUL << 4);
GPIOD->AFR[1] |= (0x7UL << 4); /* PD9 -> AF7 (USART3_RX) */
RCC->APB1ENR |= USART3EN;
uart_set_baudrate(APB1_CLK, DBG_UART_BAUDRATE);
USART3->CR1 = CR1_TE;
USART3->CR1 |= CR1_UE;
}
static void uart_write(int ch)
{
while (!(USART3->SR & SR_TXE)) { }
USART3->DR = (ch & 0xFF);
}
void uart_write_string(const char *s)
{
while (*s) uart_write((int)*s++);
}
static uint16_t compute_uart_bd(uint32_t periph_clk, uint32_t baudrate)
{
return (uint16_t)((periph_clk + (baudrate / 2U)) / baudrate);
}
static void uart_set_baudrate(uint32_t periph_clk, uint32_t baudrate)
{
USART3->BRR = compute_uart_bd(periph_clk, baudrate);
}
#include <stdio.h>
#include "adc.h"
#include "uart.h"
int sensor_value;
int main(void)
{
uart_init();
pa1_adc_init();
start_conversion();
while (1)
{
sensor_value = adc_read();
printf("Sensor Value: %d\r\n", sensor_value);
}
}
Notion ๊ธฐ๋ก ๊ธฐ์ค ์ค์ ๋ณด๋์์ potentiometer ์
๋ ฅ์ ์ฌ์ฉํด 0 ~ 4095 ๋ฒ์๋ก ADC ๊ฐ์ด ๋ณํ๋ ๊ฒ์ ํ์ธํ๋ค.
text = 1420 bytes
data = 0 bytes
bss = 2560 bytes
warning ์์
Notion ์ค๋ช
์ ์์ main()์๋ ์ถ๋ ฅ ์ฌ์ด์ systick_msec_delay(200)๊ฐ ํฌํจ๋์ด ์์ง๋ง, ํ์ฌ ์ฒจ๋ถ ZIP์ ์ค์ Ch11_adc/Src/main.c์๋ delay๊ฐ ์๋ค.
์ด Wiki์ ์ ์ฒด ์ฝ๋๋ ์ฒจ๋ถ ZIP์ ์ค์ ํ์ผ์ ๊ทธ๋๋ก ์ฃ๋๋ค.
12-bit ADC ๊ฒฐ๊ณผ ๋ฒ์๋ 0~4095๋ค.
Continuous mode์์๋ SWSTART ํ ๋ฐ๋ณต ๋ณํ๋๋ค.
EOC๋ฅผ ํ์ธํ ๋ค DR์ ์ฝ๋๋ค.