Continue to Site

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.

pic programming.................. what next?

Status
Not open for further replies.

tama182

New Member
Hi guys,

just begun pic programming, (and yes i have read the sticky post)

anyway, im following this book,

PIC: Your Personal Introductory Course, John Mortan 2005 edition,

anyway, it basicly goes through the whole book mainly focusing on the pic16f54 and then also some projects on the 16f57,

but i notice that everywhere is saying that these chips are out of date,

will the code that i learn in this book be applicable to all the up to date chips,

also the book doesn't contain any info on lcd of a/d, it just gives projects on segment displays,

and i have just order a icd2 and picdem 2 plus, and would like to learn lcd and a/d, would the best place to learn these be from a book or the tutorails in the sticky,

also im using mplab 7.3 and writing in assembly language

thanks
 
will the code that i learn in this book be applicable to all the up to date chips,

Most well-written assembly code for the PIC16F5X family should work for the mid-range PIC family with very minor modification. Once you shift to the mid-range PICs, you'll even find it easier because of the ff. enhancements:

1. GOTO's go much farther without a bank switch.
2. More scratch memory.
3. Bigger call stack.
4. Added instructions.
5. More timers, etc.
6. Interrupts....
 
ive been reading Nigel Goodwins, tutorial, but how come none of the registers that he uses have been given address's,

for example the following

;Tutorial 1.2 - Nigel Goodwin 2002
LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
__config 0x3D18 ;sets the configuration settings (oscillator type etc.)

cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc

org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)

bsf STATUS, RP0 ;select bank 1
movlw b'00000000' ;set PortB all outputs
movwf TRISB
movwf TRISA ;set PortA all outputs
bcf STATUS, RP0 ;select bank 0

Loop
movlw 0xff
movwf PORTA ;set all bits on
movwf PORTB
nop ;the nop's make up the time taken by the goto
nop ;giving a square wave output
call Delay ;this waits for a while!
movlw 0x00
movwf PORTA
movwf PORTB ;set all bits off
call Delay
goto Loop ;go back and do it again

Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
retlw 0x00

end

in the book im following, its says that you have to write at the beginning

porta equa 05
portb equa 06

and also wouldn't the count1, counta and countb that he uses need address
 
tama182 said:
ive been reading Nigel Goodwins, tutorial, but how come none of the registers that he uses have been given address's,

They are ALL given their correct addresses, in the correct way, by the line include "P16F628.inc". This is the correct way to do it, defining them yourself is completely pointless and NOT recommended by MicroChip - I'm still completely bewildered that people have actually published books showing such bad practices!.

and also wouldn't the count1, counta and countb that he uses need address

Yes they do, and they have them, the cblock/endc defines the addresses - again, this is a perfectly normal way of defining addresses, and some what easier than addressing each one individually. It's fully described (along with the various other methods) in the MPASM Helpfile.

BTW, when posting items of code, please use the 'Code' button to keep the formatting correct, this also colours the text green for you.
 
Ok, with the code below, i dont get how it makes a delay of 250ms

Code:
Delay	movlw	d'250'			;delay 250 ms (4 MHz clock)
	movwf	count1
d1	movlw	0xC7
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00

	end

from my understanding

decrement 1 form counta and keep repeating till counta is 0

when it is 0, dec 1 from countb, this wastes 200 cycles, (0xc7=199, and 0x01 = 1, so 1 + 199 =200)

when you have taken the 1 away from countb, it equals 0, so skips to decfsz count1, which decriments 1 from count1 and then jumps back up to d1, and this happens 250 times (because count1 is 250) until count1 is equal to 0, which it then reurns to program

so the delay is 200x250, so wastes 50,000 clock cycles

and the frequency of the pic is 4mhz, which gets divided by 4 by the pic,

so it is actually running at 1mhz

so is running 1,000,000 instructions a second,

so how is 50,000 missed causing a 250ms delay?

what have i done wrong?
 
tama182 said:
so is running 1,000,000 instructions a second,

so how is 50,000 missed causing a 250ms delay?

what have i done wrong?

You've missed the fact it uses 'nested loops', so the inner loop runs more than once, in this case there's actually three levels of nesting.

This routine isn't really a good one to examine, because it's a bit confusing, it's generated by a web based routine on the PICList - it seems pointless writing your own when it generates them for you in seconds :lol:

This section:

Code:
d1   movlw   0xC7 
   movwf   counta 
   movlw   0x01 
   movwf   countb 
Delay_0 
   decfsz   counta, f 
   goto   $+2 
   decfsz   countb, f 
   goto   Delay_0

Is a 1mS delay routine, generated by the PICList routine, the rest of the code is an extra loop I added round it, so you can call it a number of times - in this case 250 times, giving a 250mS delay.

If trying to understand the 1mS delay above, you need to be aware that the two 'decfsz' instructions are in the reverse order to how they normally appear!.
 
Code:
d1   movlw   0xC7 
   movwf   counta 
   movlw   0x01 
   movwf   countb 
Delay_0 
   decfsz   counta, f 
   goto   $+2 
   decfsz   countb, f 
   goto   Delay_0

so the above routine is a 1ms delay, but at only 4mhz right?

but i still dont understand how that equals 1ms of wasted time, because from what i see, it only wastes 200 cycles

once counta has gone from 199 to 0 it continues onto countb which only has 1 to begin with, and then when that goes to 0, it goes back to D1,

how is wasteing 200 clock cycles equivalent to 1ms
 
tama182 said:
Code:
d1   movlw   0xC7 
   movwf   counta 
   movlw   0x01 
   movwf   countb 
Delay_0 
   decfsz   counta, f 
   goto   $+2 
   decfsz   countb, f 
   goto   Delay_0

so the above routine is a 1ms delay, but at only 4mhz right?

but i still dont understand how that equals 1ms of wasted time, because from what i see, it only wastes 200 cycles

once counta has gone from 199 to 0 it continues onto countb which only has 1 to begin with, and then when that goes to 0, it goes back to D1,

how is wasteing 200 clock cycles equivalent to 1ms

How do you make it 200 cycles? - each goto takes two instruction cycles, and there are two goto's per loop!.
 
How do you make it 200 cycles? - each goto takes two instruction cycles, and there are two goto's per loop!.

well then could you explain it please, as i obviously don't understand it, and i would rather know how it works rather than just goto to a calculator that figures it out for me
 
tama182 said:
How do you make it 200 cycles? - each goto takes two instruction cycles, and there are two goto's per loop!.

well then could you explain it please, as i obviously don't understand it, and i would rather know how it works rather than just goto to a calculator that figures it out for me

The loop runs 199 times, the decfsz takes one cycle (for 198 of them, and two for the last), the two goto's take two each, so that's 5 cycles per loop - 5x200 is 1000uS, or 1mS. The other few uS are used in the setting up etc. This 1mS delay only really needs one counter, but the PICList generator always gives it two - the outer loop fails after only one run through.
 
Code:
d1   movlw   0xC7 
   movwf   counta 
   movlw   0x01 
   movwf   countb 
Delay_0 
   decfsz   counta, f 
   goto   $+2 
   decfsz   countb, f 
   goto   Delay_0

i thought that the $+2 means skip down two levels, so would skip to 'goto delay_0, what does $+2 actually mean?
 
tama182 said:
Code:
d1   movlw   0xC7 
   movwf   counta 
   movlw   0x01 
   movwf   countb 
Delay_0 
   decfsz   counta, f 
   goto   $+2 
   decfsz   countb, f 
   goto   Delay_0

i thought that the $+2 means skip down two levels, so would skip to 'goto delay_0, what does $+2 actually mean?

'$' is the current address, so 'goto $+2' jumps to the last line, this runs the loop again.
 
How do you make it 200 cycles? - each goto takes two instruction cycles, and there are two goto's per loop!.

does each goto take two instruction cycles because of the whole wrong number in the PC and the flushing out thing?

and why do you replys always seem so shocked that i dont know this stuff im a beginner !!!
 
My suggestion

Hola Tama,

You can simulate the snippet in question using the simulator in MPlab.

To make your life simpler, load the register with a very small value: just 2 or 3.

In the watch window, put all the registers involved to show their value in decimal and make the whole to progress using the "step" function. You could see how it goes. Don't go to the next step until you understand the current status. :shock:

You will gain experience easily. Is not that difficult, after all :!:

Buena suerte :!:
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top