Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Who is the MASTER of PIC ASM?

Status
Not open for further replies.

be80be

Well-Known Member
Alharad I posted you 3 of them even how to make you a board that lets you run the code.

And the link to where to down load the lessons for the LPC but before you get your hopes up you don't have the same chips
that are on the LPC board so you'll have to change the code to match your chips
 

Alharad

New Member
Hello,

I will read some of the tutorials and make the board while I'm waiting for the PICs to come in the mail.

if i have questions, I will let you all know. :)

thank you!
 

Alharad

New Member
Awesome! a few PIC12f648's has come in the mail today, so I'm ready to get started.

I will let you know how my first program goes!!
 

Jon Wilder

Active Member
Wow, you explained that perfectly to me! You are very good at explaining assembly, maybe you should write a book :)


I'm still not sure where the PICkit2 lessons are, can someone post the pdf? I would like to go through all of them, thanks.

I sure can't wait till I can get started, this is going to be fun!!

Thanks! As far as writing a book I honestly don't think I'm quite qualified for that yet lol. I'm actually a beginner myself when it comes to microcontrollers, PICs and assembly language. However, I've been doing electronics for almost 20 years so I understood what the hardware did. I just needed to learn the language and figure out how to bridge the gap between hardware and software.

I dabble mainly in MIDI stuff as I am a guitarist who's been doing musical electronics for 13 years.

However, the one thing I caught onto in PIC asm is that the instructions are merely just abbreviations for an instructional sentence. Just like how I explained that "movwf" means "move contents of the w register into the file address/label that proceeds this instruction". Most if not all PIC instructions are merely just abbreviations for an instructional sentence. That made it easy to grasp PIC assembly for me.
 
Last edited:

Alharad

New Member
I'm not sure i understand this

https://www.mstracey.btinternet.co.uk/pictutorial/progtut2.htm

First, we need to switch from Bank 0 to Bank 1. We do this by setting the STATUS register, which is at address 03h, bit 5 to 1.

BSF 03h,5

The BSF Means Bit Set F. The letter F means that we are going to use a memory location, or register. We are using two numbers after this instruction – 03h, which is the STATUS register address, and the number 5 which corresponds to the bit number. So, what we are saying is “Set bit 5 in address 03h to 1”.

We are now in Bank 1.

so we set bit 5 of TRISB only to the bank 1? the other bits of TRISB are left in bank 0? That would be confusing to remember all of them!!! Am I looking at this wrong?
 
Last edited:

Jon Wilder

Active Member
so we set bit 5 of TRISB only to the bank 1? the other bits of TRISB are left in bank 0? That would be confusing to remember all of them!!! Am I looking at this wrong?

No you set bit 5 of the STATUS SFR, which resides at address 0x03, to switch banks. Once you set bit 5 of STATUS, you can then address all 8 bits of TRISA, TRISB, and all other SFR's that reside in bank 1. Then you must clear bit 5 in the STATUS register to switch to bank 0 in order to address the registers which reside in bank 0.
 
Last edited:

be80be

Well-Known Member
Just use banksel and for get all that bank switching stuff

Then it's a simple
Code:
banksel TRISB           ;Since this register is in bank 1,
                          ;not default bank 0, banksel is 
                          ;used to ensure bank bits are correct.
  clrf    TRISB           ;Clear TRISB. Sets PORTB to outputs.
  banksel PORTB           ;banksel used to return to bank 0,
                          ;where PORTB is located.
  movlw   0x55            ;Set PORTB value.
  movwf   PORTB
  goto    $
  end                     ;All programs must have an end.
 
Last edited:

Jon Wilder

Active Member
Bit 5 of the STATUS SFR is bit RP0 so you can type the instruction 3 different ways -

"bsf STATUS,RP0"

"bsf STATUS,5"

"bsf 0x03,5"

Or you can simply type -

"banksel TRISA"

When you want to select bank 0, you can type -

"banksel PORTA"
 

Alharad

New Member
Using BANKSEL it would look like this.

Code:
BANKSEL TRISB
MOVLW 0xFE     ; Set first bit as output for LED
MOVWF TRISB

look good?
 
Last edited:

be80be

Well-Known Member
All of them are good but this one to big of chance to plug the wrong numbers in
"bsf 0x03,5"
 

Alharad

New Member
Alrighty, perfect! I'm going to do a blinking LED program and run it on the PIC! this will be awesome, I hope it will work!! :)

thanks to the MASTERs of PIC ASM! :D
 

Jon Wilder

