Browsing Posts tagged programming

Adding memory? I forgot. 16/04/13

You’d have thought that with all this learning and fun stuff happening here at Wardian Towers, that as I get further and further into this electronic ‘lark’, first with PIC’s, then I2C, then real time clock counters, then I2C input/output devices, then into iButtons and all the wonders they produce; you’d have thought that it would get easier as time went on.

Scientists do say that the brain is in fact like a giant muscle, and that the more you use it, the easier it gets as it becomes more familiar with cramming it with yet more and more knowledge.

Well, I can report that from this end, it most certainly doesn’t seem to.Before...

But then again, I guess I’m not as young as I used to be, and maybe doing this kind of stuff is really for the twenty-somethings.

But then again, if I was twenty-something again, I’d be either playing on my X-Box, or drooling over a new Porsche, or Armani suit.  BUT, I’ve been there.  Got the T-shirt; read the book.  Boring.  Shallow.


To the right here is the board before I fitted the new memory chip.  The chip itself is a Microchip device called a ’24FC512′.  This denotes it’s from the ’24’ series of serial EEPROM (Electrically Erasable Programmable Read Only Memory) chips, and it will store up to 512 K bits of information.  Note the ‘bits’ bit.  In more user-friendly language, it will store up to 64 K Bytes of 8-bit words. (As we all know, there are 8 bits to a byte.)

The datasheet tells me that I can have up to four of these little guys sat on the same I2C lines, each individually addressable, so I could have up to 256K bytes of information storage on just four chips.


Hooking the thing up was ridiculously easy, and took a matter of minutes -I just had to remember to wire the SDA (data) and SCL (clock) lines properly.  Mix them up and all kinds of exciting and unexpected stuff would happen....After

There’s also a pin which you can either wire up, or even have sat on another output pin from the PIC.  This pin is the ‘Write Protect’ pin.  With it sat at 5-volts, you can read the contents of the chip, but it won’t let you write, while tying the pin to ground gives you unfettered access to everything.  This is a pretty useful feature, and I may well use it if I get the opportunity.

And here is the chip in circuit.  As I said, ridiculously easy to wire up, and you can barely notice any other new components.  They’re there, though.

And so to programming it.

And this is where all the ‘fun‘ started.

I set up an entirely new project in the X-IDE program I use from Microchip so as not to contaminate the other stuff I’ve written.  Okay, doing it this way may take longer, but it gives you a ‘fresh canvas’ to work on, and it prevents great lumps of code being sat in the program for other functions that you will never use in this.  It also focuses the mind on just what you really need in there to get the program running.

For this program, I needed the new RS232 program I’d written the other day so I could actually see in a box on the screen what the processor was putting into the RAM and also what was coming off the RAM.  Yes, I could have done it with simple LED’s already sat on the board, but that would have been pratty.

After several hours, many cups of tea, and of course, ‘consultation chats’ with young Alfie (…He’s a bit ropy on I2C, but learning fast…), I finally nailed it when I realised that you never use the ‘Acknowledge’ pulse when you’re writing to the device, but when you read from it, you must acknowledge every byte apart from the last one where you send a ‘NotAcknowledge’ pulse.

Oh, and to be on the safe side, you need plenty of ‘IdleI2C()’s thrown around, just for good measure.

Again, last night was another late one, but I can report total success.

I can now write data to this chip, switch the whole thing off, have a cup of tea, switch it all back on, and the data is still exactly as I wrote it.

Today, I have other, much less fun stuff to do, and of course Wednesday and Thursday are ‘Plotting’ days.

But I shall return soon to this project -hopefully before I forget everything I’ve done.

Unlike the RAM I’ve just fitted and programmed, unfortunately, I do forget stuff when switched ‘off’.


The lightest of touches 15/04/13

When I wrote that last piece about the iButtons, yes, I’d got the iButton to display its unique serial number on the PC screen. I did this using an RS232 link to my PC using a free program called PuTTY which emulates the old ‘Hyperterminal’, not found on Windows 7.  This was no mean feat -I’d never programmed a PIC for RS232, so it was new territory all round.

