Anybody Into Programming PICs?

Black Hole

May contain traces of nut
I have a project which requires a little logic control - too much for a couple of gates and latches, so I have decided to take my first foray into the world of PICs and ordered a programmer and a 12F629.

I am familiar with Z80 machine code/assembler, but I need a conversion course. Can anyone recommend the best entry point (path of least resistance)? I need some kind of overview of the PIC programming model, a quick reference to the instructions, and recommendations for an IDE. Can they be programmed in C?
 
Yes, I have that page open on the iPad. I was hoping somebody had been through this and had some personal insight to offer. I don't want to follow the full learning curve if I can help it (life is too short)!
 
Okay I thought, let's go see what they have in the library in town. Drove down, cruised around looking for a street parking spot, walked through to the library.... closed Mondays. Walk back to car, drive back - there were better ways I could have spent my time! I have a book coming from Amazon.
 
I only have recent experience with http://www.picaxe.com/ - programmed using a BASIC-like language. Cheap as chips!
I do like the look of that! £1.80 for an 8-pin PIC-AXE chip, and program with a simple USB cable!!

However, now I have committed about £15 for a raw 8-pin PIC plus a universal PIC programmer, I am inclined to see what I can do with that. It shouldn't be too hard with my previous micro experience, once I study the data sheet and get some tips on the machine code features.

For anyone else: I have to say that the PIC-AXE approach looks like a very easy entry point. In summary, they pre-program a variety of sizes of PIC chip (according to how many inputs and outputs you want, and to some extent on how complex your application is - hence the code storage requirement) so that further programming can be done through a simple serial interface with the chip in the application circuit rather than in a special programming circuit, and includes an interpreter* so that the user program is in a simple high-level BASIC-like language rather than raw machine code.

Superb, although of course a more complex program can be squeezed into the same size chip in machine code than in interpreted code tokens, and it will run faster, but just buy a better PIC-AXE chip if it won't fit!

If I find raw PIC too hard going I'll jump ship to PIC-AXE without a second thought. It looks brilliant.

* I'm not absolutely sure about this, the BASIC code could be compiled before uploading - what do you say AF?
 
Okay I thought, let's go see what they have in the library in town. Drove down, cruised around looking for a street parking spot, walked through to the library.... closed Mondays. Walk back to car, drive back - there were better ways I could have spent my time! I have a book coming from Amazon.
I have found an on-line catalogue for the library, and as far as I can see they don't have anything "pic" at all (nothing relevant to chips, anyway).
 
In summary, they pre-program a variety of sizes of PIC chip (according to how many inputs and outputs you want, and to some extent on how complex your application is - hence the code storage requirement) so that further programming can be done through a simple serial interface with the chip in the application circuit rather than in a special programming circuit, and includes an interpreter so that the user program is in a simple high-level BASIC-like language rather than raw machine code.
I'm not absolutely sure about this, the BASIC code could be compiled before uploading - what do you say AF?
 
My PIC stuff has arrived... need to resist playing with it until the chores are done... arrggghhhh...
 
I'm not absolutely sure about this, the BASIC code could be compiled before uploading - what do you say AF?

It's compiled, but maybe into byte-code, not sure. The IDE that comes with PicAxe is fantastic, and you can download it for free and run programs through the simulator even without the hardware.

Code:
Compiled successfully.
Memory used = 217 out of 2048 bytes.

