Atmel Software Framework

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

Defines

#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.
#define FLASHCOUNT   100
#define IDLE   0x01
 Idle SMCR setting.
#define MAX_CMD_BUFFER_LEN   32
#define NUM_COMMANDS   12
#define NUMBER_OF_PORTS   1
 Number of ports using touch.
#define POWER_DOWN   0x05
 Power-Down SMCR setting.
#define POWER_SAVE   0x07
 Power-Save SMCR setting.
#define QT_KEY_DETECT()   (qt_measure_data.qt_touch_status.sensor_states[0] & 0x01)
 Macro used for touch key detection.
#define STANDBY   0x0D
 Standby SMCR setting.

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

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.
static volatile uint16_t current_time_ms_touch = 0
 Current time, set by timer1 overflow ISR.
static volatile bool light_sensor_demo_mode = false
 Global variable used to identify if demo mode is enabled.
uint16_t qt_measurement_period_msec = 25
 Touch acquisition timer period in msec.
static volatile bool time_to_measure_touch = false
 Flag set by timer1 overflow ISR when it's time to measure touch.
FILE usart1_str = FDEV_SETUP_STREAM((int(*)(char, FILE *))usart1_putchar, NULL, _FDEV_SETUP_WRITE)
 Make sure printf knows where to print.

#define ADC_NUM_OVERSAMPLING   16

Definition at line 126 of file mega_1284p_xplained_example.c.

#define ASCII_BACKSPACE   8

Definition at line 128 of file mega_1284p_xplained_example.c.

Referenced by execute_terminal_mode().

#define ASCII_CR   13

Definition at line 129 of file mega_1284p_xplained_example.c.

Referenced by execute_terminal_mode().

#define ASCII_SPACE   32

Definition at line 130 of file mega_1284p_xplained_example.c.

Referenced by execute_terminal_mode().

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

Definition at line 133 of file mega_1284p_xplained_example.c.

Referenced by execute_terminal_mode().

#define EXTENDED_STANDBY   0x0F

Extended standby SMCR setting.

Definition at line 141 of file mega_1284p_xplained_example.c.

Referenced by ext_standby_cmd().

#define FLASHCOUNT   100

Definition at line 135 of file mega_1284p_xplained_example.c.

Referenced by flash_leds_cmd().

#define IDLE   0x01

Idle SMCR setting.

Definition at line 137 of file mega_1284p_xplained_example.c.

Referenced by idle_mode_cmd().

#define MAX_CMD_BUFFER_LEN   32

Definition at line 131 of file mega_1284p_xplained_example.c.

Referenced by execute_terminal_mode().

#define NUM_COMMANDS   12

Definition at line 143 of file mega_1284p_xplained_example.c.

#define NUMBER_OF_PORTS   1

Number of ports using touch.

Definition at line 187 of file mega_1284p_xplained_example.c.

#define POWER_DOWN   0x05

Power-Down SMCR setting.

Definition at line 138 of file mega_1284p_xplained_example.c.

Referenced by execute_demo_mode(), and power_down_cmd().

#define POWER_SAVE   0x07

Power-Save SMCR setting.

Definition at line 139 of file mega_1284p_xplained_example.c.

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.

Definition at line 183 of file mega_1284p_xplained_example.c.

Referenced by execute_demo_mode().

#define STANDBY   0x0D

Standby SMCR setting.

Definition at line 140 of file mega_1284p_xplained_example.c.

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.

Definition at line 206 of file mega_1284p_xplained_example.c.

                 {
    FILTER_OUTPUT = 0x05, 
    LIGHT_SENSOR = 0x06,  
    NTC = 0x07           
};


static void crystal_start_cmd ( void   )  [static]

Function to handle 'start 32kHz crystal' command.

Definition at line 492 of file mega_1284p_xplained_example.c.

References start32crystal().

{
    start32crystal();
    // Enable timer2 overflow interrupt
    TIMSK2 |= (1<<TOIE2);
    printf("32.768kHz crystal started.\r\n");
}

static void crystal_stop_cmd ( void   )  [static]

Function to handle 'stop 32kHz crystal' command.

Definition at line 503 of file mega_1284p_xplained_example.c.

References stop32crystal().

{
    stop32crystal();
    // Disable timer2 overflow interrupt
    TIMSK2 &= ~(1<<TOIE2);
    // make sure LEDs are off before we leave
    PORTB |= (1 << PORTB3) | (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0);
    printf("32.768kHz crystal stopped.\r\n");
}

