上面的就是原理图,下面是代码
isr_compare_Start();
PWM_Window_Start();
Counter_Start();
//Clock_PWM_Start();
/* Calculate the time window during which the counter will count */
PWM_windowPeriod = PWM_Window_ReadPeriod() ;
/* Update the Time window value according to the clock given to the PWM_Window */
PWM_windowPeriod = PWM_windowPeriod/ PWM_FREQ;
/* Enable the global interrupt */
//CYGlobalIntEnable;
/* Display the term Frequency on line 1 of the LCD */
//LCD_Display_Position(0, 0);
//LCD_Display_PrintString("FREQUENCY");
for(;;)
{
/* Your code here. */
/* Check if the PWM interrupt has occurred
* The code works in such a way that, the PWM Compare event has an interrupt.
* The interrupt occurs for every 100 millisec (Interrupt on Compare Event)
* The frequency calculation is done whenever the interrupt occurs */
if (compare_occured == 1)
{
/* Read the Counter capture register */
counter_countVal = Counter_ReadCapture();
/* Convert the counts to frequency.
* Frequency is the number of counts in seconds
* In this case counts within "PWM time window" (100 millisecond in this example) is got from Counter.
* So we need to find for 1000 milliseconds */
input_freq = ((uint32)(NO_OF_MSEC * (uint32)counter_countVal) / (uint32)PWM_windowPeriod);
/* Display the value in the LCD */
//LCD_Display_Position(1, 0);
//LCD_Display_PrintInt16(HI16(input_freq));
//LCD_Display_Position(1, 4);
//LCD_Display_PrintInt16(LO16(input_freq));
///LCD_Display_Position(1, 9);
//LCD_Display_PrintString("Hz");
/* Clear the interrupt flag */
compare_occured = 0;