本帖最后由 eltonchang2001 于 2022-11-9 14:08 编辑
#申请原创#非常荣幸能够获得本次的测试的机会,非常感谢21IC和合泰。本次主要时测试12bit的ADC,并用数码管显示。收到开发板后,焊接了排针,整个板子很大气:如下
其ADC采样利用底板的变阻器来测试:
数码管的原理图: 程主要实现的时系统滴答定时器开启,ADC中断采样以及数码管的显示: 开启系统滴答定时器程序如下: SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK); // Default : CK_AHB/8 SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000); // (CK_AHB/8/1000) = 1ms on chip SYSTICK_IntConfig(ENABLE); //开启系统滴答定时器 SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR);
SYSTICK_CounterCmd(SYSTICK_COUNTER_ENABLE);
ADC初始化: void ADC_Configuration(void) { { /* Enable peripheral clock */ CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }}; CKCUClock.Bit.AFIO = 1; CKCUClock.Bit.HTCFG_ADC_IPN = 1; CKCU_PeripClockConfig(CKCUClock, ENABLE); } /* Configure AFIO mode as ADC function */ AFIO_GPxConfig(HTCFG_VR_GPIO_ID, HTCFG_VR_AFIO_PIN, HTCFG_ADC_AFIO_MODE); { /* ADC related settings */ /* CK_ADC frequency is set to (CK_AHB / 64) */ CKCU_SetADCnPrescaler(HTCFG_ADC_CKCU_ADCPRE, CKCU_ADCPRE_DIV64); /* Continuous mode, sequence length = 1 */ ADC_RegularGroupConfig(HTCFG_ADC_PORT, CONTINUOUS_MODE, 1, 0); /* ADC conversion time = (Sampling time + Latency) / CK_ADC = (1.5 + ADST + 12.5) / CK_ADC */ /* Set ADST = 0, sampling time = 1.5 + ADST */ #if (LIBCFG_ADC_SAMPLE_TIME_BY_CH) // The sampling time is set by the last parameter of the function "ADC_RegularChannelConfig()". #else ADC_SamplingTimeConfig(HTCFG_ADC_PORT, 0); #endif /* Set ADC conversion sequence as channel n */ ADC_RegularChannelConfig(HTCFG_ADC_PORT, HTCFG_VR_ADC_CH, 0, 0); /* Set software trigger as ADC trigger source */ ADC_RegularTrigConfig(HTCFG_ADC_PORT, ADC_TRIG_SOFTWARE); } /* Enable ADC single end of conversion interrupt */ ADC_IntConfig(HTCFG_ADC_PORT, ADC_INT_SINGLE_EOC, ENABLE); /* Enable the ADC interrupts */ NVIC_EnableIRQ(HTCFG_ADC_IRQn); }
数码管初始化: unsigned char NumberChar[]={ /* a: Seg4(0x10), b: Seg7(0x80), c: Seg2(0x04), d: Seg5(0x20), e: Seg3(0x08), f: Seg0(0x01), g: Seg6(0x40), dp: Seg1(0x02) */ 0xBD, /* '0' = a+b+c+d+e+f = 0x10 + 0x80 + 0x04 + 0x20 + 0x08 + 0x01 = 0xBD */ 0x84, /* '1' = b+c+ = 0x80 + 0x04 = 0x84 */ 0xF8, /* '2' = a+b +d+e +g = 0x10 + 0x80 + 0x20 + 0x08 + + 0x40 = 0xF8 */ 0xF4, /* '3' = a+b+c+d +g = 0x10 + 0x80 + 0x04 + 0x20 + + 0x40 = 0xF4 */ 0xC5, /* '4' = b+c +f+g = 0x80 + 0x04 + 0x01 + 0x40 = 0xC5 */ 0x75, /* '5' = a +c+d +f+g = 0x10 + 0x04 + 0x20 + 0x01 + 0x40 = 0x75 */ 0x7D, /* '6' = a +c+d+e+f+g = 0x10 + 0x04 + 0x20 + 0x08 + 0x01 + 0x40 = 0x7D */ 0x95, /* '7' = a+b+c +f = 0x10 + 0x80 + 0x04 + 0x01 = 0x95 */ 0xFD, /* '8' = a+b+c+d+e+f+g = 0x10 + 0x80 + 0x04 + 0x20 + 0x08 + 0x01 + 0x40 = 0xFD */ 0xF5, /* '9' = a+b+c+d +f+g = 0x10 + 0x80 + 0x04 + 0x20 + 0x01 + 0x40 = 0xF5 */ 0x02 /* '.' = dp = 0x02 = 0x02 */ };
void LEDC_Configuration(void) { LEDC_InitTypeDef LEDC_InitStruct; /* Turn on the APB clock of LEDC. */ CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}}; CKCUClock.Bit.LEDC = 1; CKCU_PeripClockConfig(CKCUClock, ENABLE);
/* Frame = fCK_LED / (LEDC_DutyClockNumber * COM Number) */ /* = 1600 / (8*4) = 50Hz */ LEDC_InitStruct.LEDC_ClockSource = LEDC_SRC_LSI; /* LSI = 32Khz */ LEDC_InitStruct.LEDC_ClockPrescaler = HTCFG_LEDC_CLOCK_PRESCAL - 1; /* fCK_LED = 32Khz / 20 = 1600 Hz */ LEDC_InitStruct.LEDC_DutyClockNumber = HTCFG_LEDC_DUTY_CLOCK_NUMBER; /* Duty CLock Number = 8 Clock */ LEDC_InitStruct.LEDC_COMxEN = HTCFG_LEDC_COM1EN | HTCFG_LEDC_COM2EN |\ HTCFG_LEDC_COM3EN | HTCFG_LEDC_COM4EN; /* Enable COMx */ /* Change the brightness, the parameter range is 0~Duty (CLock Number-1.) */ /* 0: the brightest, 7: the darkest. */ LEDC_InitStruct.LEDC_DeadTime = 0; LEDC_Init(&LEDC_InitStruct); /* LEC initialization. */
/* Set the LEDC mode to COMMON_CATHODE. */ LEDC_SetPolarityMode(HTCFG_LEDC_COM1POL | HTCFG_LEDC_COM2POL | HTCFG_LEDC_COM3POL | HTCFG_LEDC_COM4POL, LEDC_SEG0POL | LEDC_SEG1POL | LEDC_SEG2POL | LEDC_SEG3POL |\ LEDC_SEG4POL | LEDC_SEG5POL | LEDC_SEG6POL | LEDC_SEG7POL, COMMON_CATHODE);
LEDC_IntConfig(ENABLE); /* Enable frame interrupt. */ NVIC_EnableIRQ(LEDC_IRQn); /* Enable LECD IRQ. */ LEDC_Cmd(ENABLE); /* LEDC Start. */ }
void GPIO_Configuration(void) { /* Turn on the APB clock of AFIO. */ CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}}; CKCUClock.Bit.AFIO = 1; CKCU_PeripClockConfig(CKCUClock, ENABLE);
/* SEG0~7 */ AFIO_GPxConfig(HTCFG_LEDC_SEG0_GPIO_ID, HTCFG_LEDC_SEG0_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG1_GPIO_ID, HTCFG_LEDC_SEG1_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG2_GPIO_ID, HTCFG_LEDC_SEG2_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG3_GPIO_ID, HTCFG_LEDC_SEG3_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG4_GPIO_ID, HTCFG_LEDC_SEG4_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG5_GPIO_ID, HTCFG_LEDC_SEG5_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG6_GPIO_ID, HTCFG_LEDC_SEG6_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_SEG7_GPIO_ID, HTCFG_LEDC_SEG7_AFIO_PIN, AFIO_FUN_LEDC);
/* COM0~4 */ AFIO_GPxConfig(HTCFG_LEDC_COM0_GPIO_ID, HTCFG_LEDC_COM0_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_COM1_GPIO_ID, HTCFG_LEDC_COM1_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_COM2_GPIO_ID, HTCFG_LEDC_COM2_AFIO_PIN, AFIO_FUN_LEDC); AFIO_GPxConfig(HTCFG_LEDC_COM3_GPIO_ID, HTCFG_LEDC_COM3_AFIO_PIN, AFIO_FUN_LEDC); }
刷新数码管API: void DisPlay_Ledc(uint16_t num) { LEDC_SetData(HTCFG_LEDC_COM_D1, NumberChar[num/1000]); LEDC_SetData(HTCFG_LEDC_COM_D2, NumberChar[num%1000/100]); LEDC_SetData(HTCFG_LEDC_COM_D3, NumberChar[num%1000%100/10]); LEDC_SetData(HTCFG_LEDC_COM_D4, NumberChar[num%1000%100%10]);
}
main函数的while: while (1) { RUN_BLOCK(1000, Runing_LED();) ;//1S反转电平,非阻塞延时 RUN_BLOCK(100,DisPlay_Ledc(gPotentiometerLevel););//100ms刷新数码管,非阻塞延 } 实验现象可以看到LED1和LED2以1S闪烁,数码管刷新频率100ms。此时调节可变电阻,可以观察到数码管显示的变化,与仿真一致如图
自此测试完成。
|