Need help with button press on 16f684

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

Need help with button press on 16f684

Postby rb8720 » Fri May 30, 2008 6:19 pm

I can't seem to get the chip to detect a button press. Instead it starts flashing as soon as I apply power. Can someone take a look at my code and see if i am doing something wrong. I have tried configureing the chip several ways and still the same results.

Code: Select all
//PREPROCESSOR section/////////////////////////////////////////////////////////////////////////////////////////////

#include "16F684.H"      //PIC chip file (don't also specify in cmd prompt)

#include "delay_ms.h"      //Delay function used below
                          //#include files must be in same folder as cc5x.exe if no path

#pragma config = 0b.0.1100.0100   //Brown-out Detect disabled (0)
            //Code Protect off (1)
            //MCLR set to 'Internal' (1)
            //Powerup timer enabled (0)
            //Watchdog timer disabled (0)
            //Oscillator set to INTRCIO (100)

////port setup//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void init_ports(void) {
   WPUA = 0b00000111;
   TRISA = 0b00111111; // SET AS INPUT
   TRISC = 0b00111111; // set as input
   PORTA = 0b00111111;
   PORTC = 0b00111111;   
   ANSEL = 0;
  // ADCON0 = 0;
  // ADCON1 = 0;
   
 

}

void main (void) {
  while (1) { // Infinite loop
    if (PORTA.1==0) {
      TRISC.2 = 0; // set port to output
      PORTC.2 = 0; // right trigger on
      delay_ms(100);
      PORTC.2 = 1; // RT off
      TRISC.2 = 1; // port set back to input.
      delay_ms(100);
    }
  }
}

   

Thanks,
RB8720
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby riden » Fri May 30, 2008 6:39 pm

Do you have a 10k or so pull up resistor on PORTA.1?

Nothing to do with your problem, but I presume that you have a good reason for switching PORTC.2 repeatedly from an input to an output. Also, you might want to set MCLR as external and add a 10k pull up resistor on the line as it is handy to be able to reset the processor without having to power off and on.
Ralph
riden
 
Posts: 1754
Joined: Fri Jun 03, 2005 10:41 pm
Location: Illinois

Postby jasonharper » Fri May 30, 2008 6:43 pm

You also need to turn off the comparators (CMCON0 = 7) to get RA1 to be a digital input. ALWAYS check the pinout description table in the datasheet - it gives you a complete list of things that can override the function of each pin.
jasonharper
 
Posts: 845
Joined: Wed Mar 02, 2005 12:14 pm
Location: Oklahoma

Postby rb8720 » Fri May 30, 2008 6:53 pm

I do have a reason for switching from input to output. When the pins aren't used i need them to be as input to keep from intefereing with other stuff in the circuit. I have tried turning the comparators off as well no luck, same thing as soon as I apply power starts flashing. I didn't think that i needed pullup resistors with this chip if the weak pull ups are enabled on Port A.

RB8720
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby jasonharper » Fri May 30, 2008 7:13 pm

You haven't enabled any weak pull-ups - there's a bit you have to clear in OPTION_REG, in addition to the individual enables in WPUA.
jasonharper
 
Posts: 845
Joined: Wed Mar 02, 2005 12:14 pm
Location: Oklahoma

Postby rb8720 » Sat May 31, 2008 5:56 am

CAn you tell me which one it is ?
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby leon_heller » Sat May 31, 2008 6:35 am

It's in the data sheet.

Leon
Leon Heller
G1HSM
User avatar
leon_heller
 
Posts: 5675
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Postby rb8720 » Sat May 31, 2008 8:00 am

I check and I did have the pullups enabled and I even tried the bit in the option reg to enable all the pullups still no luck. I tried the same code on a 12f683 that i know is configed correctly and the same result. Its something to do with how i am detecting the button press. I used a different routing on the 12f683. This is it.
Code: Select all
int get_key(void) {

   // Is GP5 low - no so exit
   if (GPIO & (1<<0)) return 0; // no key yet

   delay_ms(1); // wait for key to settle

   // Is GP5 high ? yes so exit = false key.
   if ( (GPIO & (1<<0))>0 ) return 0; // was a false key so restart

   return 1; // key ok so return valid
}

