3.1.13 Using Code Instrumentation

Finally, in this section we will look at some other tricks which the Data Visualizer is capable of.

Aside from taking current measurements, the Power Debugger has a full Data Gateway Interface (DGI) port for streaming data from the target to the host computer. We will hook this up to the USART for simple console output.

Some hardware wiring needs to be done to link the USART. We use Synchronous mode for convenience in baud rate setting.
  • PD1 (TXD) of the Xplained Mini is connected to USART ← of the DGI header
  • PD4 (XCK) of the Xplained Mini is connected to USART CLK of the DGI header
The updated wiring diagram is shown here:

The example code makes use of the pin-change interrupt connected to the button on the Xplained Mini to toggle a LED and update a ticker. On each button-press the LED toggles and the ticker count is sent using the DGI USART along with a LED status message.

The code below is included in project low_power_105.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

volatile uint8_t led_on;
volatile uint8_t send_message;
volatile uint8_t ticker = 0;

const char* message_on = "LED ON ";
const char* message_off = "LED OFF ";

ISR (PCINT0_vect)
{
    // Each botton press generates two pin-change interrupt (press, release)
    // Ignore half of these
    if (PINB & (1 << 7))
        return;
    // Update LED
    if (led_on)
        PORTB &= ~(1 << 5);     
    else
        PORTB |= (1 << 5);
    // Invert led_on
    led_on = ~led_on;
    // Flag a message send
    send_message = 1;
    // Increment ticker
    ticker++;
    // Reset ticker
    if (ticker >= 10)
        ticker = 0;
}

void usart_send (const char data)
{
    // Send a character to the USART
    UDR0 = data;
    // Wait for the character to be sent
    while (!(UCSR0A & (1 << TXC0)))
        ;
    // Clear the flag
    UCSR0A = (1 << TXC0);
}

int main(void)
{
    // PORTB5 to output
    DDRB = (1 << 5);
    
    // LED OFF
    PORTB = 0;
    led_on = 0;
    
    // Enable Pin-change interrupt
    PCICR = (1 << PCIE0);
    PCMSK0 = (1 << PCINT7);
    
    // USART0
    // Baud trivial in synchronous mode    
    UBRR0 = 0x0FFF;
    // Enable XCK for master clock outout
    DDRD |= (1 << 4);
    // Enable USART transmitter
    UCSR0B |= (1 << TXEN0);
    // Synchronous mode
    UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00) | (1 << UMSEL00);
    
    // Interrupts on
    sei();
    
    while(1)  {
        // Sleep
        set_sleep_mode(SLEEP_MODE_IDLE);  
        sleep_mode();
        // Woken up
        if(send_message) {
            // Send a message
            const char* pmessage;
            if (led_on)
                pmessage = message_on;
            else
                pmessage = message_off;
            while (*pmessage)
                usart_send(*pmessage++);
            // Send the ticker value
            usart_send(ticker + '0');
            usart_send('\n');
            // Sent
            send_message = 0;
        }                   
    }
}

Todo:
  • Build the project/solution (F7)
  • Program the application into the target device using Start Without Debugging (Ctrl+Alt+F5)
  • Switch to Data Visualizer to see the results