## xmega-a3bu xplained pulse width measure

7 posts / 0 new
Author
Message

Hello,

Since monday i'm trying to measure pulse width on xmega-a3bu xplained board. I want measure button press time, but then i tried to set event source to clock_div_32k. Do you see what is wrong with my code?

cca_callback isn't executed any time.

I want to connect PF1 -> CH0 -> Timer.

conf_example.h

/**
* \file
*
* \brief Board- and chip-specific example configuration
*
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
*    Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifndef CONF_BOARD_H
#define CONF_BOARD_H

#include <compiler.h>

/*
#define GPIO_PUSH_BUTTON_0              IOPORT_CREATE_PIN(PORTE, 5)
#define GPIO_PUSH_BUTTON_1              IOPORT_CREATE_PIN(PORTF, 1)
#define GPIO_PUSH_BUTTON_2              IOPORT_CREATE_PIN(PORTF, 2)
*/
/* Example specific defines, please see the example documentation for details */
#define EXAMPLE_TC                       TCC0
#define LEDPORT                          PORTE
#define SWITCHPORT                       PORTF
//#define EVSYS_CHMUX_EXAMPLEPORT_PIN0_gc  EVSYS_CHMUX_PORTF_PIN0_gc;
#define EVSYS_CHMUX_EXAMPLEPORT_PIN0_gc  EVSYS_CHMUX_PORTF_PIN1_gc;

//#define TIMER_EXAMPLE_RESOLUTION 1000
#define TIMER_EXAMPLE_RESOLUTION 7812
#define TIMER_EXAMPLE_PERIOD     0x7fff
#define TIMER_PORT_LED           TCE0

#endif /* CONF_BOARD_H */


main.c

/**
* \file
*
* \brief Example application for GFX Monochrome System Font (sysfont)
*
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
*    Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include <asf.h>
#include <stdio.h>
#include <string.h>
#include <conf_example.h>

//#define CPU_PRESCALER 1
volatile uint8_t frequency_flag;
volatile uint32_t frequency;
volatile uint32_t dutyCycle;

volatile uint8_t cca_flag;
volatile uint16_t cca_capture;

/* Interrupt callback function declaration */
void cca_callback(void);

/**
* \mainpage

* \section dependencies Dependencies
* Relevant module dependencies for this application are:
* - \ref gfx_mono
* - \ref gfx_mono_font

// Wypełnienie całej matrycy
// Wymiary 128x32
//gfx_mono_generic_draw_filled_rect(0,0,128,32,1);
//gfx_mono_generic_draw_filled_rect(0,28,128,1,0);

//TC
section files Main files:
- tc.c Timer XMEGA Timer Counter driver implementation
- tc.h Timer XMEGA Timer Counter driver definitions
- tc_example4.c example application
- conf_example.h: configuration of the example

* \section contactinfo Contact Information
* For further information, visit <a href="http://www.atmel.com/">Atmel</a>.\n
*/

void cca_callback(void)
{
/* Store the compare channel result */
cca_capture = tc_read_cc(&EXAMPLE_TC, TC_CCA);
cca_flag = true;

LED_On(LED0);

/* The high 8-bits of the result are masked away while the lower 8-bits
are displayed on the LEDs. */
//LEDPORT.OUT = ~((uint8_t) (cca_capture & 0xff));
}

/**
* \brief Main entry of example application
*/
int main(void)
{
char temp[50];
char cfrequency[10];
char cdutyCycle[10];
char c_cca_capture[10];

frequency = 0;
dutyCycle = 0;

// Starts off by initializing the system clock before configuring the
// board and the monochrome graphical system.
board_init();
gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);

sysclk_init();
delay_init(F_CPU);
gfx_mono_init();

// --- TC EXAMPLE ---
ioport_configure_pin(GPIO_PUSH_BUTTON_1, IOPORT_DIR_INPUT | IOPORT_BOTHEDGES | IOPORT_PULL_UP);
/* Enable trigger on both edges */
//ioport_configure_port_pin(ioport_pin_to_port(GPIO_PUSH_BUTTON_1),
//ioport_pin_to_mask(GPIO_PUSH_BUTTON_1),	PORT_ISC_BOTHEDGES_gc | PORT_OPC_PULLUP_gc);

/* Setup pin0 (sw0) as input to event system channel 0 */
//EVSYS_CH0MUX = EVSYS_CHMUX_EXAMPLEPORT_PIN0_gc;
EVSYS_CH0MUX = EVSYS_CHMUX_PRESCALER_32768_gc;

/* Enable clock to event sys */
sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);

