# Pic 16f877

Status
Not open for further replies.

#### uaefame

##### New Member
Hello everyone,

I am not a pro electrical and this might be a simple question.

Is placing 2 capacitor 0.1uf necessary between Vdd and ground?

Q1> If yes why?

Q2> If yes shall i place 1 or 2 capacitors?

See attachment to understand more.

#### Attachments

• 12.JPG
41.1 KB · Views: 324
Last edited:

#### mvs sarma

##### Well-Known Member
Hello everyone,
I am not a pro electrical and this might be a simple question.
Is placing 2 capacitor 0.1uf necessary between Vdd and ground?
Q1> If yes why?
Q2> If yes shall i place 1 or 2 capacitors?
See attachment to understand more.
we definitely Thank you as you are rehearsing our knowledge.
1. YES, the cap is necessary to bypass all the electrical HF activity created by the chip, thus preventing noise on the Vdd line.

2 One would do if you are shorting the lines across the chip as they are on either side of the chip. If independently fed, better have two caps.

#### uaefame

##### New Member
Thanks mvs,

This circuit will be used for driving a servo motor. Many of you already know this See attachement if interested in my diagram.

Prior driving a servo motor, I am interested in testing it using Oscilloscope. Therefore, I connected RB0 to oscilloscope to see 2ms on time and 18ms off time. My problem I don’t read anything from the oscilloscope anyone knows why?

The codes are here

Code:
;*******************************************************************
;                           16F877 PWM Program
;*******************************************************************

include	"p16f877.inc"

errorlevel	-302

__CONFIG _CP_OFF & _DEBUG_ON & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF & _CPD_OFF

cblock	20h
OnTime:2
OffTime:2
endc

cblock	71h
int_work
int_status
int_pclath
endc

org     0h
nop
goto    start
nop
nop

org 	04h
interrupt	movwf	int_work
swapf	STATUS,W
movwf	int_status
bcf	STATUS,RP0
bcf	STATUS,RP1
movfw	PCLATH
movwf	int_pclath
clrf	PCLATH

btfsc	PORTB,0
goto	TurnOff
; turn on the output and write the on time
nop;		delay to make both path identical
bsf	PORTB,0;<<<<<<<<<< Break Point
movfw	OnTime+1
movwf	CCPR1H
movfw	OnTime
movwf	CCPR1L
goto	DonePWM

TurnOff		bcf	PORTB,0;<<<<<<<<<< Break Point
movfw	OffTime+1
movwf	CCPR1H
movfw	OffTime
movwf	CCPR1L
DonePWM
bcf	PIR1,CCP1IF;	reset special event trigger interupt

movfw	int_pclath
movwf	PCLATH
swapf	int_status,W
movwf	STATUS
swapf	int_work,F;	swap to file
swapf	int_work,W;	swap to work
retfie

