16f690 port A as inputs?

Thread Starter

chris13409

Joined Jan 7, 2010
7
Here I go showing just how little I know. I am making a 16F690 circuit that will monitor 4 inputs. When an input goes low it will light a green led. When an input goes high it will light a red led.
At this time I'm using port A for the input and ports B and C for the outputs.
(The actual pin assignments aren't cast in stone)
I don't know anything about writing any kind of code, so ….
I initially created the code using FlowCode, and compiled it with Hi-Tech ANSI C. Next I ran it on PIC Simulator IDE. I works, for the most part. When RA3/MRCLR is is toggled high RB7 (green) goes out and RC0 (red)comes on. Problem is the red led blinks. I have the same issue with RA4 /OSC2
I think the reason is that RA3 is MRCLR. If so, is the cure to configure port A as INPUT?
I'd try it myself if I knew what to cut and paste into the code. Could someone spoon feed me the answer?

Rich (BB code):
#define MX_PIC
//Defines for microcontroller
#define P16F690
//Functions
#define MX_CLK_SPEED 19660800
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif
//Configuration data
//__CONFIG(0x3ff4);
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT \
  & BORDIS & IESODIS & FCMDIS);
//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"
//Macro function declarations

//Variable declarations
char FCV_INPUTA4;
char FCV_INPUTA1;
char FCV_INPUTA2;
char FCV_INPUTA3;
 

//Macro implementations
void main()
{
 
 //Initialisation
// ansel = 1;
//    anselh = 1;
ANSEL = 0x00;
ANSELH = 0x00;
 //Interrupt initialisation code
 option_reg = 0xC0;

 //Loop
 //Loop: While 1
 while (1)
 {
  //Input
  //Input: A4 -> inputA4
  trisa = trisa | 0x10;
  FCV_INPUTA4 = ((porta & 0x10) == 0x10);

  //Decision
  //Decision: inputA4=1?
  if (FCV_INPUTA4==1)
  {
   //Output
   //Output: 0 -> C0
   trisc = trisc & 0xfe;
   if (1)
    portc = (portc & 0xfe) | 0x01;
   else
    portc = portc & 0xfe;

   //Output
   //Output: 1 -> B4
   trisb = trisb & 0xef;
   if (0)
    portb = (portb & 0xef) | 0x10;
   else
    portb = portb & 0xef;

  } else {
   //Output
   //Output: 1 -> C0
   trisc = trisc & 0xfe;
   if (0)
    portc = (portc & 0xfe) | 0x01;
   else
    portc = portc & 0xfe;

   //Output
   //Output: 0 -> B4
   trisb = trisb & 0xef;
   if (1)
    portb = (portb & 0xef) | 0x10;
   else
    portb = portb & 0xef;

  }

  //Input
  //Input: A1 -> inputA1
  trisa = trisa | 0x02;
  FCV_INPUTA1 = ((porta & 0x02) == 0x02);

  //Decision
  //Decision: inputA1=1?
  if (FCV_INPUTA1==1)
  {
   //Output
   //Output: 0 -> C1
   trisc = trisc & 0xfd;
   if (1)
    portc = (portc & 0xfd) | 0x02;
   else
    portc = portc & 0xfd;

   //Output
   //Output: 1 -> B5
   trisb = trisb & 0xdf;
   if (0)
    portb = (portb & 0xdf) | 0x20;
   else
    portb = portb & 0xdf;

  } else {
   //Output
   //Output: 1 -> C1
   trisc = trisc & 0xfd;
   if (0)
    portc = (portc & 0xfd) | 0x02;
   else
    portc = portc & 0xfd;

   //Output
   //Output: 0 -> B5
   trisb = trisb & 0xdf;
   if (1)
    portb = (portb & 0xdf) | 0x20;
   else
    portb = portb & 0xdf;

  }

  //Input
  //Input: A2 -> inputA2
  trisa = trisa | 0x04;
  FCV_INPUTA2 = ((porta & 0x04) == 0x04);

  //Decision
  //Decision: inputA2=1?
  if (FCV_INPUTA2==1)
  {
   //Output
   //Output: 0 -> C3
   trisc = trisc & 0xf7;
   if (1)
    portc = (portc & 0xf7) | 0x08;
   else
    portc = portc & 0xf7;

   //Output
   //Output: 1 -> B6
   trisb = trisb & 0xbf;
   if (0)
    portb = (portb & 0xbf) | 0x40;
   else
    portb = portb & 0xbf;

  } else {
   //Output
   //Output: 1 -> C3
   trisc = trisc & 0xf7;
   if (0)
    portc = (portc & 0xf7) | 0x08;
   else
    portc = portc & 0xf7;

   //Output
   //Output: 0 -> B6
   trisb = trisb & 0xbf;
   if (1)
    portb = (portb & 0xbf) | 0x40;
   else
    portb = portb & 0xbf;

  }
  //Input
  //Input: A3 -> inputA3
  trisa = trisa | 0x08;
  FCV_INPUTA3 = ((porta & 0x08) == 0x08);

  //Decision
  //Decision: inputA3=1?
  if (FCV_INPUTA3==1)
  {
   //Output
   //Output: 0 -> C0
   trisc = trisc & 0xfe;
   if (1)
    portc = (portc & 0xfe) | 0x01;
   else
    portc = portc & 0xfe;

   //Output
   //Output: 1 -> B7
   trisb = trisb & 0x7f;
   if (0)
    portb = (portb & 0x7f) | 0x80;
   else
    portb = portb & 0x7f;

  } else {
   //Output
   //Output: 1 -> C0
   trisc = trisc & 0xfe;
   if (0)
    portc = (portc & 0xfe) | 0x01;
   else
    portc = portc & 0xfe;

   //Output
   //Output: 0 -> B7
   trisb = trisb & 0x7f;
   if (1)
    portb = (portb & 0x7f) | 0x80;
   else
    portb = portb & 0x7f;
 }
}
 mainendloop: goto mainendloop;
}
void MX_INTERRUPT_MACRO(void)
{
}
 

