Introduction to Smart Systems Week 1 Laboratory task



Download 26.25 Kb.
Date22.07.2017
Size26.25 Kb.
Introduction to Smart Systems

Week 1 Laboratory task (basic familiarisation of STK300, ATmega1281)
This lab task is designed to encourage exploration of the basic I/O capabilities of the microcontroller, as well as practice using the ATMEL Studio development tool.

Part A – Use assembly language
Preparation

Download the Dragon / Debug ASM (binary count) Atmega1281 sample code project from the resources website.

(This is accessible from the Moodle course page, under the link “Richard’s technical resources” in Moodle (and then select the link “Student resources (Embedded Systems)”).
You will need to extract all the files from the .zip archive into a folder on your J: drive.

The files include an ATMEL Studio 6.0 project; clicking on this should cause it to open in Atmel Studio (takes a few seconds).




  • The assembly code should be the same as shown here:

/*******************************************

Project Binary Count On LEDs (version with Dragon / Debug guidance comments added)

Target ATmega1281 on STK300

Program Dragon_Debug_BinaryCount_1281_asm.asm

Author Richard Anthony

Date 12th September 2013 (originally 20th August 2008)
Fuse settings System clock: Use the internal RC oscillator at 8.0MHz and CKDIV8 fuse programmed, resulting in 1.0MHz system clock.

Fuse settings should be: CKSEL = "0010", SUT = "10", CKDIV8 = "0"


Function Counts in binary and displays output on the on-board LEDs

Contains guidance comments for debugging with the Dragon / JTAG device

*******************************************/
/* The DRAGON board uses the JTAG interface to the ATmega1281 to provide a programming and debug facility
Activity sequence is:

1. Develop your program as usual, using ATMEL Studio 6.

(create a project, type your code, build the code and remove any errors that are detected)
2. Connect the hardware

(the Dragon board connects to the PC via USB, and to the STK300 board via an IDC10 cable,

the Dragon board is powered through the USB, but the STK300 needs an external power supply)
3. Under the 'Tools' tab in Atmel Studio 6, select Device Programming

3A Make the following selections in the configuration window:

Tool = AVR Dragon

Device = ATmega1281

Interface = JTAG
3B click 'Apply'
3C Check that the Dragon board is correctly interfaced to the ATmega1281 - this is done by clicking the 'Read' button

in the 'Device Signature' box.

A device specific hex number should be shown, also the operating voltage will also be displayed (this will depend

on how you have configured the board but the default value should be 5Volts, or close to this value).

As long as a signature value is displayed and the voltage is as expected, continue - otherwise fault-find this step before progressing.
3D Click 'Memories' from the left-hand pane. This opens the memory-programming pane on the right.

For Flash memory, select the .hex file that you wish to load - this requires browsing to your project directory and the debug directory within it, where you will find the .hex file.

Ensure that the Erase and Verify tick boxes are selected.

3E Click the 'Program' button - this actually loads the hex code file into the Atmel Flash memory.

At the bottom left of the Device Programming window you should see:

Erasing device... OK

Programming Flash...OK

Verifying Flash...OK

This confirms that the program has been loaded correctly

4 Single-step through the program code

This step is where we use the debug facility of the Dragon board to see how our program works one instruction at a time

4A Add a breakpoint on the statement that outputs the data value to PORT B

This is done by placing the cursor on the statement and pressing F9
4B Under the 'Debug' tab in Atmel Studio 6, select Start Debugging and Break

The 'Select Tool' window will now appear. Within this window, select the Dragon device (blue highlight) and press 'OK'.


4C You are now in debug mode. The first instruction of the program will be highlighted yellow, and an arrow will point at the next instruction to be executed.

The following function keys perform the debugging functions:

F5 run to next breakpoint

F9 add / remove a breakpoint (toggles)

F10 step over a block or call

F11 step into a block or follow a call into lower level

Shift + F11 step out to previous (higher) level

Cntl + F10 run to cursor

4D Experiment with these commands until you are confident with the debugging facility

Note that you can copy the 'Count' variable into a 'Watch' window at the bottom of the screen and see

the value changing dynamically as the program runs - experiment with this.

Note also that the LEDs value will actually change as you step through the code because the program is actually running

- i.e. this is not a simulation*/
.def TEMP=r16 // Map variable names onto registers

.def Count=r17

.def Delayreg1=r29

.def Delayreg2=r30

.def Delayreg3=r31
.cseg // CODE segment

.org 0x0000 // Interrupt Vectors base address

rjmp INIT // Reset (and power-on reset) handler
.org 0x0080 // Program Code base address
INIT: ldi TEMP,0xFF // Set Stack Pointer to top of SRAM (1281 has 8K SRAM, offset by 1/2K bytes for registers)

out SPL,TEMP // so bottom of SRAM is actually address 0x200, top of SRAM is 0x21FF

ldi TEMP,0x21

out SPH,TEMP

ldi TEMP,0xFF // Set portB direction as output (mapped to the on-board LEDs)

out DDRB,TEMP

out PORTB,TEMP // Ensure LEDs are initially off
ldi Count,0x01 // Start the count sequence at 1
MAIN:

mov TEMP,Count

com TEMP // Invert the binary pattern (on the STK300 board an LED is ON when a '0' is written, OFF when a '1' is written)
// Use F9 to place a breakpoint on the line below

out PORTB,TEMP // Output the stored value onto the LEDs

inc Count // Add 1 to the count value
// The delay statement below has been commented out because it complicates the debugging process - especially for beginners

// Note that if running the code without debug mode you should un-comment the statement otherwise the display changes too quickly to see what is happening

//rcall DELAY // Delay so that the output can be seen (try removing this line, you will see that the output changes too fast to see what is happening)
rjmp MAIN // Repeat continuously (this is the 'main-loop' of the program)

DELAY: ldi Delayreg1,0x01 // 0x07 gives approx 1 second delay with 1MHz system clock

delay1: ldi Delayreg2,0xFF

delay2: ldi Delayreg3,0xFF

delay3: dec Delayreg3

brne delay3

dec Delayreg2

brne delay2

dec Delayreg1

brne delay1

ret
Task

1. Study the assembly language program (as described, and listed above).

This code produces a simple binary count on the LEDs – make sure you understand how this works.
2. Use the Run-time debugging features of the Dragon / JTAG system to step through the code and inspect the values of registers while the program is running.
3. Modify the code, so that the pattern runs backwards – i.e. instead of counting up, count down.

Use the run-time debugging to understand what your code is doing, and to help find any errors in the logic that you may have.


4. Modify the code, so that instead of a binary count, a single moving bar is displayed – i.e. a single LED is lit at any time, moving from right-to-left.

Use the run-time debugging to understand what your code is doing, and to help find any errors in the logic that you may have.


5. Use one of the switches (connected to PORT D) to change the direction of the moving bar.

This requires that you set the direction for PORT D as input and that you read the value from PORT D at regular intervals (i.e. put the IN statement in the main loop). See the switches-and-lights sample application for an example of this.


Part B – Repeat part A, this time using embedded C

Repeat the steps from Part A above, this time using the embedded C Dragon / Debug C sample code ATmega1281 project.



Part C – Study the list of application themes for the coursework.

Think about which applications are most interesting to you, and think about what might be involved in implementing them. If you can decide which are your first, second and third preferences then send an email to the course tutor to reserve your choice. The applications will be allocated on a first-come-first-served basis. If you cannot decide yet, then you still have until next week.




Richard John Anthony, Smart Systems Technology, The University of Greenwich , UK


Download 26.25 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2020
send message

    Main page