Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
MEGA-1284P Xplained example application

Description of the example

This is the example application used to demonstrate the MEGA-1284P Xplained kit. Basically it has two main modes of operation: demo mode or terminal mode.

Example application flowchart

application_flowchart.JPG

Demo mode

This mode lets the user select in which mode the ATmega1284P should operate: Active, power-save (with the 32kHz crystal connected to the TOSC pins enabled) or power-down. The user can easily measure the power consumption in the different modes by connecting an ampere meter to the two-pins power header. Please refer to the hardware user guide for more details.

The user can also try out capacitive touch sensing by pushing the QTB0 touch button on the board. LED1 will turn on when QTB0 is touched and switch off whenever QTB0 is not touched.

By shorting MISO (pin 7) and GND (pin 9) of the J1 header the ATmega1284P enters light sensor demo mode. The light sensor is then used to adjust the light intensity of the four leds (LED0:3). Normal demo mode will be resumed as soon as this short is removed.

Demo mode flowchart

demo_mode_flowchart.JPG

Terminal mode

This mode uses USART1 of the ATmega1284P to control the application. Start hyperterminal (or the terminal program of choice) and connect to the COM port where the XPLAINED Virtual Com Port is connected. Select baud rate: 57k6, data bits: 8, parity: none, stop bits: 1 and flow control: none.

To start this mode push and hold SW1 while pressing the reset button on the MEGA-1284P Xplained board. Supported commands and a description will then be printed.

Terminal mode flowchart

terminal_mode_flowchart.JPG

Macros

#define ADC_NUM_OVERSAMPLING   16
 
#define ASCII_BACKSPACE   8
 
#define ASCII_CR   13
 
#define ASCII_SPACE   32
 
#define CMD_PROMPT   "\r\nMEGA-1284P Xplained>"
 
#define EXTENDED_STANDBY   0x0F
 Extended standby SMCR setting. More...
 
#define FLASHCOUNT   100
 
#define IDLE   0x01
 Idle SMCR setting. More...
 
#define MAX_CMD_BUFFER_LEN   32
 
#define NUM_COMMANDS   12
 
#define NUMBER_OF_PORTS   1
 Number of ports using touch. More...
 
#define POWER_DOWN   0x05
 Power-Down SMCR setting. More...
 
#define POWER_SAVE   0x07
 Power-Save SMCR setting. More...
 
#define QT_KEY_DETECT()   (qt_measure_data.qt_touch_status.sensor_states[0] & 0x01)
 Macro used for touch key detection. More...
 
#define STANDBY   0x0D
 Standby SMCR setting. More...
 

Enumerations

enum  adc_sources {
  FILTER_OUTPUT = 0x05,
  LIGHT_SENSOR = 0x06,
  NTC = 0x07
}
 ADC sources enum. More...
 

Functions

static void crystal_start_cmd (void)
 Function to handle 'start 32kHz crystal' command. More...
 
static void crystal_stop_cmd (void)
 Function to handle 'stop 32kHz crystal' command. More...
 
static void enter_sleep (uint8_t sleepmode)
 Function to enter sleep. More...
 
static void execute_demo_mode (void)
 brief Function to execute Demo mode. More...
 
static void execute_terminal_mode (void)
 brief Function to execute terminal mode. More...
 
static void ext_standby_cmd (void)
 Function to handle 'extended standby' command. More...
 
static void flash_leds (uint8_t flashcount)
 Function to flash leds flashcount amount of times. More...
 
static void flash_leds_cmd (void)
 Function to handle 'flash leds' command. More...
 
static void idle_mode_cmd (void)
 Function to handle 'idle' command. More...
 
 ISR (PCINT1_vect)
 PCINT8,9,10 ISR used for wake-up from sleep only. More...
 
 ISR (TIMER1_OVF_vect)
 TIMER1 overflow ISR used to dim LEDs in light sensor demo mode. More...
 
 ISR (TIMER1_COMPB_vect)
 TIMER1 compare B ISR used to dim LEDS in light sensor demo mode. More...
 
 ISR (TIMER1_COMPA_vect)
 TIMER1 compare A ISR used to time touch measurement. More...
 
 ISR (TIMER2_OVF_vect)
 TIMER2 overflow ISR used to indicate 32kHz crystal running. More...
 
static void light_sensor_demo (void)
 Function to handle 'light sensor demo' command. More...
 
int main (void)
 brief Example code that demonstrates the some of the basic features of the MEGA-1284P Xplained kit. More...
 
static void power_down_cmd (void)
 Function to handle 'power-down' command. More...
 
