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.

12F683 PWM Duty Cycle

Status
Not open for further replies.
Well Mike, it works perfectly.

Now the question is what the heck have I been doing the last 6 hours???

Oh well, I'm sure I learned something.

It won't quite work for what I was doing in my real application. My ccpr1L was already zero, I was only using one bit in ccp1con to get the shortest possible pulse and period. But I can just set ccp1con on and off. My earlier code was pure gibberish. Too many pieces of code commented out in case I had to go back an redo something I guess. ccp1con and T2Con are NOT in bank 1. By doing a "bsf T2Con,Tmr2on" in bank one, I was actually setting pr2 to 2, I think. Ugh. Ok, I'm going back and changing that embarrasing mess.
So for me-

To stop PWM-

clrf ccp1con

to start PWM-

movlw b'001100' ; be sure to add any quarter cycle bits wanted
movwf ccp1con ; last 4 bits (1100) set pwm mode to active-high

The sad part is that this is what I tried the first time, but because I had that bogus bank switching code it didn't work so I just kept adding stuff until it did:eek:
 
Last edited:
Mike-
I was wrong about the comparator. I don't have to turn it off to get PWM. I was mislead because I was using gp0 as an input to start the PWM and of course gp0 won't work without turning the comparator off.
 
Would this be a good time to mention that one of my tutorials shows how to do PWM?, not on the 12F683, but on the 16F876/7 - but it's the same 14 bit core and essentially the same.
 
This is an excellent time Nigel, as it allowed me to learn a lot by trying different things rather than just following a tutorial. I'm not saying that at the time I wouldn't have preferred not having the typo and wasting three days trying to find it, but I am certainly going to go see what you have to say. Especially as to how you suggest starting and stopping the PWM as there seems to be several ways to do it. And I'm really glad I had to figure this out and come up with my simple description of pulse and period based upon instruction time rather than the complicated stuff in the data sheets.
 
Pommie said:
...
PapaBravo, looking at the diagram, it looks like leaving the pin as analogue and leaving the comparator on would allow the pin to still work as a digital output. BTW, it's page 37 in my DS - is yours out of date?
...
Mike.
Lemme check.
DS4121B downloaded February 2005. (c) 2004
DS4121C downloaded today (c) 2006

Yup I guess the one I had was out of date, and p.37 is correct. The point still is that a pin with multiple functions has to be specifically placed in the state that you want it to be. You cannot assume that writing a single value to a single register will get you there.

I agree that it may be hard to ferret out this information when you are in a hurry but I guess that is the price we pay for having 8-pin devices where each pin has 8 functions (a little hyperbole for effect).

The problem with a digital output with the pin configured as an analog input is that READ-MODIFY-WRITE instructions may not work correctly. Analog input pins always read as zero. These instructions depend on being able to read the present digital value of the pin. What's worse is that unrelated RMW instructions may alter the state of the output without you being aware of the problem. These types of faults are extremely difficult to identify and track down.
 
Last edited:
Papabravo said:
The problem with a digital output with the pin configured as an analog input is that READ-MODIFY-WRITE instructions may not work correctly. Analog input pins always read as zero. These instructions depend on being able to read the present digital value of the pin. What's worse is that unrelated RMW instructions may alter the state of the output without you being aware of the problem. These types of faults are extremely difficult to identify and track down.

This is true, especially the bit about being difficult to track down. BTW, I don't advocate doing this, it was just curiosity as the diagram suggested that you didn't need to set the input to digital.

One thing I have found with Microchip data sheets is the description for setting up a peripheral function is normally spot on. In this case it didn't mention CMCON or ANSEL and they didn't require changing. Another thing is, peripherals sometimes require the opposite of what you expect, such as the UART where the transmit pin has to be set to input for it to work.

Jimg, one good thing about lessons that last 3 days is that they are remembered for ever more. For future reference, when you have a bug like this, run it in the simulator, pause it and check the SFR's contain what you expect them to contain. If you don't turn case sensitivity off, you can single step through your code and hover your cursor over the SFR's name to see it's contents.

Mike.
 
You still have an error in your code. In your last init reg code, you put

bsf status,rp0
bsf trisio,2
bcf status,rp0

But this sets gp2 as input (again) and obviously doesn't work. It should be a copy&paste mistake because I'm sure you have the rigth code in your pic, otherwise it will not work.
Right one is

bsf status,rp0
bcf trisio,2
bcf status,rp0
 
Status
Not open for further replies.

Latest threads

Back
Top