Yes, the PC would display the list of numbers, but the problem was, it would only do it occasionally!Before...

By its very nature, 1-wire communications with the iButton is shaky, even at the best of times, so getting the thing to display the proper numbers consistently was something of a challenge.

In the end, I had to read seemingly thousands of pages of dry-as-a-bone documentation, both from Microchip (the makers of the PIC processor) and from Dallas Semiconductors (the makers of the iButton).

A very long and very boring story later, and very late last night, I finally cracked it.

Initially, the PC would read the correct number maybe only once in thirty button touches.  Now, it’ll do it between eight and nine times out of ten.  Okay, still not ‘production’ quality, and I’m sure I can improve it further, but you’ll agree, it’s a Big Lot better.

Up to the right here is my board.  You’ll remember the ‘flying board’ of the I2C real time clock sat there on its piece of card, attached with gaffer tape. The iButton is the red thing at the side, while the iButton reader is to the left of that, joined by its two wires....and after.

To the left here is the same board, but this is after I’d gently touched the iButton to the reader, and you can see the white LED towards the back of the board, lit to show that the button has been recognised.

I’ve also programmed it such that different iButtons will light different LED’s, while tapping an ‘unknown’ iButton illicts no response whatsoever.

So, we can safely say that this is now a ‘Job Done’.  iButtons are now recognised, and more importantly, different iButtons produce different responses, while ‘new’ iButtons, until programmed in, aren’t recognised at all.The result!

To the right here is a pretty poor picture of what the screen says when a valid iButton is touched to the reader.  Pretty neat, eh?  And yes, the black ear behind the box on the screen is that of my cat, Alfie.

And the next job, Wardo?


That’ll be the I2C non-volatile RAM I mentioned seemingly weeks ago.

With this in circuit, I’ll be able to record, using the RTCC clock for reference, just when an iButton was touched to the system, and it won’t ‘forget’ once the power is turned off.

And after that?

Ah, Dear Reader, therein lies the ‘challenge’.  After that, I plan to design and build a monitoring circuit that will measure -and record- battery usage as the unit is working.  This board will be a ‘hybrid’ board, combining both digital circuits with an analogue ‘front-end’ that will monitor the currents going both into and out of the battery, but it will also ‘time-slice’ this data into usable ‘chunks’ with an integrator.  It is this integrator that has proved most troublesome, but thanks to the excellent ‘Khan Academy‘, a free Internet maths revision site, I think I’ve got it cracked.

Tie this to iButton usage, and I’ll have a permanent record of just who was using the bike, when they were on it, and most importantly, how much juice they put into the battery.

This data could then be used as the basis for the strict rationing of the ever-popular LEAF chocolate biscuits!

No, seriously, this could have a profound effect on how we might approach possible funders, and on what basis we apply for the ever-shrinking pot of public monies.

But to be honest, I’m not thinking quite that far ahead just yet.

One problem at a time, and the current problem is the non-volatile (NV) RAM.

Then I’ll look at the challenge of power monitoring.

All clean, healthy fun.

iButton madness

For those of you not in the know, an iButton, funnily enough, is not from those wonderful boys and girls who make the phones, computers, and seemingly everything else ‘i…’.

The iButton is actually made by Dallas Semiconductors.  If you’ve ever been in a bar, then odds-on, the barperson would have had one of these little things that they simply touch to the till to identify them as your server.  Yeah?


Well, Dear Reader, I’ve known about iButtons for years -we used them in the electronics department at Sheffield University for access control into areas that undergraduates are normally not allowed.

They’re dead cute, and can fit on any keyring.

But, the best thing about them is that each one is unique.  They have a laser-imprinted 64-bit serial number that is impossible to fake or hack (…they’ve tried, believe me…), so when you touch your iButton onto the piece of gear, it checks from a list of known users, and as nightclub bouncers are so fond of saying- ‘If your name’s not on the list, you’re not coming in!’

