flat assembler
Message board for the users of flat assembler.

Index > Programming Language Design > [FASMG] PIC 14 bits

Author
Thread Post new topic Reply to topic
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 01 Oct 2015, 22:03
Here the macros and definitions for PIC 14 bits programming.
Code:
;PIC 14bits instruction set
;
;ADDWF   f, d    Add W and f                     1       00 0111 dfff ffff  C,DC,Z
;ANDWF   f, d    AND W with f                    1       00 0101 dfff ffff  Z
;CLRF    f       Clear f                         1       00 0001 1fff ffff  Z
;CLRW    -       Clear W                         1       00 0001 0xxx xxxx  Z
;COMF    f, d    Complement f                    1       00 1001 dfff ffff  Z
;DECF    f, d    Decrement f                     1       00 0011 dfff ffff  Z
;DECFSZ  f, d    Decrement f, Skip if 0          1(2)    00 1011 dfff ffff
;INCF    f, d    Increment f                     1       00 1010 dfff ffff  Z
;INCFSZ  f, d    Increment f, Skip if 0          1(2)    00 1111 dfff ffff
;IORWF   f, d    Inclusive OR W with f           1       00 0100 dfff ffff  Z
;MOVF    f, d    Move f                          1       00 1000 dfff ffff  Z
;MOVWF   f       Move W to f                     1       00 0000 1fff ffff
;NOP     -       No Operation                    1       00 0000 0xx0 0000
;RLF     f, d    Rotate Left f through Carry     1       00 1101 dfff ffff  C
;RRF     f, d    Rotate Right f through Carry    1       00 1100 dfff ffff  C
;SUBWF   f, d    Subtract W from f               1       00 0010 dfff ffff  C,DC,Z
;SWAPF   f, d    Swap nibbles in f               1       00 1110 dfff ffff
;XORWF   f, d    Exclusive OR W with f           1       00 0110 dfff ffff  Z
;
;
;BCF     f, b    Bit Clear f                     1       01 00bb bfff ffff
;BSF     f, b    Bit Set f                       1       01 01bb bfff ffff
;BTFSC   f, b    Bit Test f, Skip if Clear       1 (2)   01 10bb bfff ffff
;BTFSS   f, b    Bit Test f, Skip if Set         1 (2)   01 11bb bfff ffff
;
;
;ADDLW   k       Add literal and W               1       11 111x kkkk kkkk  C,DC,Z
;ANDLW   k       AND literal with W              1       11 1001 kkkk kkkk  Z
;CALL    a       Call subroutine                 2       10 0aaa aaaa aaaa
;CLRWDT  -       Clear Watchdog Timer            1       00 0000 0110 0100  TO,PD
;GOTO    a       Go to address                   2       10 1aaa aaaa aaaa
;IORLW   k       Inclusive OR literal with W     1       11 1000 kkkk kkkk  Z
;MOVLW   k       Move literal to W               1       11 00xx kkkk kkkk
;RETFIE  -       Return from interrupt           2       00 0000 0000 1001
;RETLW   k       Return with literal in W        2       11 01xx kkkk kkkk
;RETURN  -       Return from Subroutine          2       00 0000 0000 1000
;SLEEP   -       Go into Standby mode            1       00 0000 0110 0011  TO,PD
;SUBLW   k       Subtract W from literal         1       11 110x kkkk kkkk  C,DC,Z
;XORLW   k       Exclusive OR literal with W     1       11 1010 kkkk kkkk  Z
; f = register adress, 7 bits
; d = destination, 1=reg, 0=W, 1 bit
; b = bit adress, 3 bits
; k = immediate value, 8 bits
; a = code adress, 11 bits

REG?=1
W?=0
NONE = 0ffff'ffffh

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; PIC12F675 registers definitions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
INDF?    = 00h
TMR0?    = 01h
PCL?     = 02h
STATUS?  = 03h
rp0?     = 5
c?       = 0
dc?      = 1
z?       = 2
FSR?     = 04h
GPIO?    = 05h
PORTA?   = GPIO
PORTB?   = 06h
EEDATA?  = 08h
EEADR?   = 09h
PCLATH?  = 0ah
INTCON?  = 0bh
PIR1?    = 0ch
TMR1L?   = 0eh
TMR1H?   = 0fh
T1CON?   = 10h
CMCON?   = 19h
ADRESH?  = 1eh
ADCON0?  = 1fh
al?      = 20h
ah?      = 21h
bl?      = 22h
bh?      = 23h
cl?      = 24h
ch?      = 25h
dl?      = 26h
dh?      = 27h
el?      = 28h
eh?      = 29h
fl?      = 2Ah
fh?      = 2Bh
gl?      = 2Ch
gh?      = 2Dh
hl?      = 2Eh
hh?      = 2Fh