// Tego nie było
//tc_set_wgm(&EXAMPLE_TC, TC_WG_NORMAL);

/* Set resolution */
tc_set_resolution(&EXAMPLE_TC, TIMER_EXAMPLE_RESOLUTION);

/* Set period */
tc_write_period(&EXAMPLE_TC, TIMER_EXAMPLE_PERIOD);

/* Enable capture channels */
tc_enable_cc_channels(&EXAMPLE_TC, TC0_CCAEN_bm);

/* Clear timer interrupts */
tc_clear_cc_interrupt(&EXAMPLE_TC, TC_CCA);

/* Do capture on event channel 0 (sw0) */
tc_set_input_capture(&EXAMPLE_TC, TC_EVSEL_CH0_gc, TC_EVACT_FRQ_gc);
//tc_set_input_capture(&EXAMPLE_TC, TC_EVSEL_CH0_gc, TC_EVACT_PW_gc);

/* Register callback for interrupt */
tc_set_cca_interrupt_callback(&EXAMPLE_TC, &cca_callback);

/* Enable pmic module */
pmic_init();

/* Enable CCA interrupt at level low */
tc_set_cca_interrupt_level(&EXAMPLE_TC, TC_CCAINTLVL_LO_gc);

pmic_enable_level(PMIC_LOLVLEN_bm);

/* Enable clock to timer */
tc_enable(&EXAMPLE_TC);

/* Enable global interrupts */
cpu_irq_enable();
// === TC EXAMPLE ===

//sprintf(temp, "Frequency:%lu\r\nDutyCycle:%lu\r\nA\r\nB", frequency, dutyCycle);
//gfx_mono_draw_string(temp,0, 0, &sysfont);
gfx_mono_draw_string("Frequency:012345678901\r\nDutyCycle:012345678901\r\nA\r\nB",0, 0, &sysfont);

while (true)
{
if(gpio_pin_is_low(GPIO_PUSH_BUTTON_1))
{
LED_On(LED1);
//LED_On(LED2);	// red
//LED_On(LED3);	// green
}
else
{
LED_Off(LED1);
//LED_Off(LED2); // red
//LED_Off(LED3); // green
}

if(cca_flag)
{
cca_flag = false;

sprintf(c_cca_capture, "%lu", cca_capture);

gfx_mono_generic_draw_filled_rect(60,0,68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(c_cca_capture,60, 0, &sysfont);
}

/*
if(frequency<65535)
{
frequency += 1111;
} else {
frequency = 0;
}
if(dutyCycle<65535){
dutyCycle += 333;
} else {
dutyCycle = 0;
}
//sprintf(cfrequency, "%lu", frequency);
sprintf(cdutyCycle, "%lu", dutyCycle);

//gfx_mono_generic_draw_filled_rect(60,0,68,SYSFONT_HEIGHT,0);
//gfx_mono_draw_string(cfrequency,60, 0, &sysfont);

gfx_mono_generic_draw_filled_rect(60,(SYSFONT_HEIGHT+1),68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(cdutyCycle,60, SYSFONT_HEIGHT+1, &sysfont);
*/
}
}


This topic has a solution.
Last Edited: Thu. Dec 28, 2017 - 07:22 AM

Shouldn't you be enabling the peripheral clocks for EVSYS and EXAMPLE_TC before you configure them?

You are ignoring switch bounce, but this should not matter as you appear to be trying the frequency of TOSC.

In frequency capture mode, the XMEGA timers capture the period, not the frequency.  F = 1 / PERIOD.

You are converting an uint16_t to text as if it were an uint32_t:

volatile uint16_t cca_capture;
.
.
.
.
sprintf(cdutyCycle, "%lu", dutyCycle);


### Greg Muth

Portland, OR, US

Atmel Studio 7 (Version: 7.0.1652) on Windows 10

Xplained/Pro/Mini Boards mostly

Greg_Muth wrote:

Shouldn't you be enabling the peripheral clocks for EVSYS and EXAMPLE_TC before you configure them?

Maybe, could you tell me how to do it?

Main problem is that EVSYS doesn't work. I try next example from book Tomasz Francuz "AVR Praktyczne projekty" - ultrasonic distance measurement.

