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.

Using ccp1 with pic16f877a

Status
Not open for further replies.

abofar

New Member
HELLO ALL
I FOUND PICBASIC PRO CODE MADE BY BRUCE(ON 20th July 2006, 07:45 MELAB FORUM) FOR 18F242 TO MEASURE PW USING CCP HARDWARE. I FOUND THAT IS VERY SHORT AND,NEAT AND STRAIGHT FORWARD. I HAVE 16F877A AND I WANT TO MAKE A CAP METER. I HAVE WRITTEN CODE BASED ON BRUCE'S CODE BUT USING 16F877A. I TRIED TO SIMULATE THIS ON PROTEUS . IT NEVER WORKED. BUT BRUCE'S CODE WORK FINE FOR 18F242. I DID EVERY THING POSSIBLE TO GET IT WORKING BUT NO LUCK. I AM POSTING THIS COD FOR SOME BODY OR BRUCE TO HELP ME PLEASE ON THIS.IT LOOKED TO ME OBSCURE NO MORE I CAN DO i TRIED IT ON PIC IDE SIMULATOR i SAW THE VARIABLES THIER VALUES IS CORRECT PROBLEM IS I CAN'T SEE ANY THING IN THE DISPLAY.
CODE:
'PIC16F877A
DEFINE OSC 20
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1


Low PORTE.2
PIE1.2=1

Symbol Capture = PIR1.2 ' CCP1 capture flag
T1 VAR WORD ' 1st capture value
PW VAR WORD ' 2nd capture value & ultimately final pulse width


TRISC.2 = 1 ' CCP1 input pin (Capture input)
INTCON = 0 ' Interrupts off

ReLoad:
CCP1CON = %00000101 ' Capture mode, 4capture on rising edge
T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
TMR1H = 0 ' Clear high byte of TMR1 counter
TMR1L = 0 ' Clear low byte
T1CON.0 = 1 ' Turn TMR1 on here


Capture = 0 ' Clear capture int flag bit
While !Capture ' Wait here until capture on rising edge
Wend

' Rising edge detected / stuff Timer1 value in T1
T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

CCP1CON = %00000100 ' Configure capture for falling edge now
Capture = 0 ' Clear capture interrupt flag bit

While !Capture ' While here until capture on falling edge
Wend

' Falling edge detected / stuff Timer1 value in PW
PW.HighByte = CCPR1H
PW.LowByte = CCPR1L

PW = PW-T1 ' High pulse width = PW-T1
' Convert to uS for 20MHz osc with 200nS Timer1 ticks
PW = (PW * 2)/10
Pause 150 ' Pause to let LCD power up
LCDOut $fe,1, "PULSE WIDTH" ' Display
LCDOut $fe,$C0, #PW

GOTO ReLoad

END
OBAID
 
Last edited:
This is wrong
Code:
CCP1CON = %00000101

should be

CCP1CON =%00000110 '= Capture mode, every 4th rising edge

That's if you want 4th rising edge
 
Last edited:
Thank you
But if I don't want every 4th rising edge .What is wrong with code.
code is working to just before LCDOUT ,there it stops no display ,I saw that in pic simulator. t1 and pw data is shown in variable register window.

obaid
 
Your LCD is to slow to show the data it's more then likely showing 0. Need a delay

Your looping faster then the LCD can write by the time its ready you done reset the CCP
 
I am simulating code on proteus ,for this reason i made the delay small.still i increased the delay to 1000 .the problem ia still there no display the data lines to lcd are floating (v=flt). The original code for pic 18f242 is worhkin fine on proteus simulator. This problem is impossible ,there must be some thing , wrong with code for 16f877a.
 
proteus simulator are you using the demo

This part
Code:
Pause 150 ' Pause to let LCD power up
LCDOut $fe,1, "PULSE WIDTH" ' Display 
LCDOut $fe,$C0, #PW
needs a delay
Code:
Pause 150 ' Pause to let LCD power up
LCDOut $fe,1, "PULSE WIDTH" ' Display 
LCDOut $fe,$C0, #PW
Pause 500 ' delay here to see it happen
You'll have to change it to what works but the first delay may not be needed for proteus simulator
The second delay is needed to keep you there long enough to see it show on the display

I don't use proteus simulator it cost to much and the demo is to limited to see if it is any good.
 
Thank you burt for your concern . Proteus is ok as i mentioned the code for pic18f242 is working on proteus. What is killing me is why this code does not work ,which is similar to the other code except that pic is different, and i have taken care of the deference in my code, which is just capture mode. Burt do you know about picbasic pro debug monitor can it help?. I have changed lcd part as per your suggestion no improvement. I hope i am not overburden you with this stupid code please forgive me.
 
