View previous topic :: View next topic |
Author |
Message |
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
How to use RC2 Pin for output PWM |
Posted: Sun Nov 23, 2014 1:59 am |
|
|
Hi all.
I'm newbie.
I have PIC16F690 and I want to use the RC2 PIN to output PWM signal for dimmer a lamp (Use N-MOSFET).
Please help me.
Special thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 23, 2014 2:23 am |
|
|
Your chip doesn't have a PWM.
All you can potentially do, is loop, and pulse the line on/off in your own code. However this is not going to work well if the chip needs to do anything else other than control the output. As soon as the code goes to do anything else, the PWM will stop. You could do a timer based 'software' PWM, but the frequency will be limited. Look at:
<http://www.ccsinfo.com/forum/viewtopic.php?t=50069> |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Sun Nov 23, 2014 2:49 am |
|
|
Ttelmah wrote: | Your chip doesn't have a PWM.
All you can potentially do, is loop, and pulse the line on/off in your own code. However this is not going to work well if the chip needs to do anything else other than control the output. As soon as the code goes to do anything else, the PWM will stop. You could do a timer based 'software' PWM, but the frequency will be limited. Look at:
<http://www.ccsinfo.com/forum/viewtopic.php?t=50069> |
Thank you.
In datasheet:
Help me.
I am bad english. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 23, 2014 3:07 am |
|
|
You can only get a 'single' PWM, on P1A, not on P1D.
The only modes that give a PWM on P1D, are ones that also use other pins. (P1A). |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Sun Nov 23, 2014 3:16 am |
|
|
Ttelmah wrote: | You can only get a 'single' PWM, on P1A, not on P1D.
The only modes that give a PWM on P1D, are ones that also use other pins. (P1A). |
Thank.
In CCS HELP
Insert #USE PWM(OUTPUT=PIN_C2, FREQUENCY=10kHz, DUTY=25) in to 16F690.h file OK
But, I don't know code in .c file. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 23, 2014 4:01 am |
|
|
Understand. The compiler can _only_ do things that the hardware supports.
Lots of more powerful chips, have PWM pins that can be directed to several pins. Yours doesn't.
On the 690, you have a single PWM, that is useable on P1A _only_.
The combinations you can have are:
Code: |
00 = Single output; P1A modulated; P1B, P1C, P1D assigned as port pins
01 = Full-Bridge output forward; P1D modulated; P1A active; P1B, P1C inactive
10 = Half-Bridge output; P1A, P1B modulated with dead-band control; P1C, P1D assigned as port pins
11 = Full-Bridge output reverse; P1B modulated; P1C active; P1A, P1D inactive
|
So the _only_ combination that gives one PWM pin, is '00', which outputs the PWM on P1A.
The combination that drives P1D, is '01', where P1D, has a PWM, but P1A is also driven.
It's just down to what your chip can do. If you want a single PWM, then on the 690, you have to use P1A. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Nov 23, 2014 4:19 am |
|
|
Try this:
Code: |
setup_CCP(CCP_PWM|CCP_PULSE_STEERING_D);
setup_timer2(T2_DIV_BY_1,255,1);
output_drive(PIN_C2); //needed because compiler will not set TRIS
set_pwm1_duty(512); //50:50 mark space - adjust after test
|
This will _only_ work if you have an A5 revision chip or later. On the older chips the pulse steering has a problem, and can't be used....
Beware also, the compiler will set P1A as an output by default. Use output_float if you don't want this. |
|
|
hoa35ktxd
Joined: 23 Nov 2014 Posts: 27 Location: Vietnam
|
|
Posted: Sun Nov 23, 2014 4:23 am |
|
|
Thanks for your great enthusiasm
Because I was wrong PCB design should switch the pin.
I would redesign the PCB. |
|
|
|