Auto shut down PWM in 12F1840
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Auto shut down PWM in 12F1840
Theory
Auto shut down is a good way to prevent motor damage. The voltage over a shunt resistance will be compared to a reference voltage. If the shunt voltage becomes to large the PWM will be stop and the motor does not get any power any more.
Microcontroller
The first microcontroller I used was the 14 pins 16F1825. Here the enhanced-PWM uses the ECCP2 and the comparator uses CCP1. This CCP1 comparator interrupts if Vin->Vref.
In detail, I use for
1) PWM: CCP2CON with bit<7:6>=10 for half bridge output and bit<3:0>=1100 for PWM mode
2) Comparator: CCP1CON with bit<3:0> = 1010, compare mode with software interrupt only
Now I plan to use the 8 pins 12F1840. In the datasheet auto shut down is described in the same way as for the 16F1825. Interrupt of the PWM on comparator is possible. However, there is only 1 CCP1 control for both comparator and PWM, instead of 2.
Question
What value does CCP1CON have in for this situation? Bit<3:0> can not have the value 1100 and 1010 at the same time.
Thanks for your help.
Kind regards
Jan Lichtenbelt
Auto shut down is a good way to prevent motor damage. The voltage over a shunt resistance will be compared to a reference voltage. If the shunt voltage becomes to large the PWM will be stop and the motor does not get any power any more.
Microcontroller
The first microcontroller I used was the 14 pins 16F1825. Here the enhanced-PWM uses the ECCP2 and the comparator uses CCP1. This CCP1 comparator interrupts if Vin->Vref.
In detail, I use for
1) PWM: CCP2CON with bit<7:6>=10 for half bridge output and bit<3:0>=1100 for PWM mode
2) Comparator: CCP1CON with bit<3:0> = 1010, compare mode with software interrupt only
Now I plan to use the 8 pins 12F1840. In the datasheet auto shut down is described in the same way as for the 16F1825. Interrupt of the PWM on comparator is possible. However, there is only 1 CCP1 control for both comparator and PWM, instead of 2.
Question
What value does CCP1CON have in for this situation? Bit<3:0> can not have the value 1100 and 1010 at the same time.
Thanks for your help.
Kind regards
Jan Lichtenbelt
Last edited by Jan Lichtenbelt on Wed Nov 16, 2011 8:39 pm, edited 1 time in total.
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
Re: Auto shut down PWM in 12F1840
I just looked at the datasheet of the 12F1840 and there is no option to enable the compare mode and PWM at the same time.
I think the only way to do this is switch between PWM and compare mode in a way that will not have any effect to the motor.
Or by adding hardware to the design and only using the PWM mode ?
I think the only way to do this is switch between PWM and compare mode in a way that will not have any effect to the motor.
Or by adding hardware to the design and only using the PWM mode ?
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Auto shut down PWM in 12F1840
In chapter 24.4.2. It is clearly said that enhanced PWM auto shutdown mode can be generated by Comparator 1.
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
Re: Auto shut down PWM in 12F1840
I hadn't read that !
Anyways, I still do not see an option involving the comparator AND the PWM.
Hmm... I'm kind of curious about how that auto-shutdown thing is done now. Might come in handy some day
.
I'd like to help you Jan but I'm clueless on how to do this.
Nicolas
Anyways, I still do not see an option involving the comparator AND the PWM.
Hmm... I'm kind of curious about how that auto-shutdown thing is done now. Might come in handy some day

I'd like to help you Jan but I'm clueless on how to do this.

