Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM outputs from PIC16F628

Status
Not open for further replies.

kalingajay

New Member
Hi,

I am beginner to PIC. Is it possible to out PWM signal from PIC, only from CCP pin. I want to out various duty cycles simultaneously form PIC. (like 20%, 40%, 60%, 80%) If it's possible to do anyone can give me a example .asm or C code, suit to PIC16F628A.
 
Thank you for your support.

I checked both links. That tutorials describe only about CCP base PWM. But I want to know about software base PWM. Because I want to take several PWM outputs at once from PIC.
 
There is a thread on here for the pic16f676 ( small chip ) with a software implementation... I wrote it with C..

C:
#include<htc.h>

__CONFIG(0x3D18);	

unsigned char toggle;
unsigned char Ontime, Offtime;

void interrupt ISR()
	{
	if(TMR0IF)
		{
		toggle= ~toggle;
		if(toggle)
			TMR0 = Ontime;
		else
			TMR0 = Offtime;
		RA0 = toggle;
		TMR0IF = 0;
		}	
	}

void main()
	{
	TRISA0 = 0;	
	TRISB = 0xff;
	TMR0IE = 1;
	TMR0 = 0xff - 34;
	GIE = 1;
	Ontime = 0xff - 34;
	Offtime = Ontime;
	while(1)
		{
		if(!RB0 && (Ontime > (0xff - 66)))
			{
			Ontime--;
			Offtime++;
			} 
		if(!RB1 && (Ontime < (0xff - 2)))
			{
			Ontime++;
			Offtime--;
			} 
		
		}
	}
 
This TMR0 example below is slower, but still fast enough for things like LED PWM, or making the PWM pulse to RC servos etc.

The frequency with PWM of 0-255 is 1sec/256 TMR0 interrupts.

You can make the frequency faster by using PWM with less resolution, ie make pwm 0-99 then the frequency is 1sec/99 ints. I've used this a lot for multiple LED PWMs.

Code:
// TMR0 interrupt, 1 LED, pwm from 0-255
void interrupt ISR()
{
  TMR0IF = 0;
  if(pwm_count < pwm) RA0 = 1;  // LED on
  else                RA0 = 0;
  pwm_count++;
}
 
...
I haven't thought about this in a long time. Mr. RB's TMR0 example looks easier than mine.

It's not my original idea, that's just a popular method from the dark ages (1990's) when PICs didn't have PWM modules etc. It will run lots of simultaneous PWMs, like you said, for multiple RC servos or multiple LEDs.
 
Since you are wanting lots of PWM outputs, this unconventional method might get you one more. Post # 10
This program uses Timer0 interrupts and prescalers to send RC servo compatible PWM at 55Hz to 57Hz depending on the length of your high pulse.
https://www.electro-tech-online.com/threads/60-hz-pwm-for-rc-servo-using-postscaler.34229/

I haven't thought about this in a long time. Mr. RB's TMR0 example looks easier than mine.

I really appreciate this site for that reason alone.... Everyone can bring something to the table.... I mean EVERYONE.. Even the ones you don't think will..

Roman!! You have a learned mind... Although my code has served me well... I must admit yours is better.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top