t06afre

Joined May 11, 2009
5,934
:eek:Your code is way to complex. So I think you need a push in the correct direction. You should learn C by coding and not using Flowcode. The result from flowcode is horrifying. You should also always have the data sheet at hand then programming. Even if you use C you must understand the inner workings of the MCU. Pay attention to section 14, it is perhaps the most important section.
An important thing then working with MCUs is bit operations. Take a look at the HI-Tech C sample ...\samples\misc\doll.c to learn something. In the example I have assumed that the input is debounced. The system will run with default 4MHz clock speed. Not tested in full scale only run in simulator
Rich (BB code):
// Configuration Mask Definitions from the 16f685.h file in the HT-soft compiler
//#define EXTCLK  0x3FFF // External RC Clockout
//#define EXTIO  0x3FFE // External RC No Clock
//#define INTCLK  0x3FFD // Internal RC Clockout
//#define INTIO  0x3FFC // Internal RC No Clock
//#define EC  0x3FFB // EC
//#define HS  0x3FFA // HS
//#define XT  0x3FF9 // XT
//#define LP  0x3FF8 // LP
//// Watchdog Timer 
//#define WDTEN  0x3FFF // On
//#define WDTDIS  0x3FF7 // Off
//// Power Up Timer 
//#define PWRTDIS  0x3FFF // Off
//#define PWRTEN  0x3FEF // On
//// Master Clear Enable 
//#define MCLREN  0x3FFF // MCLR function is enabled
//#define MCLRDIS  0x3FDF // MCLR functions as IO
//// Code Protect 
//#define UNPROTECT 0x3FFF // Code is not protected
//#define CP  0x3FBF // Code is protected
//#define PROTECT  CP //alternate
//// Data EE Read Protect 
//#define UNPROTECT 0x3FFF // Do not read protect EEPROM data
//#define CPD  0x3F7F // Read protect EEPROM data
//// Brown Out Detect 
//#define BORDIS  0x3CFF // BOD and SBOREN disabled
//#define SWBOREN  0x3DFF // SBOREN controls BOR function (Software control)
//#define BORXSLP  0x3EFF // BOD enabled in run, disabled in sleep, SBOREN disabled
//#define BOREN  0x3FFF // BOD Enabled, SBOREN Disabled
//// Internal External Switch Over Mode 
//#define IESOEN  0x3FFF // Enabled
//#define IESODIS  0x3BFF // Disabled
//// Monitor Clock Fail-safe 
//#define FCMEN  0x3FFF // Enabled
//#define FCMDIS  0x37FF // Disabled
//Using PIC16f690
 
