CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Jump out from while on interrupt
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon May 09, 2005 3:59 pm     Reply with quote

Hi DragonPIC,

Take in mind that the poster problem was that he canīt detect the pushbutton
action at all and I focused the help in the hardware and not in the best way to
debounce a switch contact.
If you want to know my opinion regarding code for debouncing, by far the best way is
using interrupts:

The level change impossed by the switch action is detected by any of the uC external
hardware interrupts, inside the interrupt clear a global counter and enable one timer,
wich every time it overflows increment the counter, after n counts bit test if the switch
had changed to confirm that the toggle was done, if yes just disable the timer interrupt.

You get a clean and efective debouncing technique without overhead nor wasting
time in delays while you have been running other code in the main.

Best wishes,

Humberto
Guest








PostPosted: Tue May 10, 2005 1:53 am     Reply with quote

______________ +5V
|
|
_
| ° Button
| °
|
|_____________ PIN_A3
|
|
|
|
|_____________ Gnd
Tester21
Guest







PostPosted: Tue May 10, 2005 1:56 am     Reply with quote

Code:
            ______________ +5V
             |
             |
             \
             /   R 10K
             \
             /
           
             |
             |_____________ PIN_A3
             |
             |
             |
           | °  Button
           | °
             |
             |_____________ Gnd


This is schematic for my button...
Tester21
Guest







PostPosted: Tue May 10, 2005 6:12 am     Reply with quote

Hmmmm...
The situation is the same.
When programm hits the "first option" there is no way out.
The button function is called ok but i cant break out, to start something else...

Code:
short button(void)
{
     
   int cnt;
   output_high(PIN_B2);
   while(intr)
   {
     delay_us(5); //
     cnt++;   
     if(cnt == 25)
     output_high(PIN_B1);    
       return(1);
   }
   return(0);
}
...

lcd_init();
sec=0;
while(sec < 15)
{
printf(lcd_putc,"\f First option");
delay_ms(200);
if (button()) break;
}
printf(lcd_putc,"\f Second option");
}
Guest








PostPosted: Tue May 10, 2005 10:51 am     Reply with quote

Code:
short button(void)
{
     
   int cnt;
   output_high(PIN_B2);
   while(intr)
   {
     delay_us(5); //
     cnt++;   
     if(cnt == 25)
     {
       output_high(PIN_B1);     
       return(1);   //if you reach this point, after return you should break from loop
      }
   }
   return(0);
}
...

lcd_init();
sec=0;
while(sec < 15)
{
  printf(lcd_putc,"\f First option, sec = %u",sec);
  delay_ms(900);
  if (button())
    break;
}
printf(lcd_putc,"\f Second option");


try that
Tester21
Guest







PostPosted: Thu May 12, 2005 3:26 am     Reply with quote

Quote:
//if you reach this point, after return you should break from loop


No, i can't reach this point.
Practicly, no response on clicks...


p.s.
What is the difference between #define intr pin A_3 and
Byte, bit definitions ?
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Thu May 12, 2005 10:02 am     Reply with quote

It would be helpful if you displayed your whole program now since I do not know how you are initializing your port. You should have a "set_tris_a(0x08)" to make the first bit an input on that port (this is the pin you are using the button on right?).
_________________
-Matt
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Thu May 12, 2005 10:16 am     Reply with quote

[quote="Tester21"]
Quote:



p.s.
What is the difference between #define intr pin A_3 and
Byte, bit definitions ?


PIN_A3 id usually used with the functions like "nput(PIN_A3);" so you could use:

Code:
#define PIN_A3 43
while(input(PIN_A3))
{
...
}


which you may also use. I do not like using these functions and not to fond of letting a function handle my port directional control. See your CCS manual for information on the difference.
_________________
-Matt
Tester21
Guest







PostPosted: Fri May 13, 2005 7:30 am     Reply with quote

Button is connected like in the shematics, on the PIN A3.

Maybe i have problems with port definitions... This i the code:

Code:

...
#BYTE    port_a  = 3
#bit         intr = port_a.0
set_tris_a(0x08);