Active Member
Alrighty, perfect! I'm going to do a blinking LED program and run it on the PIC! this will be awesome, I hope it will work!! :)

thanks to the MASTERs of PIC ASM! :D

BTW...when you right your code, use the tab button rather than a single space. Single space does work, however the code becomes easier to manage/read when you use a tab space rather than a single space.

Also, instructions go in column 2. In your example you have them in column 1, which is the label column. The code will not assemble if instructions are in the label column. This means you must tab over first prior to typing instructions.

Code should look like this -

Code:
Whitespace	banksel		TRISA
         	movlw		0xFE
	        movwf		TRISA

Not this -

Code:
banksel		TRISA
movlw		0xFE
movwf		TRISA

The "Whitespace" label was merely to illustrate that there should be white space between the page margin and the instruction unless you want to label that particular instruction line.
 
Last edited:

Alharad

New Member
So here's my first blinking LED code! :) does it look ok?

Code:
#include <p16F648a.inc>
     __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
     
     ORG 0
     
     START:
     	BANKSEL TRISA
     	MOVLW 0xFE		; Arrange bit 0 as output
     	MOVWF TRISA
     	
     	BLINK:
     		BANKSEL LATA
     		MOVLW 0x01		; Turn on output to LED
     		MOVWF LATA
     		CALL Delay_1s
     		BANKSEL LATA
     		MOVLW 0x00		; Turn off output to LED
     		MOVWF LATA
     		CALL Delay_1s
     		GOTO BLINK		; Loop forever
     		
     ; Subroutines
     Delay_1s:
     	MOVLW d'1000'		; This is 1 second, I think?
     	MOVWF Delay
     	
     	Wait:
     		NOP
     		DECFSZ Delay, F
     		GOTO Wait
     		RETURN

so now I need help on setting up my PIC files. how do I add the header file for the PIC16f648a? do I need to include object and library files, etc?
 

Jon Wilder

Active Member
Here is your code corrected. Compare mine to yours and take note of all differences you see -

Code:
		list		p=16F648A	;define processor
		include		<p16F648a.inc>	;include header file
     		__CONFIG	_CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT
     		radix		dec		;define radix as decimal unless otherwise noted

     		ORG		 0

;Define RAM location

Delay1		EQU		0x20
Delay2		EQU		0x21

START
	     	banksel	 	TRISA		;switch to bank 1
     		movlw		0xFF		;Arrange Port A as output
     		movwf 		TRISA

		movlw		0xFF		;Arrange Port B as output. Even though PORT B is not used, it must be defined
		movwf		TRISB		;as output if no pull up resistors exist on the PORT B pins

		movlw		0x07
		movwf		CMCON		;disable on chip comparator (required instruction for this PIC if not using the comparator)
     	
BLINK
    		banksel		PORTA		;switch to bank 0
     		bsf		PORTA,1		;Turn on output to LED
     		call		Delay_1s	;Wait
     		bcf		PORTA,1		;Turn off output to LED
     		call		Delay_1s	;Wait
     		goto 		BLINK		;Loop forever
     		
; Subroutines
    
Delay_1s
     		movlw		255		;Pre-load delay counters with starting decriment value. Value 255 is the highest value you can
     		movwf		Delay1		;use on 8 bit processors. Decimal 255 can be noted as 0xFF or b'11111111'. For long delays, use
		movwf		Delay2		;a nested delay loop as illustrated here.
     	
Wait
     		decfsz		Delay, F
     		goto		Wait
		decfsz		Delay2,F
		goto		Wait
     		return

In regards to the header file, that is what the "include" instruction is for.

Also, "banksel" only needs to be used when switching from one bank to another. You don't have to use it every time you address a register...only when switching to the bank that said register resides in. If you're already in the bank that the register you're addressing resides in, using banksel to address the register becomes redundant and generates unneeded code.

Also, you cannot have labels on the same line that a "banksel" directive resides on.
 
Last edited:

Alharad

New Member
Wow, now i have a lot of questions!

1. What is radix?
2. How do i know my processor frequency? is it 4 MHZ or what not?
3. I read where you calculate your delay from your processor frequency, how do i do that?
4. I meant how to include my header file in the little box, it shows source file, header file, object file, library file and a few others. is the header file for p16f648a in my C drive?

I think i understand everything else, thanks man. :)
 

Alharad

New Member
I figured out how to get the header file thing working and source code. But when I build the program it says "Do you want this project to generate absolute or relocatable code?"

what should I do?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top