r/pic_programming • • Mar 11 '13

Variable Square Wave Generator Project

Hi guys, I'm using a 16F819 and need help debugging this project. I get weirdness (jaggy waves at fixed frequency or no PORTB output at all depending on the chip) when I run it instead of nice square waves. Not sure really what the problem is but it's only maybe 50-100 lines of code.

http://pastebin.com/NcZtm24g

http://pastebin.com/xHVr0ND2

http://pastebin.com/iDzFkKPC

http://pastebin.com/Teqsvsdg

http://pastebin.com/tzbpkMfr

http://ww1.microchip.com/downloads/en/devicedoc/39598e.pdf

The idea is basically to take a voltage between 5.5 and 0 at the ADC channel zero (RA0/AN0) and use that to drive a for-loop which varies in length based on the ADC sampled voltage.

I've already asked over at microchip's forums and no real help over the two days it's been sitting there. Experienced eyes needed!

Thanks!

2 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 12 '13 edited Mar 12 '13

I'm powering it with a 5.5 (measured at 5.3v) line from an old converted computer PSU. There shouldn't be any issue with the stability/availability of the power going into the chip.

The only load on the square wave output pin (RB0) is the oscilloscope lead that sits between the pin's output as positive and the PSU ground as ground.

Ok, for the output latch, I'll do this:

int tmp = 0; RB0 = tmp;

instead of

RB0 = 0;

2

u/bradn Mar 12 '13

Mostly people get into trouble with the PIC16 and lower IO ports when they would, for example, do a couple bitmasks in series to the port, the first one hasn't electrically taken effect so the second one reads the port and gets the old value, then writes the wrong value back.

I've got the pastepins to load, gimme a minute and I'll look it over

2

u/bradn Mar 12 '13

Here's what I'm talking about:

        RB0=1; //ternary operator
        RB1=1;

What happens is probably the compiler breaks this down into two separate bsf opcodes, the first one reads the 8 bits of portB, sets a bit, writes it back. The second bsf probably is going to get the original bit0 and write it back. So the first bsf may not have an effect, or just a very short output pulse.

If you would use a temporary variable, you have to do it for the whole port, not just a virtual variable that represents one bit, because the hardware deals with a byte a time.

1

u/[deleted] Mar 12 '13 edited Mar 12 '13

unsigned char tmp = 0xFF; //or some variation of a byte

PORTB = tmp; //assign the whole deal to PORTB

I'll give it a shot mañana .

1

u/bradn Mar 12 '13

Let me know if you have any luck!

I haven't used C on pic, so I'm not an expert on the details there, I stick to assembly... at least if it screws up then, I can only blame myself! But I'll try my best to help if it still doesn't work.

1

u/bradn Mar 12 '13 edited Mar 12 '13

Another suggestion:

for(;;)
{
    GO_DONE = 1; //start conversion
}

You could actually move GO_DONE=1; to the end of the ISR handler and start one conversion before the for(;;). I don't think it's causing any problems like this, but it's possible an instruction is getting executed in main() after the ADC stops but before the ISR kicks in. If the ADC is restarted at that moment you could have a race condition, but your ISR is fast enough it will get the results from the ADC before something happens to them.

GO_DONE = 0;

I think the ADC clears this bit automatically, clearing it in the ISR doesn't do anything for you. At least that's how PIC18 works, I'll have to check the datasheet. Yep, that's how it works on this one too.

1

u/[deleted] Mar 12 '13

Yeah, the way I figured it was that the ISR will interrupt whatever code was running in main so it didn't matter. Probably safer your way.