For anyone who's interested, this is a basic 'burglar alarm' program I wrote for my young son's Rudolph PicAxe kit (http://www.picaxe.com/Hardware/Project-Kits/Rudolph-Reindeer-Project-Kit/) The comments were aimed at him! It compiles to 217 bytes as shown above. He took it and evolved it somewhat, learning in the process. He also soldered the kit in the first place and his soldering is better than mine in places (I blame my eyesight - I need to look over/under my spectacles nowadays).

Code:
; This is the bit of progam that runs while the alarm is unarmed.
; It's at the top so will be run whenever Rudolph gets power.
; We want to start in an unarmed state so that's ok.
unarmed:

        ; Check to see if the button is pressed.
        if input3 != 0 then
                ; If it isn't, then briefly pause then go back to the top of the unarmed loop.
                pause 100
                goto unarmed
        end if

        ; If we get to here, then the button has been pressed.
        ; Wait until the button is released by calling the wait_for_release sub-routine
        ; that is defined near the bottom of the program
        gosub wait_for_release

        ; Flash the nose for five seconds to show that the alarm will be armed
        ; if the button is pressed again.

        for b0 = 1 to 5
                high 0
                ; If the button is pressed then exit the loop.
                if input3 = 0 then exit
                pause 1000
                low 0
                ; If the button is pressed then exit the loop.
                if input3 = 0 then exit
                pause 1000
        next b0

        ; We've finished the loop, either because the five seconds ran out
        ; or the button was pressed.

        ; Is the button pressed?
        if input3 = 0 then
                ; Yes it is! Wait until it is released.
                gosub wait_for_release
                ; Flash the nose quickly three times to show that the sytem is now armed.
                for b0 = 1 to 3
                        high 0
                        pause 200
                        low 0
                        pause 200
                next b0

                ; And go over to the armed bit of the program.
                goto armed
        end if

        ; If we get to here then the button wasn't pressed during the five seconds
        ; that the nose was flashing so we jump back to the unarmed bit of the program
        ; and start again waiting for a button press.
        goto unarmed

; This is the bit of program that runs while the alarm is armed
armed:

        ; The alarm is now armed so keep an eye on the light level
        readadc 1,b1

        ; Has someone turned on the light?
        ; If they have, then it's time to alarm!

        ; Sound the alarm if the brightness is over 75%
        if b1 > 75 then sound_alarm

        ; Has someone pressed the button to try and unarm the system?
        if input3 = 0 then
                ; Yes, they have.
                ; The eyes are flashed five times and you need to let go
                ; of the button between the third and fourth flashes to unset
                ; it, or it will sound.
                for b0 = 1 to 5
                        high 4
                        pause 1000
                        low 4
                        if input3 != 0 then exit
                        pause 1000
                next b0
                if b0 != 3 then sound_alarm

                ; Show that the system has been unarmed by flashing the eyes
                ; quickly.
                for b0 = 1 to 3
                        high 4
                        pause 200
                        low 4
                        pause 200
                next b0
                goto unarmed
        end if

        ; Alarm wasn't sounded and the button wasn't pressed.
        ; Go back to the start of the armed loop to check everything
        ; again, after a short pause to save battery and stop things getting
        ; too hot.
        pause 100
        goto armed

; This is the bit of code that sounds the alarm.
sound_alarm:
        ; Play Rudoplh
        play 3,3

        ; Now unarmed
        goto unarmed

; This is a sub-routine that waits until the button isn't pressed any more.
; It is used by other parts of the program.
wait_for_release:
        do
                pause 100
        loop until input3 != 0
        return
 
(I blame my eyesight - I need to look over/under my spectacles nowadays)
Ditto - but as I have astigmatism as well as myopia and presbyopia even that doesn't work very well. Before I got the presbyopia (inability to refocus due to the hardening of the lens in older age) I could focus (with one pair of glasses) from infinity down to inches. Now reading glasses don't come in close enough, and my varifocals do better but still not enough. Even assembling a couple of components on a bit of Veroboard was challenging (sigh). Now I know why older engineers are managers.

Have you tried a stand-mounted magnifying glass? By the time I have my lenses replaced due to cataracts, there is a chance the industry will have developed a prosthetic lens that is soft and can be focused by the eye muscles (which still work, apparently). They are working on it. Might be worth going for, even without cataracts.
 
I'm slightly astigmatic as well as short-sighted but find off-the-rack high power reading glasses (2.5x-3.5x) work well for fine modelling and soldering. However, my wife (who is long-sighted and older) finds she needs more than 3.5x which aren't available off prescription, so she has been using a 'fresnel' table magnifier I got a few years ago but couldn't get on with myself. (She also has varifocals which I have never been able to get on with.)

So try everything - gets a bit costly but with luck you'll find something that works for you.
 
Okay I thought, let's go see what they have in the library in town. Drove down, cruised around looking for a street parking spot, walked through to the library.... closed Mondays. Walk back to car, drive back - there were better ways I could have spent my time! I have a book coming from Amazon.


That's difference with Cumbria and Wales. ;) Our library is open six full days per week and four hours on Sundays. We can check on line what books they have on the shelves and if not, order on line and receive an email on arrival. And this in a small town. :)
 
...and am tee'd off with that too: I intended to have the PIC in sleep for most of the time, just reacting to (very infrequent) command inputs and then going back to sleep. There is no real facility for this in the PICAXE system, so if I go that way I will have the PIC running more or less all the time (or at least waking up every 10ms to check the inputs).

So it's back to getting assembly code going as the first preference...
 
It's compiled, but maybe into byte-code, not sure.
My reading of it is that PICAXE user code must be some kind of tokenised system. The built-in executive provides an in-circuit programming interface, but then some more system code (maybe built-in, maybe downloaded with the user program) provides a multi-tasking system and software polled interrupts which run invisibly between the user code instructions.

Obviously it would be possible to compile it all together and download as a machine code image, but much easier to have a tokenised interpreted system for the user code run by the executive. The documentation says it runs 1000 BASIC instructions per second, so plenty of time to interpret each one (1000 machine code instructions per BASIC instruction) and then sit waiting for a 1ms clock tick to start processing the next.

I don't regret buying the kit (£20 got me an 8-pin PICAXE, programming cable, and a cable adapter - everything else I either already have or is downloadable for free), but this approach is only suitable when very low power is not a consideration, and 1000 instructions per second is plenty fast enough.

For my immediate requirement it's more than adequate for speed, but it will be a permanent drain on the car battery so I will keep the power as low as I can.
 
Last edited:
I've decided to try implementing in PICAXE and see what the supply current turns out like. I might be trying to avoid a problem that isn't there.
 
Back
Top