From 6477d1281abe086bd61977679b916b2381cde318 Mon Sep 17 00:00:00 2001 From: IOsetting Date: Thu, 30 Dec 2021 12:19:02 +0800 Subject: [PATCH] feat: adc demos --- demo/adc/adc_interrupt_10bit.c | 72 ++++++++++++++ demo/adc/adc_interrupt_2ch.c | 94 +++++++++++++++++++ demo/adc/adc_poll_10bit.c | 65 +++++++++++++ demo/adc/adc_poll_8bit.c | 63 +++++++++++++ ...t1_int_tx_rx.c => uart1_interrupt_tx_rx.c} | 0 include/fw_adc.h | 9 +- include/fw_hal.h | 1 + include/fw_reg_base.h | 2 +- 8 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 demo/adc/adc_interrupt_10bit.c create mode 100644 demo/adc/adc_interrupt_2ch.c create mode 100644 demo/adc/adc_poll_10bit.c create mode 100644 demo/adc/adc_poll_8bit.c rename demo/uart/{uart1_int_tx_rx.c => uart1_interrupt_tx_rx.c} (100%) diff --git a/demo/adc/adc_interrupt_10bit.c b/demo/adc/adc_interrupt_10bit.c new file mode 100644 index 0000000..56e0214 --- /dev/null +++ b/demo/adc/adc_interrupt_10bit.c @@ -0,0 +1,72 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * STC8H3K64S2 + * AGrnd -> GND + * AVcc -> VCC + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage + * + * STC8H1K08 + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage +*/ +#include "fw_hal.h" + +uint16_t res; + +INTERRUPT(ADC_Routine, EXTI_VectADC) +{ + ADC_ClearInterrupt(); + res = ADC_RESL; + res |= (ADC_RES & 0x0F) << 8; + // Restart ADC for continuous sampling + ADC_Start(); +} + +void main(void) +{ + + SYS_Init(); + // For debug print + UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); + // Set ADC1(GPIO P1.1) HIP + GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); + // Channel: ADC1 + ADC_SetChannel(0x01); + // ADC Clock = SYSCLK / 2 / (1+15) = SYSCLK / 32 + ADC_SetSpeed(0x0F); + // Right alignment, high 2-bit in ADC_RES, low 8-bit in ADC_RESL + ADC_SetResultAlignmentRight(); + // Enable interrupts + EXTI_Global_SetIntState(HAL_State_ON); + EXTI_ADC_SetIntState(HAL_State_ON); + // Turn on ADC power + ADC_SetPowerState(HAL_State_ON); + + while(1) + { + ADC_Start(); + UART1_TxString("Result: "); + UART1_TxHex(res >> 8); + UART1_TxHex(res & 0xFF); + UART1_TxString("\r\n"); + SYS_Delay(100); + } +} \ No newline at end of file diff --git a/demo/adc/adc_interrupt_2ch.c b/demo/adc/adc_interrupt_2ch.c new file mode 100644 index 0000000..72d6dfb --- /dev/null +++ b/demo/adc/adc_interrupt_2ch.c @@ -0,0 +1,94 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * STC8H3K64S2 + * AGrnd -> GND + * AVcc -> VCC + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage + * + * STC8H1K08 + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage +*/ +#include "fw_hal.h" + +uint8_t pos; +uint16_t res[2]; + +INTERRUPT(ADC_Routine, EXTI_VectADC) +{ + ADC_ClearInterrupt(); + res[pos] = ADC_RESL; + res[pos] |= (ADC_RES & 0x0F) << 8; + // Restart ADC for continuous sampling + pos = (pos+1) & 0x1; + if (pos == 0) + { + /** + * Uncomment these lines in high speed ADC + GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Output_OD); + GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); + */ + ADC_SetChannel(0x01); + } + else + { + /** + * Uncomment these lines in high speed ADC + GPIO_P1_SetMode(GPIO_Pin_2, GPIO_Mode_Output_OD); + GPIO_P1_SetMode(GPIO_Pin_2, GPIO_Mode_Input_HIP); + */ + ADC_SetChannel(0x02); + } + ADC_Start(); +} + +void main(void) +{ + SYS_Init(); + // For debug print + UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); + // Channel: ADC1 + ADC_SetChannel(0x01); + // ADC Clock = SYSCLK / 2 / (1+15) = SYSCLK / 32 + ADC_SetSpeed(0x0F); + // Right alignment, high 2-bit in ADC_RES, low 8-bit in ADC_RESL + ADC_SetResultAlignmentRight(); + // Enable interrupts + EXTI_Global_SetIntState(HAL_State_ON); + EXTI_ADC_SetIntState(HAL_State_ON); + // Turn on ADC power + ADC_SetPowerState(HAL_State_ON); + // Set ADC1(P1.1), ADC2(P1.2) HIP + GPIO_P1_SetMode(GPIO_Pin_1|GPIO_Pin_2, GPIO_Mode_Input_HIP); + ADC_Start(); + + while(1) + { + UART1_TxString("Result: "); + UART1_TxHex(res[0] >> 8); + UART1_TxHex(res[0] & 0xFF); + UART1_TxChar(' '); + UART1_TxHex(res[1] >> 8); + UART1_TxHex(res[1] & 0xFF); + UART1_TxString("\r\n"); + SYS_Delay(100); + } +} \ No newline at end of file diff --git a/demo/adc/adc_poll_10bit.c b/demo/adc/adc_poll_10bit.c new file mode 100644 index 0000000..ce1af4f --- /dev/null +++ b/demo/adc/adc_poll_10bit.c @@ -0,0 +1,65 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * STC8H3K64S2 + * AGrnd -> GND + * AVcc -> VCC + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage + * + * STC8H1K08 + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage +*/ +#include "fw_hal.h" + +void main(void) +{ + uint16_t res; + SYS_Init(); + // For debug print + UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); + // Set ADC1(GPIO P1.1) HIP + GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); + // Channel: ADC1 + ADC_SetChannel(0x01); + // ADC Clock = SYSCLK / 2 / (1+1) = SYSCLK / 4 + ADC_SetSpeed(0x01); + // Right alignment, high 2-bit/4-bit in ADC_RES, low 8-bit in ADC_RESL + ADC_SetResultAlignmentRight(); + // Turn on ADC power + ADC_SetPowerState(HAL_State_ON); + + while(1) + { + ADC_Start(); + NOP(); + NOP(); + while (!ADC_SamplingUnfinished()); + ADC_ClearInterrupt(); + /* + res = ADC_RESL; + res |= (ADC_RES & 0x0F) << 8; + */ + UART1_TxHex(ADC_RES); + UART1_TxHex(ADC_RESL); + UART1_TxString("\r\n"); + SYS_Delay(100); + } +} \ No newline at end of file diff --git a/demo/adc/adc_poll_8bit.c b/demo/adc/adc_poll_8bit.c new file mode 100644 index 0000000..13669b9 --- /dev/null +++ b/demo/adc/adc_poll_8bit.c @@ -0,0 +1,63 @@ +// Copyright 2021 IOsetting +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * STC8H3K64S2 + * AGrnd -> GND + * AVcc -> VCC + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage + * + * STC8H1K08 + * AVref -> VCC + * Vcc -> VCC + * Gnd -> GND + * ADC1 -> Test voltage +*/ +#include "fw_hal.h" + +void main(void) +{ + uint8_t res; + SYS_Init(); + // For debug print + UART1_ConfigMode1Dyn8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200, HAL_State_OFF); + // Set ADC1(GPIO P1.1) HIP + GPIO_P1_SetMode(GPIO_Pin_1, GPIO_Mode_Input_HIP); + // Channel: ADC1 + ADC_SetChannel(0x01); + // ADC Clock = SYSCLK / 2 / (1+1) = SYSCLK / 4 + ADC_SetSpeed(0x01); + // Left alignment, high 8-bit in ADC_RES + ADC_SetResultAlignmentLeft(); + // Turn on ADC power + ADC_SetPowerState(HAL_State_ON); + + while(1) + { + ADC_Start(); + NOP(); + NOP(); + while (!ADC_SamplingUnfinished()); + ADC_ClearInterrupt(); + res = ADC_RES; + + UART1_TxString("Result: "); + UART1_TxHex(res); + UART1_TxString("\r\n"); + SYS_Delay(100); + } +} \ No newline at end of file diff --git a/demo/uart/uart1_int_tx_rx.c b/demo/uart/uart1_interrupt_tx_rx.c similarity index 100% rename from demo/uart/uart1_int_tx_rx.c rename to demo/uart/uart1_interrupt_tx_rx.c diff --git a/include/fw_adc.h b/include/fw_adc.h index 33b420c..704e2f0 100644 --- a/include/fw_adc.h +++ b/include/fw_adc.h @@ -19,8 +19,9 @@ #include "fw_types.h" #define ADC_SetPowerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 7, __STATE__) -#define ADC_Start SFR_SET(ADC_CONTR, 6) -#define ADC_ClearInterrupt SFR_RESET(ADC_CONTR, 5) +#define ADC_Start() SFR_SET(ADC_CONTR, 6) +#define ADC_SamplingUnfinished() (ADC_CONTR & (0x01 << 5)) +#define ADC_ClearInterrupt() SFR_RESET(ADC_CONTR, 5) #define ADC_SetPWMTriggerState(__STATE__) SFR_ASSIGN(ADC_CONTR, 4, __STATE__) /** * ADC input channels selection @@ -53,8 +54,8 @@ * 10-bit in [ADC_RES,ADC_RESL]: STC8H1K28,STC8H1K08 * 12-bit in [ADC_RES,ADC_RESL]: STC8H3K64S4,STC8H3K64S2,STC8H8K64U,STC8H2K64T,STC8H4K64TLR,STC8H4K64TLCD,STC8H4K64LCD */ -#define ADC_SetResultAlignmentLeft SFR_RESET(ADCCFG, 5) -#define ADC_SetResultAlignmentright SFR_SET(ADCCFG, 5) +#define ADC_SetResultAlignmentLeft() SFR_RESET(ADCCFG, 5) +#define ADC_SetResultAlignmentRight() SFR_SET(ADCCFG, 5) /** * ADC conversion speed calculation: diff --git a/include/fw_hal.h b/include/fw_hal.h index fc19bb9..00e4249 100644 --- a/include/fw_hal.h +++ b/include/fw_hal.h @@ -23,6 +23,7 @@ #include "fw_gpio.h" #include "fw_tim.h" #include "fw_uart.h" +#include "fw_adc.h" #include "fw_util.h" #endif diff --git a/include/fw_reg_base.h b/include/fw_reg_base.h index 6f6ab7f..13abf8e 100644 --- a/include/fw_reg_base.h +++ b/include/fw_reg_base.h @@ -42,7 +42,7 @@ # define SFR16X(addr) (*(unsigned char volatile *)(addr)) #define INTERRUPT(name, vector) void name (void) #define INTERRUPT_USING(name, vector, regnum) void name (void) - #define NOP() () + #define NOP() #endif