#include <htc.h>
__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
void main(void)
{
 TRISA=0xff;
 TRISB=0x00;
 TRISC = 0b00000000;// port directions: 1=input, 0=output
 PORTB=0x00;
 PORTC = 0x0;
 ANSEL=0;
 ANSELH=0;
while (1){
       if(RA0) 
    {RB4=0,RC4=1;
    }
           else 
   {RB4=1,RC4=0;
   }
   if(RA1) 
    {RB5=0,RC5=1;
    }
           else 
   {RB5=1,RC5=0;
   }
}
}
If we remove not needed brackets and clean up, the code is even more compact
Rich (BB code):
//Using PIC16f690
 #include <htc.h>
__CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
void main(void)
{
 TRISA=0xff;
 TRISB=0x00;
 TRISC = 0b00000000;// port directions: 1=input, 0=output
 PORTB=0x00;
 PORTC = 0x0;
 ANSEL=0;
 ANSELH=0;
while (1){
       if(RA0) 
   RB4=0,RC4=1;
       else 
       RB4=1,RC4=0;
    if(RA1) 
   RB5=0,RC5=1;
       else 
     RB5=1,RC5=0;
   }
}
 
Last edited:

Vaughanabe13

Joined May 4, 2009
102
The real answer to your question is that YES, the MCLR pin is what's causing the flashing. The MCLR pin MUST always be tied to VCC with a resistor (10k, for example). It's called the "master clear" pin and when you make it go low the PIC's program counter is loaded with the reset vector and the program starts from the top again. You can get around this by either changing that function to another pin, or by disabling the MCLR function in the configuration bits. I would advise just changing the port pins you are using. Also, you can do all of these functions on one port, you don't need to use multiple ports.
 

t06afre

Joined May 11, 2009
5,934
Some programmers like the PICKIT 2 and 3 also take control over the MCLR pin as long as the they are connected. So you have to disconnect the programmer before you can use this pin as a GP input pin
In your first code you have by setting the MCLRDIS option in the configuration word ,disabled the MCLR function. So the the RA3 is a standard input pin. You have also set the INTIO configuration bit. RA4,RA5 have standard I/O functions.
 

Thread Starter

chris13409

Joined Jan 7, 2010
7
Thanks t06
I've decided that flowcode is not the way to go. I added two inputs to the code you provided. Easy as pie. Just copy/paste then change the port numbers as required. My next step is to add comments to understand what each part does.
 

t06afre

Joined May 11, 2009
5,934
Thanks t06
I've decided that flowcode is not the way to go. I added two inputs to the code you provided. Easy as pie. Just copy/paste then change the port numbers as required. My next step is to add comments to understand what each part does.
That sound like a plan. Get some good books about C programming. I can recommend this book as a general book about C. http://books.google.com/books?id=Nfh5-L3NBTQC
I use it more as a reffence. Some people on Google book did not like it. This book look also like a good book. But I have not used it.
http://www.amazon.com/gp/product/1438231598/
The best thing is to go down to the book store and find a book you feel comfortable with.
The only way to learn a computer language is to use it. Many books come with a CD containing the examples. DO NOT just use cut and paste. Type in the examples. You will get it into your fingers much better that way. And learn some debugging for free.
 
Top