void AD7606_PWM_ModeConfig(uint32_t frequency)
{
uint32_t arr_value, psc_value;
GPIO_InitType GPIO_InitStructure;
tiM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_TIM1 , ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
//PWM(Hz) = 144MHz / ((ARR + 1) * (PSC + 1))
//ARR(arr_value) -> TIM_Period
//PSC(psc_value) -> TIM_Prescaler
//psc_value = 144 - 1;
//arr_value = SystemCoreClock / (psc_value + 1) / frequency - 1;
psc_value = 1;
arr_value = 899;
TIM_TimeBaseStructure.Period = arr_value;
TIM_TimeBaseStructure.Prescaler = psc_value;
TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;
TIM_OCInitStructure.Pulse = 4;
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
TIM_ConfigOc1Preload(TIM1 , TIM_OC_PRE_LOAD_ENABLE);
TIM_OCInitStructure.Pulse = 4;
TIM_InitOc4(TIM1, &TIM_OCInitStructure);
TIM_ConfigOc4Preload(TIM1 , TIM_OC_PRE_LOAD_ENABLE);
TIM_ConfigArPreload(TIM1 , ENABLE);
TIM_Enable(TIM1, ENABLE);
TIM_EnableCtrlPwmOutputs(TIM1 , ENABLE);
}
void AD7606_RD_CS_RST_Dataport_Init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD | RCC_APB2_PERIPH_GPIOE | RCC_APB2_PERIPH_AFIO , ENABLE);
//AD7606 DATA GPIO
GPIO_InitStructure.Pin = GPIO_PIN_ALL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOE , &GPIO_InitStructure);
// ADREAD->PD2
GPIO_InitStructure.Pin = GPIO_PIN_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
// ADCS->PD3
GPIO_InitStructure.Pin = GPIO_PIN_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOD , &GPIO_InitStructure);
// COVA->PA8, COVB->PA11 PWM
// ADRESET->PD4
GPIO_InitStructure.Pin = GPIO_PIN_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOD , &GPIO_InitStructure);
// ADBUSY->PA15 EXTI
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
Delay_ms(1);
AD7606_RESET_0;
AD7606_CS_0;
AD7606_RD_1;
Delay_ms(1);
AD7606_RESET_1;
Delay_us(1);
AD7606_RESET_0;
Delay_us(1);
}
void AD7606_EXTI5_Int_Init(void)
{
GPIO_InitType GPIO_InitStructure = {0};
EXTI_InitType EXTI_InitStructure = {0};
NVIC_InitType NVIC_InitStructure = {0};
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOD , ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitPeripheral(GPIOD , &GPIO_InitStructure);
GPIO_ConfigEXTILine(GPIOD_PORT_SOURCE , GPIO_PIN_SOURCE5);
EXTI_InitStructure.EXTI_Line = EXTI_LINE5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitPeripheral(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static int count_exti = 0 , count_exti_400 = 0;
void AD7606ReadSample0(void)
{
int i;
short ad0;
AD7606_RD_0;
__NOP();
ad0 = READ_AD_DATA;
AD7606_RD_1;
__NOP();
printf("ad0 = %dn" , ad0);
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_LINE5) != RESET)
{
AD7606ReadSample0();
EXTI_ClrITPendBit(EXTI_LINE5);
}
}