short button(void)
{
  int cnt;
  while(intr)
  {
   delay_us(5); //
   cnt++; 
   if(cnt == 25)
   {
    output_high(PIN_B1);   
    return(1); 
   }
  }
  return(0);
}


main()
{
lcd_init();
sec=0;
while(sec < 15)
{
 printf(lcd_putc,"\f First option, sec = %u",sec);
 delay_ms(900);
 if (button())  break;
}
printf(lcd_putc,"\f Second option");
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri May 13, 2005 8:46 am     Reply with quote

Quote:

Maybe i have problems with port definitions... This i the code:
...
#BYTE port_a = 3
#bit intr = port_a.0
set_tris_a(0x08);


For sure.

#BYTE port_a = 5

If you have the pushbutton connected in port_a.0, this PIN must be an input.

set_tris_a(0x01); // 0b00000001

Humberto
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Fri May 13, 2005 12:21 pm     Reply with quote

if your button is on pin A3 and not pin A0 then you should have

#bit intr_button = port_a.3
and
set_tris_a(0x08);

Code:
#BYTE    port_a  = 5
#BIT         intr = port_a.3

short button(void)
{
  int cnt;
  while(intr_button)
  {
   delay_us(5); //
   cnt++; 
   if(cnt == 25)
   {
    output_high(PIN_B1);   
    return(1); 
   }
  }
  return(0);
}


main()
{
set_tris_a(0x08);
lcd_init();
 
for(sec = 0;sec < 15,sec++)
{
 printf(lcd_putc,"\f First option, sec = %u",sec);
 delay_ms(900);
 if (button())  break;
}
printf(lcd_putc,"\f Second option");

}

_________________
-Matt
Tester21
Guest







PostPosted: Fri May 13, 2005 12:26 pm     Reply with quote

Quote:
if your button is on pin A3 and not pin A0 then you should have

It is on A3 ... it was my typing error.

But the program is still not working...
Is there anything else which could make a problem ?
Guest








PostPosted: Mon May 16, 2005 12:20 pm     Reply with quote

Yeah:
1) check your hardware to make sure connections are made right. Use an oscilloscope at the pin of the PIC to make sure the signal is going up and down with each button press.

2) Use a debugging program to make sure bit A3 in the internal register is being set and cleared and make sure the port is being configured correctly.


Just so I unserstand the problem fully, you do see the LCD count up the sec from 0 - 15 with no problem from the

printf(lcd_putc,"\f First option, sec = %u",sec);

then you see the message from

printf(lcd_putc,"\f Second option");

then program end. right?

The problem is that the button on A3 will not interrupt the 0-15 count. right?
Tester21
Guest







PostPosted: Tue May 17, 2005 4:58 am     Reply with quote

Code:
 you do see the LCD count up the sec from 0 - 15 with no problem from the


Yes i see the count but i can't break it...
The hardware connection is ok and cheked.

To you have some sample program so i can test it on my board?
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Tue May 17, 2005 1:03 pm     Reply with quote

see modifications

Do you have a debugger? That is the only way to be sure the PIC is reading bits from port A correctly. Or you could try printing the port information out to LCD.

I also added code to make sure the ADC was disabled.

Code:

#BYTE    port_a  = 5
#BIT     intr_button = port_a.3
int sec;


int button(void)
{
  int cnt;
  printf(lcd_putc,"\f  %u",port_a);
  while(intr_button == 0) // since you have a pullup with button grounding
  {
   delay_us(5); //
   cnt++; 
   if(cnt == 25)
   {
    output_high(PIN_B1);   
    return(1); 
   }
  }
  return(0);
}


main()
{

setup_adc_ports(ADC_OFF); // just in case
set_tris_a(0x08);
lcd_init();
 
for(sec = 0;sec < 15;sec++)
{
 printf(lcd_putc,"\f First option, sec = %u",sec);
 delay_ms(450);
 if (button())  break;
 delay_ms(450); 
}
delay_ms(450);
printf(lcd_putc,"\f Second option");

}

_________________
-Matt
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group