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.

How set PWM for PIC 16F877A

Status
Not open for further replies.

Rooney_04

New Member
hi

how i could set this PWM for 20MHz in the PIC 16F877A? if i could get the source code should be better since i m new to PIC programming
 
Stop hijacking existing threads! It's not our policy to give all the answers for problems, instead we provide advice so you may think down the right path. I already presented a frequency function, why don't you use it to make a PWM function? Did you even read anything about duty cycle and PWM like I advised?
 
Last edited:
I already explained the driving frequency in the other thread, did you read about it?
 
PWM Function

Here's a PWM function I just did using C18 with the PIC18F1320. You should be able to translate it to the PIC you want to use. If you can't even do that, you better learn some simpler projects to program first.

Code:
    void pwm(char pins, unsigned int duty, unsigned int freq, unsigned int timer)   // PWM Function
    {    
            unsigned int times, on, off;
            TRISA = ~(pins & 0xFF);
            on = (65536 - duty);
            off = (65536 - freq) - on;

            INTCONbits.TMR0IF = 0;   // Clear OVF Flag
            LATA = pins;   // Initialize LATA                  
            T0CON|=0x80;
        
            for(times=timer;times>0;times--)   // Duration of playing
            {
                if(freq<=duty)
                {
                    LATA = pins;
                }
                else
                {
                     TMR0H = (on >> 8) & 0xFF;   //Load TMR0H byte first
                    TMR0L = on & 0xFF;   // Load TMR0L byte next
                    while(!INTCONbits.TMR0IF);   // Wait for timer
                    INTCONbits.TMR0IF = 0;   // Clear OVF Flag
                    LATA = ~LATA;   // Invert output
                    TMR0H = (off >> 8) & 0xFF;
                    TMR0L = off & 0xFF;
                    while(!INTCONbits.TMR0IF);
                    LATA = ~LATA;
                }  
            }
    }
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top