need revise code for PWM AND I/O

Status
Not open for further replies.

zhu177

New Member
Hi Guys

the course work is required to design a project such that LED5 is driven by a PWM signal that causes the intensity to increase from a minimum to a maximum over 1 second then reduce back to zero over the next second repeats continuously’.
Even though I was try several times , the LED5 cannot reduce back to zero over next second. Please explain and give some idea to modify it.

cheers

Code:
#include <xc.h>


#pragma config WDTEN=OFF, FOSC=INTIO67,MCLRE=EXTMCLR,IESO=OFF, LVP=OFF

#define _XTAL_FREQ 4000000
void init(void);
void init(void)
    {
    OSCCON=0b01010010;//this to select internal osc at 4MHz
    ANSELC=0x00;//make PORTC digital I/O
    TRISC=0x03;//set appropriate data direction bits
    PORTC=0x00;//clear any PORTC outputs
    ANSELB=0x00;//make PORTB digital I/O
    TRISB=0x01;//set appropriate data direction bits (RB0 is input)
    PORTB=0x00;//clear PORTB outputs
    TRISA=0b00000001; //all outputs except RA0 analogue IP
    ANSELA=0x01; //make PORTA digital I/O except RA0 analogue
    PORTA=0x00; //clear all PORTA digital pins
    }
void init_PWM(void)
    {
    PR2=254;
    T2CON=0b00000101;//enables TMR2 hardware and prescalar to 4
    CCP1CON=0x00; //enables PWM
    CCPR1L=PR2/2; //sets equal ON/OFF time
    }


void main(void)
    {
init();
init_PWM();

    while(1)
        {
            {
             CCP1CON=0x0C;
            PR2=254;
                while(PR2>=0)
                    {
                    PR2=PR2--;
                    __delay_ms(2);
                    __delay_us(168);
                    __delay_us(168);
                    __delay_us(168);
                    __delay_us(168);
                    }              
            }
           
            {
             CCP1CON=0x0C;
            PR2=0;
                while(PR2<=254)
                    {
                    PR2=PR2++;
                    __delay_ms(2);
                    __delay_us(168);
                    __delay_us(168);
                    __delay_us(168);
                    __delay_us(168);
                }       
            }


    }
    }
 
Last edited:
Ok... What are your thoughts so far? Hopefully you know a programming language so we can assist you more..

Using a PWM module is relatively easy.... But you need to start the ball rolling..
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…