PDA

View Full Version : ASM for dummies



Squibcakes
- 12th July 2004, 01:55
Hey,

Does any body have or know of a good place to start learning basic ASM programming?

Cheers
J

Melanie
- 12th July 2004, 04:40
MPLAB... it's free... free is better than Happy Hour...

pomidor
- 12th July 2004, 20:57
WinPicProg PIC Tutorial
http://www.winpicprog.co.uk/pic_tutorial.htm

Squibcakes
- 31st August 2004, 01:16
Mel,

I downloaded MPLAB.. now I know why it is free...

You need a rocket science degree just to use it! The help files are pretty bad. In fact I followed the tutorial step by step and kept getting compile errors.

I was thinking more along the lines of very basic ASM tutorials for starters like; how to make an LED Flash and progressing from there.

All the ASM examples I have seen so far on the net are too complex, all though Pomidors site does help...


Cheers
J

Melanie
- 31st August 2004, 03:37
What PIC are you playing with for a blinky LED example?

Squibcakes
- 31st August 2004, 05:12
Hi

16F870.

Cheers
J

Melanie
- 2nd September 2004, 10:10
It took a little while to get the time together for a proper reply... geeze... I'd forgotten what a pain it is to program in Assembler and remember all the little housekeeping chores that you never need to do in PICBasic...

Anyhow... here's the PICBasic code to flash your LED... I've inclued it as reference to show how easy life is...



'
' PIC Defines
' -----------
@ DEVICE pic16F870, XT_OSC
' System Clock Options
@ DEVICE pic16F870, WDT_ON
' Watchdog Timer
@ DEVICE pic16F870, PWRT_ON
' Power-On Timer
@ DEVICE pic16F870, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F870, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F870, CPD_OFF
' Data Memory Code Protect
@ DEVICE pic16F870, PROTECT_OFF
' Program Code Protection
@ DEVICE pic16F870, WRT_OFF
' Flash Memory Word Enable
@ DEVICE pic16F870, DEBUG_OFF
' Debug

'
' Hardware Defines
' ----------------
LED var PortB.0

'
' Initialse Hardware
' ------------------
PORTB=0
TRISB=0

'
' Main Program Loop
' -----------------
Loop:
Low LED
Pause 500
High LED
Pause 500
Goto Loop

end


Now here's the Assembler... you should be able to simply cut & paste and Assemble with MPASM directly.... Note: This is NOT an exact and direct equivallent to the PICBasic code... for one thing the Delay Loop is fixed at 500mS (wild approximation at best for a very simple cascaded delay routine), and for another the delay code is only good for 4MHz.



; Blinky.ASM
; ==========
; A dinky program to Blink LED on RB0
; Includes Header for 16F870
; (c) Melanie 2-Sept-2004
; download the PICMicro Mid-Range Refernce Manual
; for full command details

;
; PIC Defines
; -----------

list P=PIC16F870 ; Tells MPASM the target processor

include "p16f870.inc"
; without this statement you'd need to define the
; addresses of EVERY register you're going to use
; BEFORE you use it

__config _XT_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_ON & _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF

; Tells MPASM our Configuration Fuse Definitions
; go look at the file P16F870.INC in your MPASM Directory