/**
* \file
*
* \brief Example application for GFX Monochrome System Font (sysfont)
*
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
*    Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include <asf.h>
#include <stdio.h>
#include <string.h>

#define CPU_PRESCALER 1
volatile uint8_t frequency_flag;
volatile uint16_t frequency;
volatile uint8_t dutyCycle_flag;
volatile uint16_t dutyCycle;

/**
* \mainpage

* \section dependencies Dependencies
* Relevant module dependencies for this application are:
* - \ref gfx_mono
* - \ref gfx_mono_font

// Wypełnienie całej matrycy
// Wymiary 128x32
//gfx_mono_generic_draw_filled_rect(0,0,128,32,1);
//gfx_mono_generic_draw_filled_rect(0,28,128,1,0);

* \section contactinfo Contact Information
* For further information, visit <a href="http://www.atmel.com/">Atmel</a>.\n
*/

ISR(TCD0_CCA_vect)
{
uint16_t tmp=TCF0_CCA;
TCF0.CTRLA=TC_CLKSEL_OFF_gc;   //Pomiar dokonany, timer niepotrzebny
dutyCycle = tmp;
dutyCycle_flag = true;
LED_On(LED0);
}

ISR(TCD0_OVF_vect)
{
TCF0.CTRLA=TC_CLKSEL_OFF_gc; //Brak echa w zadanym czasie
LED_On(LED1);
}

void US_init()
{
PORTF_OUTCLR=PIN1_bm; //Pin trigger
PORTF_DIRSET=PIN1_bm;

PORTF_PIN1CTRL=PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc | PORT_INVEN_bm;  //Pulup na pinie odbiornika IR, zbocza wywołują zdarzenia
//PORTF_PIN1CTRL=PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc;  // Button doesn't work, why?
EVSYS_CH1MUX=EVSYS_CHMUX_PORTF_PIN1_gc;                     //Transmitowane przez kanał 1
TCF0.CTRLB=TC_WGMODE_NORMAL_gc | TC0_CCAEN_bm;              //Tryb pracy normalny
TCF0.CTRLD=TC_EVSEL_CH1_gc | TC_EVACT_PW_gc;                //Kanał 1 wyzwala pomiar szerokości
TCF0.INTCTRLB=TC_CCAINTLVL_LO_gc;      //Włącz przerwanie CCA
TCF0.INTCTRLA=TC_OVFINTLVL_LO_gc;      //Włącz przerwanie nadmiaru
PMIC_CTRL=PMIC_LOLVLEN_bm;             //Odblokuj przerwania niskiego poziomu
}

void Gen_pulse()
{
LED_Off(LED1);
TCF0.CTRLA=TC_CLKSEL_DIV1_gc;
}