static void power_save_cmd (void)
 Function to handle 'power-save' command. More...
 
static void print_help ()
 Function to print supported commands. More...
 
static void process_command (const char *command)
 Function used to find and execute commands in terminal mode. More...
 
static uint16_t read_adc (enum adc_sources source)
 Function to start ADC conversion. More...
 
static void read_light_sensor (void)
 Function to read Light sensor ADC value. More...
 
static void read_ntc (void)
 Function to read NTC ADC value. More...
 
static void standby_cmd (void)
 Function to handle 'standby' command. More...
 
static void start32crystal (void)
 Function to start 32.768kHz crystal connected to TOSC1 and TOSC2. More...
 
static void stop32crystal (void)
 Function to stop 32.768kHz crystal connected to TOSC1 and TOSC2. More...
 

Variables

struct {
   char   cmd [20]
 
   void(*   func )(void)
 
   char   help [70]
 
commands [NUM_COMMANDS]
 Struct to hold all available commands, their help text and function to call. More...
 
static volatile uint16_t current_time_ms_touch = 0
 Current time, set by timer1 overflow ISR. More...
 
static volatile bool light_sensor_demo_mode = false
 Global variable used to identify if demo mode is enabled. More...
 
uint16_t qt_measurement_period_msec = 25
 Touch acquisition timer period in msec. More...
 
static volatile bool time_to_measure_touch = false
 Flag set by timer1 overflow ISR when it's time to measure touch. More...
 
FILE usart1_str = FDEV_SETUP_STREAM((int(*)(char, FILE *))usart1_putchar, NULL, _FDEV_SETUP_WRITE)
 Make sure printf knows where to print. More...
 

#define ADC_NUM_OVERSAMPLING   16

Referenced by read_adc().

#define ASCII_BACKSPACE   8

Referenced by execute_terminal_mode().

#define ASCII_CR   13

Referenced by execute_terminal_mode().

#define ASCII_SPACE   32

Referenced by execute_terminal_mode().

#define CMD_PROMPT   "\r\nMEGA-1284P Xplained>"

Referenced by execute_terminal_mode().

#define EXTENDED_STANDBY   0x0F

Extended standby SMCR setting.

Referenced by ext_standby_cmd().

#define FLASHCOUNT   100

Referenced by flash_leds_cmd().

#define IDLE   0x01

Idle SMCR setting.

Referenced by idle_mode_cmd().

#define MAX_CMD_BUFFER_LEN   32

Referenced by execute_terminal_mode().

#define NUM_COMMANDS   12

Referenced by print_help(), and process_command().

#define NUMBER_OF_PORTS   1

Number of ports using touch.

#define POWER_DOWN   0x05

Power-Down SMCR setting.

Referenced by execute_demo_mode(), and power_down_cmd().

#define POWER_SAVE   0x07

Power-Save SMCR setting.

Referenced by execute_demo_mode(), and power_save_cmd().

#define QT_KEY_DETECT ( )    (qt_measure_data.qt_touch_status.sensor_states[0] & 0x01)

Macro used for touch key detection.

Referenced by execute_demo_mode().

#define STANDBY   0x0D

Standby SMCR setting.

Referenced by standby_cmd().

ADC sources enum.

Enumerator
FILTER_OUTPUT 

RC filter on ADC Channel 5, PA5.

LIGHT_SENSOR 

Light Sensor on ADC Channel 6, PA6.

NTC 

NTC sensor on ADC Channel 7, PA7.

static void crystal_start_cmd ( void  )
static

Function to handle 'start 32kHz crystal' command.

References start32crystal().

static void crystal_stop_cmd ( void  )
static

Function to handle 'stop 32kHz crystal' command.

References stop32crystal().

static void enter_sleep ( uint8_t  sleepmode)
static

Function to enter sleep.

Parameters
sleepmodeSets sleep mode to enter

References power_reduction_enable(), and sleep_enter.

Referenced by execute_demo_mode(), ext_standby_cmd(), idle_mode_cmd(), power_down_cmd(), power_save_cmd(), and standby_cmd().

static void execute_demo_mode ( void  )
static

brief Function to execute Demo mode.

This mode lets the user select which mode the ATmega1284P should operate: Active, power-save (with the 32kHz crystal connected to the TOSC pins enabled) or power-down. It also includes a light sensor demo mode that uses the light sensor to control the LEDs.

Please refer to the detailed description for more information and flow chart.

References adc_init(), current_time_ms_touch, enter_sleep(), io_init_demo_mode(), LIGHT_SENSOR, POWER_DOWN, POWER_SAVE, QT_KEY_DETECT, qt_measure_sensors(), QTLIB_BURST_AGAIN, read_adc(), start32crystal(), stop32crystal(), stop_timer1_lightdemo(), time_to_measure_touch, timer1_lightdemo_init(), and touch_init().

Referenced by main().

static void execute_terminal_mode ( void  )
static

brief Function to execute terminal mode.

This mode uses USART1 of the ATmega1284P to control the application. Please refef to the detailed description for more information.

References adc_init(), ASCII_BACKSPACE, ASCII_CR, ASCII_SPACE, CMD_PROMPT, flash_leds(), io_init_terminal_mode(), MAX_CMD_BUFFER_LEN, print_help(), process_command(), usart1_getchar(), usart1_init(), and usart1_putchar().

Referenced by main().

static void ext_standby_cmd ( void  )
static

Function to handle 'extended standby' command.

References enter_sleep(), and EXTENDED_STANDBY.

static void flash_leds ( uint8_t  flashcount)
static

Function to flash leds flashcount amount of times.

Parameters
flashcountNumber of times to flash LEDs

References delay_us.

Referenced by execute_terminal_mode(), and flash_leds_cmd().

static void flash_leds_cmd ( void  )
static

Function to handle 'flash leds' command.

References flash_leds(), and FLASHCOUNT.

static void idle_mode_cmd ( void  )
static

Function to handle 'idle' command.

References enter_sleep(), and IDLE.

ISR ( PCINT1_vect  )

PCINT8,9,10 ISR used for wake-up from sleep only.

ISR ( TIMER1_OVF_vect  )

TIMER1 overflow ISR used to dim LEDs in light sensor demo mode.

LEDs are turned off at overflow ISR and turned on in compare match B ISR. The compare value is adjusted according to the light sensor readings. Brighter light will result in lower light sensor reading (ADC code) which results in shorter time to compare match B interrupt. This means that LEDs will be on longer than off at bright light and vice versa at dark light.

Note
Wait one cycle to be sure PINB0 value has stabilized.

References light_sensor_demo_mode.

ISR ( TIMER1_COMPB_vect  )

TIMER1 compare B ISR used to dim LEDS in light sensor demo mode.

LEDs are turned off at overflow ISR and turned on in compare match B ISR. The compare value is adjusted according to the light sensor readings. Brighter light will result in lower light sensor reading (ADC code) which results in shorter time to compare match B interrupt. This means that LEDs will be on longer than off at bright light and vice versa at dark light.

ISR ( TIMER1_COMPA_vect  )

TIMER1 compare A ISR used to time touch measurement.

The TIMER1 compare A interrupt is used to time the touch acquisition. The period of the timer is set by qt_measurement_period_msec.

References current_time_ms_touch, qt_measurement_period_msec, and time_to_measure_touch.

ISR ( TIMER2_OVF_vect  )

TIMER2 overflow ISR used to indicate 32kHz crystal running.

All LEDs (LED0:3) are toggled to indicate that the 32kHz crystal is running.

static void light_sensor_demo ( void  )
static

Function to handle 'light sensor demo' command.

References adc_init(), LIGHT_SENSOR, light_sensor_demo_mode, read_adc(), stop_timer1_lightdemo(), and timer1_lightdemo_init().

int main ( void  )

brief Example code that demonstrates the some of the basic features of the MEGA-1284P Xplained kit.

This example code shows some of the basic features of the MEGA-1284P Xplained kit. This includes sleep modes, reading the light sensor and the ntc sensor.

Please refer to the detailed description for more information.

References execute_demo_mode(), execute_terminal_mode(), power_reduction_enable(), and usart1_str.

static void power_down_cmd ( void  )
static

Function to handle 'power-down' command.

References enter_sleep(), and POWER_DOWN.

static void power_save_cmd ( void  )
static

Function to handle 'power-save' command.

References enter_sleep(), and POWER_SAVE.

static void print_help ( void  )
static

Function to print supported commands.

References cmd, commands, help, and NUM_COMMANDS.

Referenced by execute_terminal_mode().

static void process_command ( const char *  command)
static

Function used to find and execute commands in terminal mode.

References cmd, commands, and NUM_COMMANDS.

Referenced by execute_terminal_mode().

static uint16_t read_adc ( enum adc_sources  source)
static

Function to start ADC conversion.

This function will do ADC_NUM_OVERSAMPLINGx oversampling. Per default it will perform 16x oversampling.

Parameters
channelADC channel number
Returns
adc_data Result from ADC reading of selected ADC channel

References ADC_NUM_OVERSAMPLING.

Referenced by execute_demo_mode(), light_sensor_demo(), read_light_sensor(), and read_ntc().

static void read_light_sensor ( void  )
static

Function to read Light sensor ADC value.

References LIGHT_SENSOR, and read_adc().

static void read_ntc ( void  )
static

Function to read NTC ADC value.

References NTC, and read_adc().

static void standby_cmd ( void  )
static

Function to handle 'standby' command.

References enter_sleep(), and STANDBY.

static void start32crystal ( void  )
static

Function to start 32.768kHz crystal connected to TOSC1 and TOSC2.

Referenced by crystal_start_cmd(), and execute_demo_mode().

static void stop32crystal ( void  )
static

Function to stop 32.768kHz crystal connected to TOSC1 and TOSC2.

Referenced by crystal_stop_cmd(), and execute_demo_mode().

char { ... } cmd[20]
struct { ... } commands[NUM_COMMANDS]
Initial value:
= {
{ "help", "Print this help", print_help },
{ "flash leds", "Toggles leds connected to PORTB0:3", flash_leds_cmd },
{ "read ntc", "Returns NTC ADC code (ADC7)", read_ntc },
{ "read light sensor", "Returns Light Sensor ADC code (ADC6)", read_light_sensor },
{ "light sensor demo", "LED3:0 are dimmed according to the light sensor value", light_sensor_demo },
{ "start 32khz crystal", "Starts the 32.768kHz crystal", crystal_start_cmd },
{ "stop 32khz crystal", "Stops the 32.768kHz crystal", crystal_stop_cmd },
{ "idle", "MCU in idle mode. Wake up by pressing SW0", idle_mode_cmd },
{ "power-save", "MCU in Power-Save mode. Wake up by pressing SW0", power_save_cmd },
{ "power-down", "MCU in Power-Down mode. Wake up by pressing SW0", power_down_cmd },
{ "standby", "MCU in Standby mode. Wake up by pressing SW0", standby_cmd },
{ "extended standby", "MCU in Extended Standby mode. Wake up by pressing SW0", ext_standby_cmd },
}
static void crystal_start_cmd(void)
Function to handle 'start 32kHz crystal' command.
Definition: mega_1284p_xplained_example.c:485
static void read_light_sensor(void)
Function to read Light sensor ADC value.
Definition: mega_1284p_xplained_example.c:352
static void print_help(void)
Function to print supported commands.
Definition: mega_1284p_xplained_example.c:565
static void ext_standby_cmd(void)
Function to handle 'extended standby' command.
Definition: mega_1284p_xplained_example.c:553
static void power_down_cmd(void)
Function to handle 'power-down' command.
Definition: mega_1284p_xplained_example.c:531
static void read_ntc(void)
Function to read NTC ADC value.
Definition: mega_1284p_xplained_example.c:338
static void crystal_stop_cmd(void)
Function to handle 'stop 32kHz crystal' command.
Definition: mega_1284p_xplained_example.c:496
static void idle_mode_cmd(void)
Function to handle 'idle' command.
Definition: mega_1284p_xplained_example.c:509
static void standby_cmd(void)
Function to handle 'standby' command.
Definition: mega_1284p_xplained_example.c:542
static void power_save_cmd(void)
Function to handle 'power-save' command.
Definition: mega_1284p_xplained_example.c:520
static void light_sensor_demo(void)
Function to handle 'light sensor demo' command.
Definition: mega_1284p_xplained_example.c:462
static void flash_leds_cmd(void)
Function to handle 'flash leds' command.
Definition: mega_1284p_xplained_example.c:454

Struct to hold all available commands, their help text and function to call.

Referenced by print_help(), and process_command().

volatile uint16_t current_time_ms_touch = 0
static

Current time, set by timer1 overflow ISR.

Referenced by execute_demo_mode(), and ISR().

void(* { ... } func)(void)
char { ... } help[70]
volatile bool light_sensor_demo_mode = false
static

Global variable used to identify if demo mode is enabled.

Referenced by ISR(), and light_sensor_demo().

uint16_t qt_measurement_period_msec = 25

Touch acquisition timer period in msec.

Referenced by ISR(), and timer1_init().

volatile bool time_to_measure_touch = false
static

Flag set by timer1 overflow ISR when it's time to measure touch.

Referenced by execute_demo_mode(), and ISR().

FILE usart1_str = FDEV_SETUP_STREAM((int(*)(char, FILE *))usart1_putchar, NULL, _FDEV_SETUP_WRITE)

Make sure printf knows where to print.

The macro fdev_setup_stream() is used to prepare a user-supplied FILE buffer for operation with stdio.

Referenced by main().