From 99b6474f3ed1c97fa6736a13984d933ad6adecf6 Mon Sep 17 00:00:00 2001 From: liuyihui Date: Thu, 27 Apr 2023 13:26:10 +0800 Subject: [PATCH] feat: adc --- include/adc.h | 6 ++++++ src/adc.c | 25 +++++++++++++++++++++++++ src/tftlcd.c | 6 +++--- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 include/adc.h create mode 100644 src/adc.c diff --git a/include/adc.h b/include/adc.h new file mode 100644 index 0000000..56b5f45 --- /dev/null +++ b/include/adc.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void ADC0_Init(); +int16_t ADC0_TR_DATA(); diff --git a/src/adc.c b/src/adc.c new file mode 100644 index 0000000..16dbb8c --- /dev/null +++ b/src/adc.c @@ -0,0 +1,25 @@ +#include "adc.h" +#include "derivative.h" + +void ADC0_Init() { + // Enable ADC0 and PORTE clock + SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; + SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK; + + // USE AD0 (ADC0_SE4a) + PORTE->PCR[21] |= PORT_PCR_MUX(0x0); + + ADC0->CFG1 = ADC_CFG1_ADLSMP_MASK // Sample time 0 = Short, 1 = Long + | ADC_CFG1_MODE(0x01); // Conversion mode 00 = 8-bit, 01 = 12-bit, 10 = 10-bit, 11 = 16-bit + ADC0->SC3 = ADC_SC3_ADCO_MASK // Continuous Conversion if AVGE = 1 + | ADC_SC3_AVGE_MASK // Hardware average + | ADC_SC3_AVGS(0x01); // Hardware average 00 = 4 samples, 01 = 8, 10 = 16, 11 = 32 + ADC0->SC1[0] = ADC_SC1_ADCH(0b00100); // Input channel select, AD4a is selected as input +} + +int16_t ADC0_TR_DATA() { + while (!(ADC0->SC1[0] & ADC_SC1_COCO_MASK)) asm("nop"); + int16_t ADC_Result = ADC0->R[0]; + ADC0->SC1[0] &= ~ADC_SC1_COCO_MASK; + return ADC_Result; +} diff --git a/src/tftlcd.c b/src/tftlcd.c index bd592d8..cac27f8 100644 --- a/src/tftlcd.c +++ b/src/tftlcd.c @@ -2,9 +2,9 @@ #include "systick.h" #include "tftlcd.h" -uint16_t PEN_COLOR = 0x0000; // 画笔颜色 -uint16_t BACK_COLOR = 0xFFFF; // 背景颜色 -uint16_t DeviceCode; // 设备模式 +uint16_t PEN_COLOR = BLACK; // 画笔颜色 +uint16_t BACK_COLOR = WHITE; // 背景颜色 +uint16_t DeviceCode; // 设备模式 void LCD_RESET() { LCD_RST_CLR;