PIC18f45K20 LED & button programming in C

Discussion in 'Embedded Systems and Microcontrollers' started by couch8p, Feb 6, 2016.

  1. couch8p

    Thread Starter New Member

    Feb 6, 2016
    2
    0
    I have code where an 8 leds light up on my PIC18f45k20 one after the other and back. I want to be able to use a button to pause the led scan then when i push the button again it resumes the led scan. The code i have so far works except it skips an 1 led after pushing the button. below is the code any help is greatly appreciated.



    #include <stdio.h>
    #include <stdlib.h>




    /** C O N F I G U R A T I O N B I T S */
    #pragma config FOSC = INTIO67
    #pragma config WDTEN = OFF, LVP = OFF

    /** I N C L U D E S */
    #include "p18f45k20.h"


    /** D E C L A R A T I O N S **/
    // Port B configuration

    #define Button PORTBbits.RB0

    int i;
    int j;
    int k;
    int temp;
    void pause(int);


    void main (void)
    {
    // int variables
    i=0;
    j=0;
    k=0;
    temp=0;

    // Configure Port D for Output
    TRISD =0b00000000; // PORTD bit 0-7 to output

    // Configure Port B for input
    INTCON2bits.RBPU = 0; // enable PORTB internal pullups
    // note that b/c of pullup logic, the button press state is inverted
    WPUBbits.WPUB0 = 1; // enable pull up on RB0
    ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0)
    TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1)


    while (1)
    {


    // write the entire port that is attached to the LEDs
    LATD=0; // clear all LEDs to 0

    for (k=1; k<256; k=k*2)
    {
    if(!Button)
    {
    temp = k;
    pause(temp);
    }
    else
    {
    LATD = k;
    for (i=0; i<50; i++)
    {
    for (j=0; j<10; j++);

    }
    }
    }
    for (k=128; k>1; k=k*.5)
    {
    if(!Button)
    {
    temp=k;
    pause(temp);

    }
    else
    {
    LATD = k;
    for (i=0; i<50; i++)
    {
    for (j=0; j<10; j++);
    }
    }
    }



    }

    void pause(int y)
    {
    while(1)
    {
    while(Button) //while not pushed
    {
    while(1)
    {
    if(!Button) // if pushed
    {
    for(i=0; i<10; i++)
    {
    for (j=0; j<5; j++);
    }
    while(!Button)// while pushed
    {
    for(i=0; i<10; i++)
    {
    for (j=0; j<5; j++);
    }
    while(Button) //while not pushed
    {
    return;
    }
    }

    }
    else
    {
    LATD = y;
    }

    }
    }
    }

    }
     
  2. couch8p

    Thread Starter New Member

    Feb 6, 2016
    2
    0
    never mind i figured it out
     
Loading...