Assembly Delays in the AVR

There are many reasons why we must create delays in assembler, and before you use them you must think to yourself “is a delay the best way to do what it is I need to do?” Sometimes the answer is ‘Yes’, but sometimes we just can’t get around them. A classic example is when initialising an LCD screen. When initialising (and using) an LCD, there are times we simply must wait before interacting further with it.

Essentially I have two different (but similar) approaches to this and I am going to give you three ways these can be applied. All involve a seperate file that is included at compile time by using the .include directive.

Before we begin

First we need to understand how a delay works and what we need to do to make it (relatively) accurate. We need to understand that every instruction we have within a delay routine takes a certain about of time to execute. Fortunately on the AVR all instructions generally take one clock cycle, with a few exceptions (as we’ll see) that take two. Therefore the answer is to determine how many clock cycles we need to kill. On the ATMega128 running at 16MHz, we need to take out 16,000,000 cycles to delay one second. All my delay routines work around the same piece of code, and so I know that my delay loop will take 5 cycles to complete (at 16MHz this is true for timings between 4ms and just over 1 second). Therefore to delay one second I need to kill off 16,000,000 / 5 cycles.

That’s 3.2 million cycles! 3,200,000 decimal in hex is 0x30D400. That’s a three byte number and hence I use 3 registers: d0, d1 and d2.

How did I come to 5 clock cyles per loop? Well the basic delay loop consists of 4 instructions:

subi delay0, 1
sbci delay1, 0
sbci delay2, 0
brcc PC-3

Create a routine for every delay type

This is by far the method I use the most, in fact, always. It is memory hungry in that it takes a lot of code space, but my preferred chip is the ATMega128, so I have the space to play with. I’ll provide two alternatives later, but for now I simply define every delay and process them individually. For this example, let’s say I need three major delay periods: 5ms, 20ms and 100ms.

Delay_5ms:                                                        

ldi     R18,byte3(ClockMHz * 1000 * 5 / 5)
ldi     R17,high(ClockMHz * 1000 * 5 / 5)
ldi     R16,low(ClockMHz * 1000 * 5 / 5)

subi    R16,1
sbci    R17,0
sbci    R18,0
brcc    pc-3

ret

;------------------------------------------
Delay_20ms:                                                        

ldi     R18,byte3(ClockMHz * 1000 * 20 / 5)
ldi     R17,high(ClockMHz * 1000 * 20 / 5)
ldi     R16,low(ClockMHz * 1000 * 20 / 5)

subi    R16,1
sbci    R17,0
sbci    R18,0
brcc    pc-3

ret

;-------------------------------------------
Delay_100ms:                                                    

ldi     R18,byte3(ClockMHz * 1000 * 100 / 5)
ldi     R17,high(ClockMHz * 1000 * 100 / 5)
ldi     R16,low(ClockMHz * 1000 * 100 / 5)

subi    R16,1
sbci    R17,0
sbci    R18,0
brcc    pc-3

ret

unfinished business – I’ll be back

These arrived today

Rotary Encoders
Rotary Encoders from Ebay

So I have a fair idea what I’ll be doing this weekend, or hope to do anyway. There are many factors that determine what I do, the least of which is what I WANT to do!

Nah, just kidding, she’s not THAT bad!

I’ve got a micro project that I’ve been procrastinating about for a while, this has just provided some incentive to do some work on it. I have all the parts I need to complete the project, I now just have to sit down and do it!

What was that? What do i want rotary encoders for? 10 of them, no less (I hear you ask)…

I have an old clothes dryer that the mechanical timer continually sticks on. I have on occasion found that after a few hours the dryer has failed to turn off. So I have been procrastinating about building a timer for it, this was (I’m pretty sure) the last component I needed to put it together.

I’ll be prototyping it using an AVR microcontroller (ATMega128), but the final project will be an older AT90S2313 I have lying around.

Of course this is going to have on/off and a timer function, but it would be cool to allow it to have soooo much more! It would be good for it to control the heating element, maybe once internal temperature has reached a certain level, power to the heater turns on and off a minute at a time – I don’t know, just a random thought – would help to save a bit of power. Maybe incorporate a humidity sensor to detect when the clothes are dry.

But for now I’ll just concentrate on the timer. If I ever finish it, I’ll put up a post about it.

My First Born

“I’m going to take a picture of my firstborn and use age progression software to figure out what he’ll look like when he’s 16. Then I’m going to frame the picture and keep it as a centrepiece in our house, something he’ll grow up looking at. Then when the appropriate time comes, and he realises that the picture is of him, I’m going to try and convince him that he is a time traveller.”

Not mine, but I like it

update: this was first posted back in 2010. Years later (2014) I came back across it and found that such software is in development
2014-04-09 Washington.edu/news/… age-progression-software

example of age progression software
A single photo of a child (far left) is age progressed (left in each pair) and compared to actual photos of the same person at the corresponding age (right in each pair).

Subpages widget

Alper Haytabay had created a subpages widget that was pretty much exactly what I was after (for another site I was working on). I found one flaw that prevented the widget from displaying nicely. In fact, the flaw didn’t appear to come from the widget but rather some other anomaly I could not determine the origin of.

The problem only occurs when the widget is displayed in it’s own sidebar. If there are no sub-pages to the current page the sidebar (which should be empty – and hence not displayed) appears. I could not seem to find a way to override this so I created a little addition to the existing code. If there are no subpages, display a small (user-defined) tagline instead. I made one other change to include this widget on all pages/posts so that if there are no subpages, the tagline will always be present.

