Posts

Water and Electricity

Image
Water and Electricity don't mix. We all know that, right? DO NOT UNDER ANY CIRCUMSTANCES POUR WATER OVER YOUR HEAD WHILE YOU HAVE YOUR FINGERS IN A MAINS SOCKET. But water flows, like electricity does. If you take some water, in a jug, then you tip the jug, the water flows, downwards, because the gravity pulls it down. Right? Come on, it does. If you put water under pressure, from a tap, through a hose, it comes out the other end. You know it does. You can see it. Electricity flows like that too. If you turn the tap on more, water flows faster. If you put more pressure on the electricity, it does the same. The "pressure" on the electricity is called Potential Difference, or PD. We measure it in volts (named after a bloke called Voltaire which is a daft name but he was French). The more PD, or volts, the more "pressure". You can alter the flow of the water in a hose by standing on a hosepipe. You know that. You can alter the flow of electricity by u

Lights!

Image
Following on from the last post we'll set up the oscillator while we're in the configuration bits. See this one, RSTOSC? Notice how it has the Warning Sign next to it? It means we can't just change it. So what we need to do is to click the button "Generate Source Code to Output". Click it. There will be a list of what are called compiler directives. These start with #pragma. #pragma directives are not C. They are specific lines that tell a specific compiler to do a specific thing, in this case it is setting configuration bits. There are two of these you'll need, one is the watchdog, the other is the oscillator. Don't worry too much about why, just copy them in. All will become clear later. Here they are: #pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored) #pragma config RSTOSC = LFINT // Power-up default value for COSC bits (LFINTOSC) Then forget about them until a lot later. But don't leave them out! Your code should

Bases

BASES   Let's talk a bit about binary. And decimal. And hexadecimal. Don't panic. You know 1+1=2, yes? 2+2=4? 4+4=8? Of course you do. You can count them on your fingers. Now what's 6+6? You know it's 12, but it isn't really. It's 2 fingers and 1 lot of all the fingers. We call this base 10. We do this because we have ten fingers. If we had more fingers we'd count in whatever that was. With 12 fingers we'd have 0,1,2,3 ... 7,8,9,⧫,⧪ and the next number would be 10. Which is not what you know as 10, its all the fingers and no fingers. Which is what you'd call 12 but it you had 12 fingers you'd call it ten. Remember at school (I don't) where they'd say 6 plus 6 is two and carry one? Each of the "digits" we use (0,1,2,3,4,5,6,7,8,9) takes one character to write. When you go over 9 it takes two characters to write (1 lot of all the fingers and 0 fingers, or 10). Here is a sum in base ten, which is how we think

What happened?

Image
This is where you'll see the difference between playing with a little plastic chip and playing with the Web, Windows, Unix, MAC and all those other huge toys. Remember the end bit of this ? How, and why, did it jump out of the loop and start again at the beginning? A "normal" program wouldn't. But most microprocessors have a thing called a watchdog. It's a friendly thing. Here is one: The watchdog keeps an eye on what your code is doing. We'l go more into it later, but the reason the code jumps back to the beginning is because of him. You can turn him off. Put him back in his kennel. Let's do that. In the Window Menu, underTarget Memory Views, click on Configuration bits. Look down the bottom, you'll see them. There's one there called WDTE. You can find it. Click where it says ON and turn it to OFF. Now debug the program again, and see if it runs ad infinitum . That's Latin. Impressed? Bet it does. Hope it does. I&#