16f690 port A as inputs?

Discussion in 'Embedded Systems and Microcontrollers' started by chris13409, Jun 16, 2010.

  1. chris13409

    Thread Starter New Member

    Jan 7, 2010
    7
    0
    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?

    Code ( (Unknown Language)):
    1.  
    2. #define MX_PIC
    3. //Defines for microcontroller
    4. #define P16F690
    5. //Functions
    6. #define MX_CLK_SPEED 19660800
    7. #ifdef _BOOSTC
    8. #include <system.h>
    9. #endif
    10. #ifdef HI_TECH_C
    11. #include <pic.h>
    12. #endif
    13. //Configuration data
    14. //__CONFIG(0x3ff4);
    15. __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT \
    16.   & BORDIS & IESODIS & FCMDIS);
    17. //Internal functions
    18. #include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"
    19. //Macro function declarations
    20.  
    21. //Variable declarations
    22. char FCV_INPUTA4;
    23. char FCV_INPUTA1;
    24. char FCV_INPUTA2;
    25. char FCV_INPUTA3;
    26.  
    27.  
    28. //Macro implementations
    29. void main()
    30. {
    31.  
    32.  //Initialisation
    33. // ansel = 1;
    34. //    anselh = 1;
    35. ANSEL = 0x00;
    36. ANSELH = 0x00;
    37.  //Interrupt initialisation code
    38.  option_reg = 0xC0;
    39.  
    40.  //Loop
    41.  //Loop: While 1
    42.  while (1)
    43.  {
    44.   //Input
    45.   //Input: A4 -> inputA4
    46.   trisa = trisa | 0x10;
    47.   FCV_INPUTA4 = ((porta & 0x10) == 0x10);
    48.  
    49.   //Decision
    50.   //Decision: inputA4=1?
    51.   if (FCV_INPUTA4==1)
    52.   {
    53.    //Output
    54.    //Output: 0 -> C0
    55.    trisc = trisc & 0xfe;
    56.    if (1)
    57.     portc = (portc & 0xfe) | 0x01;
    58.    else
    59.     portc = portc & 0xfe;
    60.  
    61.    //Output
    62.    //Output: 1 -> B4
    63.    trisb = trisb & 0xef;
    64.    if (0)
    65.     portb = (portb & 0xef) | 0x10;
    66.    else
    67.     portb = portb & 0xef;
    68.  
    69.   } else {
    70.    //Output
    71.    //Output: 1 -> C0
    72.    trisc = trisc & 0xfe;
    73.    if (0)
    74.     portc = (portc & 0xfe) | 0x01;
    75.    else
    76.     portc = portc & 0xfe;
    77.  
    78.    //Output
    79.    //Output: 0 -> B4
    80.    trisb = trisb & 0xef;
    81.    if (1)
    82.     portb = (portb & 0xef) | 0x10;
    83.    else
    84.     portb = portb & 0xef;
    85.  
    86.   }
    87.  
    88.   //Input
    89.   //Input: A1 -> inputA1
    90.   trisa = trisa | 0x02;
    91.   FCV_INPUTA1 = ((porta & 0x02) == 0x02);
    92.  
    93.   //Decision
    94.   //Decision: inputA1=1?
    95.   if (FCV_INPUTA1==1)
    96.   {
    97.    //Output
    98.    //Output: 0 -> C1
    99.    trisc = trisc & 0xfd;
    100.    if (1)
    101.     portc = (portc & 0xfd) | 0x02;
    102.    else
    103.     portc = portc & 0xfd;
    104.  
    105.    //Output
    106.    //Output: 1 -> B5
    107.    trisb = trisb & 0xdf;
    108.    if (0)
    109.     portb = (portb & 0xdf) | 0x20;
    110.    else
    111.     portb = portb & 0xdf;
    112.  
    113.   } else {
    114.    //Output
    115.    //Output: 1 -> C1
    116.    trisc = trisc & 0xfd;
    117.    if (0)
    118.     portc = (portc & 0xfd) | 0x02;
    119.    else
    120.     portc = portc & 0xfd;
    121.  
    122.    //Output
    123.    //Output: 0 -> B5
    124.    trisb = trisb & 0xdf;
    125.    if (1)
    126.     portb = (portb & 0xdf) | 0x20;
    127.    else
    128.     portb = portb & 0xdf;
    129.  
    130.   }
    131.  
    132.   //Input
    133.   //Input: A2 -> inputA2
    134.   trisa = trisa | 0x04;
    135.   FCV_INPUTA2 = ((porta & 0x04) == 0x04);
    136.  
    137.   //Decision
    138.   //Decision: inputA2=1?
    139.   if (FCV_INPUTA2==1)
    140.   {
    141.    //Output
    142.    //Output: 0 -> C3
    143.    trisc = trisc & 0xf7;
    144.    if (1)
    145.     portc = (portc & 0xf7) | 0x08;
    146.    else
    147.     portc = portc & 0xf7;
    148.  
    149.    //Output
    150.    //Output: 1 -> B6
    151.    trisb = trisb & 0xbf;
    152.    if (0)
    153.     portb = (portb & 0xbf) | 0x40;
    154.    else
    155.     portb = portb & 0xbf;
    156.  
    157.   } else {
    158.    //Output
    159.    //Output: 1 -> C3
    160.    trisc = trisc & 0xf7;
    161.    if (0)
    162.     portc = (portc & 0xf7) | 0x08;
    163.    else
    164.     portc = portc & 0xf7;
    165.  
    166.    //Output
    167.    //Output: 0 -> B6
    168.    trisb = trisb & 0xbf;
    169.    if (1)
    170.     portb = (portb & 0xbf) | 0x40;
    171.    else
    172.     portb = portb & 0xbf;
    173.  
    174.   }
    175.   //Input
    176.   //Input: A3 -> inputA3
    177.   trisa = trisa | 0x08;
    178.   FCV_INPUTA3 = ((porta & 0x08) == 0x08);
    179.  
    180.   //Decision
    181.   //Decision: inputA3=1?
    182.   if (FCV_INPUTA3==1)
    183.   {
    184.    //Output
    185.    //Output: 0 -> C0
    186.    trisc = trisc & 0xfe;
    187.    if (1)
    188.     portc = (portc & 0xfe) | 0x01;
    189.    else
    190.     portc = portc & 0xfe;
    191.  
    192.    //Output
    193.    //Output: 1 -> B7
    194.    trisb = trisb & 0x7f;
    195.    if (0)
    196.     portb = (portb & 0x7f) | 0x80;
    197.    else
    198.     portb = portb & 0x7f;
    199.  
    200.   } else {
    201.    //Output
    202.    //Output: 1 -> C0
    203.    trisc = trisc & 0xfe;
    204.    if (0)
    205.     portc = (portc & 0xfe) | 0x01;
    206.    else
    207.     portc = portc & 0xfe;
    208.  
    209.    //Output
    210.    //Output: 0 -> B7
    211.    trisb = trisb & 0x7f;
    212.    if (1)
    213.     portb = (portb & 0x7f) | 0x80;
    214.    else
    215.     portb = portb & 0x7f;
    216.  }
    217. }
    218.  mainendloop: goto mainendloop;
    219. }
    220. void MX_INTERRUPT_MACRO(void)
    221. {
    222. }
    223.  
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    :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
    Code ( (Unknown Language)):
    1.  
    2. // Configuration Mask Definitions from the 16f685.h file in the HT-soft compiler
    3. //#define EXTCLK  0x3FFF // External RC Clockout
    4. //#define EXTIO  0x3FFE // External RC No Clock
    5. //#define INTCLK  0x3FFD // Internal RC Clockout
    6. //#define INTIO  0x3FFC // Internal RC No Clock
    7. //#define EC  0x3FFB // EC
    8. //#define HS  0x3FFA // HS
    9. //#define XT  0x3FF9 // XT
    10. //#define LP  0x3FF8 // LP
    11. //// Watchdog Timer
    12. //#define WDTEN  0x3FFF // On
    13. //#define WDTDIS  0x3FF7 // Off
    14. //// Power Up Timer
    15. //#define PWRTDIS  0x3FFF // Off
    16. //#define PWRTEN  0x3FEF // On
    17. //// Master Clear Enable
    18. //#define MCLREN  0x3FFF // MCLR function is enabled
    19. //#define MCLRDIS  0x3FDF // MCLR functions as IO
    20. //// Code Protect
    21. //#define UNPROTECT 0x3FFF // Code is not protected
    22. //#define CP  0x3FBF // Code is protected
    23. //#define PROTECT  CP //alternate
    24. //// Data EE Read Protect
    25. //#define UNPROTECT 0x3FFF // Do not read protect EEPROM data
    26. //#define CPD  0x3F7F // Read protect EEPROM data
    27. //// Brown Out Detect
    28. //#define BORDIS  0x3CFF // BOD and SBOREN disabled
    29. //#define SWBOREN  0x3DFF // SBOREN controls BOR function (Software control)
    30. //#define BORXSLP  0x3EFF // BOD enabled in run, disabled in sleep, SBOREN disabled
    31. //#define BOREN  0x3FFF // BOD Enabled, SBOREN Disabled
    32. //// Internal External Switch Over Mode
    33. //#define IESOEN  0x3FFF // Enabled
    34. //#define IESODIS  0x3BFF // Disabled
    35. //// Monitor Clock Fail-safe
    36. //#define FCMEN  0x3FFF // Enabled
    37. //#define FCMDIS  0x37FF // Disabled
    38. //Using PIC16f690
    39.  
    40. #include <htc.h>
    41. __CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
    42. void main(void)
    43. {
    44.  TRISA=0xff;
    45.  TRISB=0x00;
    46.  TRISC = 0b00000000;// port directions: 1=input, 0=output
    47.  PORTB=0x00;
    48.  PORTC = 0x0;
    49.  ANSEL=0;
    50.  ANSELH=0;
    51. while (1){
    52.        if(RA0)
    53.     {RB4=0,RC4=1;
    54.     }
    55.            else
    56.    {RB4=1,RC4=0;
    57.    }
    58.    if(RA1)
    59.     {RB5=0,RC5=1;
    60.     }
    61.            else
    62.    {RB5=1,RC5=0;
    63.    }
    64. }
    65. }
    66.  
    If we remove not needed brackets and clean up, the code is even more compact
    Code ( (Unknown Language)):
    1.  
    2. //Using PIC16f690
    3.  #include <htc.h>
    4. __CONFIG(INTIO & WDTDIS & PWRTDIS & BORDIS & MCLRDIS & FCMEN  & IESODIS & UNPROTECT);
    5. void main(void)
    6. {
    7.  TRISA=0xff;
    8.  TRISB=0x00;
    9.  TRISC = 0b00000000;// port directions: 1=input, 0=output
    10.  PORTB=0x00;
    11.  PORTC = 0x0;
    12.  ANSEL=0;
    13.  ANSELH=0;
    14. while (1){
    15.        if(RA0)
    16.    RB4=0,RC4=1;
    17.        else
    18.        RB4=1,RC4=0;
    19.     if(RA1)
    20.    RB5=0,RC5=1;
    21.        else
    22.      RB5=1,RC5=0;
    23.    }
    24. }
    25.  
    26.  
     
    Last edited: Jun 17, 2010
  3. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    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.
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  5. chris13409

    Thread Starter New Member

    Jan 7, 2010
    7
    0
    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.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
Loading...