pwm generator with a variable duty cycle pic16f877

Thread Starter

wewe

Joined Apr 9, 2010
31
hello , i need the source code in assembly language for a pic16f877
it will generate a pwm with a variable duty cycle (from 0% to 100%) , the duty cycle can be varied with a potentiometer , so you will have to use the analog to digital converter
the frequency of the pwm must be 10Khz
please help
 

t06afre

Joined May 11, 2009
5,934
Hi
Download the datasheet for your PIC. Then go to section 8 (CAPTURE/COMPARE/PWM MODULES). You should emphasize on the PWM mode in section 8.3
Hope this will give you a push in the correct direction.
 

Thread Starter

wewe

Joined Apr 9, 2010
31
Hi
Download the datasheet for your PIC. Then go to section 8 (CAPTURE/COMPARE/PWM MODULES). You should emphasize on the PWM mode in section 8.3
Hope this will give you a push in the correct direction.
hello , i have read this section in the datasheet , honestly i hate pics and programming and this is only a small part of my project
once this is done i still have a lot of work to do so i need someone to do this for me :p
it's not really a hard program but i am really an awful programmer
the issue is that the duty cycle is not fix , if it was i think that i can do it alone , but varying duty cycles from an input is my problem
 

t06afre

Joined May 11, 2009
5,934
Since you are new in this forum I will drop the sarcasm. This forum is not for providing turnkey ready applications. I do such work but that will require a signed contract from your side.
In order to get something out of this forum, you have to put in some effort your self. So sit down read the datasheet, and write some code. If you need some input or discussion around your code work. The members in this forum will be happy to help.
 

Thread Starter

wewe

Joined Apr 9, 2010
31
who said that i didn't ???
the only thing i managed to do right now is to get the analog to digital thing right
but generating pwm is another story
in my place it's 4:30 pm right now , so it's been 7 hours in a row of reading and compiling
and i don't think that my application is new or hard for those hardtime PIC programmers, i thought it's something common that might have been done and posted here already
anyway forget about it , bye
 

Thread Starter

wewe

Joined Apr 9, 2010
31
okay , some updates
now i understand how to generate the pwm however i still don't get the frequency of the oscillator
the datasheet says that the pwm period is calculated with this equation:

pwm period = (pr2+1)*4*tmr2 prescaler * Tosc
the only thing that i don't know is the Tosc , is it the same for all pic 16f ????
4 MHZ , 8 Mhz , 20 MHz ??? what is it ???
the datasheet mentioned a 20 Mhz DC oscialltor but i don't know if it's the same one that is used in this calculation

http://ww1.microchip.com/downloads/en/DeviceDoc/30292c.pdf , the pwm generation is in section 8 (for those who want to take a look at the equation i am talking about)
 

Markd77

Joined Sep 7, 2009
2,806
Tosc is the time it takes for 1 cycle of the oscillator. You will need a crystal for this PIC - see section 12.2 of the datasheet. You can choose any frequency up to 20MHz.
Tosc is then 1/frequency
so if you pick 20MHz it is
Tosc = 1/20000000 = 0.00000005 (Seconds)
 

Thread Starter

wewe

Joined Apr 9, 2010
31
okay , now i have other questions
i've wrote my program that generates PWM
please read it because i need some help , i tried to simulate it in proteus ISIS , the output should be on pin 17 , can someone identify what's missing ??
i have set the prescaler of timer 2 on 1
oscillator on 4 MHz
pr2 = 99 (so the frequency would be 10Khz)
CCPR1L:CCP1CON<5,4> = 0011001000 (for the duty cycle to be 50%)

____________________________________________________________________

; oscillator : 4 mHZ



#include <p16f877A.inc>
;***declare registeraion

__config 3fC4
ORG 0X00
GOTO MAIN
ORG 0X04
RETFIE
code
MAIN

BCF STATUS,RP0
CLRF PORTA ;clear content of port A
CLRF PORTB ;clear content of port B
CLRF PORTC ;clear content of port C
BSF STATUS,RP0 ;change to bank 1
MOVLW B'11111111'
MOVWF TRISA ;set port A as input
CLRF TRISB ;set port B as output
CLRF TRISC

movlw B'01100011' ; period = 0.0001 seconds
movwf PR2
bcf STATUS,RP0
movlw B'00110010' ; setting the duty cycle time to 50% or 0.00005 seconds
movwf CCPR1L
bcf CCP1CON,5
bcf CCP1CON,4
bsf STATUS,RP0
bcf TRISC,2 ; pin 2 of port c is an output
bcf STATUS,RP0
movlw B'00000100'
movwf T2CON ; prescale =1 , timer2 is on
bsf CCP1CON,3 ;configuring CCP1 module for PWM operation
bsf CCP1CON,2


END
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
I think you need a
Rich (BB code):
loop
goto loop
between the last instruction and END
otherwise the processor runs through all the empty memory and then starts again.
 

Thread Starter

wewe

Joined Apr 9, 2010
31
didn't work
i need to ask something , have anyone tried this PWM generator in PICs before ???
do i need to create an infinite loop when i want to make a pwm or once the instructions are over it will create PWMs for an infinite time ???
and in proteus , when i click on the pic to give it the "cof" file , there's something called PCB package , which one should i choose?
 

Markd77

Joined Sep 7, 2009
2,806
Works fine until the watchdog timer resets it. You should probably turn that off.
If you don't have some kind of loop, the processor excecutes the empty program memory as NOPs and then starts again from the beginning.
 

Attachments

Thread Starter

wewe

Joined Apr 9, 2010
31
thanks guys
yes the problem was in that watchdog
btw i have completed the entire project , my teacher helped me in the analog to digial thing and how to use it to change the duty cycle
we made a function that return the corresponding value of CCPR1L for each value of the analog to digital conversion ,because the values returned by the conversion are not ready to use directly as an input for the duty cycle , there's a correspondance that have to be made (linear) , that was the trick , i thought that i will have to go for multiple divisions and multiplications and waste my time and cause some delays
 

ErnieM

Joined Apr 24, 2011
8,415
Well, why not start your own thread?

What have YOU done already?

We are happy to answer specific questions. We are not a "do your work for free" site.
 
Top