Beginner Programming Microcontrollers

Discussion in 'Electronics Resources' started by stanman11, Oct 1, 2011.

  1. stanman11

    Thread Starter Member

    Nov 23, 2010
    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
  2. elec_mech

    Senior Member

    Nov 12, 2008
    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: - 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:

    C - very popular, but learning curve takes some time - not as bad as assembly though. MikroElektronika offers a C compiler as does HiTech ( and CSS (

    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:

    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!
  3. stanman11

    Thread Starter Member

    Nov 23, 2010
    whats a compiler?
  4. axeman22

    Active Member

    Jun 8, 2009
    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.

    - 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.
  5. DerStrom8

    Well-Known Member

    Feb 20, 2011
    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:

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

    Senior Member

    Sep 7, 2009
    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

    Code ( (Unknown Language)):
    1.     list    p=10F200
    2.     radix    hex
    4.         #include <P10F200.INC>
    6.     __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF
    10.     cblock 0x10        ;variable declarations
    11.         d1
    12.         d2
    13.         flags
    14.     endc
    16. ;flags bits
    17. button_pressed equ 0
    18. centre equ 1
    19. left equ 2
    20. right equ 3
    22. ;    
    23. ;----------------------------------------------------------------------
    25. ;**********************************************************************
    26.     ;ORG     0x1FF             ; processor reset vector
    27. ;RESET CODE 0x1FF
    29. ; Internal RC calibration value is placed at location 0x1FF by Microchip
    30. ; as a movlw k, where the k is a literal value.
    32.     ORG     0x000             ; coding begins here
    33.     movwf   OSCCAL            ; update register with factory cal value
    37.     CLRF GPIO                 ; Initialize GPIO by
    38.                             ; clearing output
    39.                             ; data latches
    40.     MOVLW B'00001001'         ; Value used to
    41.                             ; initialize data
    42.                             ; direction
    43.     TRIS GPIO             ; Set mostly outputs
    44.     movlw B'10000000'        ;weak pullups on, wake on change off
    45.     OPTION
    47.     movlw b'00000110'        ;centre and left flags set
    48.     movwf flags
    51. main
    52.     btfss flags, centre
    53.     goto test_left_or_right
    54.     call servo_centre
    55.     goto check_button            ;don't test other flags because they
    56.                             ;refer to previous position
    58. test_left_or_right
    59.     btfsc flags, left
    60.     call servo_left
    61.     btfsc flags, right
    62.     call servo_right
    64. check_button
    65.     btfsc flags, button_pressed
    66.     goto wait_for_release
    69.     btfsc GPIO, 0    ;back to main if no new button press
    70.     goto main
    72. ;process new button press
    73.     bsf flags, button_pressed
    74.     btfsc flags, centre
    75.     goto previously_centre
    77. ;was left or right, move to centre, keep left or right flag
    78. ;for future reference
    79.     bsf flags, centre
    80.     goto main
    82. previously_centre
    83.     btfsc flags, left
    84.     goto previously_left
    86. ;was right last time
    87.     bcf flags, centre
    88.     bcf flags, right
    89.     bsf flags, left
    90.     goto main
    92. previously_left
    93.     bcf flags, centre
    94.     bcf flags, left
    95.     bsf flags, right
    96.     goto main
    101. wait_for_release
    102.     btfss GPIO, 0
    103.     goto main        ;back to main if button still pressed
    105.     bcf flags, button_pressed
    110.     goto main
    112. servo_centre
    113.     movlw b'00000100'
    114.     movwf GPIO
    115.     call Delay_half_ms
    116.     call Delay_half_ms
    117.     call Delay_half_ms
    118.     clrw
    119.     movwf GPIO
    120.     call Delay_half_ms
    121.     call Delay_18_ms
    122.     btfsc GPIO, 0
    123.     retlw 0x00
    126. servo_left
    127.     movlw b'00000100'
    128.     movwf GPIO
    129.     call Delay_half_ms
    130.     call Delay_half_ms
    131.     clrw
    132.     movwf GPIO
    133.     call Delay_half_ms
    134.     call Delay_half_ms
    135.     call Delay_18_ms
    136.     btfsc GPIO, 0
    137.     retlw 0x00
    140. servo_right
    141.     movlw b'00000100'
    142.     movwf GPIO
    143.     call Delay_half_ms
    144.     call Delay_half_ms
    145.     call Delay_half_ms
    146.     call Delay_half_ms
    147.     clrw
    148.     movwf GPIO
    149.     call Delay_18_ms
    150.     btfsc GPIO, 0
    151.     retlw 0x00
    155. Delay_half_ms
    157. ; Delay = 0.0005 seconds
    158. ; Clock frequency = 4 MHz
    160. ; Actual delay = 0.0005 sec cycles
    161. ; Error = 0 %
    162. ; Generated by (December 7, 2005 version)
    163. ; Sun Oct 02 11:25:17 2011 GMT
    165. ; See also various delay routines at
    167.             ;496 cycles
    168.     movlw    0xA5
    169.     movwf    d1
    170. Delay_half_ms_0
    171.     decfsz    d1, f
    172.     goto    Delay_half_ms_0
    174.             ;4 cycles (including call)
    175.     retlw 0x00
    182. Delay_18_ms
    183. ; Delay = 0.0018 seconds
    184. ; Clock frequency = 4 MHz
    186. ; Actual delay = 0.0018 sec cycles
    187. ; Error = 0 %
    189.             ;1793 cycles
    190.     movlw    0x66
    191.     movwf    d1
    192.     movlw    0x02
    193.     movwf    d2
    194. Delay_18_ms_0
    195.     decfsz    d1, f
    196.     goto    $+2
    197.     decfsz    d2, f
    198.     goto    Delay_18_ms_0
    200.             ;3 cycles
    201.     goto    $+1
    202.     nop
    204.             ;4 cycles (including call)
    205.     retlw 0x00
    212.     END
    Last edited: Oct 2, 2011
  7. ajm113


    Feb 19, 2011
    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.

    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
  8. stanman11

    Thread Starter Member

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

    Retired Moderator

    Jan 28, 2005
    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.