![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
berlin vince joe V S
Joined: 26 Jun 2015 Posts: 16
|
can i generate pwm in pic 16f676 |
Posted: Fri Jun 26, 2015 9:02 pm |
|
|
I am now working on varying intensity of led using pwm. For that i need three different mode is that possible to get exact output using interrupt in pic 16f676 because it does not have pwm output. And also i need to control it using a switch ............... _________________ Thanks |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Jun 26, 2015 9:58 pm |
|
|
the 16f676 is $1.24 US
the 16fF616 $ .86
they both have 14 pins but only
ONE of them has a lovely 10 bit pwm generator with timer 2 control.
If this is a school project, my condolences since if the photons you wish to modulate are for human eye consumption, && you wish some kind of programmable linearity of said emission - then you have problems that
extend beyond the bare PWM question you posted.
However if this is for real- then get real - and use the 616 instead.
my clients like the 'cost to build' with it as the MCU. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Sat Jun 27, 2015 1:04 am |
|
|
Agree totally, much easier with a chip with hardware PWM. However, that 'being said', you can easily do this in software, but it depends what else your PIC has to do at the same time?. You refer to reading a switch, but don't say if anything else is needed?. Also what frequency you are running the PIC off?.
Lets make some assumptions:
You are using the 4MHz internal oscillator.
You say 'three different mode'. Is this including or excluding the 'off', and the 'on'?. Presume excluding, so three power levels and off (four PWM settings).
You need something comfortably over 100Hz to be properly flicker free, so with (say) full on, 3/4 on, 1/2 on, and 1/4 on, and off, the lowest comfortable interrupt frequency would be 400Hz. Now, Timer0, gives 8bit (/256). Use an /8 prescaler with this, and you have /2048, which of the 1Mhz instruction clock, gives 488Hz for the interrupt. So you could generate a 4 level PWM at a reasonable frequency with something like:
Code: |
#include <16F676.h>
#device ADC=10
#fuses INTRC_IO,NOMCLR,NOWDT
#use delay(internal=4000000)
int8 pwmval=0; //Off to start
#define LIMIT 4 //Maximum number for PWM
#define PWM_PIN PIN_A3 //set to choose pin for output
#INT_TIMER0
void tick(void)
{
static int8 pwm_state=0;
if (pwmval==0)
output_low(PWM_PIN); //Off
else
{
//actual PWM
pwm_state++;
if (pwm_state>=LIMIT)
pwm_state=0;
if (pwm_state>=pwmval)
output_low(PWM_PIN); //off cycles
else
output_high(PWM_PIN); //on cycles
}
}
void main()
{
int8 ctr;
setup_timer_0(T0_DIV_8 | T0_INTERNAL | T0_8_BIT);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL); //start the PWM
//Now, the real code is going to have to read the button and adjust the
//pwmval value. However for demo, run 5 seconds at each level
while(TRUE)
{
for (ctr=0;ctr<=LIMIT;ctr++)
{
pwmval=ctr;
delay_ms(5000); //display this level for 5 seconds
}
}
}
|
Now this gives 'levels' of 0,1,2,3 & 4 with 0, and 4 being full off, and full on, with three levels between.
Now the limitation is that to go any finer, you have to raise the interrupt frequency. The chip will already be spending perhaps 5% of it's time just handling the interrupt at this frequency, so there is not much margin, if the settings do not give the linearity of response required..... |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|