I use Picbasic Pro Love it the two chips your using are real close the 18F242 and 18F452 where made to be easy to use the same code as the 16f877a registers use same names and pins are same

There are ADC pins on PORTE you need to add ADCON1 =$06 at the start of your code

I was thinking the LCD library took care of it but it doesn't
 
Last edited:
I really like Oshonsoft here your code working
Code:
'PIC16F877A
DEFINE OSC 20
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
 ADCON1 =$06    'you need this to set as digital I/O on portE
Low PORTE.2
PIE1.2=1

Symbol Capture = PIR1.2 ' CCP1 capture flag
T1 VAR WORD ' 1st capture value
PW VAR WORD ' 2nd capture value & ultimately final pulse width


TRISC.2 = 1 ' CCP1 input pin (Capture input)
INTCON = 0 ' Interrupts off

ReLoad:
CCP1CON = %00000101 ' Capture mode, 4capture on rising edge
T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
TMR1H = 0 ' Clear high byte of TMR1 counter
TMR1L = 0 ' Clear low byte
T1CON.0 = 1 ' Turn TMR1 on here


Capture = 0 ' Clear capture int flag bit
While !Capture ' Wait here until capture on rising edge
Wend

' Rising edge detected / stuff Timer1 value in T1
T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

CCP1CON = %00000100 ' Configure capture for falling edge now
Capture = 0 ' Clear capture interrupt flag bit

While !Capture ' While here until capture on falling edge
Wend

' Falling edge detected / stuff Timer1 value in PW
PW.HighByte = CCPR1H
PW.LowByte = CCPR1L

PW = PW-T1 ' High pulse width = PW-T1
' Convert to uS for 20MHz osc with 200nS Timer1 ticks
PW = (PW * 2)/10
Pause 50 ' Pause to let LCD power up and 150 takes a long time so I lowered it
LCDOut $fe,1, "PULSE WIDTH" ' Display 
LCDOut $fe,$C0, #PW
Pause 50  ' you need a delay here to see it 
GOTO ReLoad

END

And a nice picture of it working in Oshonsoft
View attachment 62348
 
Last edited:
Hi Burt
It is great, but I cuuld'nt get it displaying any thing using pic simulator ide evaluation copy similar to yours. Can you please tell me what setting did you use for sim lcd "set up module busy delays" and how many clock cycles did you use for sim signal generator
obaid
 
You have to toggle the CCP1 pin the code is waiting for change on that pin

I just used the default settings for LCD timing and set the port pins to match code

I also just toggled the CCP1 pin four times using the micro view
 
Last edited:
Hi Burt
I did not understand what you are saying about toggling. The program suppose to do every thing.Another thing I noticed about lcd no matter what I set RE2. when simulating starts you can't toggle it, it is not active,Microcontroller view deactivate toggling capability of RE2 .See image belowView attachment 62375
 
The CCP! pin can be toggled or you can send it a signal take your pick the code works

Set simulation rate to ultimate and toggle CCP1 pin look at pic
View attachment 62376

The code ran on hardware too. You can't use signal generator, But toggling the CCP1 pin works fine

The signal generator starts at the beginning before the registers are set in the program and hangs it in a loop
 
Last edited:
I do not know what to say.I did every thing possible to get simulator displaying the pw but no luck. i just quit the pic sim and try build it.

Thank you Burt for you care and help. One more thing can I use capture flag PIR1.2 as interrupt instead of holding the mcu waiting for the flag to be set.

Obaid
 
Last edited:
Hi Burt
Finely it works the problem is that Oshonsoft lcd is too slow. It took 30min to display resulting pw. This way does not help designer to test circuits.May be there is some thing wrong with the application. I once tried Real pic simulator it was very much faster . I will try that.
Thank you for the posted link.

Obaid
 
In Oshonsoft you can include the line

#define SIMULATION_WAITMS_VALUE = 1

All delays with over 1mS will be executed in 1mS for simulation within Oshonsoft's IDE

To use the code in a real device

#define SIMULATION_WAITMS_VALUE = 0
 
In Oshonsoft you can include the line

#define SIMULATION_WAITMS_VALUE = 1

All delays with over 1mS will be executed in 1mS for simulation within Oshonsoft's IDE

To use the code in a real device

#define SIMULATION_WAITMS_VALUE = 0
That's nice Ian but don't that only work if your compiling in oshonsoft
Were using PIC Basic PRO

What we need to no is what this changes the delays by "#define SIMULATION_WAITMS_VALUE = 1"
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top