Sunday, May 24, 2015

Pulse width modulation

Good simulation vid

The Arduino page on analogWrite() here

You can use pwm to control higher voltages like here.





i2c 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.



Interesting use of i2c OLED display.

SPI or i2c. Which one is ""better"?

Wednesday, May 13, 2015

The 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.





Tuesday, May 12, 2015

Using hex files from AVR studio to program 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


Each AVR assembler instruction has its own help page with example.

Each op code has an explanation via the help screen



You can also go to the Atmel AVR assembler site and look at instruction like SER.

Port Input and Output

All AVR devices, like our Mega328, have ports. Usually they are a group of 8 pins that send or receive binary to or from the outside world.

There are three registers associated with each port.

  1. The output register PORTX
  2. The input register PINX
  3. The data direction register DDRX.
For instance port B is made up of 8 pins PB0, PB1, PB2 ....PB7. It has three associated registers:
  1. PORTB
  2. PINB
  3. DDRB
These registers live in the I/O register space just above the 32 general purpose registers.




Some good port pages:
Useful but about GCC.
Some good stuff here.

Tuesday, May 5, 2015

There are four sorts of memory on the avr devices like the mega328


Setting up the stack before using subroutines

The code for the above program is:

.include "m328pdef.inc"
start: ;set up the stack
ldi r16, low(RAMEND)
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16

nop

call below
nop

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