Search code examples
embeddedmicrocontrollerpicmplabinterfacing

Not able to set value 1 to pin as Input in MPLAB


I am using MPLAB to perform an operation on PIC kit 14f458.

Now I want to create a code to pass the different amount of voltage supply LED through the Microcontroller using a button. When button pressed the first time I should get waveform with 10% DC, for second and third time, 50% and 95% respectively.

I have worked on this But still getting Values OF RE2 PORT as 0 rather than 1. And I also don't know how to stop timer while I release the button.

My code is as follow:

#include<stdio.h>                       //Standard I/O Library
#include<p18cxxx.h>                     //for TRISA,E and PORTA,E declaration
#pragma config WDT = OFF                //watchdog counter
#pragma config OSC = HS, OSCS = OFF
#pragma config PWRT = OFF, BOR = ON, BORV = 45
#pragma config DEBUG = OFF, LVP = OFF, STVR = OFF

void timer_10()
{
    CCP1CON = 0;
    PR2 = 249;
    CCPR1L = 24;
    TRISCbits.TRISC2 = 0;
    T2CON = 0x00;
    CCP1CON = 0x3c;
    TMR2 = 0;
    T2CONbits.TMR2ON = 1;
    while(1)
    {
        PIR1bits.TMR2IF = 0;
        while(PIR1bits.TMR2IF == 0);
    }
}

void timer_50()
{
    CCP1CON = 0;
    PR2 = 249;
    CCPR1L = 124;
    TRISCbits.TRISC2 = 0;
    T2CON = 0x00;
    CCP1CON = 0x2c;
    TMR2 = 0;
    T2CONbits.TMR2ON = 1;
    while(1)
    {
        PIR1bits.TMR2IF = 0;
        while(PIR1bits.TMR2IF == 0);
    }
}

void timer_95()
{
    CCP1CON = 0;
    PR2 = 249;
    CCPR1L = 236;
    TRISCbits.TRISC2 = 0;
    T2CON = 0x00;
    CCP1CON = 0x2c;
    TMR2 = 0;
    T2CONbits.TMR2ON = 1;
    while(1)
    {
        PIR1bits.TMR2IF = 0;
        while(PIR1bits.TMR2IF == 0);
    }
}

void main()
{
    int i = 1;
    ADCON1 = 0x06;                      //Sets RA0 to digital mode
    CMCON = 0x07;
    TRISEbits.TRISE2 = 1;               //set E2 PORTE pins as input
    PORTEbits.RE2 = 1;                  //Here I am not able to SET Value 1

    while(1)
    {
        while(PORTEbits.RE2 == 0)
        {
            switch(i)
            {
                case 1:
                    timer_10();
                    break;
                case 2:
                    timer_50();
                    break;
                case 3:
                    timer_95();
                    break;
            }
            if(i<4)
            {
                i++;
                if(i>=4)
                {
                    i=1;
                }
            }
        }
    }
}

And My compiler Get Stuck in function timer_10(). Please help me.


Solution

  • This is a bit too long for a comment, and I think you're probably able to discover the answer yourself - however, I'll give you some pointers.

    In main()

    TRISEbits.TRISE2 = 1;               //set E2 PORTE pins as input
    PORTEbits.RE2 = 1;                  //Here I am not able to SET Value 1
    

    What behaviour do you expect when you write to port E2 after configuring E2 as an input? It is described in the PIC18F458 data sheet, specifically section 9.5

    In timer_10()

    void timer_10()
    {
        CCP1CON = 0;
        PR2 = 249;
        CCPR1L = 24;
        TRISCbits.TRISC2 = 0;
        T2CON = 0x00;
        CCP1CON = 0x3c;
        TMR2 = 0;
        T2CONbits.TMR2ON = 1;
        while(1)
        {
            PIR1bits.TMR2IF = 0;
            while(PIR1bits.TMR2IF == 0);
        }
    }
    

    You said your compiler got stuck, but I assume you mean debugger, as you were able to run main() to observe PORTEbits.RE2 = 1; not doing what you expected. If you single-step this in function a debugger, where does it get stuck? can you see why?