CCP Module of PIC18F4620 incrementing with Timer1

Status
Not open for further replies.
Yes, Vmin = -200mv ; Vmax = 5.20 V

- - - Updated - - -

A little bit of calculation :
Input Signal = 20 KHz , 50% duty cycle.
Time period of the signal = 1/20Khz = 5*10^-5 seconds
Therefore, gap = 5*10^-5 / 2 = 2.5*10^-5 seconds

Timer 1 clock = 8Mhz
Number of timer 1 increments observed in the gap = 2.5*10^-5 /(1/8Mhz) = 200

Therefore if the code works fine, I should get 200 out continually.
 

OK so the code sort of works. I used a different serial-to-usb cable and my problem was solved.

The problem now is , I am getting wrong gap with the right gap value.

For example: Using 20 Khz, 50% duty cycle, Fosc = 8MHz

F= 20 Khz, T = 5 *10^-5 s , Gap = 2.5*10^-5 sec

Count = 2.5*10^-5 / (1/2MHz) = 50


I do get 50 counts out but along with other values such as 9, 11 etc

Example output :
50
50
50
50
50
9
50
9
50
9
11
14
27
38
45
50
50
50
50
50
50
9
50


Now where am I going wrong?
 

If you are still using the code you posted in post #5 then it is not surprising that you are getting unreliable results. You are setting up and responding to CCP events in the same code thread that writes out data to the serial port. It takes so much time to write out a line of results that the input square wave could be in either state by the time you are ready to look at it again. I think that when you prime CCP to look for rising edges by writing to CCP1CON and the input is already high then a CCP event is triggered immediately, giving you a random trigger. If you want to do it right then use interrupts to capture every edge. Respond to every CCP1 interrupt and switch the polarity of your edge-triggered event after each event. Leave data behind for your main program to pick up and print. You won't be able to print every sample continuously because the samples are being generated faster than you can report on them with the serial port. But at least you will a sampling of those samples which should all be correct (~50).
 
Good point. I have made some amendment to the code and was wondering how do I know how long an instruction takes to execute?

Also, if the UART is taking way too long to print out the data , causing the error. Why and how do i get 50? Shouldn't all data be wrong?
 

Good point. I have made some amendment to the code and was wondering how do I know how long an instruction takes to execute?
This is well explained in the manual for the PIC chip. But I don't know why you need this information.
Also, if the UART is taking way too long to print out the data , causing the error. Why and how do i get 50? Shouldn't all data be wrong?
No, if the polarity is right you could get a correct answer at various times.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…