Nicolas
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Auto shut down PWM in 12F1840
Dear Nicolas
Thanks for helping.
I think that the solution is that CCP1CON has to be set only for PWM. The comparator output will always be used for the auto shut down procedure, if this action has been enabled with CCP1AS.
But I'm not sure about this.
Kind regards
Jan Lichtenbelt
Thanks for helping.
I think that the solution is that CCP1CON has to be set only for PWM. The comparator output will always be used for the auto shut down procedure, if this action has been enabled with CCP1AS.
But I'm not sure about this.
Kind regards
Jan Lichtenbelt
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Auto shut down PWM in 12F1840
Hello Jan,
Microchip datasheets can unfortunately sometimes be rather ambiguous, so my advice would be to try it out on the hardware, see what that does and then post your finding here. I've recently done some code with the DMA peripheral and following this through from datasheet to working code was not straight forward, especially seeing as it interacts with so many other peripherals. Examples can help if you can find any related to exactly what you are trying to do.
Microchip datasheets can unfortunately sometimes be rather ambiguous, so my advice would be to try it out on the hardware, see what that does and then post your finding here. I've recently done some code with the DMA peripheral and following this through from datasheet to working code was not straight forward, especially seeing as it interacts with so many other peripherals. Examples can help if you can find any related to exactly what you are trying to do.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Auto shut down PWM in 12F1840
Hi Jan,
Rather than using comparator function, would it be easier to set up an analogue channel (E.g AN1) that is monitoring the voltage across shunt motor resistance. Then the PWM output component can be within a decision branch:
If AN1 > threshold then PWM = 0 duty : Continuous loop with LED flashing until manual reset intervention.
Else PWM = required PWM.
You will need a continuous loop since if PWM is set to 0 the motor will stop so the current drawn will reduce to 0 which will cause motor to run again etc.
Martin
Rather than using comparator function, would it be easier to set up an analogue channel (E.g AN1) that is monitoring the voltage across shunt motor resistance. Then the PWM output component can be within a decision branch:
If AN1 > threshold then PWM = 0 duty : Continuous loop with LED flashing until manual reset intervention.
Else PWM = required PWM.
You will need a continuous loop since if PWM is set to 0 the motor will stop so the current drawn will reduce to 0 which will cause motor to run again etc.
Martin
Last edited by medelec35 on Thu Nov 17, 2011 8:21 pm, edited 1 time in total.
Martin
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Auto shut down PWM in 12F1840
Dear Ben and Martin,
I first try the Comparator interrupt with the 12F1840. I just received it today. If that not works, I will try the Martins solutions.
Kind regards
Jan
I first try the Comparator interrupt with the 12F1840. I just received it today. If that not works, I will try the Martins solutions.
Kind regards
Jan
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: Auto shut down PWM in 12F1840
If you do decide to try the way I suggested, I can post a flowchart I created some time ago to drive someone's golf trolley, which automatically stopped PWM driven motor if battery fell to 10V or too much current drawn.
Martin
Martin
Martin
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Auto shut down PWM in 12F1840
I choose the 12F1840, because it has 8 pins with enhanced PWM . Today I received the microcontroller. But there is a (big) problem. Whatever I do, I do not see any PWM neither on P1A (pin RA4) nor on P1B (pin A5).
I used:
1) timer2 with:
t2con=0x04;
pr=255;
2) Cap/Comp/PWM settings:
ccpr1l= 0x80; // for 50% duty or 512 with 0x80 the MSB
ccp1con= 0x8C; // enhanced PWM enabled (0x80), with PWM mode (0x0C)
3) set RA4 and RA5 as output
The 8 pins microcontroller is placed in the upper part of the 14 pins socket of EB006 board. After a little bit puzzeling, one find that RA4 and RA5 can be measured at J5/25 and 27, with the crystal/RC switch on RC.
What to do???
Kind regards
Jan Lichtenbelt
I used:
1) timer2 with:
t2con=0x04;
pr=255;
2) Cap/Comp/PWM settings:
ccpr1l= 0x80; // for 50% duty or 512 with 0x80 the MSB
ccp1con= 0x8C; // enhanced PWM enabled (0x80), with PWM mode (0x0C)
3) set RA4 and RA5 as output
The 8 pins microcontroller is placed in the upper part of the 14 pins socket of EB006 board. After a little bit puzzeling, one find that RA4 and RA5 can be measured at J5/25 and 27, with the crystal/RC switch on RC.
What to do???
Kind regards
Jan Lichtenbelt
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Auto shut down PWM in 12F1840
I found the solution!!
First I took the microcontroller out of the EB 006 board. I was afraid to use the osc PINs for other pruposes. Then I found out that the PWM was at other pins as expected (even with 8 pins that is possible!!). Route the PWM pins to RA4 and RA5 needs apfcon0x03;
Kind regards
Jan
First I took the microcontroller out of the EB 006 board. I was afraid to use the osc PINs for other pruposes. Then I found out that the PWM was at other pins as expected (even with 8 pins that is possible!!). Route the PWM pins to RA4 and RA5 needs apfcon0x03;
Kind regards
Jan