static void enter_sleep ( uint8_t  sleepmode  )  [static]

Function to enter sleep.

Parameters:
sleepmode Sets sleep mode to enter

Definition at line 404 of file mega_1284p_xplained_example.c.

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().

{
    uint8_t temp_timsk1;
    uint8_t temp_timsk2;
    uint8_t temp_portb;
    uint8_t temp_ddrb;
    uint8_t temp_prr0;

    // Push temp values and disable interrupts
    temp_timsk1 = TIMSK1;
    temp_timsk2 = TIMSK2;
    temp_portb = PORTB;
    temp_ddrb = DDRB;
    TIMSK1 = 0x00;
    TIMSK2 = 0x00;
    temp_prr0 = PRR0;

    // Enable PORTB0 as input with pull-up enabled
    PORTB |= (1 << PORTB0);
    DDRB &= ~(1 << DDB0);

    // Clear Pin Change Interrupt flag 1
    PCIFR = (1 << PCIF1);

    // Enable pinchange interrupt on SW0 (PCINT8/PB0)
    PCICR = (1 << PCIE1);
    PCMSK1 = (1 << PCINT10) | (1 << PCINT9) |(1 << PCINT8);

    // Configure sleep mode
    SMCR = sleepmode;

    // Enable power saving features before going to sleep
    power_reduction_enable();

    // Go to sleep
    sleep_enter();

    // Clear SMCR to avoid entering sleep unintended
    SMCR = 0x00;

    // Pull temp values
    PRR0 = temp_prr0;

    // Make sure interrupts flags are reset
    TIFR1 =(1 << OCF1B) | (1 << OCF1A) | (1 << TOV1);
    TIFR2 = (1 << TOV2);

    // Pull temp values
    TIMSK1 = temp_timsk1;
    TIMSK2 = temp_timsk2;
    PORTB = temp_portb;
    DDRB = temp_ddrb;
}

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.

Definition at line 615 of file mega_1284p_xplained_example.c.

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(), read_adc(), start32crystal(), stop32crystal(), stop_timer1_lightdemo(), time_to_measure_touch, timer1_lightdemo_init(), and touch_init().

Referenced by main().

{
    // Status flags to indicate the re-burst for touch library
    uint16_t status_flag = 0;
    uint16_t burst_flag = 0;

    io_init_demo_mode();
    touch_init();
    sei();

    while (1) {
        if (time_to_measure_touch) {

            time_to_measure_touch = false;
            MCUCR |= (1 << PUD);

            do {
                // One time measure touch sensors
                status_flag = qt_measure_sensors(current_time_ms_touch);
                burst_flag = status_flag & QTLIB_BURST_AGAIN;
                if (QT_KEY_DETECT()) {
                    PORTB &= ~(1 << PORTB3);
                }
                else {
                    PORTB |= (1 << PORTB3);
                }

            } while (burst_flag);
        MCUCR &= ~(1 << PUD);
        }

        if (!(PINB & (1 << PINB0))) {
            // Turn LED0 on to indicate active mode
            DDRB |= (1 << DDB3) | (1 << DDB0);
            DDRB &= ~((1 << DDB2) | (1 << DDB1));
            PORTB &= ~(1 << PORTB0);
            PORTB |= (1 << PORTB2) | (1 << PORTB1);
        }

        if (!(PINB & (1 << PINB1))){
            // Turn LED2 on to indicate power-save mode
            DDRB |= (1 << DDB3) | (1 << DDB1);
            DDRB &= ~((1 << DDB2) | (1 << DDB0));
            PORTB &= ~(1 << PORTB1);
            PORTB |= (1 << PORTB2) | (1 << PORTB0);

            /*
             * Start asynchronous clocking of Timer/Counter 2 using
             * 32.768kHz crystal as clock source.
             */
            start32crystal();
            enter_sleep(POWER_SAVE);
            stop32crystal();
        }

        if (!(PINB & (1 << PINB2))) {
            // Turn LED3 on to indicate power-down mode
            DDRB |= (1 << DDB3) | (1 << DDB2);
            DDRB &= ~(1 << DDB1) & ~(1 << DDB0);
            PORTB &= ~(1 << PORTB2);
            PORTB |= (1 << PORTB1) | (1 << PORTB0);
            enter_sleep(POWER_DOWN);
        }

        if (!(PINB & (1 << PINB6))) {
            // Disable pinchange interrupts (SW0:2)
            PCICR = (0 << PCIE1);

            adc_init();
            timer1_lightdemo_init();

            // Enable output for LED control of LED0:3
            DDRB |= (1 << DDB3) | (1 << DDB2) | (1 << DDB1) | (1 << DDB0);

            // Run light sensor demo as long as PORTB6 is pulled low
            while (!(PINB & (1 << PINB6))) {

                /*
                 * Update compare B value to adjust PWM according
                 * to light sensor value
                 */
                OCR1B = read_adc(LIGHT_SENSOR);
            }

            stop_timer1_lightdemo();

            // Set back to input for demo mode
            DDRB &= ~(1 << DDB2) & ~(1 << DDB1) & ~(1 << DDB0);

            touch_init();

            // Clear Pin Change Interrupt flag 1
            PCIFR = (1 << PCIF1);
            // Enable pinchange interrupts (SW0:2)
            PCICR = (1 << PCIE1);
        }
    }
}

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.