INDF1?   = 80h
OPTION   = 81h
PCL?     = 82h
STATUS?  = 83h
FSR?     = 84h
TRISIO?  = 85h
input?   = 1
output?  = 0
PCLATH?  = 8ah
INTCON?  = 8bh
PIE1?    = 8ch
PCON?    = 8eh
OSCCAL?  = 90h
WPU?     = 95h
IOC?     = 96h
VRCON?   = 99h
EEDATA?  = 9ah
EEADR?   = 9bh
EECON1?  = 9ch
EECON21? = 9dh
ADRESL?  = 9eh
ANSEL?   = 9fh

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;configuration bits
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LP      = 0
XT      = 1
HS      = 2
CLKin   = 3
INTGP4  = 4
INTout  = 5
RCGP4   = 6
RCout   = 7

WDT     = 8
PWRToff = 10h
MCLR    = 20h
BOD     = 40h
CPoff   = 80h
CPDoff  = 100h

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;used to declare instructions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro inst value
      db (value and 3f00h) shr 8,value and 0ffh
end macro

macro Kinst opcode,k
      inst opcode or (k and 0ffh)
end macro

macro Finst opcode,f,d
  if d = NONE
      inst opcode or (f and 7fh)
  else
      inst opcode or (f and 7fh) or ((d and 1) shl 7)
  end if
end macro

macro Binst opcode,f,b
      inst opcode or (f and 7fh) or ((b and 7) shl 7)
end macro

macro Ainst opcode,a
      inst opcode or ((a/2) and 7ffh)
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;used to align code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro calign n
      align n*2
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;register operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro addwf? f,d
      Finst 0700h,f,d
end macro

macro andwf? f,d
      Finst 0500h,f,d
end macro

macro clrf? f
      Finst 0180h,f,NONE
end macro

macro clrw?
      inst 0100h
end macro

macro comf? f,d
      Finst 0900h,f,d
end macro

macro decf? f,d
      Finst 0300h,f,d
end macro

macro decfsz? f,d
      Finst 0b00h,f,d
end macro

macro incf? f,d
      Finst 0a00h,f,d
end macro

macro incfsz? f,d
      Finst 0f00h,f,d
end macro

macro iorwf? f,d
      Finst 0400h,f,d
end macro

macro movf? f,d
      Finst 0800h,f,d
end macro

macro movwf? f
      Finst 0080h,f,NONE
end macro

macro nop?
      inst 0000h ;20h, 40h, 60h
end macro

macro rlf? f,d
      Finst 0d00h,f,d
end macro

macro rrf? f,d
      Finst 0c00h,f,d
end macro

macro subwf? f,d
      Finst 0200h,f,d
end macro

macro swapf? f,d
      Finst 0e00h,f,d
end macro

macro xorwf? f,d
      Finst 0600h,f,d
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;bit operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro bcf? f,b
      Binst 1000h,f,b
end macro

macro bsf? f,b
      Binst 1400h,f,b
end macro

macro btfsc? f,b
      Binst 1800h,f,b
end macro

macro btfss? f,b
      Binst 1c00h,f,b
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;literal operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro addlw? k
      Kinst 3e00h,k
end macro

macro andlw? k
      Kinst 3900h,k
end macro

macro call? a
      Ainst 2000h,a
end macro

macro clrwdt?
      inst 0064h
end macro

macro goto? a
      Ainst 2800h,a
end macro

macro iorlw? k
      Kinst 3800h,k
end macro

macro movlw? k
      Kinst 3000h,k
end macro

macro retfie?
      inst 0009h
end macro

macro retlw? k
      Kinst 3400h,k
end macro

macro return?
      inst 0008h
end macro

macro sleep?
      inst 0063h
end macro

macro sublw? k
      Kinst 3c00h,k
end macro

macro xorlw? k
      Kinst 3a00h,k
end macro

macro null?
      inst 3fffh
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;undocumented:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro option
      inst 0062h
end macro

