6.5 Dashboard Example Code

To demonstrate how to use the Dashboard module, an example with a Night mode switch will be used. An ATmega256RFR2 Xplained Pro with an I/O1 Xplained Pro extension is suitable as target hardware. The I/O1 extension board features a light sensor that can be used to detect whether it is night or day. This information can be used, for example, to switch a lamp on when the surroundings turn dark.

The example requires the following equipment and software:
  • Host computer with Atmel Studio 7 or later installed (Data Visualizer is included)
  • ATmega256RFR2 Xplained Pro kit
  • I/O1 Xplained Pro extension
To run the example, the following hardware setup is required:
  • I/O1 Xplained Pro extension connected to ATmega256RFR2 Xplained Pro EXT1 connector
  • USB cable connected from host computer to ATmega256RFR2 Xplained Pro
A picture of the setup is shown below.

The ATmega256RFR2 target on the Xplained Pro must be programmed with code that implements the Night mode switch.

Todo:
  • Make a new project in Atmel Studio (FileNewProjectGCC C Executable Project)
  • Replace the content of the automatically generated main.c file with the code below
#include <avr/io.h>
#include <avr/interrupt.h>

union u_double{
	double dbl;
	char data[8];
};

uint16_t adc_value = 0;
uint8_t nightmode_threshold;
uint8_t nightmode_active = 0;
union u_double cdc_received_data;
uint8_t cdc_read_index=0;

ISR (USART1_RX_vect){
	// A byte is received on the CDC UART, MSB first
	cdc_received_data.data[cdc_read_index] = UDR1 & 0xFF;
	if (7 == cdc_read_index){
		// A complete double value is received
		nightmode_threshold = (uint8_t) cdc_received_data.dbl;
		cdc_read_index = 0;
	}
	else {
		cdc_read_index++;
	}
}
void adc_init(void){
	// Internal 1.5V reference, ADC0 as single ended input
	ADMUX = (1 << REFS1);
	// Enable the ADC,
	ADCSRA |= (1<<ADEN);
	// Check that the reference is OK
	while (0x00 == (ADCSRB & (1 << REFOK)));
}
uint16_t adc_sample(void){
	// Trigger an ADC conversion
	ADCSRA |= (1<<ADSC);
	// Wait for conversion to finish
	while (0x00 == (ADCSRA & (1 << ADIF)));
	// Clear the interrupt flag
	ADCSRA |= (1 << ADIF);
	return (ADC);
}
void spi_init(void){
	// Slave select (PB0), MOSI (PB2) and SCK (PB1) as output
	DDRB |= (1<<PINB0) | (1<<PINB2) | (1<<PINB1);
	//Slave select high (inactive)
	PORTB |= (1<<PINB0);
	// Master mode, enable SPI module. Clock polarity and phase is kept at default (Rising edge is leading edge and sample on leading edge)
	SPCR = (1<<SPE) | (1<<MSTR);
}
void spi_send(uint8_t data){
	// Slave select low
	PORTB &= ~(1<<PINB0);
	// Write data to shift register
	SPDR = data;
	// Wait for the transmission to complete
	while (0x00 == (SPSR & (1<<SPIF)));
	// Slave select high
	PORTB |= (1<<PINB0);
}
void cdc_init(void){
	// Baud rate 9600 based on 8 MHz CPU clock
	UBRR1 = 51;
	// Enable the transmitter and receiver, 8 bit character size, receive interrupts enabled
	UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);
}
void cdc_send(const char data){
	// Wait for transmitter to be ready for more data
	while (0x00 == (UCSR1A & (1<<UDRE1)));
	// Send the data
	UDR1 = data;
}
int main(void){
	adc_init();
	spi_init();
	cdc_init();
	// Interrupts on
	sei();
	while (1){
		adc_value = adc_sample();
		// Send the ADC value over SPI to the host
		// Only the 8 lower bits contain useful data
		spi_send(adc_value & 0xFF);
		//  higher adc value == less light
		if (adc_value > nightmode_threshold){
			if (0x00 == nightmode_active){
				// Changing from nightmode inactive to active
				nightmode_active = 0x01;				
			}
		} else {
			if (0x01 == nightmode_active){
				// Changing from nightmode active to inactive
				nightmode_active = 0x00;				
			}
		}
		cdc_send(nightmode_active);
	}
}

The code samples the ADC continuously and sends the data over the SPI interface to the EDBG (Embedded Debugger) on the ATmega256RFR2 Xplained Pro board. The EDBG then sends the SPI data over DGI to the host computer. The ATmega256RFR2 ADC is 10-bit but only the lower 8 bits contain useful data in this example.

In addition, the code sets up the CDC USART and sends the state of the Night mode switch as a single byte. The received data on the CDC USART is parsed as a double value and is used as threshold for the Night mode switch.

Todo: Build the project/solution (F7).
Todo:
  • Open the project properties (right click the project in the Solution Explorer and select Properties)
  • On the Tool tab, select the appropriate tool and interface
Todo: Program the application into the target and start the debugging by selecting Continue (F5).
Todo: Open the Data Visualizer as an extension inside Atmel Studio by selecting it in the Tools menu.