Same kind of thing, but without the badly-fitting suits or tattoos.

Well, to be honest, like the dreaded (no longer!) I2C that I mentioned previously, iButtons were things that I’d kind of been putting off for years.  In reality, they’re pretty difficult to program for, because everything is so dependent on timings and all other kinds of fun stuff.

The thing is, for the electric bikes to move forward, I needed a simple system -like an iButton- that regular riders could simply tap onto the top of the box, and it would instantly know who they were.  iButtons seemed ideal, but as I said, they’re a real drag to get right.

Well no longer!

Just a few minutes ago, with the help of my trusty feline (?!?), I finally cracked it.

As young Alfie so kindly reminded me from his position on my knee, the reason it wasn’t reading was that I hadn’t unset the bloody analogue inputs from PORTA, so it wasn’t reading it properly.

Well, now it is!

Now, the long-suffering Mr Ward Senior will not be around for our regular Sunday brunch tomorrow morning, so I’ve got all day to get the program working as I want.  Little things like the unit sensing when an iButton has been touched, interrupting what it’s doing, then reading the number from it.

Then I can program in the list of my six iButtons, so it will know exactly which one has been touched, and give an appropriate message.

All tremendously good fun, and it keeps me off the streets.

Oh, and there’ll be a ‘regular’ LEAF blog entry up first thing in the morning after I’ve processed all the photos and thought of just what to write.

From flashing LED’s…

No photos tonight, I’m afraid.  The camera battery is flat, and anyway, in this light, they probably wouldn’t have come out.

So you’re going to have to take my word for this…

After long, long hours of head scratching, and moments of seeming madness, I’ve finally cracked this i2c lark.

Saturday’s missive was all about how I’d managed to write data down the i2c bus.

As it turns out, this was the easy part.  Apparently, even my cat could have done it, if he’d been bothered.

Getting i2c to receive coherently and on time is apparently quite a big beef amongst the electronics fraternity, and Microchip -the makers of the PIC’s I’m using, haven’t done themselves many favours. Briefly, the way they’ve implemented it is, well, decidedly pratty.

A very long and boring story shorter, but just suffice to say I’ve now got the little board you saw on Saturday not only flashing the LED’s if I want to, but I’ve whacked on an RTCC (Real Time Clock Counter) onto the i2c bus too.

The input/output chip and the clock are quite happily sat on the same two lines (SCL and SDA if you’re that bothered), because they have different i2c addresses.  Before you talk to a chip, you have to give it the right address, which kind of makes sense.

SO, I’ve now got that same board, and you’ll remember it has those four displays left over on it.  Well, these are now coded up so the display simply reads minutes and seconds from the RTCC.  I haven’t set the clock properly, so the numbers are pretty meaningless, but the fact is that even if I completely unplug the board to switch it off, this little clock keeps on ticking -in much the same way as your PC clocks does when you power it down at night.

So, when I come to switch it on again, not only has the clock advanced, but it’s advanced properly to show the correct minutes and seconds that it was when it was on previously.

I guess I’m not explaining this very well, I guess I’m a little tired, but I’ll get some proper photos tomorrow to explain.

Hells, I may even set the clock properly so it shows the correct time!

The next job will be to work with that little input/output chip to get it to input, rather than simply lighting LED’s.

This shouldn’t be quite as difficult as today’s marathon effort, but I have the added ‘fun factor’ of having to set it up so that a button press will interrupt the processor.  This will then send out a command to find out just which button was pressed.


Well, this vastly saves on processor time.  Rather than the processor having to ‘poll’ the inputs every few milliseconds, and all the bother that entails, this way, it’ll only do something when it’s interrupted.

There’s even a possibility of being able to ‘wake’ the processor from ‘sleep mode’ with a button press.  This then does away with a whopping great on/off switch -it’ll just be a button press to turn on.  …Maybe, we’ll see…

I guess it all sounds dead simple, but take it from me -it’s not!

This is One Tired Wardo, signing off!

Ladies and Gentlemen…

