This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

PIC 12F683 and PWM

Last update: 31/07/2015

pic 12f683The pic 12F683 is a really interesting, useful and cheap picmicro of only 8 pins. It can be considered, in fact, an enhanced and updated version of the 'old' 12F675 because it belongs all characteristics of the 12F675 but runs upto 20mhz (using external crystals) or 8mhz (internal oscillator), 2K flash memory, 4 adc modules, 1 pwm etc.


Using mikrobasic:

program adc_pwm
dim adc as word
CMCON0 = 7                'comparators off
ANSEL = %00101000         'AN3 enabled
TRISIO.3 = 1                       'set input
TRISIO.2 = 0
PWM1_Init (10000)           'frequency at 10Khtz
PWM1_Set_duty (127)       '50% duty cicle

     adc = Adc_Read(3)    'read the value: 0-1023
     PWM1_Set_duty (adc/4)      'divided it: 0-254

12f683 pwm

The program is elementary.

We have to:

  • setting off the comparators of the picmicro
  • sets and starts the pwm
  • sets the pwm duty cicle according to the analog value on (see sch). 

The analog input (10bit) will report a value betweeen 0 and 1023. We'll report such a value to set the duty cicle (divided by 4 because: 0-255 = 0-100% duty cicle). That's all.


But how, for instance, we can vary the frequency (and not the duty cicle) of the PWM signal? unfortunately it is not directly possible because we cannot report the value of the adc_read routine as parameter of the pwm1_init() routine.

The solution consist in the replacement of all the pwm instructions and using the delay function. For example:


program adc_pwm
dim adc as word
CMCON0 = 7                   'comparators off
ANSEL = %00001000       'AN3 enabled
TRISIO.3 = 1                   'set input
TRISIO.2 = 0

        gpio.2 = 0
        delay_us (4)                   (it is a small delay necessary to be recognized by a stepper motor driver, for instance)
        gpio.2 = 1
        for t=1 to adc next t    (a delay proportional to the adc value read by the adc_read function)

Such a solution will generates (according to the speed of the crystal) a simulated pwm signal on GP2.


 Using Mikroc:


void main() {
   unsigned int temp;
   GPIO = 0;
   cmcon0 = 7 ;                        //disable comparators
   cmcon1 = 0;
   osccon = 0b01110001;          //set int osc to 8MHz
   ansel = 0b00001000;             //An3 enabled
   TRISIO = 0b00001000;
    do {
        temp = ADC_Read(ANS3);  //read
        GPIO.F2 = 1;
        delay_cyc (temp);              //proportional delay
        GPIO.F2 = 0;
    } while(1);

Diritti riservati 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: (contenuti) (webserver) per domande e/o suggerimenti.