start		bsf	STATUS,RP0
bcf	STATUS,RP1
bsf	STATUS,IRP;	all indirest access is to 100h - 1ffh
movlw	(0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
movwf	OPTION_REG
movlw	b'11111110'
movwf	TRISB
bcf	STATUS,RP0
movlw	(b'01'<<T1CKPS0|0<<T1OSCEN|0<<NOT_T1SYNC|0<<TMR1CS|1<<TMR1ON)
movwf	T1CON;		enable timer 1
movlw	low(5000)
movwf	CCPR1L
movwf	OnTime
movlw	high(5000)
movwf	CCPR1H
movwf	OnTime+1
movlw	low(45000)
movwf	OffTime
movlw	high(45000)
movwf	OffTime+1
movlw	(0<<CCP1X|0<<CCP1Y|b'1011'<<CCP1M0);	enable special event trigger on CCP1
movwf	CCP1CON;
bsf	STATUS,RP0
bsf	PIE1,CCP1IE;	enable CCP1 interupt
bcf	STATUS,RP0

movlw	(1<<GIE|1<<PEIE|0<<T0IE|0<<INTE|0<<RBIE|0<<T0IF|0<<INTF|0<<RBIF)
movwf	INTCON;		enable Peripheral interupts

Loop
goto	Loop

END

I start a new thread here because the title matches the problem I used to ask this in a thread called 16F818 which causes alot of confusion. Now the title matches the problem I hope no more confusion.

#### Attachments

• niceday.JPG
43.7 KB · Views: 180
Last edited:

#### uaefame

##### New Member
Hmm, I wanted to solve the problem above.

So, I just paused and said let me build a circuit that turn LED on and off. Simple Right.

My problem is how can I tell the PIC i am using an internal CLOCK of 20MHz?

Here are my codes
Code:
    list      p=p16F877             ; list directive to define processor
#include <p16F877.inc>         ; processor specific variable definitions

cblock 0x20
d1
d2
d3
endc

org 0x00

movlw (0<<T0CS)
movwf OPTION_REG

bsf STATUS, RP0
bcf STATUS, RP1
movlw 0x00
movwf TRISB
bcf STATUS,RP0

START movlw 0x00
movwf PORTB
call Delay1sec
movlw 0x01
movwf PORTB
call Delay5sec
goto START

Delay1sec
;4999993 cycles
movlw	0x2C
movwf	d1
movlw	0xE7
movwf	d2
movlw	0x0B
movwf	d3
Delay1sec_0
decfsz	d1, f
goto	$+2 decfsz d2, f goto$+2
decfsz	d3, f
goto	Delay1sec_0

;3 cycles
goto	$+1 nop ;4 cycles (including call) return Delay5sec ;4999993 cycles movlw 0x2C movwf d1 movlw 0xE7 movwf d2 movlw 0x0B movwf d3 Delay5sec_0 decfsz d1, f goto$+2
decfsz	d2, f
goto	$+2 decfsz d3, f goto Delay5sec_0 ;3 cycles goto$+1
nop

;4 cycles (including call)
return

END

The problem in the codes is I didn't set the internal clock to 20Mhz. Anyknow what i should do?

#### Nigel Goodwin

##### Super Moderator
The problem in the codes is I didn't set the internal clock to 20Mhz. Anyknow what i should do?

You don't set the clock speed anywhere, you write the code to use the clock speed you choose. In particular the delay routines in the posted code need to be written specifically for the clock speed - and if generated by the PICList delay code generator you have to input the clock speed.

#### uaefame

##### New Member
You don't set the clock speed anywhere, you write the code to use the clock speed you choose. In particular the delay routines in the posted code need to be written specifically for the clock speed - and if generated by the PICList delay code generator you have to input the clock speed.

I did it with a PIC delay generator and specified the 20Mhz frequency value.

My question is how I can tell the PIC that I am using 20MHz internal crystal?

OMG, I think what is going wrong PIC16F877 doesn't have an internal clock! I see why the servo didn't rotate. Tommorrow I will go get 20Mhz oscillator. What a discovery .

Last edited:

#### Nigel Goodwin

##### Super Moderator
I did it with a PIC delay generator and specified the 20Mhz frequency value.

My question is how I can tell the PIC that I am using 20MHz internal crystal?

OMG, I think what is going wrong PIC16F877 doesn't have an internal clock! I see why the servo didn't rotate. Tommorrow I will go get 20Mhz oscillator. What a discovery .

You need a 20MHz crystal, two small capacitors, and to set the oscillator config fuses to HS.

Consider;

#### ArtemisGoldfish

##### Member
You need a 20MHz crystal, two small capacitors, and to set the oscillator config fuses to HS.

Or one of those 3-pin oscillators with internal capacitors for simplicity.

#### blueroomelectronics

##### Well-Known Member
Or a 16F887 (has built in osc)

#### ericgibbs

##### Well-Known Member
Or a 16F887 (has built in osc)

hi Bill,

This OP 'Hesham' has now got 3 threads running, on virtually the same project.

#### uaefame

##### New Member
what is OP?

Q> These codes are taking the motor to one direction suppose after 2second I want it to shift or go to other direction? How can I do this using Interrputs?

Any ideas?

##### New Member
any one have pic 16f877a ic tester code?

#### uaefame

##### New Member
Hello again,

Here is my new diagram I only added 20Mhz crystal and 2X22pf capacitor, I am trying to control a servo motor from pin RB0 of the PIC16F877. Nothing happen, You think my diagram is wrong? or the codes? I posted already?

#### Attachments

• 16F877 picture.JPG
59.4 KB · Views: 351

#### futz

##### Active Member
Hello again,

Here is my new diagram I only added 20Mhz crystal and 2X22pf capacitor, I am trying to control a servo motor from pin RB0 of the PIC16F877. Nothing happen, You think my diagram is wrong? or the codes? I posted already?

You have a pulldown on MCLR. That's backward. Put a pullup - the resistor should connect to VCC/5V.

That might work as long as the programmer is connected, overriding the pulldown, but the second you disconnect it the PIC is in RESET. Nothing happens while in reset. MCLR must go high for the chip to run.

Last edited:

#### uaefame

##### New Member

Here is the new diagram, The problem now I am reading 5V continuous 5V from RB0 from my oscilloscope? Any idea what wrong?

General Question> why there is 2 power supply and 2 ground in this PIC, can i only connect one of them?

#### Attachments

• 16F877 picture.JPG
58.8 KB · Views: 268
Last edited:

#### blueroomelectronics

##### Well-Known Member
Connect them both.

#### uaefame

##### New Member
Hello again,

Anyone like my new avatar .

To the main point, the codes and the circuit is fine, I reprogrammed it and now it's working fine I am reading 2ms from oscilloscope .

I am only controlling one output. I want to send same codes to three output any idea?

#### uaefame

##### New Member
I am trying to control 2 servo motor at the same time and do excatly the same thing. I know just doing normal codes like
Code:
start
movlw b'00000011'
movwf PORTB
call delay1ms450us
movlw b'00000000'
movwf PORTB
call delay18ms550us
goto start

But I want to control it using interrupt
here is editted pommies code for interrupt but it's not working anyidea?
Code:
;*******************************************************************
;                           16F877 PWM Program
;*******************************************************************

include	"p16f877.inc"

errorlevel	-302

__CONFIG _CP_OFF & _DEBUG_ON & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF & _CPD_OFF

cblock	20h
OnTime:2
OffTime:2
endc

cblock	71h
int_work
int_status
int_pclath
endc

org     0h
nop
goto    start
nop
nop

org 	04h
interrupt	movwf	int_work
swapf	STATUS,W
movwf	int_status
bcf	STATUS,RP0
bcf	STATUS,RP1
movfw	PCLATH
movwf	int_pclath
clrf	PCLATH

btfsc	PORTB,0
goto	TurnOff
; turn on the output and write the on time
nop;		delay to make both path identical
bsf	PORTB,0
bsf PORTB,1;<<<<<<<<<< Break Point
movfw	OnTime+1
movwf	CCPR1H
movfw	OnTime
movwf	CCPR1L
goto	DonePWM

TurnOff		bcf	PORTB,0
bcf PORTB,1;<<<<<<<<<< Break Point
movfw	OffTime+1
movwf	CCPR1H
movfw	OffTime
movwf	CCPR1L
DonePWM
bcf	PIR1,CCP1IF;	reset special event trigger interupt

movfw	int_pclath
movwf	PCLATH
swapf	int_status,W
movwf	STATUS
swapf	int_work,F;	swap to file
swapf	int_work,W;	swap to work
retfie

start		bsf	STATUS,RP0
bcf	STATUS,RP1
bsf	STATUS,IRP;	all indirest access is to 100h - 1ffh
movlw	(0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
movwf	OPTION_REG
movlw	b'11111100'
movwf	TRISB
bcf	STATUS,RP0
movlw	(b'01'<<T1CKPS0|0<<T1OSCEN|0<<NOT_T1SYNC|0<<TMR1CS|1<<TMR1ON)
movwf	T1CON;		enable timer 1
movlw	low(5000)
movwf	CCPR1L
movwf	OnTime
movlw	high(5000)
movwf	CCPR1H
movwf	OnTime+1
movlw	low(45000)
movwf	OffTime
movlw	high(45000)
movwf	OffTime+1
movlw	(0<<CCP1X|0<<CCP1Y|b'1011'<<CCP1M0);	enable special event trigger on CCP1
movwf	CCP1CON;
bsf	STATUS,RP0
bsf	PIE1,CCP1IE;	enable CCP1 interupt
bcf	STATUS,RP0

movlw	(1<<GIE|1<<PEIE|0<<T0IE|0<<INTE|0<<RBIE|0<<T0IF|0<<INTF|0<<RBIF)
movwf	INTCON;		enable Peripheral interupts

Loop
goto	Loop

END

Last edited:
Status
Not open for further replies.

Replies
2
Views
1K
Replies
4
Views
2K
Replies
10
Views
6K
Replies
6
Views
9K
Replies
31
Views
10K