…And cats.

At about midday today, there was great jubilation here at Wardian Towers.

Yup, you guessed it.  I got a couple of LED’s to light.Two LED's.

‘No Big Deal,’ you may think.  ‘Surely, a switch and a battery would achieve the same thing?’

And you’d be dead right.  ‘Why use microprocessors and the ‘C’ programming language, when all you need is a switch?’

Ah, but there lies the rub.

Those two humble (…but very, very bright…) LED’s are being controlled by the black chip just down and to the right of them.  For the more technically minded, it’s a Microchip MCP23008e i2c input/output chip.

However, the key bit of that information is ‘i2c’.

After what seems like years of half-heartedly trying to ‘get into’ i2c, then giving up, I’ve now finally achieved i2c communication across two chips in the programming language ‘C’.

Even though I’d studied it extensively with Bang & Olufsen many years ago, I’d never built and programmed an i2c system from scratch until this lunchtime.And the other two lit!

The proof here is to the left.  I changed one byte of information sent to the MCP23008, and the other two LED’s are now lit.

Okay, not earth-shattering in itself, but you may remember my excitement all those years ago when I got my first ever LED to light when programmed from a PIC?

The same feeling, but better.

These four little LED’s now pave the way for the really fun stuff as I whack the RTCC (Real Time Clock Counter) into it. This will then tell the PIC the exact time and date whenever it switches on.  This paves the way for ‘workouts’ on the bike to be correctly time and date stamped, so I’ll know just how much was put in, or taken out, of the battery over a given period.

Then, I’ve got the excitement of adding the cute little LCD display panel that also runs from i2c.  Different chip, but same protocol.

So, you can see, Dear Reader, things at Wardian Towers are moving forward.

…Even if the weather isn’t!

Second Box? Collected!

Yesterday, Phil from Trade Base came over to pick up his shiny new Electric Bike Box from us.

Okay, so it’s only months late, but as he himself admitted, they didn’t have anywhere secure to store it until a few days ago.

In the meantime, I’m working on the mkII version of ‘The Box’.

This version does away with all the confusing 7-segment displays, and makes do with a single larger LCD display.  At the touch of a button, this will show what the rider needs to know, but more importantly, because the display unit is almost self-contained, it relieves the main processor of all the work of updating all 20 7-segment displays.  All this unit needs are four wires to connect it to the processor -all the communications are handled serially via I2C.

I’ve also got a ‘Real Time Clock Counter’ on order from eBay as well.  This will mean the unit knows what time it is, and even what day of the year it is.  Again, this communicates via I2C.

If, like me, you’re of the ‘Propellor Head’ persuasion, you’ll find this pretty exciting.

More news as it happens…

Second Box? Done! 02/02/13

Yes, yes, yes, I know, Dear Reader.  You’re about to faint.

But it is true.  The second box for TradeBase is actually finished.  What isn’t finished is the A-frame bit that the bike sits in.

But I will finish it, come Hell or high water.  It only needs the wheel sensor whacking on, and if I get a clear run at it (HA!), it’ll only take me a morning or an afternoon.

This second box now has much brighter LED’s than the original ‘demo’ box -bright enough to be easily visible even in direct sunshine.  And it only took another 16 transistors and 30 resistors on one board to achieve this.  Piece of cake!

Other news is that since Ian (no relation) gave me that little TV with the HDMI input socket, I’ve been ‘playing’ with the Raspberry Pi, and having a Big Lot of fun in the process.

I ordered a tiny little ‘wireless dongle’ for it -so I can use it on the Internet at home running wirelessly from my router. This really was a little swine to get going, and it took me a full half-day to get there, but it’s now working fine and very fast when you consider it’s a full computer the size of a credit card for less than £30.00.

“So what?” you quite rightly shout.

Ah, I actually plan to have a Pi down on The Plots in the top shed.  Because it’s so small, I can easily hide it away -probably behind somewhere or even under something, so any potential thieves won’t be able to find it.