Definition at line 721 of file mega_1284p_xplained_example.c.

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().

{
    // Variable used for USART communication
    uint8_t data = 0;
    // Command buffer pointer used for USART communication
    uint8_t cmd_buf_ptr = 0;
    // Command buffer used for USART communication
    char cmd_buffer[MAX_CMD_BUFFER_LEN];

    // misc initialization
    io_init_terminal_mode();
    usart1_init();
    adc_init();
    // Enable interrupts
    sei();

    flash_leds(20);
    print_help();
    printf(CMD_PROMPT);

    while (1) {

        // Read and handle incoming data on usart1
        data = usart1_getchar();
        switch (data)   {

        case ASCII_CR:
            if (cmd_buf_ptr > 0) {
                usart1_putchar('\n');
                cmd_buffer[cmd_buf_ptr] = '\0';
                process_command(cmd_buffer);
                cmd_buf_ptr = 0;
            }
            printf(CMD_PROMPT);
            break;

        case ASCII_BACKSPACE:
            if (cmd_buf_ptr == 0) {
                break;
            }
            printf("\b \b");
            cmd_buf_ptr--;
            break;

        default:
            if (cmd_buf_ptr >= MAX_CMD_BUFFER_LEN || data < ASCII_SPACE ) {
                // Break if command string is too long/ignore non-printable characters
                break;
            }
            cmd_buffer[cmd_buf_ptr++] = data;
            usart1_putchar(data);
            break;
        }
    }
}

static void ext_standby_cmd ( void   )  [static]

Function to handle 'extended standby' command.

Definition at line 560 of file mega_1284p_xplained_example.c.

References enter_sleep(), and EXTENDED_STANDBY.

{
    printf("Entering extended standby mode.\r\n");
    printf("Push SW0 to wake-up.\r\n");
    while (!(UCSR1A & (1 << TXC1)));
    enter_sleep(EXTENDED_STANDBY);
    printf("Woke up from extended standby mode.\r\n");
}

static void flash_leds ( uint8_t  flashcount  )  [static]

Function to flash leds flashcount amount of times.

Parameters:
flashcount Number of times to flash LEDs

Definition at line 293 of file mega_1284p_xplained_example.c.

References delay_us.

Referenced by execute_terminal_mode(), and flash_leds_cmd().

{
    uint8_t i;

    for (i = 0; i < flashcount; i++) {
        // toggle LEDs
        PINB |= (1 << PINB3) | (1 << PINB2) | (1 << PINB1) | (1 << PINB0);
        // wait a while
        delay_us(20000);
    }
    // make sure LEDs are off before we leave
    PORTB |= (1 << PORTB3) | (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0);
}

static void flash_leds_cmd ( void   )  [static]

Function to handle 'flash leds' command.

Definition at line 461 of file mega_1284p_xplained_example.c.

References flash_leds(), and FLASHCOUNT.

static void idle_mode_cmd ( void   )  [static]

Function to handle 'idle' command.

Definition at line 516 of file mega_1284p_xplained_example.c.

References enter_sleep(), and IDLE.

{
    printf("Entering idle mode. Push SW0 to wake-up.\r\n");
    while (!(UCSR1A & (1 << TXC1)));
    enter_sleep(IDLE);
    printf("Woke up from idle mode.\r\n");
}

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.

Definition at line 282 of file mega_1284p_xplained_example.c.

{
    // Toggle LEDs
    PINB |= (1 << PINB3) | (1 << PINB2) | (1 << PINB1) | (1 << PINB0);
}

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.

Definition at line 268 of file mega_1284p_xplained_example.c.

