Hi, I've just recently picked up coding the chipKIT board and ran into a problem with button inputs. When I run my code on the board, I need to push my button1 about five times before it exits the while loop and executes the next function. What is wrong with my code?
Question
davc
Hi, I've just recently picked up coding the chipKIT board and ran into a problem with button inputs. When I run my code on the board, I need to push my button1 about five times before it exits the while loop and executes the next function. What is wrong with my code?
Thanks.
The following is my code
#include <plib.h> //include peripheral library
#include <stdio.h>
#include <stdlib.h>
#pragma config ICESEL = ICS_PGx1 //configure on board licensed debugger
#pragma config FNOSC = PRIPLL //configure system clock 80 MHz
#pragma config POSCMOD = EC
#pragma config FPLLIDIV = DIV_2
#pragma config FPLLMUL = MUL_20
#pragma config FPLLODIV = DIV_1
#pragma config FPBDIV = DIV_8 //configure peripheral bus clock to 10 MHz
#define TIMER 100000 //Set the period or flashing rate
int press;
int input;
int i;
int count;
void turnOn(void){
//Set button 1 as an input
PORTSetPinsDigitalIn(IOPORT_G, BIT_6);
//Set LEDs as output
PORTSetPinsDigitalOut(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
input = 0;
while(!input)
{
PORTSetBits(IOPORT_G,BIT_12);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn off LEDs
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn on LED 2
PORTSetBits(IOPORT_G, BIT_13);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn off LEDs
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn on LED 3
PORTSetBits(IOPORT_G, BIT_14);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn off LEDs
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn on LED 4
PORTSetBits(IOPORT_G, BIT_15);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
//Turn off LEDs
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
for(i = 0; i < TIMER && !input; i++)
{
input = PORTReadBits(IOPORT_G, BIT_6);
}
}
}
void main(void){
//Set button 1 as an input
PORTSetPinsDigitalIn(IOPORT_G, BIT_6);
//Set LEDs as output
PORTSetPinsDigitalOut(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
while (1) //perform the following loop indefinitely
{
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
press = 0;
while(!press)
{
press = PORTReadBits(IOPORT_G, BIT_6);
}
turnOn();
PORTClearBits(IOPORT_G, BIT_12 | BIT_13 | BIT_14 | BIT_15);
}
}
Link to comment
Share on other sites
1 answer to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.