P12f683 Turning on/off Led Problems

General discussion on mikroC.
Post Reply
Author
Message
cduryee
Posts: 2
Joined: 05 Feb 2016 14:14

P12f683 Turning on/off Led Problems

#1 Post by cduryee » 05 Feb 2016 14:38

//So, I am trying to light a Led with momentary button press for on and off.
//Pic 12f683 4MHZ
//MikroC Pro V. 6.6.1 IDE
//PICkit 2 programmer with demo board for programming
//Delaying Led coming on for 3 seconds after first button press and stays on.//
//Pressing button again turns led off.
Using 3v to Pic
10k R on GP3 // button has pull up resistor 10k
330ohm on GP0,1

Right now, I feel I'm close to seeing this working. But something is wrong and I can't seem to figure it out.
So nothing is working as desired right now. I hook power and press button led comes on after 3 seconds. Second button press and led turns of briefly then comes back on and I don't know why right now.
I'm hoping someone might see a problem glaring at them. Take a look please.
Thanks,
Chris

unsigned char db_cnt; //Button Debounce timer
unsigned char bttn_cnt; //Button Press counter
unsigned char i = 0; //GPIO var for reading pin/bit status
//Blinking working
/*void main(void)
{
TRISIO.B0 = 0;
TRISIO.B1 = 0;
while(1)
{
GPIO = 0b00110111; //Binary works
//GPIO.B0 = 1;
//GPIO.B1 = 1; // Specified bit does work now see my notes below
Delay_ms(1000);
GPIO = 0b00000000;
//GPIO.B0 = 0;
//GPIO.B1 = 1; didn't work, due to CMCON1 = 0x00; was not set. 0x07 was not right.
Delay_ms(1000);
}
}*/

/* void bttn_db() {
while (db_cnt < 10){
Delay_ms(1);
if (GP3_bit == 0)
db_cnt++;
else
db_cnt = 0;
}
}
*/

void main() {
TRISIO = 0b00101111;
GPIO = 0b00000000;
ANSEL = 0x00;
CMCON0 = 0x07;
CMCON1 = 0x00;


db_cnt = 0;
bttn_cnt = 0;

/* while (GP3_bit == 0) {
Delay_ms(10);
if (GP3_bit == 0)
bttn_cnt++;
} */

while(1){
i = GP3_bit;
if (bttn_cnt > 2){ bttn_cnt = 0;}
while (db_cnt < 10){
Delay_ms(1);
if (GP3_bit == 0) //GP3_bit == 0 could be replaced with i == 0
db_cnt++; // i == 0
else if (GP3_bit == 1){
db_cnt = 0;
}
} //go to try if> else if> else
bttn_cnt++;
db_cnt = 0;
if (bttn_cnt == 1){
Delay_ms(3000);
//GPIO = 0b00000011;
TRISIO0_bit = 0;
TRISIO1_bit = 0;
GPIO.B0 = 1;
GPIO.B1 = 1;
}
else if (bttn_cnt == 2){
// GPIO = 0b00000000;
//TRISIO0_bit = 1;
// TRISIO1_bit = 1;
GPIO.B0 = 0;
GPIO.B1 = 0;
TRISIO = 0xFF;
// bttn_cnt = 0;
// break;
}
else {
0xFF; //to make sure leds are off
bttn_cnt =0;
}

// if (bttn_cnt == 0){
/*while (db_cnt < 10){
Delay_ms(1);
if (GP3_bit == 0) //GP3_bit == 0 could be replaced with i == 0
db_cnt++; // i == 0
else if (GP3_bit == 1){
db_cnt = 0;
}*/
}
// bttn_cnt++;
}
Last edited by cduryee on 08 Feb 2016 18:23, edited 1 time in total.

cduryee
Posts: 2
Joined: 05 Feb 2016 14:14

Re: P12f683 Turning on/off Led Problems

#2 Post by cduryee » 08 Feb 2016 17:56

So, I have it working with 1st button press 3 second delay led comes on and stays on. second press led turns off now but something is still wrong because led comes back on after a couple of seconds and stays on. Please help!

unsigned char db_cnt; //Button Debounce timer
unsigned char bttn_cnt; //Button Press counter
//unsigned char i = 0; //GPIO var for reading pin or bit status



void main() {
TRISIO = 0b00101000;
GPIO = 0b00000000;
ANSEL = 0x00;
CMCON0 = 0x07;
CMCON1 = 0x00;


db_cnt = 0;
bttn_cnt = 0;

/* while (GP3_bit == 0) {
Delay_ms(10);
if (GP3_bit == 0)
bttn_cnt++;
} */

while(1){
i = GP3_bit;
while (db_cnt < 10){
Delay_ms(1);
if (GP3_bit == 0)
db_cnt++;
else if (GP3_bit == 1){
db_cnt = 0;
}
}
bttn_cnt++;
db_cnt = 0;
if (bttn_cnt == 1){
Delay_ms(3000);
//GPIO = 0b00000011;
TRISIO0_bit = 0;
TRISIO1_bit = 0;
GPIO.B0 = 1;
GPIO.B1 = 1;
}
else if (bttn_cnt == 2){
// GPIO = 0b00000000;
TRISIO0_bit = 1;
TRISIO1_bit = 1;
GPIO.B0 = 0;
GPIO.B1 = 0;
// TRISIO = 0xFF;
bttn_cnt = 0;
}
}
}

Post Reply

Return to “mikroC General”