Learning to program the PIC10LF322

Discussion in 'Embedded Systems and Microcontrollers' started by cmartinez, Jul 6, 2018.

  1. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    I'm completely unfamiliar with any of Microchip's products, but now it has become imperative that I work on a small project involving the PIC10LF322. As a reference, I consider myself quite proficient with the 8051 architecture, but the PIC architecture is alien to me. So I'd very much appreciate it if any of you had the patience of helping me walk through my first baby steps.

    I've already bought a few of said chips, and bought a PICkit 3 programmer for this application. My most important requirement is that I learn how to program this chip using assembly language, and not C.

    Specifically, I'd like to be pointed to:
    - Where can I download an assembly compiler for this chip? I've already downloaded the MPLAB X IDE software from MicroChip, and I haven't been able to find anything related to assembly language in it.
    - Where can I download a few samples in assembly for this chip? I need to learn and study its syntax and general rules, so some sort of manual or even book that I could buy or download would be appreciated.

    Of course, I've already studied the datasheet, and have begun to understand some of the concepts involving its different registers and interrupts. But I have no clue as to how and where to start programming and compiling assembly, and then loading the program into the chip.
  2. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    MPASM will go right into MPLAB X IDE (I actually thought it gets downloaded with the IDE). I'm sure you can find it, but when you create a project you will be asked what you want to use debugger/compiler and so on - see if it is there.

    I would suggest the Gooligum tutorials https://www.gooligum.com.au/PIC-Tutorials
    A few of these are free and likely to be a couple that you will appreciate as they are (still) a good get-your-feet-wet by doing something.
    cmartinez likes this.
  3. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    Thanks Ray! ... as a matter of fact, I found that the MPASM was installed along with the MPLAB software a few moments before I saw your post. Right now I'm studing the examples included in the software. But I'm not sure I like the MPLAB's user interface, I find it a bit overbearing and confusing ... same thing happened to me when I tried to learn how to use Atmel's software ... in the end, I used a text editor of my choice (programmer's notepad 2, which is an extremely friendly and simple to use editor) and ran the compiler from the command line.

    Thanks for linking me to the PIC tutorials, I'll have a look at them and let you know how it went.
  4. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    I both love it and hate it. But, as usual, you use it and get used to it and it works, pretty much, anyways. Then, you switch to something else and have to get used to it and switch back and so on and so forth. I felt the same way about Eclipse, and then got used to it. I can even get used to Visual Studio stuff. Still, there is pain in spending more time in figuring out how to use the program instead of using the program to write programs. I think the end result is that I get a little neophobic - I was starting to get into some PSoC Creator and after a while I just said...enough...Atmel Studio has been sitting on my desktop for a year and I have only flashed an LED. For all of its limitations, Arduino IDE took all of about 10 minutes to learn (or so it seems).
    absf and cmartinez like this.
  5. jpanhalt


    Jan 18, 2008
    I use MPLab 8.92. It is still available for download, but does not support the very newest chips. I did try on an early version of MPLabX and found it unusable for Assembly. I understand from @Ian Rogers and others that those earlier problems have been somewhat resolved, but I have not ventured out into that realm again.
    Can you be more specific? The instruction set for that chip is very similar to the other mid-range chips. It has a single FSR. If you can stand an 8-pin version in a small footprint, the enhanced mid-range chips (e.g., 12F1840) are superior. If you just want an extended example of the instruction set, I will attach a link. There is only one operand and a destination ('d')(i.e., F = a file, or W = working register). STATUS lets you keep track of carry (STATUS,0), zero (STATUS,2), and nibble carry (STATUS,1). It can be involved in bank select. EDIT: Chip has only one bank. Rest is General Purpose Registers (RAM). I would have to review that chip again to know that (never actually used that chip).
    What sort of thing would you like? I have some stuff for the 12F683 that is very similar. One advantage of the enhanced mid-range, besides having 2 FSR's, is automatic context saving and restoration. Doesn't save a lot of processor time, but it does save having to type it.


    Edit#2: MMcLaren has done a bit with that chip. Here is one example: http://picforum.ric323.com/viewtopic.php?t=8&p=21#p1112 As I recall, he has also done a backpack driver for an LCD despite having only 6 pins to use (See: Edit #3).
    Edit#3: Memory failed. It was a 12F6xx for the LCD driver directly or 2 pins from an MCU plus a counter (e.g., 74HC164). Unfortunately, the code for the latter was for an 18F PIC, not the 10F.
    Last edited: Jul 7, 2018
    cmartinez likes this.
  6. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    The arduino IDE is easy because it doesn't do much.
    MPLABX includes a simulator which allows you to single step through your program (C or assembler), view or change RAM or SFRs, and you can set up a file to simulate external hardware. You don't need the actual hardware to do some quite detailed debugging. Natuarlly all this takes some learning.
  7. Dodgydave

    AAC Fanatic!

    Jun 22, 2012
    I would start with the 12F675 or 609 pics, these are in the library samples in Mplab, it will give you an idea of how to set up ports for I/O and the main registers like Status, Gpio, Intosc, start by flashing a led on a port, and button pressing, then you can move onto your pic, here is a page for the 12f675 .


    Last edited: Jul 7, 2018
    cmartinez likes this.
  8. MaxHeadRoom


    Jul 18, 2013
    Also look at the free Gooligum tutorials, they start off with the 12F pin series in assembly, C later on.
    Edit: I see Gooligum already mentioned!
    I agree the 12F675 or for a few extras, the 12F1822/23 etc.
    Also I much prefer the MPLAB IDE for assembley rather than the X ver.
    The IDE has a simulator also.
    Last edited: Jul 7, 2018
  9. MaxHeadRoom


    Jul 18, 2013
    BTW there is a 12F development board in ebay for $5.00.
    Comes with a 12F675.
    Includes ICP connector.
    Last edited: Jul 7, 2018
  10. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    Thank you all for your responses. I've already found a few samples specific of the 10LF322 and I'm currently studying them. My learning has been exponential thanks to my previous experience with the 8051.

    Anyway, I'm going to:
    1.- Turn a LED on and off using an output pin while pressing a pushbutton connected to an input pin (configured with a weak pull-up).
    2.- Flash said LED using a delay loop.
    3.- Flash the LED using a timer interrupt.
    4.- Flash the LED a number of times using a timer interrupt activated by an external interrupt.

    And then I'll decide what's next. I'll be posting my code (or snippets of it) in this thread to record my progress.

    Thanks again to all.
    absf and JohnInTX like this.
  11. MaxHeadRoom


    Jul 18, 2013
    The code in the Gooligum tutorials should transfer over easily , just watch the bankswitching and any difference in the particular registers used.
    Otherwise should be very compatible.
    cmartinez likes this.
  12. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    I doubt there will be any bank switching involved in my project. The 10LF322 has only one register bank, from what I gather. But it's good to know to watch out for that feature in other PICs if in the future I decide to use a more advanced model.
  13. MMcLaren

    Distinguished Member

    Feb 14, 2010
    I've only done a couple simple programs for 10F322. If it helps... here's one that displays the Arecibo message in Morse code on an LED at approximately 12-wpm. It demonstrates how to read a table of 14-bit 'words' from program memory.

    Good luck in your studies. Have fun.

    Cheerful regards, Mike

    Code (ASM):
    1. ;******************************************************************
    2. ;                                                                 *
    3. ;   Filename: 10F320 Arecibo.asm                                  *
    4. ;     Author: Mike McLaren, K8LH                                  *
    5. ;       Date: 19-Sep-2014                                         *
    6. ;                                                                 *
    7. ;   Send Arecibo message as continuous ~12-WPM stream of Morse    *
    8. ;   code on the active lo LED connected between RA2 and VDD.      *
    9. ;                                                                 *
    10. ;      MPLab: 8.92    (tabs = 8)                                  *
    11. ;      MPAsm: 5.51                                                *
    12. ;                                                                 *
    13. ;******************************************************************
    15.         list p=10f322, st=off
    16.         #include "p10f322.inc"
    17.         radix dec
    22. ;--< variables >---------------------------------------------------
    24.         cblock 0x40             ; 10F320 RAM (0x40..0x7F)
    26.         endc
    28. ;--< constants >---------------------------------------------------
    30. #define led PORTA,2             ; LED on RA2
    31. #define timer NCO1INCL          ; use a register instead of RAM
    32. #define bitctr NCO1INCH         ;  "
    34. ;******************************************************************
    35. ;  reset vector                                                   *
    36. ;******************************************************************
    37.         org 0x000
    38. v_reset
    40. ;******************************************************************
    41. ;  main init                                                      *
    42. ;******************************************************************
    44. init
    45.         clrf    OSCCON          ; INTOSC = ~31-kHz, Tcy ~129-us   |B0
    46. ;
    47. ;  setup PORTA
    48. ;
    49.         clrf    TRISA           ; RA3 input, RA2-RA0 outputs      |B0
    50.         clrf    ANSELA          ; analog off for digital I/O      |B0
    51.         bsf     led             ; turn active lo LED off          |B0
    53. ;******************************************************************
    54. ;  main loop                                                      *
    55. ;******************************************************************
    57. loop
    58.         movlw   low(arecibo)    ; prep memory address pointers    |B0
    59.         movwf   PMADRL          ;  "                              |B0
    60.         clrf    PMADRH          ;  "                              |B0
    61.         bcf     PMCON1,CFGS     ; prep to read program memory     |B0
    62.         call    wait2           ; message repeat delay            |B0
    63.         call    wait2           ;  "                              |B0
    64.         call    wait2           ;  "                              |B0
    65. nxtbyte
    66.         movlw   13              ;                                 |B0
    67.         movwf   bitctr          ; bitctr = 13                     |B0
    68.         bsf     PMCON1,RD       ; read 14-bit data from flash     |B0
    69.         nop                     ; required nop                    |B0
    70.         nop                     ; required nop                    |B0
    71. nxtbit
    72.         bcf     PORTA,2         ; turn active lo LED 'on'         |B0
    73.         btfsc   PMDATH,5        ; b13 = '0'? yes, skip, else      |B0
    74.         call    wait2           ; do ~99-ms * 3 for a '1' bit     |B0
    75.         call    wait1           ; do ~99-ms * 1 for a '0' bit     |B0
    76.         bsf     PORTA,2         ; turn active lo LED 'off'        |B0
    77.         call    wait1           ; do ~99-ms inter-bit space       |B0
    78.         rlf     PMDATL,F        ; shift 14-bit data left to get   |B0
    79.         rlf     PMDATH,F        ; next bit into b13 position      |B0
    80.         decfsz  bitctr,F        ; last bit? yes, skip, else       |B0
    81.         goto    nxtbit          ; branch (send another bit)       |B0
    82.         incfsz  PMADRL,F        ; last byte? yes, skip, else      |B0
    83.         goto    nxtbyte         ; branch (send next byte)         |B0
    84.         goto    loop            ; branch (done, start over)       |B0
    86. wait2
    87.         call    wait1           ;                                 |B0
    88. wait1
    89.         decfsz  timer,F         ; ~99-msecs                       |B0
    90.         goto    wait1           ;                                 |B0
    91.         return                  ;                                 |B0
    93. ;******************************************************************
    94. ;
    95. ;  1679 bits (very last bit in the table isn't used) organized
    96. ;  and displayed as 73 rows and 23 columns.  First and most sig-
    97. ;  nificant bit in table is the top left of image.
    98. ;
    99.         org 0x088
    100. arecibo
    101.         dw b'00000010101010', b'00000000000101', b'00000101000000'
    102.         dw b'01001000100010', b'00100101100101', b'01010101010101'
    103.         dw b'00100100000000', b'00000000000000', b'00000000000000'
    104.         dw b'01100000000000', b'00000000110100', b'00000000000000'
    105.         dw b'00011010000000', b'00000000000101', b'01000000000000'
    106.         dw b'00000011111000', b'00000000000000', b'00000000000000'
    107.         dw b'01100001110001', b'10000110001000', b'00000000001100'
    108.         dw b'10000110100011', b'00011000011010', b'11111011111011'
    109.         dw b'11101111100000', b'00000000000000', b'00000001000000'
    110.         dw b'00000000000100', b'00000000000000', b'00000000000010'
    111.         dw b'00000000000000', b'00111111000000', b'00000001111100'
    112.         dw b'00000000000000', b'00000001100001', b'10000111000110'
    113.         dw b'00100000001000', b'00000010000110', b'10000110001110'
    114.         dw b'01101011111011', b'11101111101111', b'10000000000000'
    115.         dw b'00000000000001', b'00000011000000', b'00010000000000'
    116.         dw b'01100000000000', b'00001000001100', b'00000000111111'
    117.         dw b'00000110000001', b'11110000000000', b'11000000000000'
    118.         dw b'01000000001000', b'00000100000100', b'00001100000001'
    119.         dw b'00000001100001', b'10000001000000', b'00001100010000'
    120.         dw b'11000000000000', b'00011001100000', b'00000000110001'
    121.         dw b'00001100000000', b'01100001100000', b'01000000010000'
    122.         dw b'00100000000100', b'00010000000110', b'00000001000100'
    123.         dw b'00000011000000', b'00100010000000', b'00100000001000'
    124.         dw b'00100000001000', b'00001000000010', b'00000000000110'
    125.         dw b'00000000110000', b'00001100000000', b'01000111010110'
    126.         dw b'00000000001000', b'00001000000000', b'00000100000111'
    127.         dw b'11000000000000', b'10000101110100', b'10110110000001'
    128.         dw b'00111001001111', b'11101110000111', b'00000110111000'
    129.         dw b'00000010100000', b'11101100100000', b'01010000011111'
    130.         dw b'10010000001010', b'00001100000010', b'00001101100000'
    131.         dw b'00000000000000', b'00000000000000', b'00111000001000'
    132.         dw b'00000000000111', b'01010001010101', b'01010011100000'
    133.         dw b'00001010101000', b'00000000000001', b'01000000000000'
    134.         dw b'00111110000000', b'00000000011111', b'11110000000000'
    135.         dw b'00111000000011', b'10000000001100', b'00000000011000'
    136.         dw b'00001101000000', b'00010110000011', b'00110000000110'
    137.         dw b'01100001000101', b'00000101000100', b'00100010010001'
    138.         dw b'00100010000000', b'01000101000100', b'00000000001000'
    139.         dw b'01000010000000', b'00000100000000', b'01000000000000'
    140.         dw b'00100101000000', b'00000111100111', b'11010011110000'
    142. ;******************************************************************
    143.         end
    Last edited: Jul 8, 2018
  14. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    Ok, here's the code for a "blinkie" very generoulsy posted by @OBW0549 in another thread:

    Code (Text):
    2. ;**********************************************************************
    3. ;                                                                     *
    4. ;   Basic 10F322 "blinky"-- blinks LED on Port A bit 2                *
    5. ;   Derived from 10F222TMPO.ASM                                       *
    6. ;                                                                     *
    7. ;**********************************************************************
    8.     list      p=10F322            ; list directive to define processor
    9.     #include <p10F322.inc>        ; processor specific variable definitions
    10. ;    Configure chip via IDE menu and ignore the following line:
    11. ;   __CONFIG   _MCLRE_ON & _CP_OFF & _WDT_OFF & _MCPU_OFF & _IOFSCS_4MHZ
    12. ;***** RAM VARIABLES
    13. TEMP_VAR    UDATA
    14. temp1       RES     1
    15. ;***** CODE
    16. MAIN    CODE    0x000
    17.     movlw   0x0F
    18.     movwf   LATA            ; preset all outputs to 0
    19.     clrf    ANSELA          ; disable analog inputs
    20.     movlw   0x00            ; set all bits as outputs
    21.     movwf   TRISA
    22. ;    movlw   0x0F            ; enable weak pullups on all inputs
    23. ;    movwf   WPUA
    24. ;    bcf     OPTION_REG, NOT_WPUEN   ; WPU master enable
    25.     clrf    temp1
    27.     movlw   0x10
    28.     movwf   OSCCON
    29.     nop
    30. mainloop
    31.     bsf     LATA, LATA2        ; turn the LED on
    32.     movlw   .250            ; set up 1/4 second delay
    33.     movwf   temp1
    35. delayloop1
    36.     nop
    37.     nop
    38.     nop
    39.     nop
    40.     nop
    41.     nop
    42.     nop
    43.     nop
    44.     nop
    45.     nop
    46.     nop
    47.     nop
    48.     nop
    49.     nop
    50.     nop
    51.     nop
    52.     decfsz  temp1
    53.     goto    delayloop1
    55.     bcf     LATA, LATA2        ; turn the LED off
    56.     movlw   .250            ; set up 1/4 second delay
    57.     movwf   temp1
    59. delayloop2
    60.     nop
    61.     nop
    62.     nop
    63.     nop
    64.     nop
    65.     nop
    66.     nop
    67.     nop
    68.     nop
    69.     nop
    70.     nop
    71.     nop
    72.     nop
    73.     nop
    74.     nop
    75.     nop
    76.     decfsz  temp1
    77.     goto    delayloop2
    79.     nop
    80.     goto    mainloop
    81.     END                       ; directive 'end of program'
    First, I'm trying to understand what the _CONFIG line is all about. Where can I find a list of the constants involved and their proper use?
  15. jpanhalt


    Jan 18, 2008
    The Configuration sets various MCU operating conditions, such as oscillator type, whether MCLR is enabled for reset (setting doesn't affection programming), watch dog timer ( a timer to reset if the program hangs) and so forth. It is best described by checking the datasheet and begin at page 19. It can be one word or several. Each bit is set to set one of the conditions. In the IDE, you can find settings when setting it up to program.

    Here is what the configure window looks like in MPLAB 8.92:


    You can click on the drop-down menus to change things. I have found just using the datasheet is easier, unless I am in the middle of something and just want to change something (like oscillator type) and can't remember the proper abbreviation.
    cmartinez likes this.
  16. Raymond Genovese

    Well-Known Member

    Mar 5, 2016

    There are options that can be programmed - see section 3.1, p20 of the data sheet. BTW, I only have experience with the 202-204 PIC10 series - this one looks better and I have to put it in my 'save for later' list.
    cmartinez likes this.
  17. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    Thanks John ... I've located the Configuration Word description in the datasheet, and the function of each of its bits. What has me confused is that I'm thinking about the Configuration Word as a register, and that it might be accessed through move instructions, or by simply reading and writing its bits individually, but I'm seeing that maybe that's not the case.
  18. MaxHeadRoom


    Jul 18, 2013
    See page 20 of the manual also the .INC file.

    Code (ASM):
    2. ;==========================================================================
    3. ;
    4. ;  RAM Definitions
    5. ;
    6. ;==========================================================================
    7.   __MAXRAM  H'007F'
    8.   __BADRAM  H'002F'
    10. ;==========================================================================
    11. ;
    12. ;  Configuration Bits
    13. ;
    14. ;  NAME  Address
    15. ;  CONFIG  2007h
    16. ;
    17. ;==========================================================================
    19. ; The following is an assignment of address values for all of the
    20. ; configuration registers for the purpose of table reads
    21. _CONFIG  EQU  H'2007'
    23. ;----- CONFIG Options --------------------------------------------------
    24. _FOSC_INTOSC  EQU  H'3FFE'  ; INTOSC oscillator: CLKIN function disabled
    25. _FOSC_EC  EQU  H'3FFF'  ; EC: CLKIN function enabled
    27. _BOREN_OFF  EQU  H'3FF9'  ; Brown-out Reset disabled
    28. _BOREN_SBODEN  EQU  H'3FFB'  ; Brown-out Reset controlled by the SBOREN bit in the BORCON register
    29. _BOREN_NSLEEP  EQU  H'3FFD'  ; Brown-out Reset enabled while running and disabled in Sleep
    30. _BOREN_ON  EQU  H'3FFF'  ; Brown-out Reset enabled
    32. _WDTE_OFF  EQU  H'3FE7'  ; WDT disabled
    33. _WDTE_SWDTEN  EQU  H'3FEF'  ; WDT controlled by the SWDTEN bit in the WDTCON register
    34. _WDTE_NSLEEP  EQU  H'3FF7'  ; WDT enabled while running and disabled in Sleep
    35. _WDTE_ON  EQU  H'3FFF'  ; WDT enabled
    37. _PWRTE_ON  EQU  H'3FDF'  ; PWRT enabled
    38. _PWRTE_OFF  EQU  H'3FFF'  ; PWRT disabled
    40. _MCLRE_OFF  EQU  H'3FBF'  ; MCLR pin function is digital input, MCLR internally tied to VDD
    41. _MCLRE_ON  EQU  H'3FFF'  ; MCLR pin function is MCLR
    43. _CP_ON  EQU  H'3F7F'  ; Program memory code protection is enabled
    44. _CP_OFF  EQU  H'3FFF'  ; Program memory code protection is disabled
    46. _LVP_OFF  EQU  H'3EFF'  ; High-voltage on MCLR/VPP must be used for programming
    47. _LVP_ON  EQU  H'3FFF'  ; Low-voltage programming enabled
    49. _LPBOR_OFF  EQU  H'3DFF'  ; BOR disabled
    50. _LPBOR_ON  EQU  H'3FFF'  ; BOR enabled
    52. _BORV_HI  EQU  H'3BFF'  ; Brown-out Reset Voltage (Vbor), high trip point selected.
    53. _BORV_27  EQU  H'3BFF'  ; Brown-out Reset Voltage (Vbor), high trip point selected.
    54. _BORV_LO  EQU  H'3FFF'  ; Brown-out Reset Voltage (Vbor), low trip point selected.
    55. _BORV_24  EQU  H'3FFF'  ; Brown-out Reset Voltage (Vbor), low trip point selected.
    57. _WRT_ALL  EQU  H'27FF'  ; 000h to 1FFh write protected, no addresses may be modified by PMCON control
    58. _WRT_HALF  EQU  H'2FFF'  ; 000h to 0FFh write protected, 100h to 1FFh may be modified by PMCON control
    59. _WRT_BOOT  EQU  H'37FF'  ; 000h to 07Fh write protected, 080h to 1FFh may be modified by PMCON control
    60. _WRT_OFF  EQU  H'3FFF'  ; Write protection off
    62. ;----- DEVID Equates --------------------------------------------------
    63. _DEVID1  EQU  H'2006'
    65. ;----- IDLOC Equates --------------------------------------------------
    66. _IDLOC0  EQU  H'2000'
    67. _IDLOC1  EQU  H'2001'
    68. _IDLOC2  EQU  H'2002'
    69. _IDLOC3  EQU  H'2003'
    71.   LIST
    Last edited: Jul 10, 2018
    absf likes this.
  19. cmartinez

    Thread Starter AAC Fanatic!

    Jan 17, 2007
    For instance, I can see that the internal oscillator can be configured through the OSCCON register, which is located in 10H, according to the memory map. But where the hell is the CONFIG register?
  20. jpanhalt


    Jan 18, 2008
    "__Config" (notice the 2 underscores) is an Assembler directive t set the bits for your. You can also just use the hex equivalent. I will work that out for you and post an edit. I have never tried doing it with inline code. It may not work.

    Edit: Those configuration bits must be Assembler specific. They are not recognized in MPLAB 8.92 nor the current datasheet. I have attached the .inc file for the configuration bits from MPLAB8.92.

    The biggest difference is the oscillator settings. Usually one declares the type (e.g, HS, INTOSC w/ or w/o CLKIN, etc.) in the configuration word, then in OPTION_REG you set the frequency.
    Last edited: Jul 10, 2018
    cmartinez likes this.