Tuesday, 13 August 2013

First Code For PIC18F26J50

Goal : Toggle the LED at different frequencies

Before we start to write the code we need to include necessay libraries and set the configuration Bits of the controller.
For the current microcontroller , We can set the configuration bits using
#pragma config

Though It can be generated using Mplabx as follows.
Go to the Window-> PIC memory views -> configuration Bits
after setting configuration bits .
Click - Geneate Source code to output and It will post the configuration Bits in the same file.

//Using external oscillator 16Mhz.

// User Defined Delay Function DelayMs
void DelayMs(int dly) {
    unsigned int x;
    while (dly > 0) {
        x = 625;
        while (--x)continue;
        dly--;
    }
}

//Main Code
int main(int argc, char** argv) {
 
    TRISC = 0xFF;    //make PortC input Port
    TRISB = 0x00;     //make PortB Output Port
    LATB7 = 0;          //Initiallly All LEDS are off
    LATB6 = 0;
    LATB5 = 0;
    LATC0 = 1;          // Making input high for Pull Up

    //Pull up Resistor
    //pressing RC0 pin will blink LED slower on B7 pin
    while(RC0 == 0){
          LATB7 = 1;
          DelayMs(2000);
          LATB7 = 0;
          DelayMs(2000);}

    //pressing RC1 pin will blink LED faster on B6 pin
    while(RC1 == 0){
         LATB6 = 1;
         DelayMs(500);
         LATB6 = 0;
         DelayMs(500);}

    return (EXIT_SUCCESS);
}

No comments:

Post a Comment