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.

16F876A PIC software PWM

Status
Not open for further replies.

thilan

New Member
Dear friends
am trying to write a code with for 16F876A PIC using Hitech C

i wanted to make a software PWM
and i would like to used it make a knight rider circuit with pwm efeft
as it can use a trail when the lights are chasing
actually a led chaser as we seen on youtube

unfortunately i make some pwm codes but am not able to make it use with that knight rider light system
can some one guide me to make it or share me a code to make a light system

thank you
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
First!! The pic16f876a HAS a ccp module so you can do it in hardware...

Secondly I did this with Ritesh some months back...

C:
#include<htc.h>
#define _XTAL_FREQ 4000000
#define abs(n) ((n) >= 0 ? (n) : -(n)) 

char A,B,tog;        ; Couple of variables

void main()
    {
    OPTION_REG = 0;        ; prescaler off
    TMR0IE = 1;            ; Timer Interrupt on
    TRISB2 = 0;            ; Output on RB2 
    A = B = 34;            ; 
    TMR0 = A;            ; initial PWM 50%
    GIE = 1;            ; Global Interrupt on
    while(1)
        {
        if(RA0 && (A < 62))    ; Button one will increase PWM
            {            ; 95% max
            A++;
            B--;
            __delay_ms(50);    ; debounce
            }
        if(RA1 && (A > 6))    ; Button two will decrease PWM
            {            ; 5% min
            A--;
            B++;
            __delay_ms(50);    ; debounce
            }
        }
    }

void interrupt ISR()
    {
    if(TMR0IF)                ; If timer 0 spilled over
        {
        tog = ~tog;            ; toggle duty cycle
        if(tog)
            TMR0 = 0xff - A;    ; preset timer for on
        else
            TMR0 = 0xff - B;    ; preset timer for off
        RB2 = tog;
        TMR0IF = 0;            ; clear Interrupt flag.
        }   
    }

As this was for the pic16f676, there is no ADCON to deal with... You will need to turn it off..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Top