CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

can i generate pwm in pic 16f676

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
berlin vince joe V S



Joined: 26 Jun 2015
Posts: 16

View user's profile Send private message

can i generate pwm in pic 16f676
PostPosted: Fri Jun 26, 2015 9:02 pm     Reply with quote

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
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Jun 26, 2015 9:58 pm     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Sat Jun 27, 2015 1:04 am     Reply with quote

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.....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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