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.

My PIC16F628 doubt threat

Status
Not open for further replies.

AMSA84

New Member
Hello there. I've just started to read the PIC16F628 datasheet/manual and I started to understand some concepts. Of course, there are some doubts, specially with some technical arguments that are in the datasheet..

I hope this threat may help some other people besides me.

Well, here there are my doubts (i'm gonna put a number before each doubt in order to be more easily to answer.

1- What is the prescaler? What is his funtion and for what it's usefull?

2- For what are those register and how usefull they are (I know that they are explained in the datasheet, but there are some stuff's that I can't understand - I'm not english native speaker, because the expressions used are too technical, so if are you guys telling by your words, I might understand better):

-STATUS
-OPTION
-INTCON
-PIE1
-PIR1
-PCON
-PCL and PCLATH

3- What is the function of TMR0? Why we need to select for example, TMR0 1:2 WDT 1:1, TMR0 1:4 WDT 1:2? What differences it makes?

4- What this means (there are lot's of stuffs like this in the manual):

PORTB<7:4> Inputs ; PORTB <3:00> (pag.42 of PIC16F628 datasheet)

5- I'd like to know too what means this MOVLW 0x07 (well in part I know what means, but I do not understand why is written like that 0x07. Basicly what means the 0x07? is equivalent to 07h? what is the difference to write in one way and another?)

Here is another: 0xC0.. - where can I locate this on the PIC memory?
Movlw 0xA6 (the same) pag 60...

6- Another notation used in the datasheet to identify something that I cannot figure it out..

CCP1M3:CCP1M0 (CCP1CON<3:0> (pag 62) What this means? Is some way to identify a place on something? Where?

7- Another example is the PWM formula, on pag 65..

there are some codes like the previows one, CCPR1L:CCP1CON<5:4> * Tosc (..).. what that means?

I hpe made myself explained, an please be patient, don't say anything to put me down.. this is the first time I'm trying to learn by myself programming a PIC.. specially to do a R/C project.

Best regards.
 
Last edited:
did you really read the datasheet? All the answers are there.

Read up here:

https://www.mikroe.com/en/books/picbook/picbook.htm

1. Prescaler just divides the clock.
2. Read datasheet
3. its a timer. read datasheet
4. PORTB<7:4> is the same as saying PORTB Pins 7,6,5,4 ( : the semicolon means through so 7 through 4 )
5. W is a register used for moving stuff and calculations. You use W alot:

Movlw 0x07 - means move 0x07 (07h) to W register.
You usually dont type the memory address ... you type what to put into it. Like:
Code:
    MOVLW 0x07 ; RA3-RA0 are
    MOVWF TRISA ; outputs

This moves 0x07 to W then W to TRISA
0x07 in Binary is: 111 or 00000111 - This will set PORTA a PINS 0,1 & 2 to Inputs.

PORTx = Read/Write pin data (on 16F)
TRISx = Set Pin data direction. INput or output

6. CCP1M3:CCP1M0 is the same. On page 61 you will find that:
CCP1CON REGISTER (ADDRESS: 17h)
CCP1M3:CCP1M0: CCPx Mode Select bits

Notice the numbers at the end

CCP1M 3: CC1m 0

3:0
from 3 through 0. Which is 4 bits long.

7. Replace CCPR1L:CCP1CON<5:4> with what you set that register to be.
PWM duty cycle = (CCPR1L:CCP1CON<5:4>) • Tosc • (TMR2 prescale value)



MAYBE someone can explain more.
 
Last edited:
Hello there. I've just started to read the PIC16F628 datasheet/manual and I started to understand some concepts. Of course, there are some doubts,

Here are a couple of really good beginner tutorials available on the net:

**broken link removed**

www.amqrp.org/elmer160/ click on "lessons"

You will see reference to the old 16F84, but the lessons still apply, and the 16F88 or 16f628 are good substitutes for the obsolete chip.

Hope these help.
 
1- What is the prescaler? What is his funtion and for what it's usefull?

An example will be good. Let's say the PIC is running at 4MHz, so each instruction cycle is 1µs. Say if the internal timer run from internal clock, so it takes 256µs for the timer to run from 0 to 255 (8-bit). If the timer is prescaled, say 1:2, then 512µs is taken for the timer to run from 0 to 255.

2- For what are those register and how usefull they are (I know that they are explained in the datasheet, but there are some stuff's that I can't understand - I'm not english native speaker, because the expressions used are too technical, so if are you guys telling by your words, I might understand better):

-STATUS
-the status of the PIC. Example: after performing 7-7, the result is 0 so the Z bit of this register is set.
-OPTION
-some settings for PORTB, timer0, external interrupt
-INTCON
-enable or disable the interrupt, or read which interrupt has occurred with this register
-PIE1
-there're many sources of interrupt, so one register (INTCON) cannot control that much, this peripheral interrupt enable register is used to enable or disable some other interrupt
-PIR1
-used to read which interrupt of PIE1 has occurred
-PCON
-besides changing the internal oscillator frequency, this is used to check what type of reset has occurred, POR or BOR
-PCL and PCLATH
-program counter register. since this is 14-bit core, a 8-bit register is not enough for it, so 2 register is used (PCL and PCH). PCLATH is the latch of PCH. read page 28

3- What is the function of TMR0? Why we need to select for example, TMR0 1:2 WDT 1:1, TMR0 1:4 WDT 1:2? What differences it makes?

TMR0 is just a 8-bit timer. It can be used for counting, intertupt, etc. The scaling is explained above, same to the prescaling of WDT.

4- What this means (there are lot's of stuffs like this in the manual):

PORTB<7:4> Inputs ; PORTB <3:00> (pag.42 of PIC16F628 datasheet)

AtomSoft has explained.

5- I'd like to know too what means this MOVLW 0x07 (well in part I know what means, but I do not understand why is written like that 0x07. Basicly what means the 0x07? is equivalent to 07h? what is the difference to write in one way and another?)

0x07 is hex of 7
d'7' or .7 is decimal of 7
b'00000111' is binary of 7


Here is another: 0xC0.. - where can I locate this on the PIC memory?
Movlw 0xA6 (the same) pag 60...

6- Another notation used in the datasheet to identify something that I cannot figure it out..

CCP1M3:CCP1M0 (CCP1CON<3:0> (pag 62) What this means? Is some way to identify a place on something? Where?

Explained by AtomSoft

7- Another example is the PWM formula, on pag 65..

there are some codes like the previows one, CCPR1L:CCP1CON<5:4> * Tosc (..).. what that means?

Tosc is the oscillator period, with 4Mhz, it is 0.25µs
Hope it helps.

*EDIT: I was referring to PIC16F628A datasheet for the page #.
 
Last edited:
Hi guys, thnk u very much for answer my questions. I've been reading a lot about PICs.

Taking the last post, "Here is another: 0xC0.. - where can I locate this..." This is not an adress but a number. When we make MOVLW this means to mov into W the number 0xC0 (in this case 120? In binary 0111 1000, right?)

If we wanted to get into the W a content that is located at memory adress we should use the EQU - right? Or is a way to give another name to the content in the memory location?
 
Last edited:
Are you trying to say : How do i make a variable?

at the top you can declare variables in CBLOCK. Goto on your computer.

C:\Program Files\Microchip\MPASM Suite\Template\Code

There you can open up a template to see how variables are declared for different chips. I learned some of the basics from there.
 
Ok. I'll read that.

btw:

__config 0x3D18 ;sets the configuration settings
;(oscillator type etc.)

Sets the config settings, but wich config settings? Where can I see those config sets made by the 0x3D18? I've searched in the datasheet but i didn't found it..
 
btw:

__config 0x3D18 ;sets the configuration settings
;(oscillator type etc.)

Sets the config settings, but wich config settings? Where can I see those config sets made by the 0x3D18? I've searched in the datasheet but i didn't found it..
You should avoid setting your config bits with a number like that. It tells you nothing about which bits you have on or off. Use the mnemonics. Page 92 of the datasheet (section 14.1) explains what each bit does.

If you're programming MPLAB assembler, navigate to c:\program files\microchip\mpasm suite and open the file P16F628.INC. Scroll right to the bottom and you'll see the mnemonics for the config bits.

They're typically used something like this. Change them to suit your setup:
Code:
		__config _HS_OSC & _WDT_OFF & _LVP_OFF
There are two underscores in front of config.
 
Hello there. Now I understand ;)

Thnks for the tip, about the __config.

Another doubt, wich are the diferences between using the #include <pic.inc> and using the LIST <pic> ?

Another doubt, now related with the structure of the program. First wee need to identify the PIC that we want to use then (here is te doubt) we set all the basic configurations to the PIC like I/O ports, digit/analg ports.. etc.. and then is the main routine.. right?

There is any kind of pattern to do the program structure? Can someone tell me?

Best regards

EDIT: Another doubt.

I've here a program, and in somewhere I find this: counter equ 0x20. This means that in the GPR will reserve a memory space that will save the data into that location? Is something like C when we do INT <name> or CHAR <name>..
 
Last edited:
Ok. btw:

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

Could someone explain me why we need the count a and count b? The count 1 wouldn't be enought?

Best regards
 
Could someone explain me why we need the count a and count b? The count 1 wouldn't be enough?
I don't think you've quite got it into your head just how fast 4MHz is. A PIC at 4MHz is doing just shy of 1 million instructions per second. A single byte counter would count down so fast you wouldn't even notice the delay.

Here's the code for a 250ms delay on a 4MHz PIC, generated by the PICList delay generator:
Code:
	cblock
	d1
	d2
	endc

Delay
			;249993 cycles
	movlw	0x4E
	movwf	d1
	movlw	0xC4
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return
That cblock variable definition block goes at the top, above your code, after the #includes.
 
Last edited:
which are the differences between using the #include <pic.inc> and using the LIST <pic> ?
Here's a typical assembly program first three lines:
Code:
	list	p=18F1320
	include	<p18f1320.inc>
	CONFIG	OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF
"list" enables listing output. Generates an assembly output list that you can look at to see what the assembler actually output and where it put things. Not totally necessary, but very useful sometimes.

The include file has all the equates for all the special function registers in the PIC you're working on. All that work is done for you by Microchip. Nice! :p I remember when you had to do all that yourself.

then (here is the doubt) we set all the basic configurations to the PIC like I/O ports, digit/analog ports.. etc.. and then is the main routine.. right?

There is any kind of pattern to do the program structure? Can someone tell me?
That's the usual layout. You configure any registers that you're going to use in your program, or that would affect what you're going to use. Once that's set up, you start actually doing things.

I have here a program, and in somewhere I find this: counter equ 0x20. This means that in the GPR will reserve a memory space that will save the data into that location?
Rather than use equ's for variable definitions, use cblock. Like this:
Code:
	cblock	0x00
	char,numr,bitnum,loop1,loop2,temp1,d1,d2,d3,iflag
	endc
Each of those variables is one byte. There are ways to make word and multi-byte variables too. The number after cblock tells cblock where to put that block of variables.

Is something like C when we do INT <name> or CHAR <name>
Kind of, but not nearly so complex. Cblock just reserves space in RAM and sets some labels to point to the variables. What kind of variable you store there is up to you. Your brain is the compiler when working in assembly.
 
Could you comment and explain each part?
Umm... Each part of what?

Does this help? This is an ultra simple assembly language program for a 16F819 that blinks an LED on RB0 on and off:
Code:
	LIST	p=16F819
	include "P16F819.inc"
	__config _INTRC_IO & _WDT_OFF & _LVP_OFF

	cblock	0x20			;start of general purpose registers
	d1,d2,d3
	endc

	org	0x0000
init
	banksel OSCCON			;bank 1
	movlw	b'01110000'		;set 8MHz osc
	movwf	OSCCON
	clrf	TRISB
	banksel	PORTA			;bank 0

main	bsf	PORTB,0
	call	delay
	bcf	PORTB,0
	call	delay
	goto	main

delay	movlw	0x3f
	movwf	d1
	movlw	0x9d
	movwf	d2
delay_0	decfsz	d1,f
	goto	dd3
	decfsz	d2,f
dd3	goto	delay_0
	return

	end
 
Last edited:
Each part of the block that you have just written. The most confusing thing for me is understand what means loading the 0x4E and 0xC4 to the d1 and d2.

The decfsz says decrement file, skip if it's zero..

Well 0x4E is 78 in decimal.. so he will decrement the d1 until he reach 0 when he reach 0 he skip the goto $+2 part and start decrementing the d2 until reach again 0 .. this means that he as just spent 78+196 cycles doing the decrement? Each cycle takes 1uS to complet.. so he as just spent 274 uS and not 250mS.. are you following ?

Sorry again... about being borring.

best regards

EDIT: this doubt as just emerged from the winpic tutorials.. 1.2 specifically.
 
Last edited:
Each part of the block that you have just written. The most confusing thing for me is understand what means loading the 0x4E and 0xC4 to the d1 and d2.

The decfsz says decrement file, skip if it's zero..

Well 0x4E is 78 in decimal.. so he will decrement the d1 until he reach 0 when he reach 0 he skip the goto $+2 part and start decrementing the d2 until reach again 0 .. this means that he as just spent 78+196 cycles doing the decrement? Each cycle takes 1uS to complet.. so he as just spent 274 uS and not 250mS.. are you following ?
It actually takes longer than that because it's doing a lot more than you're thinking. Follow the flow closely. It decrements d1 until it's zero. Then it decrements d2 by one and starts decrementing d1 until zero again (d1 has wrapped around and become 255 again). Then it decrements d2 by one and does d1 again. It keeps doing this until d2 reaches zero. Then it wastes a few cycles to pad out the delay to exactly 250ms.

Also, watch for those few instructions that are more than one cycle. I don't know if there are any in this delay routine. But there are a few instructions that are that way. So don't automatically assume that every instruction is one cycle.
 
Last edited:
AHH! So you mean that d1 decrements like this:

77, zero? No. $+2, goto Delay_0 again. 76.. zero? No.. etc.. Zero? Yes. Skip $+2 decrement 196 decimal to 195 decimal, goto Delay_0, starts decrementing again the 78 decimal.. and so on until the 196 reach 0. When he reach 0 he goto nop (spending more cycles) and then goes back to the main routine. That's it? BTW, it's necessary to add the goto $+1 and nop?

so that means 78x196=15288uS ?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top