OK, so let's write a simple test program...
Fire up MikroBasic, close the example project it gives you, and start a new project.
It'll ask you to enter a bunch of options. Under "Device", select our baby: PIC 16F628A. Under "Clock", choose 20.0000MHz.
On the left, you'll see a list of "Device Flags". These are "Fuses" in the MCU that enable or disable certain features on the chip. If we don't select anything here, we'll have to select them when it comes time to program the chip. That becomes tiresome fast when you program a chip many times over for testing. If we set them here, the programmer should follow our settings, like a faithful dog.
Here are the settings:
Suffice to say, check the "_OFF" options for everything, and choose the "HS" option to tell it we're using a High Speed crystal.
Now, some bright spark at MicroBasik obviously thinks it's a great idea to let you tick all the boxes... including _ON and _OFF fuses for the same function. If you think it's a good idea to select both _ON and _OFF for a function, then you win a very special Dunce Of The Year (tm) cap (limited edition).
OK, now we've set the options up, it's time to write a program.
First we need to understand a bit about the chip. 8O Here's a pic of it:
See, the chip is divided up into two ports - PORTA and PORTB. Each port has eight pins. Like this:
These pins are controlled by "registers" inside the chip. A register is just a memory location that we can put a number in. The value of the number controls features on the chip.
The pins are called "tri-state". That is, they can be an input (read data in), or an output (send data out, or turn stuff off and on), and the output can either be ON or OFF.
Note that there are eight pins on each port. This is not some random number chosen at will because of the chip designer's love of the number eight.
No.
You see, the chip works in binary. Sounds complicated? falling asleep? No need to, Binary is simple. It means things are either ON or OFF. 1 or 0. No 2 or 3. Just 1 or 0. Without getting too complicated, for our chip, "1" is 5V, and "0" is 0V.
One binary number - 1 or 0, is called a BIT.
You know what a BYTE is, right? You probably talk about megabytes and gigabytes pretty often. Well, a byte is eight bits. A chip usually operates on one eight-bit register at a time.
So our pins are split into two bytes - PORTA and PORTB.
Here's a random byte for you:
11101010
(Some Super-Geeks can convert this into a decimal number in their head. I can't. I use the Windows Calculator. It's 234, if you're interested

).
This byte is eight bits long. Of course. All bytes are. The first number (1). is called BIT 7. The last number ( 0 ) is called BIT 0. (Computers always start counting at zero, just to annoy us)
OK, OK.. enough of all this nonsense..... I couldn't give a flying hoot how the darn chip works.. how do I program it? Some of us did say we wanted to build first, not do this programming rubbish.
It's OK. I understand. That's all the theory there is. It's explained. Done.
See, The above is key. Remember we have eight pins on each port? We control whether they are inputs or outputs by dropping a byte into a register. The TRI-STATE registers. (known by the chip as TRISA and TRISB). We use a 1 to set it as an input, and a 0 to set it as an putput.
So, if we want PORTA.7 to be an input, and all the others to be an output, then we set:
The "%" just tells MikroBasic that this is a binary number, and not a really big decimal number.
For our test program, we won't be reading any data in, so we can set all the ports to outputs. Like this:
Code: Select all
TRISA = %00000000
TRISB = %00000000
Tha'ts the most complicated thing in our program!!! And understanding it will give you enough insight into understanding the rest.
You can understand the rest by just looking at the comments in a code.
Comments are denoted with an apostrophe (').
For example
See?
OK, let's write our test program then. Our program will flash our moonlights on and off once every second, for EVER.
Here's the full code for this simple but sexy program.
Code: Select all
program Moonlights ' This is just the name of the program
Symbol Moon = PORTB.3 'This assigns a name to PORTB.3
'Our moonlights are on here. So now,
'instead of having to remember which port pin
' they are on, I can just write "Moon".
main: ' The main program starts here
TRISA = %00000000 ' All pins on PORTA are outputs
TRISB = %00000000 ' All pins on PORTB are outputs
Here:
Moon = 0 ' Turn the moonlights off
Delay_ms(500) ' Pause for 500 milliseconds
Moon = 1 ' Turn the moonlights on
Delay_ms(500) ' Pause for 500 milliseconds
Goto Here ' Go back to the label marked "Here:"
end. ' This is the end of the program
Type this into MikroBasic, and then choose "Build". MB will compile and build our program and save the machine readable code in a .HEX file, which we can then download to our chip.
Find the .HEX file, and open it in the programmer. Check that the fuses are the same as we want them to be :
Yep, the fuses are all as we set in MikroBasic... good.
Then program the thing. Here she is in the programmer, all ready to be delivered with her new brain:
Done!
Slip the chip into the socket in your circuit (The right way round, unless you like the smell of burning toast -- pin one goes to the top left. )
Dadaaaaaaa! the moonlight flashes. Not the super brightness I promised..... but you must admit, flashing moonlihghts are cool enough!
OK, so we can contain our excitement, it's not PWMing yet... but we've tested our circuit and proved that it works. The chip controls the moonlights. Time to sit back and give yourself a pat on the back. It's all downhill from here. Really, this time.
In future programming installments, I'm just going to post the code with comments for you to copy and paste into MikroBasic.

... .
ANYWAY, ON TO SOUPING THIS BABY UP! WATCH THIS SPACE!!!!