Beginner Programming Microcontrollers

Thread Starter

stanman11

Joined Nov 23, 2010
228
I need to know all the basics on programming. I've never done it before but I have always wanted to.
I have 4 microchips I'm ready to try and program one.
I have an off brand usb programmer and have downloaded MLAB but havnt got around to figuring stuff out.
I have the 10F200 data sheet.

I need books on what C is? although I dont think my chips program in C.
I need books on how it is that you program.
The only thing i have done is make a blinking LED with an 555 timer.
So ever bit of information with any shortcuts you can give me would be great.
I just dont know what I'm looking for and need a push
 

elec_mech

Joined Nov 12, 2008
1,500
Hi Stanman11,

So it looks like you have four PIC microcontrollers and one of them is a 10F200?

MLAB is great. Now you need to decide that programming language to use.

Assembly - free, but it has a steep learning curve and takes a long time, relative to other options, to write code especially if you're starting out.

Basic - there are free versions with limited capabilities or time limits you can try out and if you like them, you can pay for the full version. I like Pic Basic Pro by microEngineering Labs: http://www.pbp3.com/downloadtrial/index.html?src=pbpdemo - this is the latest version, but I believe they offer an older one with limited capabilities without a time limit. MikroElektronika also offers a Basic compiler, but I do not know if they offer a trial version: http://www.mikroe.com/eng/categories/view/10/pic-compilers/

C - very popular, but learning curve takes some time - not as bad as assembly though. MikroElektronika offers a C compiler as does HiTech (http://www.htsoft.com/) and CSS (http://www.ccsinfo.com/).

If you're planning to learn for hobby use and want to get up and running quickly, I recommend Basic regardless of who's brand you use. If you're learning with the intention of getting a job in programming, you're better off with C - not because C can necessarily do more, it's just industry standard.

If you want some help programming for the first time, I highly recommend Chuck Hellebuyck's books. He has books on both Basic and C focused on showing you how to start: http://www.elproducts.com/.

Before purchasing a compiler, note different compilers will work with different microcontrollers, 8-bit vs 16-bit vs 32-bit. Each compiler has a list of compatiable PIC chips they will work with. Check out this list to be sure you're getting the right compiler - for the 10F200 series and most things you may do as a hobbyist, 8-bit is the way to go. Also refer to this list after you've selected one but before you buy new microcontrollers so you don't buy something that won't work with your compiler.

Lastly, after you've chosen a compiler, look for forums dedicated to that compiler which will have people, similar to AAC, willing to help you out.

Good luck!
 

axeman22

Joined Jun 8, 2009
54
I've been down this road myself... if I could share my 'opinion' with you in point form please..

- PICAXE is the way to go..
- PIC is awesome but too hard for a novice, in so many ways - even with the nice programmers and basic compilers etc
- when you 'get good' - you'll probably be very keen to go with PIC - an evolution from you just doing things on PICAXE.

..PICAXE..?
- a PICAXE is actually a PIC chip which has been loaded with a bootloader which make the thing a TON easier to use..
- if you want, you can turn your PICAXE chips into PIC chips when you are ready - but for the cost, I wouldn't :)
- you can make a full funcitonal simple circuit with a PICAXE with just the chip, 2 resistors and a serial cable.. no special 'programmer' needed etc.. and you program it in BASIC - tooo easy.
- the PICAXE's BASIC is very comprehensive and there are lots of people willing to help with knowledge and ideas.
- practically speaking, I have found that you can do most everything with the PICAXE - however if you need to do something where 'timing' it critical then you'll need to use a PIC - when you get to that point you'll understand why.

I'm talking PIC and PICAXE - there are others,, like Arduino, STAMP, etc etc.. but all in all I have found that the PIC/PICAXE seem the easiest. I guess it's fair to say when you can 'easily' program up a PIC.. you can probably program up any other assembly language type microcontroller just as easily.

so.. start of with PICAXE.. get your head around that, make up some simple circuits(and you CAN do very complex ones also!) and then see where that takes you.

Many people find that PICAXE does all they need... if you want to go into production of an idea it usually goes like this... 1. do all you prototype testing with PICAXE/BASIC... then when you want to go into production either learn or hire a programmer to make the functionality you want to be done on a PIC chip using ASSEMBLER or C etc.

note that there are SOME functions of the PIC chip which you cannot make use of in PICAXE - ie there are no PICAXE BASIC commands to use the underlying PIC function - from memory one of those functions was to make use of the in built watchdog timer that is in the PIC chip.. not really a big deal.. just something to be aware of.
 

DerStrom8

Joined Feb 20, 2011
2,390
I started with a PIC18F1330, which has turned out to be a fine beginner's chip. I use the C18 lite compiler (free download from Microchip) and MPLAB (also a free download). You can find lots of C tutorials out there on the web. One of the best ones I've come across is this one:

http://www.howardthehumble.com/Programming.htm

That would be a good place to start.
I hope this helps!
Regards,
Der Strom
 

Markd77

Joined Sep 7, 2009
2,806
I just learned from the datasheet and looking at code examples from Microchip and other peoples projects.
The most important bits are the sections on ports, the status and option registers and the instruction set.

I don't think assembler is very complicated and easier to learn than C, but opinions vary. It's a whole long debate that's been done a thousand times already.

I bashed this together, I haven't fully tested it but I think it is nearly working, it takes care of button debouncing in a basic way. Have a look through, look up in the datasheet anything you don't understand, and ask questions if that doesn't help.
I have probably made a few stupid mistakes somewhere.

<ed> Just thought of the first one :) Instead of doing what you wanted, each button press changes it in the sequence, centre, left, centre, right, etc. It doesen't return to centre when the button is released. Easy to fix later though. </ed>

