Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

CCP1 Capture mode for pic 16f877a

Status
Not open for further replies.

dayanpad

Advanced Member level 4
Joined
Mar 23, 2009
Messages
114
Helped
17
Reputation
34
Reaction score
17
Trophy points
1,298
Location
Colombo
Activity points
1,921
Dear all
following codes using assembly for CCP capture mode (MPLAB IDE)
after compile, I loaded hex file in to PIC simulator IDE
then i pressed several time RC2 pin
as per my program TMR1L and TMR1H should be capture to CCPR1L and CCPR1H
but it does not happened
let me know the reason
MCU 16F877A
HTML:
movlw 	b'00000101'
			movwf 	ccp1con    		 ;start with rising CAPTURE 
			bcf		pir1,2
			movlw 	b'00000001'
			movwf 	t1con          ; on timer
Wait
			btfss 	pir1,2
			goto 	Wait
 
Last edited:

Hi,

Nice try, but your code is a little bit off the mark, have a look at this long thread, it contains lots of information and code examples for doing what you want down on pages 4 and 5. Can CCP do this on 16F876A? :(
 

Dear All
Can I have more reply on this

Hi,


From that link given above I have extracted the most simple capture so you have something that works.

Also look at this tutorial to understand how it works.

https://ww1.microchip.com/downloads/en/DeviceDoc/ccpwm.pdf



Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
List p=16f877A                          ; List directive mentioning the PIC Microcontroller part number
        #include p16f877A.inc           ; file having standard definitions
        
 
        
        __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
 
        ; code assumes 20meg xtal
 
 
 
 
 
        cblock  0x020                   ;start of general purpose registers
        
                CCP1aH                          ; first leading edge timer1
                CCP1aL  
                CCP1bH                          ; second leading edge timer1
                CCP1bL  
 
                endc
 
 
 
; Start at the reset vector
        org     0x000
        goto    Start
 
Start   org             0x010
 
                clrf    PORTB
                clrf    PORTD
                clrf    PORTC
 
 
                clrf    TMR1H
                clrf    TMR1L
 
                clrf    CCPR1H
                clrf    CCPR1L
 
        bsf     STATUS,RP0      ;bank 1
                movlw   b'00000100'             ; RC2 set as input
                movwf   TRISC
        movlw   b'00000000'
        movwf   TRISB           ;portb  outputs
                movlw   b'00000000'
        movwf   TRISD                   ;portd output
                bcf             STATUS,RP0              ;bank 0
 
 
 
Main
                movlw   b'00000000'     ;timer 1 using to capture, prescaler 1:1 
                movwf   T1CON 
                bsf             T1CON,TMR1ON    ; start imer
 
 
                bcf             PIR1,CCP1IF             ; clear flag
 
                movlw   b'00000101'
                movwf   CCP1CON         ;start with CCP1 rising CAPTURE 
 
 
Wait
                btfss   PIR1,CCP1IF     ; wait for rising edge on ccp1
                goto    Wait
                movf    CCPR1H,W        ;save the value in CCP1aH and CCP1aL {lower value} 
                movwf   CCP1aH
                movf    CCPR1L,W
                movwf   CCP1aL
                bcf             PIR1,CCP1IF
 
 
Wait1
                btfss   PIR1,CCP1IF     ; wait for 2nd rising edge
                goto    Wait1
 
                movf    CCPR1H,W        ; save now the value in CCP1bH and CCP1bL
                movwf   CCP1bH
                movf    CCPR1L,W  
                movwf   CCP1bL
                bcf             PIR1,CCP1IF     ;clr flag CCP1                     
 
                bcf             PIR1,CCP1IF
 
loop    goto    loop                    ; knowing the osc freq of 20meg  you can uses the 2 sets of timer1 vlaues to calculate the time / frequency  of the pulses
                
 
                end

 

Dear wp,

Thank you very much details reply
I know how capture modual is working
my concen is, when I load to it PIC Simulator IDE
it does not work well, I mean after pullse detected by RC2,
CCP1IF must be set, and contain of TMR1L and TMR1H should be captured by CCPR1H and CCPR1L
let me know that I am in right track
 
Last edited:

Hi,

Do not use that Pic Simulator IDE but its quiet a popular one so seem sure it will work with such a simple function.

You sound as if you are on the right track.

On that example I did, you start Timer 1 , free running,

CCP1 is then activated to record when it senses the rising edge of a signal, when it does it moves Timer1s 16 bit value into the 2 registers which are then copied into the 2 user 'a' registers. It then Sets the IF bit so you know the conversion has completed.
You must now Reset the IF bit so its clear for the next signal.

The next rising edge is recorded as above but placed in the user 'b' registers.

By subtracting the users 'a' and 'b' registers you can determine the time /frequency between the two rising edges.


However as I said that is the routine in its most simple state.
Timer1 is free running so when it counts up to FF FF it just overflows to 00 00 .
The time between 0000 and FFFF depends on your oscillator and Timer1 settings , but is typically less than 1 second.

Depending on the expected speed of your pulses, you may need to uses Timer1s Interrupt so you can have additional counter to record how many times it overflows before the next pulse.

Here is a pic of my Sim showing the values placed in the a and b registers and Timer1 current values.
 

Attachments

  • ScreenShot001.jpg
    ScreenShot001.jpg
    109.2 KB · Views: 118
Last edited:
I do not know how thank you
can I have a good simulator
 

dayanpad said:
thanks your reply

in mplab sim, how we give the input to RC2 pin
how know that data captured


Not something I have done before, but you need to create a Stimuli for the RC2 pin.
This seems to work, though it only returns the results when you Run and then Stop the Sim.
If you animate or single step it does not seem to notice RC2 ..?
 

Attachments

  • ScreenShot002.jpg
    ScreenShot002.jpg
    140.8 KB · Views: 120

Dear wp

as I know. we canot give Stmuli in mplab sim for RC2
can you Please try with PIC Simulator IDE then please advice me
 

Dear wp

as I know. we canot give Stmuli in mplab sim for RC2

Hi,

Well think these screen shots show it does work.

Here is the code without the Stimulus on RC2, you Run the code but if you Halt it you see it is just looping around the Btfss waiting for RC2 to change.

On the second screen you can see the RC2 Stimulus added, just Run the code and let it end, I have added a BreakPoint to make it stop automatically, just cursor to the last line and right click on it, from the menu that opens click Add Breakpoint.
 

Attachments

  • ScreenShot001.jpg
    ScreenShot001.jpg
    95 KB · Views: 104
  • ScreenShot003.jpg
    ScreenShot003.jpg
    115.3 KB · Views: 100
Dear WP

That is grate help
I got your point and I simulated using MPLAB SIM and I added watch and now I trapped and unclear how add Stimulus while it is simulating please kindly advice me

Thanks in advance
 

Dear WP

That is grate help
I got your point and I simulated using MPLAB SIM and I added watch and now I trapped and unclear how add Stimulus while it is simulating please kindly advice me

Thanks in advance
 

Dear WP
unclear how add Stimulus while it is simulating please kindly advice me


Hi,

Build your project and open the watch window, then look at the screenshot in post #8, it shows how to open the menu and some values to enter, you then APPLY the settings and Save them if you want.
Then RUN the debugger until it reaches the Breakpoint at the Loop - then it stops and updates the values of the Watch window.

On the attached shot you can see how I have put a Breakpoint just after the BTFSS so it shows when the IF is set on ,
 

Attachments

  • ScreenShot002.jpg
    ScreenShot002.jpg
    130.6 KB · Views: 108

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top