I may take an old, empty PC case, and put that in there, complete with a couple of bricks in the bottom.  A scabby old 14″ CRT monitor (…which won’t work…), and a knackered keyboard and mouse will finish off my ‘stage set’.  If any thieves do break in, they see these, and take them.

I can imagine them picking up the empty PC case (…plus two bricks), and saying to each other;-

“Eh, Wayne!  Feel the weight of this!  Must cost a fortune, one of these!”

Ha!  Wait ’til they get it all home and try to plug it in.

Okay, an idle, but very fun thought…

Where do you go? 12/12/11

Beyond: ‘Whose Cat!!’?

This morning, I waited for the postie like a little kid on Christmas Eve.  I was so excited, I really couldn’t sit still; pacing to and fro, nervously looking at my watch every two minutes.

Would he or wouldn’t he?

…And much to my chagrin, he didn’t.

All I received was some junk mail and some other stuff that wasn’t really important.

Ya, boo and sucks.

But then, just as I was glumly getting ready for the imminent arrival of Mr Ward Senior for our regular Monday lunch date, another postie turned up, big parcel in his hands and a large grin on his face.

“Waiting for this were you, Mr Ward?’  He happily exclaimed.

“You bet!  Merry Christmas!”  And I meant it.

So, before my dad arrived, I just had to open it up and check it was right.

And it was!

Throughout lunch, I’ll admit I kept looking at my watch.  I could hear the programmer, still in its wrapper at home, laughing at me.

As soon as I got in…

…I very calmly got a shower & shave, changed my clothes into the ‘All Grey’ comfy programming gear I seem to prefer, made myself a cup of tea then sat down at my bench and carefully opened it all up and plugged it all in.It works!

The Integrated Development Environment (IDE) was pretty cool about it being plugged in, so I set it up in software (…minimal, really…), then realised I had to make a programming lead to go to my new super-duper 8 digit display board.

ANYWAY, to cut a long and boring story only slightly shorter, suffice to say that my new board works (…Whose Cat??!!…), and not only that, it works exactly as I planned and built it to!

Okay, I tell a fib.  I’d forgotten that PORTA4 in an open-drain output.  This means that it can’t drive current, only sink it.  A quick 10K resistor up to the top rail soon cured that.  Oh, and there was a slight short between two of the segment address lines, but on clearing that, it worked absolutely perfectly.

And here you can see it to the right.  You can see the 10K just below to the left of the big chip towards the left of the board.

The top right and middle left digits are counting from 0 to 9 together on an interrupt.

The ‘.0′ you can see is the rev counter, sat at zero because I’m not pedaling!

What you can’t see because it’s so quick is the millisecond flash every second just to the right of that which tells me that the other timer is working perfectly.

Now, don’t worry.  I’ll very easily move the digits around so the top right two are showing revs and the bottom six will, at first, just provide a simple timer.  Once I’ve got this timer code in, I’ll leave it overnight to see if its drifts any.

Then I have to pop a couple of buttons on -one to change between km/h and mph and the other for that bottom display so it will show either and incremental counter, or on repeated pressing, count-downs from 10, 15 and possibly twenty minutes.

Then I have to carry on learning that circuit and circuit board making software I keep blathering on about.

Happy Days!

And then, to my… 05/12/11

…utter dismay, it stopped counting.

The units counted fine, the tens counted okay too.

But the hundreds?

No chance!

It is no word of a lie to say that this problem has taken up virtually all my ‘thinking’ time over the last ten days or so.

What I found totally perplexing by this bug was that just when I though I’d cracked it and the hundreds must click over to the magic ‘1’ from zero, the left hand display which was quite happily counting up from zero to nine repeatedly would reset itself to zero, then carry on counting from there as if nothing had happened!

Was it the Watch Dog Timer?  Was it somehow resetting itself because of an unknown time-out?

Was my wiring faulty?

And for that matter, why did it sometimes stop counting -on both numbers- or even fail to start when I switched it on?

Was I going mad???