The button is on GP0 to ground and the servo control pin is GP2


Rich (BB code):
    list    p=10F200
    radix    hex

        #include <P10F200.INC>

    __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF



    cblock 0x10        ;variable declarations
        d1
        d2
        flags
    endc

;flags bits
button_pressed equ 0
centre equ 1
left equ 2
right equ 3

;    
;----------------------------------------------------------------------

;**********************************************************************
    ;ORG     0x1FF             ; processor reset vector
;RESET CODE 0x1FF

; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.

    ORG     0x000             ; coding begins here
    movwf   OSCCAL            ; update register with factory cal value 



    CLRF GPIO                 ; Initialize GPIO by
                            ; clearing output
                            ; data latches
    MOVLW B'00001001'         ; Value used to
                            ; initialize data
                            ; direction
    TRIS GPIO             ; Set mostly outputs
    movlw B'10000000'        ;weak pullups on, wake on change off
    OPTION

    movlw b'00000110'        ;centre and left flags set
    movwf flags


main
    btfss flags, centre
    goto test_left_or_right
    call servo_centre
    goto check_button            ;don't test other flags because they
                            ;refer to previous position

test_left_or_right
    btfsc flags, left
    call servo_left
    btfsc flags, right
    call servo_right

check_button
    btfsc flags, button_pressed
    goto wait_for_release
    
    
    btfsc GPIO, 0    ;back to main if no new button press
    goto main

;process new button press
    bsf flags, button_pressed
    btfsc flags, centre
    goto previously_centre

;was left or right, move to centre, keep left or right flag
;for future reference
    bsf flags, centre
    goto main

previously_centre
    btfsc flags, left
    goto previously_left

;was right last time
    bcf flags, centre
    bcf flags, right
    bsf flags, left
    goto main

previously_left
    bcf flags, centre
    bcf flags, left
    bsf flags, right
    goto main




wait_for_release
    btfss GPIO, 0
    goto main        ;back to main if button still pressed
    
    bcf flags, button_pressed




    goto main

servo_centre
    movlw b'00000100'
    movwf GPIO
    call Delay_half_ms
    call Delay_half_ms
    call Delay_half_ms
    clrw
    movwf GPIO
    call Delay_half_ms
    call Delay_18_ms
    btfsc GPIO, 0
    retlw 0x00


servo_left
    movlw b'00000100'
    movwf GPIO
    call Delay_half_ms
    call Delay_half_ms
    clrw
    movwf GPIO
    call Delay_half_ms
    call Delay_half_ms
    call Delay_18_ms
    btfsc GPIO, 0
    retlw 0x00


servo_right
    movlw b'00000100'
    movwf GPIO
    call Delay_half_ms
    call Delay_half_ms
    call Delay_half_ms
    call Delay_half_ms
    clrw
    movwf GPIO
    call Delay_18_ms
    btfsc GPIO, 0
    retlw 0x00



Delay_half_ms

; Delay = 0.0005 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.0005 sec cycles
; Error = 0 %
; Generated by http://www.piclist.com/cgi-bin/delay.exe (December 7, 2005 version)
; Sun Oct 02 11:25:17 2011 GMT

; See also various delay routines at http://www.piclist.com/techref/microchip/delays.htm 

            ;496 cycles
    movlw    0xA5
    movwf    d1
Delay_half_ms_0
    decfsz    d1, f
    goto    Delay_half_ms_0

            ;4 cycles (including call)
    retlw 0x00






Delay_18_ms
; Delay = 0.0018 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.0018 sec cycles
; Error = 0 %

            ;1793 cycles
    movlw    0x66
    movwf    d1
    movlw    0x02
    movwf    d2
Delay_18_ms_0
    decfsz    d1, f
    goto    $+2
    decfsz    d2, f
    goto    Delay_18_ms_0

            ;3 cycles
    goto    $+1
    nop

            ;4 cycles (including call)
    retlw 0x00






    END
 
Last edited:

ajm113

Joined Feb 19, 2011
174
If you choose PIC, you could use Great Cow Basic: It's free, and its VERY user friendly, heck you don't even have to touch code if you dont want to. http://gcbasic.sourceforge.net/

Works on all PIC programmers and I've found it a great intro into microcontrollers. :)

The program is sorta buggy here and there, I'm kinda ready to jump into it's source code to do tune ups just for the heck of it, but anyways go with what you think will work for you.

But if you want something that has lots of community support, source codes and addons to make life much easier, so your not engineering everything yourself go with the Arduino. :)

I didn't use mine too much, because of my own little reasons. :p
 

Thread Starter

stanman11

Joined Nov 23, 2010
228
Thanks for the information guys. Coding seems hard. Guess I should just take it slow and not rush myself to learn
 

hgmjr

Joined Jan 28, 2005
9,027
One technique that may help you organize your programming is called a "finite state machine" . This tutorial even uses the Arduino.

Don't let the complex sounding name fool you. It is a very good/easy way to organize your program into easy to debug steps.

hgmjr
 
Top