/**
* \brief Main entry of example application
*/
int main(void)
{
char temp[50];
char cfrequency[30];
char cdutyCycle[30];

uint16_t frequency_last = 0;
bool first_time_fr = true;

frequency = 0;
dutyCycle = 0;

// Starts off by initializing the system clock before configuring the
// board and the monochrome graphical system.
sysclk_init();
board_init();
gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
delay_init(F_CPU);
gfx_mono_init();
gfx_mono_draw_string("Frequency:012345678901\r\nDutyCycle:012345678901\r\nA\r\nB",0, 0, &sysfont);

US_init();
sei();

Gen_pulse();
while (true)
{
//if(dutyCycle_flag) // To display TCF0_CCA
{
dutyCycle_flag = false;
dutyCycle = TCF0_CCA;
sprintf(cdutyCycle, "%hu", dutyCycle);

gfx_mono_generic_draw_filled_rect(60,(SYSFONT_HEIGHT+1),68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(cdutyCycle,60, SYSFONT_HEIGHT+1, &sysfont);
}

if(frequency<65535)
{
frequency += 1111;
} else {
frequency = 0;
}

frequency = TCF0_CCA;
sprintf(cfrequency, "%hu", frequency);
if(frequency_last != frequency || first_time_fr)
{
first_time_fr = false;
frequency_last = frequency;
gfx_mono_generic_draw_filled_rect(60,0,68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(cfrequency,60, 0, &sysfont);
}

if(gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) gfx_mono_draw_string("HIGH",8, 2*(SYSFONT_HEIGHT+1), &sysfont);
else gfx_mono_draw_string("LOW ",8, 2*(SYSFONT_HEIGHT+1), &sysfont);
}
}


P.S. I paste new code. In pictures below DutyCycle is always 0.

I don't know why button doesn't work without INVEN

PORTF_PIN1CTRL=PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc | PORT_INVEN_bm;  //Pulup na pinie odbiornika IR, zbocza wywołują zdarzenia
//PORTF_PIN1CTRL=PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc;  // Button doesn't work, why?

Button UP

Button down

The interrupts are never executed. I don't know why.

Examples always use PIN0 and CCA. I use PIN1 PORTF.

Maybe I can't use pin1 and CCA. If you know how to enable EVENT system you can tell me.

Last Edited: Fri. Dec 22, 2017 - 08:37 AM

Maybe, could you tell me how to do it?

You are enabling the peripheral clocks in your first post, but are doing so after configuring them:

    /* Setup pin0 (sw0) as input to event system channel 0 */
//EVSYS_CH0MUX = EVSYS_CHMUX_EXAMPLEPORT_PIN0_gc;
EVSYS_CH0MUX = EVSYS_CHMUX_PRESCALER_32768_gc;

/* Enable clock to event sys */
sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
.
.
.
.
/* Enable clock to timer */
tc_enable(&EXAMPLE_TC);

I can't find these in the last code you posted .

If you're going to use sysclk_init(), be aware that it turns off the peripheral clock to ALL peripherals.  If you use ASF calls to initialize peripherals the clock is turned on for you.  If you're using your own initialization code, you need to turn on the peripheral clock at the beginning of your init code using sysclk_enable_module().

The interrupts are never executed. I don't know why.

Examples always use PIN0 and CCA. I use PIN1 PORTF.

You can use any pin you want/need to use.  Figure 3.1 above has nothing to do with what you are doing.  The figure shows output compare (waveform generation) functionality.

I've got code floating around on my PC for doing this.  I'll post it when I find it.

### Greg Muth

Portland, OR, US

Atmel Studio 7 (Version: 7.0.1652) on Windows 10

Xplained/Pro/Mini Boards mostly

This reply has been marked as the solution.

I couldn't find what I was looking for.  It was something like this UNTESTED code:

/*
* Use Event System and TCC1 to capture the width of the pulse on PORT C, Pin 4
*/

#include <asf.h>
#include <util/atomic.h>

uint16_t volatile data = 0;
uint8_t volatile update = 0;

int main (void)
{
uint16_t value;

board_init();
sysclk_init();

// clear bit 4 in the PORTC DDR
PORTC.DIRCLR = 1 << 4;

// enable pull-up and set edge sense to BOTH edges
PORTC.PIN4CTRL = PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc;

// enable peripheral clock to the event system
sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);

// put PINC4 onto event channel 5
EVSYS.CH5MUX = EVSYS_CHMUX_PORTC_PIN4_gc;

// enabler peripheral clock to TCC1
sysclk_enable_module(SYSCLK_PORT_C, SYSCLK_TC1);

/*
* configure TCC1 to capture Pulse Width on
* Event Channel 5, enable Capture Channel A
* and enable its interrupt, select CLK/8
*/
TCC1.CTRLD = TC_EVSEL_CH5_gc | TC_EVACT_PW_gc;
TCC1.CTRLB = TC1_CCAEN_bm;			// WGM = 0 (normal)
TCC1.INTCTRLA = TC_CCAINTLVL_LO_gc;
TCC1.CTRLA = TC_CLKSEL_DIV8_gc;		// whatever makes sense for your app

sei();

do
{
if (update) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
value = data;
update = 0;
}
/*
* output value
*/
}
} while (1);
}

ISR(TCC1_CCA_vect)
{
data = TCC1.CCA;
update++;
}

### Greg Muth

Portland, OR, US

Atmel Studio 7 (Version: 7.0.1652) on Windows 10

Xplained/Pro/Mini Boards mostly

I can paste good code in New Year. I open example for display, then connect with example CTC including ASF libraries and at the end I edit CTC example to PW using Greg_Muth example and I switch access to TCC1 on ASF functions. Probably the most important thing is enable timer and sysclocks for modules before initialization.

/* Enable clock to timer */
tc_enable(&EXAMPLE_TC);
// enable peripheral clock to the event system
sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
// enabler peripheral clock to TCC1
sysclk_enable_module(SYSCLK_PORT_C, SYSCLK_TC1);
Last Edited: Thu. Dec 28, 2017 - 07:27 AM

Working example of button pressure time measurement (XmegaA3BU):

/**
* \file
*
* \brief Example application for GFX Monochrome System Font (sysfont)
*
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
*    Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include <asf.h>
#include <stdio.h>
#include <string.h>

#define CPU_PRESCALER 1
volatile uint8_t frequency_flag;
volatile uint16_t frequency;
volatile uint16_t dutyCycle;

volatile uint8_t cca_flag;
volatile uint16_t cca_capture;

/**
* \mainpage

* \section dependencies Dependencies
* Relevant module dependencies for this application are:
* - \ref gfx_mono
* - \ref gfx_mono_font

// Wypełnienie całej matrycy
// Wymiary 128x32
//gfx_mono_generic_draw_filled_rect(0,0,128,32,1);
//gfx_mono_generic_draw_filled_rect(0,28,128,1,0);

* \section contactinfo Contact Information
* For further information, visit <a href="http://www.atmel.com/">Atmel</a>.\n
*/

