PIC 18F2431

Hello!
I recently purchased a PIC 18F2431 from Microchip for the express purpose of reading a pwm signal. The 18Fxx31 series have a special CAPx feature in the Motion Feedback Module that can measure the PWM signal using a timer and some edge-triggering hardware, but I cannot get the buffer where the timer gives its output for the pulse width to update: it simply stays where I initialized it. I have gone through the configuration bits so many times it isn’t even funny, and tried more programming techniques than I have resistors (that’s a lot!) and still cannot get it to work. Furthermore, I replaced the microprocessor, and it CONTINUES TO DEFY ME!!! ARGH!!!
sorry.
In short, I have tested every piece of the module, from the timer to the input pins, and still it does not work. It is very difficult for me to post my programs, because it has many obscure references to buffers nonexistent in other microcontrollers. If anyone has any experience with this type of PIC, please respond!

Thanks!

Sparks333

Dane,

Have you been able to get your code to work under simulation (MPLAB SIM)?

Mike

nope. I’ve also used the watch window to see if the CAPxBUF buffer ever changes. No joy.

Sparks333

Can you post some code snips of setting the registers and details of the signal your trying to measure?

Dane,

I strongly suggest that you watch the web seminar “Introduction to MPLAB® SIM Software Simulator” presented by Darrel Johansen at this site. Pay particular attention to his discussion of the Stimulus Controller.

Peace,

Mike

Mike,
thanks for the link. I will definitely go in deeper once I get a spare minute.

Gdeaver,
The signal I am trying to capture is a 200khz PWM signal from a Memsic thermal inclimometer. Isn’t that a mouthful? I am on the wrong computer to give code, but I’ll put them on as soon as I can.

Sparks333

Okay,
Sorry it took so long, but I was on vacation.

Here’s the code. Keep in mind that I am a beginner, with rudimentary understanding of C, and even less experience with PICs.

#include <stdio.h>
#include <p18f2431.h>

void main (void)
{

CAP1BUFL = 0x00;
INTCONbits.GIE = 0;
INTCONbits.PEIE = 0;
IPR3bits.IC1IP = 0;
IPR3bits.IC2QEIP = 0;
IPR3bits.IC3DRIP = 0;
PIE3bits.IC1IE = 0;
PIE3bits.IC2QEIE = 0;
PIE3bits.IC3DRIE = 0;
PIR3bits.IC1IF = 0;
PIR3bits.IC2QEIF = 0;
PIR3bits.IC3DRIF = 0;
T5CONbits.TMR5ON = 1;
T5CONbits.TMR5CS = 0;
T5CONbits.T5PS1 = 0;
T5CONbits.T5PS0 = 0;
T5CONbits.T5MOD = 0;
T5CONbits.RESEN = 0;
T5CONbits.T5SEN = 1;
CAP1CON = 0x00;
CAP1CONbits.CAP1REN = 1;
CAP1CONbits.CAP1M3 = 0;
CAP1CONbits.CAP1M2 = 1;
CAP1CONbits.CAP1M1 = 1;
CAP1CONbits.CAP1M0 = 1;
DFLTCONbits.FLTCK0 = 0;
DFLTCONbits.FLTCK1 = 0;
DFLTCONbits.FLTCK2 = 0;
DFLTCONbits.FLT1EN = 1;
DFLTCONbits.FLT2EN = 0;
DFLTCONbits.FLT3EN = 0;
DFLTCONbits.FLT4EN = 0;
QEICONbits.PDEC0 = 0;
QEICONbits.PDEC1 = 0;
QEICONbits.QEIM0 = 0;
QEICONbits.QEIM1 = 0;
QEICONbits.QEIM2 = 0;
QEICONbits.UP_DOWN = 1;
QEICONbits.ERROR = 0;
QEICONbits.VELM = 1;
ADCON0bits.ADON = 0;
ANSEL0bits.ANS4 = 0;
ANSEL0bits.ANS3 = 0;
ANSEL0bits.ANS2 = 0;
ANSEL0bits.ANS1 = 0;
ANSEL0bits.ANS0 = 0;
LATC = 0;
LATA = 0;
TRISA = 0;
PORTA = 0;
PORTC = 0;
TRISC = 1;
OSCCONbits.IDLEN = 0;


	PORTC = CAP1BUFL;

}

Yes, I am fully aware it’s not good programming practice to set option registers in the main loop, but it should work. I am also aware that the setting of the registers could be shorter, but I wanted maximum control over the bits so I could get nothing wrong.

Oh, and what does this code do vs what its supposed to? The CAP1CON register sets a capture device that measures PWM duty cycle. From there, the other registers make sure nothing is in the way. Then, PORTC (which is an 8-bit port I have set up with LEDs, and have tested time and time again) is set to the CAP1BUF buffer, where TIMER5’s value is supposed to be latched. This is theoretical, since it doesn’t work. What does happen is if I initialize CAP1BUF to anything, it will always be that something, and never change, not to mention TMR5 never resets as it should. If I don’t ititialize it, it sets itself to a crazy number and the same thing happens. I am dead sure this isn’t the microcontroller’s fault (I already replaced it once) and the wiring it exactly as it should.

If you’d like the datasheet to see what the registers do, here it is.

Thanks in advance!

Sparks

Dane,

Absolutely, definitely change the initialization to be done once and once only.

As an example, you are setting CAP1BUF to zero every time through the loop. Except for a brief microsecond or two, why would it ever be anything else?

Mike

Mike,
This particular code is a revision, to test to see if the CAP1BUFL ever did become anything except the initailization… In earlier versions, I did not initalize CAP1BUFL, allowing it to at first float, then become whatever it should be assigned to. Sadly, the program did exactly the same thing. I will remove it, but it makes no difference. However, you do bring up a good point. I am now wondering if continually resetting configuration buffers resets CAP1BUFL? There was nothing about that in the documentation…

Sparks

Everyone:
I found the problem, and it had nothing at all to do with the program. Not even the config bits. The problem was with the sensor itself. The PIC was pulling more current than the sensor could put out, and so the pulse ‘highs’ were being pulled to almost ground, so the device could not read the pulses as occuring. Throw in an NPN transistor, and the whole thing was working fine. Thanks for all your time, especially Mike Betts.
This post can now languish into obscurity.

Sparks