References current_time_ms_touch, qt_measurement_period_msec, and time_to_measure_touch.

{
    // Set flag, it's time to measure touch
    time_to_measure_touch = true;

    // Update the current time
    current_time_ms_touch += qt_measurement_period_msec;
}

ISR ( PCINT1_vect   ) 

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

Definition at line 215 of file mega_1284p_xplained_example.c.

{
    return;
}

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.

Definition at line 230 of file mega_1284p_xplained_example.c.

References light_sensor_demo_mode.

{
    // Turn off LEDs
    PORTB |= (1 << PORTB3) | (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0);
    // PORTB0 set to input to enable sensing on SW0
    DDRB &= ~(1 << DDB0);

    asm("nop");
    // Stop demo mode if SW0 is pushed
    if (!(PINB & (1 << PINB0))) {
        light_sensor_demo_mode = false;
    }
}

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.

Definition at line 255 of file mega_1284p_xplained_example.c.

{
    // Turn on LEDs (make sure PORTB0 is set as output)
    DDRB |= (1 << DDB0);
    PORTB &= ~((1 << PORTB3) | (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0));
}

static void light_sensor_demo ( void   )  [static]

Function to handle 'light sensor demo' command.

Definition at line 469 of file mega_1284p_xplained_example.c.

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

{
    timer1_lightdemo_init();
    adc_init();
    light_sensor_demo_mode = true;
    printf("Light sensor demo mode enabled.\r\n");
    printf("Push SW0 to exit light sensor demo.\r\n");

    /*
    * Run light sensor demo as long as light_sensor_demo_mode is true.
    * The light sensor demo mode is disabled in the timer1 overflow ISR.
    */
    while(light_sensor_demo_mode) {
        // Update compare B value to adjust PWM according to light sensor value
        OCR1B = read_adc(LIGHT_SENSOR);
    }
    stop_timer1_lightdemo();
    printf("Light sensor demo mode stopped.\r\n");
}

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.

Definition at line 788 of file mega_1284p_xplained_example.c.

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

{
#if defined( __GNUC__ )
    /* Setup stdout to point to the correct usart (USART1). This is needed to
     * use the fdev_setup_stream() macro in GCC.
     */
    stdout = &usart1_str;
#endif

    // Make sure to use lowest possible power consumption
    power_reduction_enable();

    // Enter terminal mode if SW1 is pressed
    if (!(PINB & (1 << PORTB1))) {
        execute_terminal_mode();
    }   else {
        execute_demo_mode();
    }
}

static void power_down_cmd ( void   )  [static]

Function to handle 'power-down' command.

Definition at line 538 of file mega_1284p_xplained_example.c.

References enter_sleep(), and POWER_DOWN.

{
    printf("Entering power-down mode. Push SW0 to wake-up.\r\n");
    while (!(UCSR1A & (1 << TXC1)));
    enter_sleep(POWER_DOWN);
    printf("Woke up from power-down mode.\r\n");
}

static void power_save_cmd ( void   )  [static]

Function to handle 'power-save' command.

Definition at line 527 of file mega_1284p_xplained_example.c.

References enter_sleep(), and POWER_SAVE.

{
    printf("Entering power-save mode. Push SW0 to wake-up.\r\n");
    while (!(UCSR1A & (1 << TXC1)));
    enter_sleep(POWER_SAVE);
    printf("Woke up from power-save mode.\r\n");
}

static void print_help ( void   )  [static]

Function to print supported commands.

Definition at line 572 of file mega_1284p_xplained_example.c.

References cmd, commands, and help.

Referenced by execute_terminal_mode().

{
    uint8_t cnt;

    printf("\r\n------------------------------------------------------------------------------\r\n");
    printf("#  COMMAND:             DESCRIPTION:\r\n");
    printf("------------------------------------------------------------------------------\r\n");
    for (cnt = 0; cnt < NUM_COMMANDS; cnt++) {
        printf("%-2d %-20s %s\r\n", cnt+1, commands[cnt].cmd, commands[cnt].help);
    }
    printf("------------------------------------------------------------------------------\r\n");
    printf("To perform a command, enter command number or command string\r\n");
}

static void process_command ( const char *  command  )  [static]

Function used to find and execute commands in terminal mode.

Definition at line 591 of file mega_1284p_xplained_example.c.

References cmd, and commands.

Referenced by execute_terminal_mode().

