# Class notes

EPROM: config flash: code fuses: see helpful fuse calculator

Look at that insane MegaProcessor project

# Individual assignment

# Basic debugging with Arduino

Recent version of the Arduino IDE added a neat too for debugging / visualization: the "Serial Plotter", found in the Tools menu.

This will let you do something like this:

This actually really easy to do. Here's some example code for an ultrasonic distance sensor:

const int trigPin = 11;
const int echoPin = 12;

void setup()
{
  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  float distance = measureDistance();
  Serial.println(distance);  
  delay(100);
}

float measureDistance() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  float echoTime = pulseIn(echoPin, HIGH);
  float distance = echoTime / 58.0f;

  return distance;
}

Ignoring the parts that are specific to this particular sensor, all you really need to do is:

  • add this to your setup(), if it's not there already:
Serial.begin(9600);
  • somewhere in your loop(), output a number to serial, on a single line:
Serial.println(<your value>);
  • Start the Serial Plotter from the Tools menu in the IDE

# Exploring Atmel Studio 7

Requirements:

  • ATMEL-ICE (borrowed from lab during shutdown!)
  • Arduino Uno
  • Atmel Studio 7

Steps:

  • Cut the reset trace shown below on the Arduino (this will permanently remove the ability to program it normally from the Arduino IDE, unless you add a jumper back on!). More information why we're doing that.
  • Set Atmel Studio 7 to DebugWire (as shown in the tutorial above)
  • Verify that the trace is cut with a continuity tester
  • If cut but Atmel Studio 7 fails to start debugging and overs to set the correct fuse to enable DebugWire, let it do so

You should now be able to write some simple code and debug it!

NB: GCC-AVR and AVR debugging are apparently notorious for making debugging optimized code difficult; we'll see how it goes!

Here's some super simple code for a blink program:

#define F_CPU 8000000L

#include <avr/io.h>
#include <util/delay.h>

// NB: the built in LED in the Arduino Uno R3 is on port B5
int main(void)
{
	DDRB = (1 << DDB5);
	
    while (1) 
    {
		 _delay_ms(250);
		PORTB = (0 << PB5);
		 _delay_ms(250);
		 PORTB = (1 << PB5);
    }
}

With the reset trace cut, programming from Atmel Studio succeeded. But more importantly, we can now debug the program on real hardware!

# Notes