;
; Hardware Defines (what's where in the real world)
; -------------------------------------------------

#DEFINE LED PORTB,0 ; Neat way of defining what's on a pin
; You can only use references like PORTB when you have
; previously defined them... that's been neatly done
; for you in the above include file

;
; Software Defines (what's where in our virtual world)
; ----------------------------------------------------

COUNTERA equ 20h ; 8-bit Byte (RAM) see PIC16F870 datasheet Figure 2-2
COUNTERB equ 21h
COUNTERC equ 22h

;
; Program always Starts executing at the Reset Vector
; ---------------------------------------------------
org 000h ; PIC Starts execution here
goto Start

;
; Actual Application Code Starts here (beyond the Vector Area)
; ------------------------------------------------------------

org 005h ; See PIC16F870 Datasheet Figure 2-1

;
; Subroutines get dumped first
; ============================

;
; Subroutine Kills 500mS (assumes 4MHz Clock)
; -------------------------------------------
; 500mS=(4uS Loop * 250 * 100 * 5)
;
Delay
movlw 0FAh ; Load COUNTERA with decimal 250
movwf COUNTERA
movlw 064h ; Load COUNTERB with decimal 100
movwf COUNTERB
movlw 005h ; Load COUNTERC with decimal 5
movwf COUNTERC
DelayA
CLRWDT ; DelayA gives 4uS Loop and keeps Watchdog chained up
decfsz COUNTERA,1 ; with COUNTERA preloaded with 0FAh (Decimal 250)
Goto DelayA ; this Loop Kills 1ms (4uS * 250)

decfsz COUNTERB,1 ; with COUNTERB preloaded with 064h (Decimal 100)
Goto DelayA ; this loop kills 100ms (1ms * 100)

decfsz COUNTERC,1 ; with COUNTERC preloaded with 005h (Decimal 5)
Goto DelayA ; this loop kills 500mS (100mS * 5)
return

;
; Actual Program Begins Here
; ==========================

Start
;
; Initialise Registers
; --------------------
clrf PORTB ; Zero PORTB
bsf STATUS,RP0 ; Switch to Bank 1
clrf TRISB ; Set PORTB all Outputs

; MPASM will report a reminder Warning here, because TRISB is in Bank1
; but we've handled it by Bank-Switching so we can ignore it.
; You just HAD to pick a PIC (16F870) with an ERROR in the datasheet...
; The RP Table in section 2.2 is CORRECT, however the RP bits (bits 6:5)
; in the STATUS Register description on DS30569B-Page-16 are SCREWED!

bcf STATUS,RP0 ; Return to Bank 0
;
; Main Program Loop
; -----------------
Loop
bcf LED ; Drive LED Low
call Delay ; Timeout
bsf LED ; Drive LED High
call Delay ; Timeout
Goto Loop ; do it till Doomsday

end



Yes, I did check it all worked before posting the code here... Enjoy...

Melanie

carl_schell
- 3rd September 2004, 06:47
Squibcakes -

Hi. I found a book that I consider to be one of the best availible sources to beginners in ASM programming. It is:

PIC In Practice by: David W. Smith

a quick search at amazon.com should lead you to this book

He does an outstanding job of taking you from knowing absolutely nothing about asm and pics to feeling like you could easily write lengthy programs in assembly. He has many projects in the book, such as LCD, A to D, EEPROM, Receivers and Transmitters and of course the blinky program. He uses 5 different pics to demonstrate with and gives very good directions, tips, ideas and encouragement.

His book is actually what got me interested in PICs to begin with. It is a 240 page book that you will read cover to cover in 1 month...you will become addicted to it. It is very worth the $26 dollars I paid for it! However, in time you will probably drift back to pbp. But I think you will get a lot out of this book and learning a bit about assembly...kinda makes pbp easier to picture what is going on inside. Anyhow, enough of my speach...Best of luck to you

Carl

------------------------------------------------------
PIC Development Boards with Bootloader!

www.schellelectronics.com
------------------------------------------------------

i_am_tye
- 18th February 2007, 00:32
hi i used the code form this page and adapted it, thank you,
it uses all 8 bits of portb and 2 bits of portc, the 2 bits of port c are used to multiplex 2 sets of 8 leds, so ya get 16 outputs. 3 shown here. what i want to know how could i impliment a lookup table for setting the data in port b and port c. i have shown 3 patterns in this code , but has 16, long code.
please help driving me nuts, thought it may also help others who got to this section




list P=PIC16F870 ; Tells MPASM the target processor

include "p16f870.inc"

__config _XT_OSC & _WDT_ON & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_ON & _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF

; Tells MPASM our Configuration Fuse Definitions
;
STATUS EQU 3h ;Define lable STATUS to 3
PORTB EQU 6h ;Define lable PORTB to 6 (Bank 0)
TRISB EQU 86h ;Define lable TRISB to 6 (Bank 1)
PORTC EQU 7h ;Define lable PORTB to 6 (Bank 0)
TRISC EQU 87h ;Define lable TRISB to 6 (Bank 1)
;

COUNT_a EQU 20h ;Define lable COUNT_1 to RAM adress 20h
COUNT_b EQU 21h ;Define lable COUNT_2 to RAM adress 21h
DELAY EQU 22H

;
STARTADR: ORG 000h ;Point to adress 000h which is the start adress
GOTO START ;Jump over the fist adresses and to START
;
ORG 010h ;Point to adress 010h.
START: MOVLW b'00100000' ;Load W register so bank 1 will be set (see statusreg in datasheets)
MOVWF STATUS ;Move W into STATUS and now Bank 1 will be selected
MOVLW b'00000000' ;Load W with data direction for TRISB 1=input 0=output (binary for lights)
MOVWF TRISB ;Move W into TRISB and RB0 will be an output(all outputs hehe =00000000)
;extra bits i added for setting trisc
MOVLW b'11001111' ;Load W with data direction for TRISC 1=input 0=output (binary for lights)rc4 and rc5 as outputs
MOVWF TRISC ;Move W into TRISC and RB0 will be an output(all outputs hehe =00000000)
;
MOVLW b'00000000' ;Load W register so bank 0 will be set (see statusreg in datasheets)
MOVWF STATUS ;Move W into STATUS and now Bank 0 will be selected
;

BACK:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1st set led is 10000000 00000000
;port b is the pattern and port c is the bank
;
MOVLW b'00000001' ;load pattern into W
CALL ledbankleft ;load pattern into portb and select right led bank, ie port rc04

MOVLW b'00000000' ;
CALL ledbankright ; select right led bank, ie port rc05
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;
;2nd set led is 110000000 00000000

MOVLW b'00000011' ;
CALL ledbankleft

MOVLW b'00000000' ;
CALL ledbankright
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;
;3nd set led is 11100000 00000000

MOVLW b'00000111' ;
CALL ledbankleft

MOVLW b'00000000' ;
CALL ledbankright
GOTO BACK

loop:
MOVLW 07fh ;Load W with Loop time
; MOVLW DELAY
MOVWF COUNT_a ;Move W to RAM adress
;
NEXT_a: MOVLW 07fh ;Load W with Loop time
MOVWF COUNT_b ;Move W to RAM adress
NEXT_b: DECFSZ COUNT_b,F ;Decrease COUNT_2 and execute next instruction if result not zero.
GOTO NEXT_b ;
;
DECFSZ COUNT_a,F ;Decrease COUNT_1 and execute next instruction if result not zero.
GOTO NEXT_a ;
;2ndloop
MOVLW 07fh ;Load W with Loop time
MOVWF COUNT_a ;Move W to RAM adress

NEXT_c: MOVLW 07fh ;Load W with Loop time
MOVWF COUNT_b ;Move W to RAM adress
NEXT_d: DECFSZ COUNT_b,F ;Decrease COUNT_2 and execute next instruction if result not zero.
GOTO NEXT_d ;
;
DECFSZ COUNT_a,F ;Decrease COUNT_1 and execute next instruction if result not zero.
GOTO NEXT_c ;
RETURN

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;
ledbankright: MOVWF PORTB
MOVLW b'00100000' ;select led bank 1
MOVWF PORTC ;Move W to port B and now RC5 will go Hi.
CALL loop
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;
ledbankleft: MOVWF PORTB
MOVLW b'00010000' ;select led bank 1
MOVWF PORTC ;Move W to port B and now RC5 will go Hi.
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;
END

dw_picbasic
- 28th February 2007, 06:08
Check out this PIC simulator software.
Its around $40 U.S. and well worth it!!!
The simulator is really awesome.
If you know what having an oscilloscope does for you in terms of seeing whats going on inside a circuit. You'l find the same thing with this simulator.

Its one of the best tools for playing with assembly I've found.

InstPIC Simulator IDE official web site is on the following address:
http://www.oshonsoft.com/alls and is up and running in a heart-beat.