I don’t use this widget on this site, but an example appears here

The widget (my version) can be found here: subpages-widget

The new look of dav3

wordpress logoThe last year has been pretty out there, and often times I seem to have too much time on my hands and at other times I’m absolutely swamped! My biggest problem is I have so many projects on at the moment I just don’t know where to start. Seriously.

One of the projects has been getting the the new look dav3.net website back up and running. A new look and feel, a new back end, and (soon to be) new and fresh content. The site has been about many things over the last few years, the latest of which has been my attempt at weight loss. It didn’t go so well, and so much of that content has been removed.

I’ve been pretty busy migrating everything across here (it’s taking a lot longer than anticipated). In doing the upgrade I have given WordPress a real workout. I’m pretty happy with it. I have tried a few CMS options over the years and (over recent times) gone to more ‘cloud(y)’ solutions. My favourite of late has been Google Sites, but it too has it’s limitations.

The cloud solution for WordPress limits some of the functionality I was after (eg, using your own domain or editing of certain style rules),  unless you opted for paid option packs. Since I have the server space and bandwidth I thought I would run my own and once you get to play with the back-end you can really do quite a lot to it.

As I said, I’m pretty happy, the only major problem is that it’s soooo customisable that I am spending far too much time experimenting with what I can actually make it do!

I need to get outside!

teardrops on my guitar

Drew looks at me, I fake a smile so he won’t see
What I want and I need and everything that we should be

I’ll bet she’s beautiful, that girl he talks about
And she’s got everything that I have to live without

Drew talks to me, I laugh ’cause it’s so damn funny
I can’t even see anyone when he’s with me

He says he’s so in love, he’s finally got it right
I wonder if he knows he’s all I think about at night

He’s the reason for the teardrops on my guitar
The only thing that keeps me wishing on a wishing star
He’s the song in the car I keep singing, don’t know why I do

Drew walks by me, can he tell that I can’t breathe?
And there he goes, so perfectly
The kind of flawless I wish I could be

She better hold him tight, give him all her love
Look in those beautiful eyes, and know she’s lucky ’cause

He’s the reason for the teardrops on my guitar
The only thing that keeps me wishing on a wishing star
He’s the song in the car I keep singing, don’t know why I do

So I drive home alone, as I turn out the light
I’ll put his picture down and maybe get some sleep tonight

‘Cuz he’s the reason for the teardrops on my guitar
The only one who’s got enough of me to break my heart
He’s the song in the car I keep singing, don’t know why I do

He’s the time taken up but there’s never enough
And he’s all that I need to fall into
Drew looks at me, I fake a smile so he won’t see

ebay

Yeah
A used … pink bathrobe
A rare … mint snowglobe
A Smurf … TV tray
I bought on eBay

My house … is filled with this crap
Shows up in bubble wrap
Most every day
What I bought on eBay

Tell me why (I need another pet rock)
Tell me why (I got that Alf alarm clock)
Tell me why (I bid on Shatner’s old toupee)
They had it on eBay

I’ll buy … your knick-knack
Just check … my feedback
“A++!” they all say
They love me on eBay

Gonna buy (a slightly-damaged golf bag)
Gonna buy (some Beanie Babies, new with tag)
(From some guy) I’ve never met in Norway
Found him on eBay

I am the type who is liable to snipe you
With two seconds left to go, whoa
Got Paypal or Visa, what ever’ll please ya
As long as I’ve got the dough

I’ll buy … your tchotchkes
Sell me … your watch, please
I’ll buy (I’ll buy, I’ll buy, I’ll buy …)
I’m highest bidder now

(Junk keeps arriving in the mail)
(From that worldwide garage sale) (Dukes Of Hazard ashtray)
(Hey! A Dukes Of Hazard ashtray)
Oh yeah … (I bought it on eBay)

Wanna buy (a PacMan Fever lunchbox)
Wanna buy (a case of vintage tube socks)
Wanna buy (a Kleenex used by Dr. Dre, Dr. Dre)
(Found it on eBay)

Wanna buy (that Farrah Fawcet poster)
(Pez dispensers and a toaster)
(Don’t know why … the kind of stuff you’d throw away)
(I’ll buy on eBay)

What I bought on eBay-y-y-y-y-y-y-y-y-y

21 guns

Do you know what’s worth fighting for?
When it’s not worth dying for?
Does it take your breath away
And you feel yourself suffocating?

Does the pain weigh out the pride?
And you look for a place to hide?
Did someone break your heart inside?
You’re in ruins

One, 21 guns, lay down your arms, give up the fight
One, 21 guns, throw up your arms into the sky, you and I

When you’re at the end of the road
And you lost all sense of control
And your thoughts have taken their toll
When your mind breaks the spirit of your soul

Your faith walks on broken glass
And the hangover doesn’t pass
Nothing’s ever built to last
You’re in ruins

One, 21 guns, lay down your arms, give up the fight
One, 21 guns, throw up your arms into the sky, you and I

Did you try to live on your own
When you burned down the house and home?
Did you stand too close to the fire
Like a liar looking for forgiveness from a stone?

When it’s time to live and let die
And you can’t get another try
Something inside this heart has died
You’re in ruins

One, 21 guns, lay down your arms, give up the fight
One, 21 guns, throw up your arms into the sky

One, 21 guns, lay down your arms, give up the fight
One, 21 guns, throw up your arms into the sky, you and I