Help testing little code

Discussion in 'Embedded Systems and Microcontrollers' started by Kenny, Oct 7, 2015.

  1. Kenny

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    55
    0
    I made a little 18F1320 code for a friend, because I don't have the real hardware.
    I simulated and everything works well. However, he burned the chip and its behavior is completely erroneus.

    The question is: Is there somebody here who can make the same test on real hardware?
    Thanks in advance!

    PS. The code is made for MPLABX with XC8. It has 4 outputs and 1 input (all digital). The erroneus behavior has to do with the detection of the interrupt.
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Hi, try posting a schematic diagram or a high resolution picture of the circuit, along with the code.
     
  3. Kenny

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    55
    0
    Here it goes:
    This code is one of many different versions that I have tested with the same result: The software simulation works flawlessly but the real hardware doesn't. The real hardware is tested by a friend who doesn't know code programming, so I don't have access to real debugging -That is why I asked for somebody who can test it and debug it on real hardware.
    PS. There are code here that is not used on this trimmed version, like the variable ab.

    The proper behavior of this thing is:
    1) TURN OFF RA0,RA1,RA2,RA3. WAIT 3s
    2) TURN ON RA0,RA1,RA2,RA3. WAIT 1s
    3) TURN OFF RA0,RA1,RA2,RA3. WAIT 3s
    4) Enable interrupts. It makes the swtich closing turn on RA0, RA1, RA7

    The erratic behavior, present on the real hardware is:
    At power on, TURN ON RA0, RA1 and the switch makes nothing at all.

    From the various versions that we have tested, it seems to be the interrupt code that works in a totally strange manner: like interrupting without any change on the switch, even with no switch -leaving RB7 fixed connected to GND.

    Simple 18F1320.jpg

    Code (Text):
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include "abcd.h"
    4.  
    5.  
    6. #define     ON      1
    7. #define     OFF     0
    8.  
    9.  
    10.  
    11. int ab;
    12.  
    13.  
    14.  
    15. void interrupt input_rised(void)
    16. {
    17.     int count = 0, state = OFF;
    18.  
    19.     if (INTCONbits.RBIF && INTCONbits.RBIE)
    20.     {
    21.         state = PORTBbits.RB7;
    22.         count = 0;
    23.  
    24.         do {
    25.             if (PORTBbits.RB7 != state) {
    26.                 goto _exitint;
    27.             } else {
    28.                 count++;
    29.                 __delay_ms(1);
    30.             }
    31.         } while(count < 5);
    32.  
    33.  
    34.         state = PORTBbits.RB7;
    35.         LATA7 = state;
    36.  
    37.         if (ab) {
    38.             LATA0 = state;        // A
    39.             LATA1 = state;        // B
    40.         } else {
    41.             LATA2 = state;        // C
    42.             LATA3 = state;        // D
    43.         }
    44.  
    45.         _exitint:
    46.             INTCONbits.RBIF = 0;
    47.     }
    48. }
    49.  
    50.  
    51.  
    52.  
    53.  
    54. void delay1s(int n)
    55. {
    56.     for (int j = 1; j <= n; j++)
    57.         for (int i = 0; i < 20; i++)
    58.             __delay_ms(50);
    59. }
    60.  
    61.  
    62.  
    63.  
    64.  
    65. void main(void)
    66. {
    67.     OSCCON = 0x76;              // 8 MHz
    68.     ADCON1 = 0x7F;
    69.     ab = 1;
    70.  
    71.     TRISAbits.RA0 = 0;
    72.     TRISAbits.RA1 = 0;
    73.     TRISAbits.RA2 = 0;
    74.     TRISAbits.RA3 = 0;
    75.     TRISAbits.RA4 = 0;
    76.     TRISAbits.RA6 = 0;
    77.     TRISAbits.RA7 = 0;
    78.  
    79.     TRISBbits.RB0 = 0;
    80.     TRISBbits.RB1 = 0;
    81.     TRISBbits.RB2 = 0;
    82.     TRISBbits.RB3 = 0;
    83.     TRISBbits.RB4 = 0;
    84.     TRISBbits.RB5 = 0;
    85.     TRISBbits.RB6 = 0;
    86.     TRISBbits.RB7 = 1;
    87.  
    88.     LATA0 = OFF;
    89.     LATA1 = OFF;
    90.     LATA2 = OFF;
    91.     LATA3 = OFF;
    92.  
    93.     delay1s(3);
    94.  
    95.     LATA0 = ON;
    96.     LATA1 = ON;
    97.     LATA2 = ON;
    98.     LATA3 = ON;
    99.     delay1s(1);
    100.     LATA0 = OFF;
    101.     LATA1 = OFF;
    102.     LATA2 = OFF;
    103.     LATA3 = OFF;
    104.  
    105.     delay1s(3);
    106.  
    107.  
    108.     INTCONbits.RBIF = 0;
    109.     INTCONbits.RBIE = 1;
    110.     ei();
    111.  
    112.     // main
    113.     while(1);
    114. }
    115.  
    116.  
    117.  
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    A couple of quick observations:

    Before turning on interrupt-on-change be sure to read PORTB to clear any pending mismatch conditions. Be sure to read PORTB in the interrupt routine to clear the mismatch condition. Keep in mind that switch bounce will create several interrupts as will switch release.

    Your code does not have any CONFIG bits set - that is necessary for actually running the code on a chip:
    In MPLABX, open Window->PIC Memory Views->Configuration Bits.
    In the Configuration Bits Window, visit and set up ALL config bits for your particular setup, especially the oscillator, watchdog and MCLR pin select. If you don't know about an option - find out.
    Click Generate Source Code to Output. A window will open with the source code you need to implement your selections.
    Copy and paste into your source code.
    Rebuild.

    If you are using the internal oscillator block, don't forget to set OSCCON in your init code to what you want it to run at.

    Lots of sims will 'run' without oscillators etc. Chips will not.

    You should consider getting a PICkit 3 for debugging on real hardware.

    I may have commented on your earlier posts using interrupt on change for switch detection. I do not like it. You are seeing why that is.

    Good luck.
     
  5. Kenny

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    55
    0
    Thanks for your answers!
    However, I think I have made all that you suggest.
    - The config bits are on the file .h that I didn't show.
    - About the mismatch condition, I read there are two ways of avoiding it, one is reading PORTB like you say, the other is what I did.
    - There is antidebouncing code.
    - OSCCON is set.

    Do you think interrupt on change is not a good way to detect switching? I need to detect both condtions (CLOSED, OPEN) at the proper moment.
    I tried also, on another version, to detect CLOSE in one pin and detect OPEN on another one, but it happened the same: it worked well on simulation but failed on real hardware.

    I know is completely annoying to read posted code, but I made it because one user suggest it.
    Now, I have written a new (simplest) code, I sent it to my friend and I waiting for test results.
     
  6. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Not true. Now we have a bit more of your project. How about that abcd.h file?
     
  7. Kenny

    Thread Starter AAC Fanatic!

    Oct 11, 2004
    55
    0
    I said it because is that way to me. I have had people who show me pages of very bad written code and look for my advice in a narrow time...

    Whatever, as I said before, I'm waiting for the test results of a shorter code that I sent to my friend and then I will tell you more...
    Thanks!
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Where are your VDD and VPP connections?

    MCLR is floating. You need to be certain to disable it in your config bits.

    Post your config bits.

    Does the 18F1320 have an IOCx register? If so I do not see it being set.

    But as John mentioned IOC is not a good choice for manual switch change. You will need software or hardware debouncing. It would be a better option o just poll the switch.
     
Loading...