//tc_get_resolution
//tc_set_input_capture
//tc_is_cc_interrupt

void cca_callback(void)
{
LED_Toggle(LED0);
/* Store the compare channel result */
cca_capture = tc_read_cc(&TCC0, TC_CCA);
cca_flag = true;

LED_On(LED0);

/* The high 8-bits of the result are masked away while the lower 8-bits
are displayed on the LEDs. */
//LEDPORT.OUT = ~((uint8_t) (cca_capture & 0xff));
}

/**
* \brief Main entry of example application
*/
int main(void)
{
char temp[50];
char cfrequency[30];
char cdutyCycle[30];

frequency = 0;
dutyCycle = 0;

// Starts off by initializing the system clock before configuring the
// board and the monochrome graphical system.
board_init();
pmic_init();
sysclk_init();

gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);

//sysclk_init();
delay_init(F_CPU);
gfx_mono_init();

//sprintf(temp, "Frequency:%lu\r\nDutyCycle:%lu\r\nA\r\nB", frequency, dutyCycle);
//gfx_mono_draw_string(temp,0, 0, &sysfont);
gfx_mono_draw_string("Frequency:012345678901\r\nDutyCycle:012345678901\r\nA\r\nB",0, 0, &sysfont);

//pmic_init();
//sysclk_init();
cpu_irq_enable();
tc_enable(&TCC0);

// enable peripheral clock to the event system
sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);

// Enable trigger on both edges
ioport_configure_port_pin(ioport_pin_to_port(GPIO_PUSH_BUTTON_1),
ioport_pin_to_mask(GPIO_PUSH_BUTTON_1),	PORT_ISC_BOTHEDGES_gc | PORT_OPC_PULLUP_gc);
PORTF.PIN1CTRL |= PORT_INVEN_bm;

// enabler peripheral clock to TCC0 - Work without enable module
//sysclk_enable_module(SYSCLK_PORT_C, SYSCLK_TC0);

// put PINF1 onto event channel 1
EVSYS.CH1MUX = EVSYS_CHMUX_PORTF_PIN1_gc;
//EVSYS.CH1MUX = EVSYS_CHMUX_PRESCALER_4096_gc;
EVSYS.CH1CTRL = EVSYS_DIGFILT_8SAMPLES_gc;

tc_set_cca_interrupt_callback(&TCC0, cca_callback);
tc_set_wgm(&TCC0, TC_WG_NORMAL);
// Set resolution
//tc_set_resolution(&TCC0, 10000);
// Set peroid
tc_write_period(&TCC0, 0x7fff);
// Enable capture channels
//tc_write_cc(&TCC0, TC_CCA, 100);
//tc_write_cc(&TCC0, TC_CCB, 1000);
tc_enable_cc_channels(&TCC0,TC_CCAEN);
//Clear timer interrupts
tc_clear_cc_interrupt(&TCC0, TC_CCA);
// Do capture on event channel 1 (sw1) !
tc_set_input_capture(&TCC0, TC_EVSEL_CH1_gc, TC_EVACT_PW_gc);
//Enable CCA interrupt at level low
tc_set_cca_interrupt_level(&TCC0, TC_INT_LVL_LO);
// Register callback for interrupt
//tc_set_cca_interrupt_callback(&TCC0, cca_callback);
tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1024_gc);

while (true) {
/*
if(frequency<65535)
{
frequency += 1111;
} else {
frequency = 0;
}
*/

//frequency = tc_read_cc(&TCC0, TC_CCA);	// Do ilu zlicza CCA
//dutyCycle = tc_read_cc(&TCC0, TC_CCB);	// Do ilu zlicza CCB
sprintf(cfrequency, "%hu", frequency);
gfx_mono_generic_draw_filled_rect(60,0,68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(cfrequency,60, 0, &sysfont);

if(cca_flag)
{
dutyCycle = cca_capture;
sprintf(cdutyCycle, "%hu", dutyCycle);

gfx_mono_generic_draw_filled_rect(60,(SYSFONT_HEIGHT+1),68,SYSFONT_HEIGHT,0);
gfx_mono_draw_string(cdutyCycle,60, SYSFONT_HEIGHT+1, &sysfont);
}

if(gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) LED_On(LED1);
else LED_Off(LED1);
}
}