# Who is the MASTER of PIC ASM?

Status
Not open for further replies.

##### New Member
Nevermind, figured that out too. See post #59 again for my questions, thanks.

#### Jon Wilder

##### Active Member

Radix pre-specifies to the assembler whether your literal values are in decimal (dec), hexadecimal (hex), or binary (bin). When in decimal radix, you can simply type the number without having to specify the radix (i.e. d'05', which would be decimal 5...you can just simply type "5" and the assembler will know that you mean decimal 5 or any other number you wish up to 255).

2. How do i know my processor frequency? is it 4 MHZ or what not?

When using the internal oscillator, bit 3 in the PCON register sets the internal oscillator frequency. Setting bit 3 in the PCON register sets it up for a nominal 4MHz while clearing bit 3 sets it up for a nominal 37kHz.

For 4MHZ -

Code:
		banksel		PCON		;switch to bank 1
bsf		PCON,3		;set internal oscillator for 4MHz

For 37kHz -

Code:
		banksel		PCON		;switch to bank 1
bcf		PCON,3		;set internal oscillator for 37kHz

You would place this code prior to your port configuration routine (i.e. where you set TRIS A and B for I/O).

Now this only applies when using the PIC's internal oscillator. When using a crystal, the crystal frequency dictates the oscillator frequency.

3. I read where you calculate your delay from your processor frequency, how do i do that?

You have to know how many clock cycles per instruction each instruction is as well as the instruction cycle clock. The instruction cycle clock is divide by 4...meaning that you take the oscillator frequency and divide it by 4 to get the instruction clock frequency. For a 4MHz clock this would mean that the instruction clock runs at 1MHz. At 1MHz, the PIC executes 1 instruction per microsecond.

All instructions are single cycle instructions except for instructions which modify the program counter, which take 2 cycles to complete. Call, goto, and computed goto instructions all modify the program counter. This means every time you decrement your counter registers in your delay loop, then go back to decrement it again until the register = 0, this takes 3 instructions every time you decrement the counter registers. At 1 instruction per microsecond, this takes 3 microseconds to complete.

If you're starting the decrement at the maximum value (i.e. 255), you would multiply 255 by 0.000003, or 3 microseconds to figure out how long it takes to decrement the register from 255 to 0, which would be 765 microseconds. If you make it decrement a second register every time it fully decrements the first one, you would multiply your .000765 seconds by another 255 to figure out how long it takes to complete the entire loop. This would give us a delay of 195 milliseconds, or 0.195 seconds. This is called a "nested delay loop".

If you nest in yet a 3rd register, but start it at a lower number, you can get delays in the seconds range. Here's an example code that will get you close to a 1 second delay -

Code:
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.

movlw		0x05
movwf		Delay3

Wait
decfsz		Delay, F
goto		Wait
decfsz		Delay2,F
goto		Wait
decfsz		Delay3,F
return

At the top of your code you will need this to label the general purpose RAM locations -

Code:
Delay1		EQU		0x20
Delay2		EQU		0x21
Delay3		EQU		0x22

You can also use -

Code:
		cblock		0x20
Delay1
Delay2
Delay3
endc

This allows you to label multiple registers in one shot.

Now...3 loops with those values assuming a 4MHz main clock, which would give us a 1MHz instruction clock -

.000003 x 255 = .000765 Seconds - Delay 1
.000765 x 255 = .195075 Seconds - Delay 2
.195075 x 5 = .975375 Seconds - Delay 3

Which would give us close to a 1 second delay.

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?

Can you take a screen shot of what you're seeing here and post it? Pull up the screen on your PC, press "Print Screen", then open up Microsoft Paint and paste it in there. Finally, save the pic as a JPEG, then post it up.

#### be80be

##### Well-Known Member
1.
In arithmetic, the radix or base refers to the number b in an expression of the form bn. The number n is called the exponent and the expression is known formally as exponentiation of b by n or the exponential of n with base b. It is more commonly expressed as "the nth power of b", "b to the nth power" or "b to the power n".
2.You pick a crystal say 4 mhz divide that by 4 1000000 instructions per sec
3.same as 2

4. you don't need to include it in you project it's just listed at the top of your code
#include <p16F648a.inc>

Last edited:

#### Jon Wilder

##### Active Member
1.
2.You pick a crystal say 4 mhz divide that by 4 1000000 instructions per sec

He's using the internal oscillator.

#### be80be

##### Well-Known Member
It's still the same divide by 4 gives instructions per sec just have to set it in configure INTOSC

And seeing he had that right wasn't no need to bring that up
__CONFIG _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT

Last edited:

#### sahu

##### Member
It's still the same divide by 4 gives instructions per sec just have to set it in configure INTOSC

And seeing he had that right wasn't no need to bring that up

pl give a example for 16f676 for 1 sec delay

#### be80be

##### Well-Known Member
This is for a 4mhz clock

You can generate code from here https://www.piclist.com/techref/piclist/codegen/delay.htm
Code:
; Delay = 1 seconds
; Clock frequency = 4 MHz

; Actual delay = 1 seconds = 1000000 cycles
; Error = 0 %

cblock
d1
d2
d3
endc

;999997 cycles
movlw	0x08
movwf	d1
movlw	0x2F
movwf	d2
movlw	0x03
movwf	d3
Delay_0
decfsz	d1, f
goto	$+2 decfsz d2, f goto$+2
decfsz	d3, f
goto	Delay_0

;3 cycles
goto	$+1 nop Last edited: #### sahu ##### Member This is for a 4mhz clock You can generate code from here Microchip PIC, ASM Delay Code, Code Generator Code: ; Delay = 1 seconds ; Clock frequency = 4 MHz ; Actual delay = 1 seconds = 1000000 cycles ; Error = 0 % cblock d1 d2 d3 endc ;999997 cycles movlw 0x08 movwf d1 movlw 0x2F movwf d2 movlw 0x03 movwf d3 Delay_0 decfsz d1, f goto$+2
decfsz	d2, f
goto	$+2 decfsz d3, f goto Delay_0 ;3 cycles goto$+1
nop

what's changing with 8 MHz clock ?

#### be80be

##### Well-Known Member
Here look it over
Code:
; Delay = 1 seconds
; Clock frequency = 8 MHz

; Actual delay = 1 seconds = 2000000 cycles
; Error = 0 %

cblock
d1
d2
d3
endc

Delay
;1999996 cycles
movlw	0x11
movwf	d1
movlw	0x5D
movwf	d2
movlw	0x05
movwf	d3
Delay_0
decfsz	d1, f
goto	$+2 decfsz d2, f goto$+2
decfsz	d3, f
goto	Delay_0

;4 cycles (including call)
return

##### New Member
Hi out there! I just blinked an LED successfully thanks to all your help!!

I'm going to make cool patterns with more LEDs now.

Ohh, if anyone asks, I am the Master of PIC ASM!

Status
Not open for further replies.

Replies
0
Views
1K
Replies
0
Views
988
Replies
2
Views
1K
Replies
0
Views
2K
Replies
3
Views
2K