Skip to content

Commit 5cf2f46

Browse files
Erling Holten WikenErling Holten Wiken
authored andcommitted
MPAE-12175: first commit
1 parent 093bd3e commit 5cf2f46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4637
-30
lines changed

.main-meta/main.json

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"content": {
55
"metaDataVersion": "1.1.0",
66
"name": "com.microchip.mcu8.mplabx.project.avr128db48-blink-with-timer-mplab-mcc",
7-
"version": " ",
8-
"displayName": "AVR128DB48 blink with timer mplab mcc",
7+
"version": "1.0.0",
8+
"displayName": "LED Blink using Timer",
99
"projectName": "avr128db48-blink-with-timer-mplab-mcc",
10-
"shortDescription": "This example uses the MCC Melody Library to show how to configure Timer Counter A TCA in overflow interrupt mode to perform Pulse Width Modulation PWM using the AVR128DB48 Curiosity Nano development kit Works with MPLAB X or MPLAB XPRESS IDE",
10+
"shortDescription": "This example uses the MCC Melody Library to show how to configure Timer/Counter A (TCA) in overflow interrupt mode to perform Pulse Width Modulation (PWM) using the AVR128DB48 Curiosity Nano development kit. Works with MPLAB X or MPLAB XPRESS IDE.",
1111
"ide": {
1212
"name": "MPLAB X",
1313
"semverRange": ">=5.45.0"
@@ -19,12 +19,12 @@
1919
}
2020
],
2121
"dfp": {
22-
"name": "",
23-
"semverRange": ""
22+
"name": "AVR-Dx_DFP",
23+
"semverRange": "^1.7.98"
2424
},
2525
"configurator": {
26-
"name": "",
27-
"semverRange": ""
26+
"name": "MCC",
27+
"semverRange": ">=4.1.0"
2828
},
2929
"device": {
3030
"metaDataVersion": "1.0.0",
@@ -36,10 +36,24 @@
3636
"versionRange": "*"
3737
}
3838
},
39-
"author": "Prasad Aradhya",
40-
"subcategories": [],
41-
"peripherals": [],
42-
"keywords": [],
39+
"author": "Erling Holten Wiken",
40+
"subcategories": [
41+
[
42+
"Peripherals", "TCA"
43+
],
44+
[
45+
"Peripherals", "PORT"
46+
]
47+
],
48+
"peripherals": [
49+
"TCA", "PORT"
50+
],
51+
"keywords": [
52+
"PWM",
53+
"8/16 bit timer",
54+
"Curiosity Nano",
55+
"Melody"
56+
],
4357
"additionalData": {
4458
"longDescription": {
4559
"metaDataVersion": "1.0.0",

README.md

Lines changed: 135 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,157 @@
11
<!-- Please do not change this logo with link -->
22
[![MCHP](images/microchip.png)](https://www.microchip.com)
33

4-
# Update the title for avr128db48-blink-with-timer-mplab-mcc here
4+
# Blink with Timer Overflow
55

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+
![cnano](images/avr128db48_cnano_board.png)
79

810
## Related Documentation
911

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+
1414

1515
## Software Used
1616

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+
2327

2428
## Hardware Used
2529

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+
![Pheripherals](images/peripherals.png)
38+
39+
![system_cfg](images/config_overview.png)
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+
![Interrupts](images/interrupt_manager_needupdate.png)
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+
![pins_grid](images/pin_grid.png)
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+
![pins](images/pins.png)
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+
![clk_cfg](images/clk_cfg.png)
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+
![tca_normalmode_fig](images/tca0_normal_mode_fig.png)
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+
![tca0](images/TCA0_needsupdate.png)
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+
3078

3179
## Setup
3280

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+
3484

3585
## Operation
3686

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+
![clean_and_build](images/clean_and_build.png)
94+
95+
1. Then make and progam the project to the AVR128DB48 by clicking the *Make and Program Device Main Project* button
96+
97+
![make_and_prog](images/make_and_prog.png)
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
38104

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+
![pins_functions](images/pins_functions.png)
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+
![tca0_view](images/tca0_view.png)
114+
115+
This screenshot shows the actual interrupt handler function (ISR), located further down in the same file:
116+
117+
![tca0_view2](images/tca0_view2.png)
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+
39152
## Summary
40153
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+
![blinky_gif](images/blinky_pwd.gif)
157+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#
2+
# There exist several targets which are by default empty and which can be
3+
# used for execution of your targets. These targets are usually executed
4+
# before and after some main targets. They are:
5+
#
6+
# .build-pre: called before 'build' target
7+
# .build-post: called after 'build' target
8+
# .clean-pre: called before 'clean' target
9+
# .clean-post: called after 'clean' target
10+
# .clobber-pre: called before 'clobber' target
11+
# .clobber-post: called after 'clobber' target
12+
# .all-pre: called before 'all' target
13+
# .all-post: called after 'all' target
14+
# .help-pre: called before 'help' target
15+
# .help-post: called after 'help' target
16+
#
17+
# Targets beginning with '.' are not intended to be called on their own.
18+
#
19+
# Main targets can be executed directly, and they are:
20+
#
21+
# build build a specific configuration
22+
# clean remove built files from a configuration
23+
# clobber remove all built files
24+
# all build all configurations
25+
# help print help mesage
26+
#
27+
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
28+
# .help-impl are implemented in nbproject/makefile-impl.mk.
29+
#
30+
# Available make variables:
31+
#
32+
# CND_BASEDIR base directory for relative paths
33+
# CND_DISTDIR default top distribution directory (build artifacts)
34+
# CND_BUILDDIR default top build directory (object files, ...)
35+
# CONF name of current configuration
36+
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
37+
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
38+
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
39+
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
40+
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
41+
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
42+
#
43+
# NOCDDL
44+
45+
46+
# Environment
47+
MKDIR=mkdir
48+
CP=cp
49+
CCADMIN=CCadmin
50+
RANLIB=ranlib
51+
52+
53+
# build
54+
build: .build-post
55+
56+
.build-pre:
57+
# Add your pre 'build' code here...
58+
59+
.build-post: .build-impl
60+
# Add your post 'build' code here...
61+
62+
63+
# clean
64+
clean: .clean-post
65+
66+
.clean-pre:
67+
# Add your pre 'clean' code here...
68+
# WARNING: the IDE does not call this target since it takes a long time to
69+
# simply run make. Instead, the IDE removes the configuration directories
70+
# under build and dist directly without calling make.
71+
# This target is left here so people can do a clean when running a clean
72+
# outside the IDE.
73+
74+
.clean-post: .clean-impl
75+
# Add your post 'clean' code here...
76+
77+
78+
# clobber
79+
clobber: .clobber-post
80+
81+
.clobber-pre:
82+
# Add your pre 'clobber' code here...
83+
84+
.clobber-post: .clobber-impl
85+
# Add your post 'clobber' code here...
86+
87+
88+
# all
89+
all: .all-post
90+
91+
.all-pre:
92+
# Add your pre 'all' code here...
93+
94+
.all-post: .all-impl
95+
# Add your post 'all' code here...
96+
97+
98+
# help
99+
help: .help-post
100+
101+
.help-pre:
102+
# Add your pre 'help' code here...
103+
104+
.help-post: .help-impl
105+
# Add your post 'help' code here...
106+
107+
108+
109+
# include project implementation makefile
110+
include nbproject/Makefile-impl.mk
111+
112+
# include project make variables
113+
include nbproject/Makefile-variables.mk

0 commit comments

Comments
 (0)