• 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.

16f690 CCP - newbie needs programming help

Not open for further replies.


New Member
Hi, new today - and of course a question to ask....
Having forgotten most of what I knew when my hobby was programming in the early 1970s I have now, in retirement, decided to play with PICS.
I've got a PICkit2, so am using the 16f690 to play with.
I have got LEDs flashing etc. so am now moving on to a gadget I want to make. It will time frequency and duty cycle, use a look-up table to work out a new time and duration and send it out again.
Seems OK ... I've read the Tips'nTricks and have started on the program - just the capture and interrupt part to begin with.
Now all I have to do is ask someone who knows rather than me keep working by trial and error - as it seems to be all error right now.
The program listed below should just change a variable "Semaphore" and show it on an LED - so I know the interrupt routine and Capture is working. I've added some flashing LEDS so I know the chip is actually doing something.
Would someone have a look and tell me what I've done wrong?
Thanks in advance.
;* Trying to learn how to use capture mode*
; *******************************************************************
; * 16/5/89 version 1 *
; *******************************************************************

#include <p16F690.inc>

w_temp EQU 0x7D ; variable used for context saving
status_temp EQU 0x7E ; variable used for context saving
pclath_temp EQU 0x7F ; variable used for context saving

cblock 0x20
;begin here

ORG 0x000
goto main
nop ;fill up some memory until interrupt vector

; interrupt vector location
; interrupt routine - save current status

movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,w ; move pclath register into W register
movwf pclath_temp ; save off contents of PCLATH register
; turn off interrupts
bcf PIR1,CCP1IE ;turn off ccp interrupt
bcf INTCON,GIE ;turn off global interrupts
clrf PIR1 ;clear interrupt flag
; ******************************************************
bsf Semaphore,0 ; set as interrupt routine has been called
; reinstate status
movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents

; reset interrupts
bsf PIE1,CCP1IE ; turn on ccp interrupt
bcf INTCON,GIE ;turn global interrupts on

retfie ; return from interrupt

;main program here

bsf STATUS,RP0 ; select Register Page 1
movlw b'00100000' ; make IO PortC,5 input the rest output
movwf TRISC
bcf STATUS,RP0 ; back to Register Page 0

movlw b'00110011' ;set timer controls - as below
;set timer 1 internal bit 1 set
;set prescaler to /8 bit 5,4 set
movwf T1CON ;start timer 1 bit 0 set

movlw b'00000101' ;set ccp on rising edge
movwf CCP1CON
clrf PIR1 ;clear ccp interrupt flag
bsf PIE1,CCP1IE ; turn on ccp interrupt
bsf INTCON,GIE ;turn on global interrupts

clrf Semaphore


;do a flash on LEDs to show something is working, use 1 and 3 so 0 is free to show the interrupt has been called
decfsz Delay1,f
goto OffDelayLoop
decfsz Delay2,f
goto OffDelayLoop
movlw b'00000010'
movwf PORTC

decfsz Delay1,f
goto OnDelayLoop
decfsz Delay2,f
goto OnDelayLoop
movlw b'00001000'
addwf Semaphore,0 ;this should flash portC,0 if the interrupt routine has been called
movwf PORTC

goto Mainloop



Well-Known Member

Welcome to the forum and the wonderfull world of the Pic..

You do not say what your errors are - have built your code and it does give three ' banking' messages - 302.

These are warning messages - noting that the specified register is not in bank0
( the fun of using the 16F range ).

If you click on each error message, mplab will take you to that point in your assembler code.

In your case - one is for TrisC which you have correctly 'banked', but the other two are for PIE1 which you have not.

If you correct this it might run ok.



New Member
I'll do that later.

The whole thing of banking seems straighforward! It's not the principal of having the banks - but I can't get my head round the coding! It's just so simple - a couple of bits, but it seems like juggling - I know the principal but see me try to do it!
Apart from those assembly errors the thing blinks it's LEDs but does not seem to do the interrupt - which, of course, it won't if I have not set the interrupt enable bits.
Thanks for your help,
I'll be back either to cry or to cheer!


Well-Known Member
Know what you mean . . its a bit confusing for everyone to begin with.

While testing ,use plenty of long delays ( 1second ) so you have chance to see your leds etc working.

Having the Pk2 means you can use the Debugger tool, where you can run your code on the hardware and control it with BreakPoints and single step it etc.

Sim is also a handy tool to check your code logic and timings with the StopWatch etc.


New Member

thanks a million!

I sorted out the memory banks. That led me to realise that I had some lines int he interrupt routine I didn't need as it set the interrupt enable for the CCP every time, and I only needed to toggle the the GIE to ensure no interrupts while in the routine.
Then of course I had changed lines here and there with no result - until I realised there was a rather important omission - setting the PEIE !
So, now working!

Next stage - doing the timing part - which (ha ha) should be easy!

Back soon I expect!

Thanks again - you made me a happy old codger.


Well-Known Member
Having forgotten most of what I knew when my hobby was programming in the early 1970s
That made me reminisce last night about when I started machine coding my Sinclair Mk14 kit micro in the late seventies - just wondered what you programmed in the early sevenites.

Think you will find there's a good few 'oldies' ;) on this site with similar early beginnings..


New Member
Ah, those were the days! I went on a course for teachers in '73 or 4, but that was using a BIG computer with all the flashing lights and whirring tape reels. It probably had the power of a current day toy of the type they would give away at McDonalds. We used Cobol and Fortran. While there they had a pre-release Research Machines computer that was about the size of a suitcase and was a major attraction - especially when they got it to work off two tape recorders so they could do database stuff.
Then I had a break - and caught up again just after you with the Z80 which we interfaced for control work and very simple forward backward robots.
Next, of course, was the BBC B. Some control work and a very short delve into eprom based programs for it.
Then I just used a computer for boring stuff with a bit of Basic for the students' projects. Then even more boring stuff teaching student teachers what they needed to get through the ICT requirements of their certificate.
So - my re-birth is back to assembler and enjoying life (except when I forget a command and get lost with banked memory).

Thanks again.
Not open for further replies.

Latest threads

EE World Online Articles