|
1 | 1 | <!-- Please do not change this logo with link --> |
2 | 2 | [](https://www.microchip.com) |
3 | 3 |
|
4 | | -# Update the title for avr128db48-blink-with-timer-mplab-mcc here |
| 4 | +# Blink with Timer Overflow |
5 | 5 |
|
6 | | -<!-- This is where the introduction to the example goes, including mentioning the peripherals used --> |
| 6 | +This code example shows how to use single slope Pulse-Width Modulation (PWM) in the Timer/Counter A (TCA) on the AVR® DB Family of microcontrollers to generate overflow interrupts that toggle a LED. The time between each overflow interrupt is determined by the TCA clock prescaler selection and the value loaded into the period register. Then the overflow interrupt is enabled and the TCA is started. Inside the interrupt handler function, the LED is toggled. The result is that the amber LED0 on the AVR128DB48 Curiosity Nano development board is blinking with an equal period on and off (50% duty cycle). |
| 7 | + |
| 8 | + |
7 | 9 |
|
8 | 10 | ## Related Documentation |
9 | 11 |
|
10 | | -<!-- Any information about an application note or tech brief can be linked here. Use unbreakable links! |
11 | | - In addition a link to the device family landing page and relevant peripheral pages as well: |
12 | | - - [AN3381 - Brushless DC Fan Speed Control Using Temperature Input and Tachometer Feedback](https://microchip.com/00003381/) |
13 | | - - [PIC18F-Q10 Family Product Page](https://www.microchip.com/design-centers/8-bit/pic-mcus/device-selection/pic18f-q10-product-family) --> |
| 12 | +- [AVR128DB48 device page](https://www.microchip.com/wwwproducts/en/AVR128DB48) |
| 13 | + |
14 | 14 |
|
15 | 15 | ## Software Used |
16 | 16 |
|
17 | | -<!-- All software used in this example must be listed here. Use unbreakable links! |
18 | | - - MPLAB® X IDE 5.30 or newer [(microchip.com/mplab/mplab-x-ide)](http://www.microchip.com/mplab/mplab-x-ide) |
19 | | - - MPLAB® XC8 2.10 or a newer compiler [(microchip.com/mplab/compilers)](http://www.microchip.com/mplab/compilers) |
20 | | - - MPLAB® Code Configurator (MCC) 3.95.0 or newer [(microchip.com/mplab/mplab-code-configurator)](https://www.microchip.com/mplab/mplab-code-configurator) |
21 | | - - MPLAB® Code Configurator (MCC) Device Libraries PIC10 / PIC12 / PIC16 / PIC18 MCUs [(microchip.com/mplab/mplab-code-configurator)](https://www.microchip.com/mplab/mplab-code-configurator) |
22 | | - - Microchip PIC18F-Q Series Device Support (1.4.109) or newer [(packs.download.microchip.com/)](https://packs.download.microchip.com/) --> |
| 17 | +Microchip’s free MPLAB X IDE, compiler and MPLAB Code Configurator (MCC) graphical code generator are used throughout the application firmware development to provide easy and hassle-free user experience. Following are the tool versions used for this example: |
| 18 | + |
| 19 | +- [MPLAB® X IDE v5.45](https://www.microchip.com/mplab/mplab-x-ide) or newer |
| 20 | +- [MPLAB® Xpress IDE](https://www.microchip.com/xpress) (alternative to MPLAB X IDE) |
| 21 | +- [XC8 Compiler v2.31](https://www.microchip.com/mplab/compilers) or newer |
| 22 | +- [MPLAB® Code Configurator (MCC) v4.1.0](https://www.microchip.com/mplab/mplab-code-configurator) or newer |
| 23 | +- [MPLAB® Melody Library 1.37.24 or newer](https://www.microchip.com/mplab/mplab-code-configurator) or newer |
| 24 | +- [MCC Device Libraries 8-bit AVR MCUs 2.7.0](https://www.microchip.com/mplab/mplab-code-configurator) or newer |
| 25 | +- [Microchip AVR128DB48 Device Support Pack AVR-Dx_DFP 1.2.98](https://packs.download.microchip.com/) or newer |
| 26 | + |
23 | 27 |
|
24 | 28 | ## Hardware Used |
25 | 29 |
|
26 | | -<!-- All hardware used in this example must be listed here. Use unbreakable links! |
27 | | - - PIC18F47Q10 Curiosity Nano [(DM182029)](https://www.microchip.com/Developmenttools/ProductDetails/DM182029) |
28 | | - - Curiosity Nano Base for Click boards™ [(AC164162)](https://www.microchip.com/Developmenttools/ProductDetails/AC164162) |
29 | | - - POT Click board™ [(MIKROE-3402)](https://www.mikroe.com/pot-click) --> |
| 30 | +- [AVR128DB48 Curiosity Nano](https://www.microchip.com/DevelopmentTools/ProductDetails/PartNO/EV35L43A) |
| 31 | + |
| 32 | + |
| 33 | +## Peripherals Configuration using MCC |
| 34 | + |
| 35 | +### Added Peripherals |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +### System Configuration: INTERRUPT MANAGER |
| 42 | + |
| 43 | +In the INTERRUPT MANAGER, global interrupts is enabled. The interrupt assosiated with TCA0 (overflow) is enabled in the TCA0 confguration. |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +### System Configuration: PINS |
| 48 | + |
| 49 | +The PINS configuration is made up of two boxes: the *Pins Grid View* and the *Pins* configuration box. |
| 50 | + |
| 51 | +In the *Pins Grid View*, left-click on PORTB pin3 in the "Pin Module, GPIO, Output" row. |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +In the *Pins* configuration box, now the pin you added will show (Pin Name = PB3). In column *Custom Name*, enter "LED0_PIN". The helper functions for PB3 will now get the prefix "LED0_PIN" instead of the default prefix ("IO_PB3"). On the AVR128DB48 Curiosity Nano board this is the pin that controls LED0. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +### TCA0 Configuration |
| 60 | + |
| 61 | +- TCA0 must be enabled by turning on "Enable Timer" button. |
| 62 | +- TCA clock should be set to "System Clock / 16". The Timer Clock frequency is automatically calculated and shown. Since we have not made any configuration to system clock, this is set to the default system clock which is 4 MHz. 4 MHz / 16 = 250 kHz (figure below shows System Clock Configuration for reference). |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +- Timer mode should be set to "16 Bit (Normal)" mode, as we are not using split mode operation (figure is taken from data sheet showing TCA normal mode operation). |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +- Count Direction should be "UP". |
| 71 | +- Requested Timeout used in this example is 0.25s. For different periods, use a combination of Clock Selection and Requested Timeout to create the desired period (based on the Clock Selection prescaler value, the possible range for Requested Timeout will change, higer prescaler gives longer timeout options. The actual timeout will be calculated and shown in next line (this should be identical to Requested Timout in normal mode). |
| 72 | +- Turn on "Enable Overflow Interrupt" button to enable overflow interrupts from TCA0. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +Now all configuration is complete. You have now enabled global interrupts, set PB3 as output in order to blink the LED and configured the timer (TCA0) to count up to a given value that gives you a certain time period (250 ms), then trigger an overflow interrupt. Next steps is setting up the hardware and programming the device. |
| 77 | + |
30 | 78 |
|
31 | 79 | ## Setup |
32 | 80 |
|
33 | | -<!-- Explain how to connect hardware and set up software. Depending on complexity, step-by-step instructions and/or tables and/or images can be used --> |
| 81 | +- Connect the AVR128DB48 Curiosity Nano board to your computer using a USB cable |
| 82 | +- Download and install all software components as listed under 'Software Used' (note that MPLAB Xpress IDE is an online tool that can not be downloaded) |
| 83 | + |
34 | 84 |
|
35 | 85 | ## Operation |
36 | 86 |
|
37 | | -<!-- Explain how to operate the example. Depending on complexity, step-by-step instructions and/or tables and/or images can be used --> |
| 87 | +1. Download the zip file or clone the example to get the source code |
| 88 | +1. Open the .X file with the MPLAB® X IDE |
| 89 | +1. Set the project as Main project by right-clicking the project name in the *Projects* window (Ctrl+1) and select *"Set as Main Project"* from the drop-down menu |
| 90 | +1. Program the project to the AVR128DB48 Curiosity Nano: |
| 91 | + 1. First clean and build the project by pressing the *Clean and Build Main Project* button |
| 92 | + |
| 93 | +  |
| 94 | + |
| 95 | + 1. Then make and progam the project to the AVR128DB48 by clicking the *Make and Program Device Main Project* button |
| 96 | + |
| 97 | +  |
| 98 | + |
| 99 | +1. To verify that the code is working: |
| 100 | + 1. Watch that the amber LED on the Curiosity Nano board is blinking |
| 101 | + |
| 102 | + |
| 103 | +## Some details regarding the code |
38 | 104 |
|
| 105 | +The function used to toggle the LED is called *"LED0_PIN_Toggle()"* and is generated by Melody when enabling the PINS configuration. The function (and several others) is found in the *"pins.h"* header file. Note that the function name will change according to the Pin name given in the "Custom Name" cell of PINS setup: *"<pin_name>_Toggle()"* . |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +The TCA period (duty cycle for LED0) is given by the value loaded into the TCA0.PER register. The value used in the example is 250 ms. This translates to a TCA0.PER register value as: TCA0.PER = 250 ms / 4 us = 62 500 = 0xF424. By using different TCA clock prescalers and different period values you can make a lot of different duty cycles or periods. |
| 110 | + |
| 111 | +The functions related to TCA0 are found in the *tca0.c* file. This screenshot shows the functions that executes the interrupt code: |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +This screenshot shows the actual interrupt handler function (ISR), located further down in the same file: |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +The TCA0 overflow interrupt handler is not where the actual code that is executed on each interrupt is located. |
| 120 | + |
| 121 | +```c |
| 122 | +ISR(TCA0_OVF_vect) |
| 123 | +{ |
| 124 | + if (TCA0_OVF_isr_cb != NULL) |
| 125 | + (*TCA0_OVF_isr_cb)(); |
| 126 | + |
| 127 | + TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm; |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +The interrupt handler jumps to a pointer to the function that contains the interrupt code to be executed: |
| 132 | + |
| 133 | +```c |
| 134 | +void TCA0_DefaultOverflowCallbackRegister(void); |
| 135 | +void (*TCA0_OVF_isr_cb)(void) = &TCA0_DefaultOverflowCallbackRegister; |
| 136 | +``` |
| 137 | + |
| 138 | +Inside this function we add the helper function that toggles PB3 ( which is given the custom name "LED0_PIN") mentioned above. |
| 139 | + |
| 140 | +```c |
| 141 | +void TCA0_DefaultOverflowCallbackRegister(void) |
| 142 | +{ |
| 143 | + //Add your ISR code here |
| 144 | + //When TCA0 overflow interrupt is triggered, toggle output value on LED0 pin (PB3) |
| 145 | + LED0_PIN_Toggle(); |
| 146 | +} |
| 147 | +``` |
| 148 | +
|
| 149 | +The exact same result can be achieved by placing this code inside the actual interrupt handler. |
| 150 | +
|
| 151 | + |
39 | 152 | ## Summary |
40 | 153 |
|
41 | | -<!-- Summarize what the example has shown --> |
| 154 | +This example shows how to configure the TCA0 for single slope PWM operation with overflow interrupt, using the interrupt handler to toggle a LED. |
| 155 | +
|
| 156 | + |
| 157 | +
|
0 commit comments