Anonymous - 2012-03-25

Hello,

I am new to this programming and I'm not sure I'm asking this in the right place. I have programmed a PIC16f84a chip with sdcc, and the behaviour is very different from that that gpsim gave - I just wondered if someone might know why. The code I wrote is included at the bottom.

The intended behaviour is to attach a momentary switch to RB7. Initially an led is connected to RB0 - when the switch is pressed and then released RB1 switches on, and RB0 switches off - this activates on release. It is supposed to cycle through RB0, RB1 RB2, RB0, RB1 etc…

Everything seemed ok on programming and on compiling but the behaviour of the board is very odd - the leds cycle continuously until power is given to RB7 - then upon release they cycle very fast again. I wondered if I had some mistake with the configuration, or possibly do I have a defective chip?

Any help very much appreciated - thanks a lot in advance!

Ewen

==================

#define __16f84A
#include "pic16f84a.h"

/* Setup chip configuration */
typedef unsigned char config;
config at 0x2007 __CONFIG = _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF;

/*  set up booleans for button pressed   */
int i;
int pressed;

void main(void) {
  TRISB=0xF0; // RB4:RB7 are input ; 11110000 in binary
  i = 0;
  pressed = 0;

  RB0 = 1;  //clean
  RB1 = 0;  //octave
  RB2 = 0;  //effect

  for(;;)
    {
     
      if(RB4)
        {
  pressed = 1;
}
      else
{
  if (pressed == 1)
    {     
      i = (i+1)%3;
      if (i == 0)
{
  RB0 = 1;
  RB1 = 0;
  RB2 = 0;
}
      else if (i == 1)
{
  RB0 = 0;
  RB1 = 1;
  RB2 = 0;
}
      else if (i == 2)
{
  RB0 = 0;
  RB1 = 0;
  RB2 = 1;
}
      pressed = 0;       
    }
}

    }
 
}