////
//////////////////////////////////////////////////////////////////////
// Start here
void main() {
unsigned int i,c;

   init_ports();

   while (1) {

      // Key hit yet ?
      while(GPIO & (1<<0)) { ; } // wait for input going low.

      // Is the key hit (and vis it valid) ?
      if ( get_key() ) {
         while( !(GPIO & (1<<0)) ) { // loop until input goes high.


I tried to make this work with the 16f684 by changing GPIO to PORTA and (1<<0) to RS ( RS was defined as PORTA.0) THe compiler (cc5x)told me that the syntax was correct but it couldn't compile. All of this is telling me that there is something wrong with my main code. Any suggestions?
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby leon_heller » Sat May 31, 2008 8:30 am

if (GPIO & (1<<0)) return 0; // no key yet

doesn't test for bit 5! 1 << 0 doesn't do any shifting, anyway. It's the same as

if (GPIO & 1) return 0; // no key yet

Try something much simpler like flashing an LED first, and then turn it on and off with the switch.

Leon
Leon Heller
G1HSM
User avatar
leon_heller
 
Posts: 5675
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Postby rb8720 » Sat May 31, 2008 9:33 am


PostPosted: Sat May 31, 2008 8:30 am Post subject:
if (GPIO & (1<<0)) return 0; // no key yet

doesn't test for bit 5! 1 << 0 doesn't do any shifting, anyway. It's the same as

if (GPIO & 1) return 0; // no key yet

Try something much simpler like flashing an LED first, and then turn it on and off with the switch.

Leon


THat was from working code on a 12f683. I changed GPIO to PORTA and 1<<0 to PORTA.0. Ingnore the comments I never changed them when i changed the code. SO how do I port the about debounce sequence to work with a 16f684.

I have no problem flashing the led. The problem is that It starts flashing as soon as I connect power and does not wait for switch detection.
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby leon_heller » Sat May 31, 2008 10:20 am

1 ms might not be enough debounce delay, most switches need 20 ms or so. You need to debounce both the switch closure and opening. I think your problem is somewhere else, though.

Are you sure the switch is connected to RA0? Do you have a pull-up resistor?

Test the switch operation by turning the LED on when the switch is closed, and off when it is open. You don't need any debouncing for that.

The full program and schematic would help.

Leon
Leon Heller
G1HSM
User avatar
leon_heller
 
Posts: 5675
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Postby rb8720 » Sat May 31, 2008 2:20 pm

THe debounce code that I posted works just fine I just can't seem to port it from the 12f683 to the 16f684. It's the other code that I was trying to use that doesn't work. THis is it.
Code: Select all
//PREPROCESSOR section/////////////////////////////////////////////////////////////////////////////////////////////

#include "16F684.H"      //PIC chip file (don't also specify in cmd prompt)

#include "delay_ms.h"      //Delay function used below
                          //#include files must be in same folder as cc5x.exe if no path





#pragma config = 0b.0.1100.0100   //Brown-out Detect disabled (0)
            //Code Protect off (1)
            //MCLR set to 'Internal' (1) (LEDs don't flash with other setting)
            //Powerup timer enabled (0)
            //Watchdog timer disabled (0)
            //Oscillator set to INTRCIO (100)

////port setup//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void init_ports(void) {
   CMCON0 = 0x07;
   WPUA = 0b00000111;
   TRISA = 0b00111111; // SET AS INPUT
   TRISC = 0b00111111; // set as input
   PORTA = 0b00111111;
   PORTC = 0b00111111;   
   ANSEL = 0;
   ADCON0 = 0;
   ADCON1 = 0;
   
   
 

}

void main (void) {
  while (1) { // Infinite loop
    if (PORTA.0==0) {
      TRISC.2 = 0; // set port to output
      PORTC.2 = 0; // right trigger on
      delay_ms(100);
      PORTC.2 = 1; // RT off
      TRISC.2 = 1; // port set back to input.
      delay_ms(100);
    }
  }
}

   


I have tried detecting the pin in the low state and in the high state. When I have it set to detect low, it starts flashing as soon as I apply power. When i have it detect high it on'tflash at all.
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby leon_heller » Sat May 31, 2008 2:57 pm

Why are you changing RC2 from an input to an output and back again? What is connected to it? A schematic would help.

What happens if you try (pseudocode):

do forever
if (button is pressed)
LED on
else
LED off
end do

You don't need a delay. It's what I suggested earlier.

Leon
Leon Heller
G1HSM
User avatar
leon_heller
 
Posts: 5675
Joined: Sun May 01, 2005 11:20 am
Location: St. Leonards-on-Sea, E. Sussex, UK.

Postby rb8720 » Sat May 31, 2008 3:15 pm

I have to change from input to output to keep if from interfering with the rest of the circuit. I haven't tried the Psuedocode. I'll try it and see what happens.
rb8720
 
Posts: 26
Joined: Fri May 09, 2008 12:24 pm

Postby Philba » Sat May 31, 2008 5:17 pm

Hmmm, seems like a fair amount of repetition. I'll give it a try.

The first thing I would do is make sure that you are actually calling the init routing. several of the code sequences you have posted didn't have it called.

Next thing to do is put a DVM on the actual input pin and check to see if the pin is actually seeing different voltage levels when putton pressed or not. Verify that it is, indeed, the pin you are testing (given the way microchip moves things around, it never hurts). Verify that weak pullups are working - disconnect the switch and check the pin with a DVM - it should read Vcc, Once you are certain that the pin is seeing the right things, move onto the software.

You've gotten a fair number of good suggestions on that front but I would start from zero with Leon's suggestions. Cut the program down to the absolute simplest piece of code. Verify that the chip actually got programmed. I'd blink an LED just to make sure that it's running.
Philba
 
Posts: 2503
Joined: Sun Feb 13, 2005 11:33 pm
Location: Seattle

Next

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest