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.

Pic16f818

Status
Not open for further replies.
Thanks all for giving me these links,

But it seems PWM is complicated!!

I think i need to find a baby guide to PWM because i don't know where to start!

Here are some questions I have

How to do this step? and is it important?

First we turn off the analogue to digital converters for PortA, they default to analogue, so it's good practice to set them as digital I/O if they are not being used, if we need them later we can turn them back on (or simply remove the code which turns them off).


I will try reading the tutorial again and again till i understand it!

Thanks in advance
 
Thanks all for giving me these links,

But it seems PWM is complicated!!

I think i need to find a baby guide to PWM because i don't know where to start!

Here are some questions I have

How to do this step? and is it important?
I will try reading the tutorial again and again till i understand it!

Thanks in advance

Hi,
Look at this conversion I have done of Nigels dual PWM into 16F818, it does work in a 16F818. OK.

Connect a 10K potentiometer between +5V and 0V, with the pot wiper connected to pin 17 [an0]
Varying the pot varies the PWM signal.
0 to 2.5V = Fwd, getting faster and 2.5 to 5V = Rev getting faster.

pin 8 is the PWM signal output [there is only one PWM on the 818]
PORTB.2 , its a nominal 2000Hz signal using the PIC's internal 4MHz clock.

Pins 6 and 7 are the Fwd and Rev outputs for the motor.
PORTB.0 and PORTB.1

Give it a try.:)

Code:
; 16F818 PWM example code  
;
; Device 16F818  
	
	list	p=16F818	; list directive to define processor
	#include <p16F818.inc>	; processor specific variable definitions
	errorlevel -302, -207

	__config H'3F10'

	 
	cblock	0x20		;start of general purpose registers
	count			;used in delay routine
	count1			;used in delay routine
	counta			;used in delay routine
	countb			;used in delay routine
	temp			;temp storage
	rate			;adc value
	endc



Fwd1	equ	0x00		;pin for left motor reverse
Rev1	equ	0x01		;pin for left motor forward

;pins 1 and 2 are the 1 PWM channel  

	org	0x0000
	nop			;for bootloader compatibility
	nop
	nop
	goto	START
	org	0x0010

START	call	Initialise

MainLoop: 
	
	call readadc
	;movf ADRESL,W
	movf ADRESH,W
	movwf temp
	
	goto Speed

; Adcin Routine
readadc	
	movlw 0x41;;01000000;;;0xC0
	movwf ADCON0
	
	movlw 0x14
	movwf count1
	call X001

	bsf ADCON0,GO
A002:	btfsc ADCON0,GO
	goto A002
	BCF PIR1,ADIF
	BCF ADCON0,ADON
	return
;
; Waitus Routine - Byte Argument
X001:	movlw 0x0A
	subwf count1,F
	btfss STATUS,C
	return
	goto X002
X002:	movlw 0x06
	subwf count1,F
	btfss STATUS,C
	return
	goto X002

	;goto MainLoop;;;;;;;;;;
Speed:
	movf	temp,W
	call	MtrSpeed		
	call	Long_Delay
	goto	MainLoop

Initialise: 

	banksel OSCCON
	movlw	b'01100000' ;uae1
	movwf	OSCCON 
	
	banksel	ADCON1		;turn off A2D
	movlw	0x0E; AN0 analog, rest digital, left justified [255]
	movwf	ADCON1

	banksel	PORTA
	banksel	TRISB
	movlw	0		;set PORTB as all outputs
	movwf	TRISB
	banksel	PORTB

	movf	CCP1CON,W	;set CCP1 as PWM
	andlw	0xF0
	iorlw	0x0C
	movwf	CCP1CON

	movlw	.126		;set highest PWM value
	banksel	PR2		;over this (127) is permanently on
	movwf	PR2
	banksel	TMR2

	movf	T2CON,W		;set prescaler to 4;;;16
	andlw	0xF8		;PWM at 2500HZ
	iorlw	0x01;;;2
	movwf	T2CON

	movf	T2CON,W		;set postscaler to 1
	andlw	0x07
	iorlw	0x00
	movwf	T2CON

	clrf	CCPR1L		;set PWM to zero

	bsf	T2CON,TMR2ON	;and start the timer running
	return

MtrSpeed:				;use value in W to set speed (0-127) 
	movwf	temp
	btfsc	temp,7		;if more than 128 set speed in reverse
	call	ReverseMtr	;so '1' is very slow forward
	btfss	temp,7		;and '129' is very slow reverse
	call	ForwardMtr
	andlw	0x7F
	movwf	CCPR1L
	return


ReverseMtr: 
	bsf	PORTB,Rev1	;set pins for reverse
	bcf	PORTB,Fwd1
	return

ForwardMtr: 
	bcf	PORTB,Rev1	;set pins for forward
	bsf	PORTB,Fwd1
	return


;Delay routines  

Long_Delay 
	return

	movlw	.10		;delay 5 seconds
	call	Delay100W
	return

Delay100W movwf	count		;delay W x 100mS
d2	call	Delay100	;maximum delay 25.5 seconds
	decfsz	count	,f
	goto	d2
	return

Delay255 movlw	0xff		;delay 255 mS
	goto	d0
Delay100 movlw	.100		;delay 100mS
	goto	d0
Delay50	movlw	.50		;delay 50mS
	goto	d0
Delay20	movlw	.20		;delay 20mS
	goto	d0
Delay10	movlw	.10		;delay 10mS
	goto	d0
Delay1	movlw	.1		;delay 1mS
	goto	d0
Delay5	movlw	0x05		;delay 5.000 ms (4 MHz clock)
d0	movwf	count1
d1	movlw	0xE7
	movwf	counta
	movlw	0x04
	movwf	countb
Delay_0	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	return

;end of Delay routines  

	end
 
Last edited:
Thanks i will try it soon.

Some simple question about PIC

what does it mean. 0x0 why x in between?

I know F8 but what is 0xF8 for??

Just want to know??

Thanks again i will try soon :) :)
 
Thanks skyhawk

Can't we just say H'F8' instead of 0xF8??

Just wanted to know

Thanks in advance
 
Sorry for the trouble!

Can you explain these lines
banksel ADCON1 ;turn off A2D
movlw 0x0E; AN0 analog, rest digital, left justified [255]
movwf ADCON1

I understand that banksel is used to directly moved to the address of interest by just specifying the address for example ADCON1

but why we moved 0E to ADCON1 NO IDEA i am trying to figure it out from page 82 in the datasheet of PIC 16F818!

Thanks in advance
 
but why we moved 0E to ADCON1 NO IDEA i am trying to figure it out from page 82 in the datasheet of PIC 16F818!
0x0e = 00001110

Bit 7 - ADFM: 0=Left justified, 6 Least Significant bits of ADRESL are read as 0
Bit 6 - ADCS2: 0=Disabled
Bit 5-4 - Unimplemented
Bit 3-0 - PCFG<3:0>: A/D Port configuration control bits

So a 1110 in bit 3-0 means that AN1, 2, 3 and 4 are set as digital, AN0 is set as analog (for A/D use), VREF+ is set to AVDD (uses VDD as the positive reference) and VREF- is set to AVSS (uses VSS as the negative reference).

Look on Page 82 of the datasheet for more details.
 
Sorry for the trouble!

but why we moved 0E to ADCON1 NO IDEA i am trying to figure it out from page 82 in the datasheet of PIC 16F818!

hi,
The explanation that Futz gives is very clear, just in case, I have attached a 'pictorial' description.:)
 

Attachments

  • esp01 Oct. 24.gif
    esp01 Oct. 24.gif
    35.9 KB · Views: 176
I understand that banksel is used to directly moved to the address of interest by just specifying the address for example ADCON1

You're confusing yourself there - 'banksel' doesn't 'move to an address', all it does is switch to a specific bank of memory. Different registers are in different banks, and the 'banksel' directive causes the compiler to generate code to switch to the correct bank for the specified register.
 
Wow Thanks for giving me so much detail I am feeling that I can somehow read Assembly codes what a relief. Thanks all again!!!

Still i am interested in knowing what is AN0, AN1??? what are these I know how to set them now to digital or analog but what are these???

Q2> Why we select 0 for bit 7?? why left Justified why not right???

bit 7 ADFM: A/D Result Format Select bit
1 = Right justified, 6 Most Significant bits of ADRESH are read as ‘0’
0 = Left justified, 6 Least Significant bits of ADRESL are read as ‘0’

Thanks again :) I really think of this forum as my virtual school !!!!
 
Wow Thanks for giving me so much detail I am feeling that I can somehow read Assembly codes what a relief. Thanks all again!!!

Still i am interested in knowing what is AN0, AN1??? what are these I know how to set them now to digital or analog but what are these???

Q2> Why we select 0 for bit 7?? why left Justified why not right???

Thanks again :) I really think of this forum as my virtual school !!!!

hi,
The AN is AN-alog, which is a variable voltage input from 0V to +5V
Its been left justified because we are only using a maximum 255 [8 bit] value for the PWM.

If we had wanted a 10bit [1023] value we we would have used right adjust.

Do you follow.?
 
Last edited:
Thanks for replying I got what it mean 8bit =255 left justified 10bit right justified thanks

I am facing problem understanding AN so what does it mean AN0 or AN1 does it mean portA pin 0 is analogy ??

Thanks again
 
I am trying to understand these lines
Code:
	banksel OSCCON
	movlw	b'01100000' ;uae1
	movwf	OSCCON
We set the internal clock to 4Mhz
Code:
	banksel	ADCON1		;turn off A2D
	movlw	0x0E; AN0 analog, rest digital, left justified [255]
	movwf	ADCON1
We Set port A,0 to analog and portA,1,2,3,4 to digital?? But in PIC16F818 we have 8 port for A and we only define 5???
Code:
	banksel	PORTA
	banksel	TRISB
	movlw	0		;set PORTB as all outputs
	movwf	TRISB
	banksel	PORTB

Why we banksel to PORTA ?? Can we just ignore it like this

Code:
	banksel	TRISB
	movlw	0		;set PORTB as all outputs
	movwf	TRISB
	banksel	PORTB


Thanks again for replying :)
 
hi Hesham,

The PIC has a number of internal devices, eg: analog convertors, Timers, internal oscillators,UART etc,
which must be instructed to work in the way required by the programmer.
These 'instructions' are loaded into the internal devices control registers, all the device registers have a unique name.

Example1:
We want to select the PIC's internal clock to run at 4MHz, so we load the OSCCON register with the required bit pattern
In this case its 01100000 [look at the 818 datasheet for the OSCCON reg]

We set the internal clock to 4Mhz
banksel OSCCON
movlw b'01100000' ; 60h
movwf OSCCON


Example2:
So that we can use an Analog voltage on the AN0 input, we load the ADCON1 reg with the bit pattern.
We require this pin to work as an analog input so that we can connect a 10K potentiometer between +5V and 0V,
with the pot wiper as the input to the AN0 pin.
As we adjust this pot the PWM signal on PIC pin 8 will vary.
The remainder of PORTA pins are set as Digital. [ we are going to use the PIC's internal +/-Vref]

We Set PORTA,0 to analog and PORTA,1,2,3,4 to digital??
But in PIC16F818 we have 8 port for A and we only define 5?

If you look closely at the 818 pin names, you will see only 5 can be analog [AN0 thru AN4]
the remainder are RA5,6,7

You use TRISA/PORTA to set the remaining PORTA pins as required as input or output.
banksel ADCON1 ;turn off A2D
movlw 0x0E; AN0 analog, rest digital, left justified [255]
movwf ADCON1



Example3:
We want PORTB to be all output pins, so we select the BANK for PORTB/TRISB using Banksel, then
we load TRISB with 00000000 which makes all the PORTB pins outputs.
banksel TRISB
movlw 0 ;set PORTB as all outputs
movwf TRISB


Its very important that you have read and understood the 16F818 datasheet sections that deal with these types of register.
You dont have to remember the bit patterns that are loaded in these control registers, BUT should be aware of ALL the control register names and their function, always have the datasheet for reference.

Do you follow OK.?
 
Last edited:
Wow amazing took me sometime to understand but I am willing to learn :)

Thanks eric

Here is the codes

Code:
	banksel	PORTA
	banksel	TRISB
	movlw	0		;set PORTB as all outputs
	movwf	TRISB
	banksel	PORTB

	movf	CCP1CON,W	;set CCP1 as PWM
	andlw	0xF0
	iorlw	0x0C
	movwf	CCP1CON

Again why Banksel PortA do u think its not needed?

Code:
	movf	CCP1CON,W

Copy the value of CCP1CON to W

Code:
andlw	0xF0
xxxxxxxx
11110000
-------------
xxxx0000 After andlw (note x is the orgin value of CCP1CON)

Code:
iorlw	0x0C

xxxx0000
00001100
------------
xxxx1100 which is PWM mode (refer to page 65 in manual)

Code:
movwf	CCP1CON
Move the new value to the address CCP1CON

First, is my analyzes correct.
2nd IF my analyzes correct why we didn't change bit 4-7 of the address CCP1CON??

Thanks in advance
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top