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.

Help needed - PWM generation using PIC16F676

Status
Not open for further replies.
Now the only difference between you code and mine.. I dont use the XC compiler or MPLABX

Here's mine
Code:
#include<htc.h>
#define _XTAL_FREQ  4000000
__CONFIG(0x31C4);


int OnPulse = 217;
int OffPulse = 38;
char TOG = 0;
int i = 0;

void interrupt Interrupt_Service_Subroutine(void)
    {
        T0IF = 0;
        i += 1;
    TOG = ~TOG;

        if(i>=8000)    // two seconds
        {
                i=0;
        }

    if(i<=4000)     // First second 85%
        {
        if(TOG)
            {
            RA5 = 1;
            TMR0 = 217;
            }

        else
            {
            RA5 = 0;
            TMR0 = 38;
            }
        }

        if(i>4000)    // Second second 5%
        {
        if(TOG)       
            {
            RA5 = 1;
            TMR0 = 13;
            }
        else
            {
            RA5 = 0;
            TMR0 = 242;
            }
        }
    }


void main()
    {
    int pulse;
    TRISCbits.TRISC3 = 0;
        TRISCbits.TRISC0 = 1;
    TRISAbits.TRISA5 = 0;
    CMCON = 7;
    TMR0 = OnPulse;
    ANSEL = 0x10;
    OPTION_REG = 0;
    T0IE = 1;
    GIE = 1;
        RC3=1;

    while(1)
        {

        }
   }

You'll notice that the config bits are always set correctly as I use a HEX value..

Nice
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top