/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : atim_inputcapture.c
 * Description : atim_inputcapture source file
 * Author(s)   : Hackett
 * version     : V1.0
 * Modify date : 2022-07-10
 ***********************************************************************/
#include  "atim_inputcapture.h"
#include  "uart0.h"
#include  "uart1.h"
#include  "uart2.h"
#include  "atimer.h"
#include  "gpio.h"

uint16_t atimer_arr = 48000, atimer_psc = 2;

/************************************************************************
 * function   : atimer_inputcapture_pro
 * Description: atimer_inputcapture_pro atimer处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void atimer_inputcapture_pro(void)
{
	float ccr1 = 0.0, ccr2 = 0.0, frequency = 0.0, duty_cycle = 0.0;
	
//	//普通输入捕获
//	if(atimer_get_status(ATIM_STATUS_CC1IF))
//	{
//		printfS("REG_ATIM_CCR1 : %d \r\n", atimer_get_capture(ATIM_CHANNEL1));
//	}	
//	if(atimer_get_status(ATIM_STATUS_CC2IF))
//	{
//		printfS("REG_ATIM_CCR2 : %d \r\n", atimer_get_capture(ATIM_CHANNEL2));
//	}
//	if(atimer_get_status(ATIM_STATUS_CC3IF))
//	{
//		printfS("REG_ATIM_CCR3 : %d \r\n", atimer_get_capture(ATIM_CHANNEL3));
//	}	
//	if(atimer_get_status(ATIM_STATUS_CC4IF))
//	{
//		printfS("REG_ATIM_CCR4 : %d \r\n", atimer_get_capture(ATIM_CHANNEL4));
//	}

	//PWM输入捕获
	if(atimer_get_status(ATIM_STATUS_UIF))
	{	
		ccr1 = REG_ATIM_CCR1;
		ccr2 = REG_ATIM_CCR2;										
		frequency = (float)(48000000 / (atimer_psc * ccr1));
		duty_cycle = (float)((ccr2 * 100) / ccr1);
		
		printfS("REG_ATIM_CCR1 : %d \r\n", atimer_get_capture(ATIM_CHANNEL1));
		printfS("REG_ATIM_CCR2 : %d \r\n", atimer_get_capture(ATIM_CHANNEL2));									
		printfS("频率：%0.2fHz \r\n占空比：%0.2f%%  \r\n ", frequency, duty_cycle);
	}
}


/************************************************************************
 * function   : atimer_encoder_test
 * Description: 输入捕获模式
 * input : none
 * return: none
 ************************************************************************/
void atimer_inputcapture_test(void)
{
	printfS("start atimer inputcapture test! \r\n");
	
	gpio_init(GPIOF, PIN0, 7);		//PF0 ATIM_CH1
	atimer_module_enable();			//使能时钟并复位定时器0模块				
	/*定时器计数周期不能小于输入的PWM周期，捕获PWM的精度取决于计数次数和计数时间！！！*/
	atimer_inputcapture_init(atimer_arr, atimer_psc, ATIM_PWMINPUT, ATIM_CHANNEL1);		
	atimer_counter(ATIM_ENABLE);	//计数器使能
	atimer_irq(ATIM_UIE, ATIMER_IRQn, ATIM_ENABLE, atimer_inputcapture_pro);//允许更新事件中断
	
}