macro tris? r
      inst 0060h or (r and 7) ;r={1,5,6,7
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;to format a HEX file for icprog:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro oscc? o
      org 7FEh
      retlw o
end macro

macro conf? c
      org 400Eh
      inst c
end macro

macro eeprom?
      org 4200h
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;complex instructions.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
macro gpout? o
      movlw o
      movwf GPIO
end macro

macro bank0?
      bcf STATUS,5
end macro

macro bank1?
      bsf STATUS,5
end macro

macro loop? r,@
      decfsz r,REG
      goto @
end macro

macro mov? d,s
      movf s,W
      movwf d
end macro

macro movb? d,s
      movlw s
      movwf d
end macro

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;output in HEX file format
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
include 'hex.inc'

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;your source should start after this line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org 0h

    

The examples will compile as .HEX files compatible with icprog.
Code:
;   use this conneciton driagram to test this code.   ;
;                                                     ;
;              vdd 5v    vss gnd                      ;
;                 |         |                         ;
;                 1|"""V"""|8                         ;
;       ----------2|P12F675|7---------------+----PGC  ;
;       |    -----3|       |6----PGD        |         ;
;       |    |    4|_______|5-------       R10k       ;
;       |    |    |                |        |         ;
;      R3k  R3K  VPP     vdd       |    [white led]   ;
;       |    |            |        v        |         ;
; [red and green led]     ---[trimmer 10k]--+         ;
;          |                                |         ;
;         GND                              GND        ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
include 'pic14.inc'

macro delai d
      movlw d
      call delay
end macro

green =0001'0000b
red   =0010'0000b
white =0000'0001b
black =0000'0000b

start:
        call gpinit
        call adinit
        call ledinit
        goto .a
.a:
        delai 10                 ;1=from 760Hz to 25540Hz
        gpout green+red+white
        delai 100                ;1=50%
        gpout black
        goto .a
;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay:
        call adsample
        movwf al
.a:
        mov ah,bl
.b:
        loop ah,.b
        loop al,.a
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
gpinit:
        movlw 1100'1110b
        tris GPIO
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
ledinit:
        gpout 0011'0001b
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
adinit:
        movb ADCON0,0000'1001b
        bank1
        movb ANSEL,0001'0100b
        bank0
        bsf ADCON0,1
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
adsample:
        btfsc ADCON0,1
        return
        incf ADRESH,W
        movwf bl
        bsf ADCON0,1
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        oscc 66h
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        conf (CPDoff + CPoff + PWRToff + INTGP4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        eeprom
        db ' h e l l o w o r l d'

    


Depending on your environment, you should execute this command to obtain the output file below.

Code:
 
Z:>fasmg blinkled.asm blinkled.hex
flat assembler g  version 0.90.1441575087
2 passes, 359 bytes.
    


The output file is like this:
Code:
:1000000016201C20192004280A300D2031308500CC
:1000100064300D200030850004282420A000220830
:10002000A100A10B1128A00B0F280800CE306500FD
:10003000080031308500080009309F008316143015
:100040009F0083129F1408009F1808001E0AA20038
:040050009F140800F1
:0207FE0066345F
:02400E0094011B
:10420000682065206C206C206F2077206F20722042
:044210006C2064209A
:00000001FF
    


It works very well in icprog, and i suppose it will be ok in any pic programming software that uses .HEX file format as binary records.

Later, there will be out of range managements (operands and program memory), and features specific to various PIC MCU models, like register definitions, memory mapping and limitation, extended instructions and so on.


Description: here comes the pic 14 bits folder to include in fasmg project.
Download
Filename: PIC14.zip
Filesize: 4.32 KB
Downloaded: 1371 Time(s)

Post 01 Oct 2015, 22:03
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 22 Nov 2017, 23:12
tested with a pickit2 and it's dedicated application from microchip on a 12f675, all ok.

just have in mind one thing when programming for an ADC equiped pic, digital inputs should be configured to don't be analog inputs or you'll loose many minutes to undestand what's goin on:lol:

i had good fun in a simple application to do. Wink

Code:
MagicMirror:
;   use this conneciton driagram to test this code.   ;
; 2 from elga switch
; 3 from magic wand controller
; 5 to fan
; 6 to front led
; 7 to back led


include 'pic14.inc'

elga  = 4
wand  = 5

back  = 0000'0001b
front = 0000'0010b
fan   = 0000'0100b

off   = 0

start:
        call gpinit
        goto .a
.a:
        btfss GPIO,elga
        goto .a
        gpout fan+front
.b:
        btfss GPIO,wand
        goto .b
        gpout fan+back

        mov bh,2
.c:
        call delay
        loop bh,.c
        gpout 0
        goto .a
;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay:
        mov al,0ffh
.a:
        mov ah,0ffh
.b:
        mov bl,0ffh
.c:
        loop bl,.c
        loop ah,.b
        loop al,.a
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
gpinit:
        bank1
        bcf ANSEL,3
        bcf OPTION,7
        movlw 1111'1000b
        movwf TRISIO
        movlw 0011'0000b
        movwf WPU
        bank0
        gpout 0
        return
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        oscc 66h
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        conf (CPDoff + CPoff + PWRToff + INTGP4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
        eeprom
        db ' h e l l o w o r l d'

    
Post 22 Nov 2017, 23:12
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.