//main.c Main program V1.0.0 2024-03-20 Copyright (C) 2024 Geehy Semiconductor //Includes #include "user_config.h" #include "board.h" #include <stdio.h> #include <stdbool.h> #include "g32a1xxx_int.h" #include "g32a1xxx_adc.h" #include "g32a1xxx_pins.h" //#include "osif.h" #include "12864.h" //G32A1465_Examples //ADC_SoftwareConversion_Macros Macros #define ADC_INSTANCE (0U) static __IO uint32_t g_timingDelay; float g_adcMaxValue = 4096; float g_adcDifference = 3.3f; int k=0,kk=0; uint32_t js=0; float adcValue; //PINS_Led_Macros Macros #define LED_GPIO GPIOD #define BLUE_LED_PIN (0U) #define RED_LED_PIN (15U) #define GREEN_LED_PIN (16U) #define WHITE_LED_PIN (~0x00018001)//NOTE:For testing reference only! // Start SysTick void SysTick_Init(void){ //Update System Core Clock info SystemCoreClockUpdate(); //SystemFrequency/1000=1ms if(SysTick_Config(SystemCoreClock / 1000)) {while(1);}//Capture error } //Time in milliseconds Decrements the TimingDelay void SysTick_Delay_ms(__IO uint32_t nTime){g_timingDelay = nTime;while (g_timingDelay != 0);} void TimingDelay_Decrement(void){if (g_timingDelay != 0){g_timingDelay--;}} //Key IniteruptHandler void KEY_IRQHandler(void){ //Get interrupt flag if((PINS_ReadPmInt**(PMC) & (1<<KEY1_PIN)) != 0){ LED_On(LED_RED); //Clear interrupt flag PINS_ClrPinInt**Cmd(PMC, KEY1_PIN);kk=101; }else if((PINS_ReadPmInt**(PMC) & (1<<KEY2_PIN)) != 0){ LED_Off(LED_RED); //Clear interrupt flag PINS_ClrPinInt**Cmd(PMC, KEY2_PIN);kk=202; }else{ //Clear interrupt flags for all pins in the port PINS_ClrPmInt**Cmd(PMC); } } //ADC software conversion initialization configuration void ADC_init(void){ ADC_CONV_CFG_T tempAdcConvCfg0; ADC_AVG_CFG_T tempAdcAverCfg0; ADC_CALIBRATION_T tempAdcCalibration0; //Reset adc module ADC_Reset(ADC_INSTANCE); //Call default configuration ADC_ConfigConverterStruct(&tempAdcConvCfg0); tempAdcConvCfg0.clockDivision = ADC_CLK_DIVISION_4; tempAdcConvCfg0.resolution = ADC_RESOLUTION_RATIO_12BIT; //Call default configuration ADC_ConfigHwAverageStruct(&tempAdcAverCfg0); tempAdcAverCfg0.hwAvgEnable = true; tempAdcAverCfg0.hwAverage = ADC_HW_AVERAGE_32; //Call default configuration ADC_ConfigUserCalibrationStruct(&tempAdcCalibration0); tempAdcCalibration0.userGain = ADC_DEFAULT_SAMPLE_TIME; tempAdcCalibration0.userOffset = 20000u; //Set software pre triggering ADC_SetSwPretrigger(ADC_INSTANCE,ADC_SW_PRE_TRIGGER_0); //Initialize ADC based on configuration values ADC_ConfigConverter(ADC_INSTANCE,&tempAdcConvCfg0); //Initialize ADC hardware averaging function based on configuration values ADC_ConfigHwAverage(ADC_INSTANCE,&tempAdcAverCfg0); //Enable calibration function ADC_ConfigUserCalibration(ADC_INSTANCE,&tempAdcCalibration0); //Verify ADC ADC_AutoCalibration(ADC_INSTANCE); } //Read ADC value float ADC_readValue(void){ uint16_t partAdcValue; ADC_CHAN_CONFIG_T tempAdcChanCfg0; //Call default configuration ADC_ConfigChanStruct(&tempAdcChanCfg0); tempAdcChanCfg0.interruptEnable = false; tempAdcChanCfg0.channel = ADC_INPUT_CHANNEL_EXT12; //Trigger using software ADC_ConfigChan(ADC_INSTANCE, 0u, &tempAdcChanCfg0); //waiting for conversion to complete ADC_WaitConvDone(ADC_INSTANCE); //Obtain conversion results ADC_ReadChanResult(ADC_INSTANCE, 0u, &partAdcValue); return (float)partAdcValue; } void PINS_Led_Init(){ //Enable Clock to Port D CLOCK_SYS_ConfigModuleClock(PMD_CLK, NULL); //Set pin mode PINS_SetMuxModeSel(LED_RED_PORT_BASE, LED_RED_PIN, PM_MUX_AS_GPIO); PINS_SetMuxModeSel(LED_GREEN_PORT_BASE, LED_GREEN_PIN, PM_MUX_AS_GPIO); PINS_SetMuxModeSel(LED_BLUE_PORT_BASE, LED_BLUE_PIN, PM_MUX_AS_GPIO); //Set pin interrupt PINS_SetPinIntSel(LED_RED_PORT_BASE, LED_RED_PIN, PM_DMA_INT_DISABLED); PINS_SetPinIntSel(LED_GREEN_PORT_BASE, LED_GREEN_PIN, PM_DMA_INT_DISABLED); PINS_SetPinIntSel(LED_BLUE_PORT_BASE, LED_BLUE_PIN, PM_DMA_INT_DISABLED); //GPIO Initialization PINS_SetPins(LED_RED_GPIO_BASE, 1U << LED_RED_PIN); PINS_SetPins(LED_GREEN_GPIO_BASE, 1U << LED_GREEN_PIN); PINS_SetPins(LED_BLUE_GPIO_BASE, 1U << LED_BLUE_PIN); //Set pin as output PINS_SetPinDir(LED_RED_GPIO_BASE, LED_RED_PIN, 1U); PINS_SetPinDir(LED_GREEN_GPIO_BASE, LED_GREEN_PIN, 1U); PINS_SetPinDir(LED_BLUE_GPIO_BASE, LED_BLUE_PIN, 1U); } //Pins module initialization button void PINS_Btn_Init(){ //Enable PMC clock CLOCK_SYS_ConfigModuleClock(PMC_CLK, NULL); //Set pin mode PINS_SetMuxModeSel(KEY1_PORT_BASE, KEY1_PIN, PM_MUX_AS_GPIO); PINS_SetMuxModeSel(KEY2_PORT_BASE, KEY2_PIN, PM_MUX_AS_GPIO); //Set pin interrupt PINS_SetPinIntSel(KEY1_PORT_BASE, KEY1_PIN, PM_INT_RISING_EDGE); PINS_SetPinIntSel(KEY2_PORT_BASE, KEY2_PIN, PM_INT_RISING_EDGE); //Set input direction PINS_SetPinDir(KEY1_GPIO_BASE, KEY1_PIN, 0); PINS_SetPinDir(KEY2_GPIO_BASE, KEY2_PIN, 0); PINS_ClrPinInt**Cmd(KEY1_PORT_BASE, KEY1_PIN); PINS_ClrPinInt**Cmd(KEY2_PORT_BASE, KEY2_PIN); } //Main function int main(void){ //Initialize clock CLOCK_SYS_Init(&g_clockConfig); //Led init LED_Init(); PINS_WritePin(LED_GPIO, BLUE_LED_PIN, 1); PINS_WritePin(LED_GPIO, GREEN_LED_PIN, 1); PINS_WritePin(LED_GPIO, RED_LED_PIN, 1); //UART init COM_Init(); //button init PINS_Btn_Init(); //set PMC interrupt callback function INT_SYS_InstallHandler(PMC_IRQn,&KEY_IRQHandler,(ISR_T*) 0); INT_SYS_InstallHandler(SysTick_IRQn,&SysTick_Handler,(ISR_T*) 0); //Enable PMC interrupt INT_SYS_EnableIRQ(PMC_IRQn); INT_SYS_EnableIRQ(SysTick_IRQn); //ADC init ADC_init(); SysTick_Init(); lcdInit();lcdClear();dispBase(); printf("Test G32A1465 lcdDisp adc keyInt and ...\r\n"); while (1){ if(js>999999)js=0;//else js++; //Calculated voltage value Output ADC conversion value adcValue = ADC_readValue()/g_adcMaxValue*g_adcDifference; printf("ADC conversion voltage value = %fV Key=%3d \r\n",adcValue,kk); dispMes(); LED_On(2);SysTick_Delay_ms(1);LED_Off(2); SysTick_Delay_ms(100); } } //end of group ADC_SoftwareConversion_Functions and group ADC_SoftwareConversion and group Examples |