{
    uint8_t cnt;
    uint8_t num_cmd = atoi(command);

    for (cnt = 0; cnt < NUM_COMMANDS; cnt++) {
        if ((strcmp(command, commands[cnt].cmd) == 0) || (num_cmd == cnt+1)) {
            commands[cnt].func();
            return;
        }
    }
    printf("Invalid command. Type 'help' for a list of available commands.\r\n");
}

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:
channel ADC channel number
Returns:
adc_data Result from ADC reading of selected ADC channel

Definition at line 317 of file mega_1284p_xplained_example.c.

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

{
    uint16_t adc_data = 0;

    // Clear old source and setup new source to sample from
    ADMUX &= 0xE0;
    ADMUX |= source;

    for (int i = 0 ; i < ADC_NUM_OVERSAMPLING ; i++) {
        //Start single conversion
        ADCSRA |= (1<<ADSC);

        //Wait for conversion complete
        while(!(ADCSRA & (1<<ADIF)));

        //Read ADC conversion result
        adc_data += ADC;
    }

    adc_data = adc_data/ADC_NUM_OVERSAMPLING;

    return adc_data;
}

static void read_light_sensor ( void   )  [static]

Function to read Light sensor ADC value.

Definition at line 359 of file mega_1284p_xplained_example.c.

References LIGHT_SENSOR, and read_adc().

{
    uint16_t light_sensor_data;
    // Read LIGHT SENSOR
    light_sensor_data = read_adc(LIGHT_SENSOR);

    // Send LIGHT_SENSOR_data to PC
    printf("LIGHT SENSOR ADC value: %4d\r\n", light_sensor_data);
}

static void read_ntc ( void   )  [static]

Function to read NTC ADC value.

Definition at line 345 of file mega_1284p_xplained_example.c.

References NTC, and read_adc().

{
    uint16_t ntc_data;

    // Read NTC sensor
    ntc_data = read_adc(NTC);

    // Send NTC_data to PC
    printf("NTC ADC value: %4d\r\n", ntc_data);
}

static void standby_cmd ( void   )  [static]

Function to handle 'standby' command.

Definition at line 549 of file mega_1284p_xplained_example.c.

References enter_sleep(), and STANDBY.

{
    printf("Entering standby mode. Push SW0 to wake-up.\r\n");
    while (!(UCSR1A & (1 << TXC1)));
    enter_sleep(STANDBY);
    printf("Woke up from standby mode.\r\n");
}

static void start32crystal ( void   )  [static]

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

Definition at line 372 of file mega_1284p_xplained_example.c.

Referenced by crystal_start_cmd(), and execute_demo_mode().

{
    // Make sure clock to timer2 is enabled
    PRR0 &= ~(1 << PRTIM2);

    // Start Timer 2, prescale 8
    TCCR2B = (0 << WGM22) | (0 << CS22) | (1 << CS21) | (0 << CS20);

    // Enable clocking of timer2 using 32.768kHz crystal
    ASSR = (1 << AS2);
}

static void stop32crystal ( void   )  [static]

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

Definition at line 387 of file mega_1284p_xplained_example.c.

Referenced by crystal_stop_cmd(), and execute_demo_mode().

{
    // Disable clocking of timer2 using 32.768kHz crystal
    ASSR = (0 << AS2);

    // Stop Timer 2
    TCCR2B = (0 << WGM22) | (0 << CS22) | (0 << CS21) | (0 << CS20);

    // Turn off clock to timer2 as it's not in use anymore
    PRR0 |= (1 << PRTIM2);
}


char { ... } cmd[20] [inherited]

Definition at line 161 of file mega_1284p_xplained_example.c.

struct { ... } commands[NUM_COMMANDS]

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.

Definition at line 196 of file mega_1284p_xplained_example.c.

Referenced by execute_demo_mode(), and ISR().

void(* { ... } func)(void) [inherited]

Definition at line 163 of file mega_1284p_xplained_example.c.

char { ... } help[70] [inherited]

Definition at line 162 of file mega_1284p_xplained_example.c.

volatile bool light_sensor_demo_mode = false [static]

Global variable used to identify if demo mode is enabled.

Definition at line 180 of file mega_1284p_xplained_example.c.

Referenced by ISR(), and light_sensor_demo().

Touch acquisition timer period in msec.

Definition at line 190 of file mega_1284p_xplained_example.c.

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.

Definition at line 193 of file mega_1284p_xplained_example.c.

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.

Definition at line 202 of file mega_1284p_xplained_example.c.

Referenced by main().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines