Wednesday, June 4, 2014

What's in the theory test

A outline of what's in the test.

The test will be in our normal classroom on Thursday 12th June at 3:13pm

There will be a picture of an Arduino like this but red and you'll annotate it with text and arrows.

There will be one of the two pages from the Atmel mega 328 family's data pdf. You'll be required to comment on some of the functions in the page below and most of the features in the very front page.



There will be questions about a schematic involving the Arduino like the one below.

Test Information

There will be a theory test on Thursday 12 June. It will be closed book and you must complete it in 1 hour and 45 minutes. We will discuss some of the items in the test, the previous class.

There will be a question on the main parts of the Arduino. See the labelled diagram above. This is not exactly the same as our Arduino but, apart from the DIL processor and colour, is pretty close.

There will be a question on the page above called The Front Page of the Mega328. You will be expected to write a sentence explaining some of the features listed. 

There will be questions on the functions diagram of the Mega328. You will be given a copy of this diagram and be expected to write a short paragraph on up to three blocks.

There will be a question about the Arduino schematic diagram above or similar.

Also there's buggy program. A program will be given to you with errors in it. You will have to identify the errors. Mostly syntax violations.

A problem to do with sampling real world inputs will be given and you will be asked how you would respond to this using the Arduino.

You will be asked to write a simple assembly language program. You will be asked to write a program in Arduino C.

There may be other questions but only on items we have studied in class including serial buses, pin outs of the Arduino and the mega328p, voltage dividers, the stack and interrupts. A simple question on using timers may be asked.

Please contact me about any problems. Good luck.

PeterB





i2c two wire bus

Most popular of the serial buses. Uses one wire for clock and one wire for bidirectional data. Based on Phillips' iic network. Lots of chips use the i2c protocol.

Good short introduction here. Not Arduino based.

Using i2c to connect arduino to eeprom chip that holds 256k bits, here.




SPI Bus

Fastest of the serial I/O on the Arduino.

A good tutorial on spi using a digital potentiometer chip here

A tutorial on the spi bus, more technical here

Two Arduino's connected via spi here. Code via link to github.





Monday, June 2, 2014

AVR Interrupts






Some interesting sites:

General AVR Interrupt using GCC here
Arduino simple interrupt Click here

Not a bad video here

Redboard and schematic

Schematic diagram for our Redboard Arduino




Above: The Sparkfun Red Board Arduino. Link to it here.

Monday, May 26, 2014

You need to set up the stack before using calls and interrupts using assembler

The code for the above program is:

.include "m328pdef.inc"
main: ;set up the stack
ldi r16, low(RAMEND)
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16
nop call below
finish: rjmp finish
below:
ldi r17,$99
ret

The relevant part of the def.inc file the shows what RAMEND is for this processor is:

; ***** CPU REGISTER DEFINITIONS *****************************************
.def XH = r27
.def XL = r26
.def YH = r29
.def YL = r28
.def ZH = r31
.def ZL = r30



; ***** DATA MEMORY DECLARATIONS *****************************************
.equ FLASHEND = 0x3fff ; Note: Word address
.equ IOEND = 0x00ff
.equ SRAM_START = 0x0100
.equ SRAM_SIZE = 2048
.equ RAMEND = 0x08ff
.equ XRAMEND = 0x0000
.equ E2END = 0x03ff
.equ EEPROMEND = 0x03ff
.equ EEADRBITS = 10
#pragma AVRPART MEMORY PROG_FLASH 32768
#pragma AVRPART MEMORY EEPROM 1024
#pragma AVRPART MEMORY INT_SRAM SIZE 2048
#pragma AVRPART MEMORY INT_SRAM START_ADDR 0x100



; ***** BOOTLOADER DECLARATIONS ******************************************
.equ NRWW_START_ADDR = 0x3800
.equ NRWW_STOP_ADDR = 0x3fff
.equ RWW_START_ADDR = 0x0
.equ RWW_STOP_ADDR = 0x37ff
.equ PAGESIZE = 64
.equ FIRSTBOOTSTART = 0x3f00

Thursday, May 22, 2014

Writing hex files directly to an Arduino

Programs needed are just AVR Studio and avrdude. I used AVR Studio version 4.14 and the avrdude that came with the Arduino suite.

Step 1. Write your program in assembler in AVR Studio, build then save the hex file.
Step 2. Go into the command prompt and invoke avrdude with the right switches.

I started with the usual Arduino C blink program, just to make sure I got the COM port right and to see the hardware was all working. Then did steps above.

Below are some relevant screen shots.




Blink for AVR Studio 4 assembler


.include "m328pdef.inc"


;Based on http://www.robertoinzerillo.com/wordpress/?p=5
;-----------------------------------------;
; FIRST WE'LL DEFINE SOME REGISTER TO USE ;
;-----------------------------------------;
.DEF A = R16   ;GENERAL PURPOSE ACCUMULATOR
.DEF I = R21   ;INDEXES FOR LOOP CONTROL

.ORG $0000

;-----------------------------------------;
; FIRST WE SETUP A STACK AREA THEN SET    ;
; DIRECTION BIT ON PORT-B FOR OUTPUT/SPKR ;
;-----------------------------------------;
START:
  LDI A,LOW(RAMEND)   ;SETUP STACK POINTER
  OUT SPL,A           ;SO CALLS TO SUBROUTINES
  LDI A,HIGH(RAMEND)  ;SETUP STACK POINTER
  OUT SPH,A           ;SO CALLS TO SUBROUTINES

  LDI A,0b1111_1111   ;SET ALL PORTB FOR OUTPUT
  OUT DDRB,A          ;WRITE 1s TO DIRECTN REGS

;--------------;
; MAIN ROUTINE ;
;--------------;
LEDONOFF:
  SER A
  OUT  PORTB,A
   RCALL DELAYLONG
  CLR A
  OUT PORTB,A
   RCALL DELAYLONG
  RJMP LEDONOFF
 

DELAYLONG:
; =============================
; Delaying approximately 1 sec at 8Mhz.
; This code has been created with  “AVR Delay Loop Generator V1.2?.
          ldi  R17, $48
WGLOOP0:  ldi  R18, $BC
WGLOOP1:  ldi  R19, $C4
WGLOOP2:  dec  R19
          brne WGLOOP2
          dec  R18
          brne WGLOOP1
          dec  R17
          brne WGLOOP0
; =============================
RET


Wednesday, May 14, 2014

Delays in assembler and some tasks.

We often need to introduce a small delay into our programs. We often do this decrementing  or incrementing a register until it becomes a zero.

.include "m328pdef.inc"
ldi r16,1
ldi r17,1
call delay
here: nop
rjmp here
delay:
inc r16
brne delay
;rjmp here
ret

-----------------------------------some tasks----------------------------------------------
;helpful code. See below
.include "m328pdef.inc"
startprog: nop
up1:inc r5
brne up1
nop
nop
nop
inc r6
brne up1
end: rjmp end

Write short code segments in AVR Studio 4 to do the following. Put the code into your blog. Also add the relevant part of

the list file. Say how many cycles at 16MHz your segemnt used.

1) Put $16 into r16 and $17 into r17.
2) Put $11 in r11 and $12 into r12.
3) The above program takes over 49ms to get to the end: label. Say exactly how many micro-seconds.
4) Tweak number 3 above to take exactly 50ms.
5) In the Arduino we have a delay() for so many milli-seconds. Write a segment to delay r0 milli seconds. For instance if r0 contained 50 the delay would be 50ms.
6) Repeat number 4 but use dec instead of inc.
7) Write a segment to swap the contents of r0 and r1.Use specific numbers that you chose for contents.
8) Write a segment to reverse the order of the contents in r0,r1 and r2.
9) Replace any $01 in r0,r1,r2 with $ff. (Hint. Dec and check Z flag with brne or breq.)
10) Using inc, increment r0 as many times as the number in r1. Eg if r0=3 and r1= 4, then r0 would become 7.

Monday, May 5, 2014

Monday, March 31, 2014

RS232 protocol

The RS232 protocol has been around for many years now. There are various versions and we have to be aware that non-standard applications are used often. The main aspects are the sockets and plugs and the packet sending rules.





Monday, March 17, 2014

First sensor: the LDR






Some good code and information here.
Light dependent resistor circuit. See http://www.ladyada.net/learn/sensors/cds.html





Some voltage divider images

We often want a sensor to send its signal using a changing voltage. For instance when the temperature is high we get a big voltage, and when the temperature is low we get a low voltage. Note that normally the input to an Arduino analog pin has to be between 0 and 5 volts.






Monday, March 3, 2014

Running two LEDs

/*
Blink2
Turns on a LED on for half a second, then off for half a second, repeatedly. It also does the same to an off-board LED connected to pin 12 so that when one LED is on the other is off.
The circuit:
* LED connected from digital pin 13 to ground via resistor.
* second LED connected from digital pin 12 to ground via resistor. I used 330 ohms.
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13.
Created 1 June 2005
By David Cuartielles. Adapted by Peter Brook
based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
int redLedPin = 12; // LED connected to digital pin 13
int del =500;
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
digitalWrite(redLedPin, LOW); // set the LED on
delay(del); // wait
digitalWrite(ledPin, LOW); // set the LED off
digitalWrite(redLedPin, HIGH); // set the LED on
delay(del); // wait
}
 Blink

 Turns on an LED on for one second, then off for one second, repeatedly.

 The circuit:
 * LED connected from digital pin 13 to ground.

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.


 Created 1 June 2005
 By David Cuartielles

 http://arduino.cc/en/Tutorial/Blink

 based on an orginal by H. Barragan for the Wiring i/o board

 */
int ledPin =  13;    // LED connected to digital pin 13
// The setup() method runs once, when the sketch starts
void setup()   {              
 // initialize the digital pin as an output:
 pinMode(ledPin, OUTPUT);   
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()                   
{
 digitalWrite(ledPin, HIGH);   // set the LED on
 delay(1000);                  // wait for a second
 digitalWrite(ledPin, LOW);    // set the LED off
 delay(1000);                  // wait for a second
}