Well, Dear Reader, you can make your own mind up about the last question, but this morning, either side of an excellent shower*, I figured out I had not one, but two main problems.

The first, and most pressing was this last one -the irregular start-up and occasional freezing of the display -and 40 MHz clock.

I checked through the hardware, and found everything all A-Okay.  Well, actually I must qualify that last statement.  The best electronics engineer I mentioned a couple of posts ago would’ve had to go and have a lie-down somewhere dark and quiet had he seen my board, but for me, it was pretty neat and everything ‘bleeped’ through fine with a continuity tester and there were no dry joints.

I finally traced it down to some pre-processing commands I didn’t actually need that were confusing the programmer.  Of course, this being Microchip and all, they didn’t actually say in Big Red Letters not to do this.  Like our good friends at Microsoft, Microchip tend to be a little ‘cryptic’ with their clues.  The way they figure it is, Hells, you’re going to have to learn this, so why make it easy for you?  Have a little fun on the way!  As I’ve always said, you only learn when things screw up.

Okay, number one problem out of the way, what about the real ‘Biggie’?  Why would it not count past the tens, and why when it tried did it ‘crash out’ the other display back to zero?

And therein lies the rub.

While washing my hair for the second time this morning, I had it figured.

It was crashing out the other display because it was doing just that -producing an error!

For those who’ve never attempted to program in ‘C’ -or its little brother ‘Assembler’- for the PIC processor, let me tell you that the error-trapping is worse than useless.  There are no big, friendly boxes appearing on the screen saying ‘Error in line xxxx’, because as far as the PIC is concerned, there’s no screen to display it on.

What was actually happening was that I was trying to address a part of the array used for displaying the LED display that didn’t actually exist!

I’d totally forgotten that in  ten element array, the last available address is the ninth element -because you start from the zero element.  Like a first-semester rookie, I’d been trying to address the tenth element when it was meant to be carrying over to the next digit.

Hence the reset.

Unsurprisingly, I didn’t do the full ‘I’m Too Sexy For My Cat’ dance when I learned of my error.

I didn’t even do the half-version.

I went very quietly into the kitchen, half opened one of the top cupboard doors and repeatedly smacked my head against it.

Then I did it again.

So, Dear Reader, we’re running a little behind schedule right now, so I’d better get back to it and make it measure speed of wheel rotation.

This will be through a different interrupt system, but that’s not a huge problem now.  As I said last post, I now know more that I’d ever wanted to know about the PIC interrupt system.

But rest assured:  When I get that cracked, it will be the FULL ‘I’m Too Sexy For My Cat’  Dance.

Oh yes.

*Note.  I know that amongst programmers, when you’re stuck on such a problem, little social niceties like personal hygiene go straight out of the window.  Fact.

And counting. 25/11/11

I finally did it!

Okay, with a little help from undoubtedly Sheffield’s best electronic engineer, who actually said it as an almost throw-away comment; “…You have to reset everything to zero with a PIC because by default, everything is left on.”

Too true, as I finally turned OFF the PORT B analogue inputs and shazam!  Its counting like crazy.

So, in just a few minutes I have to pop back up to Diane’s office in the library to finish up the wording for a bid we’re putting together, then on my return, I will connect my circuit board up to the bike, do a little math as to how far you cycle with one wheel revolution and then, when I cycle, I’ll be able to tell how far I’ve gone.


Of course, the more cynical reader will simply say; “Why not buy one of those cheap bike computers you can pick up for less than a fiver?” And they’d be right to say this, but that’s missing the point.

The point is, without having to hack a ready-made piece of kit, I’ve actually done it myself.

And very quickly, from getting the distance travelled, I can work out all kinds of other stuff -speed, calories burned off; all manner of interesting stuff.

Yes, you can do that with a cheap bike computer, but it wouldn’t be anything like as much ‘fun’ to simply do that!

I use the word ‘fun’ there in inverted commas, because if truth be told, sometimes its been anything but fun, but I’ve sure leaned a Big Lot about the PIC interrupt system!

More soon…

SEO Powered By SEOPressor