PIC18F87K22 is programmed successfully but does not work as expected

Discussion in 'Embedded Systems and Microcontrollers' started by zaferaltun, Aug 31, 2016.

  1. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    Hello

    I 'm not newbie about PICs, I did somethings with 16F690 and 18F2550 models before but I this 18F87K22 is very complicated for me since I 'm not expert, even not experinced so. I 'm using Pickit3 to program the device and it is successfull, however the device does not work as I expected, I wrote some led blink code to test it and I 'm masuring the output pin with multimeter but nothing.. I think my configuration is wrong or missing something.

    Can you help me please? I 'm trying to use internal oscillator at 16Mhz. I have all 100nF decoupling capacitor between vdd and vss pins included avdd and avss, not using on-chip regulator, ENVREG is not tied to VDD and VDDCORE is tied to ground with 100nF capacitor. It is not programmed if I missed something about the schematics according to the documentation already, I mean I 'm sure physical needs are ok otherwise it will not be programmed.

    Thanks
    Code (C):
    1.  
    2. /*
    3. * File: main.c
    4. * Author: ZAFER
    5. *
    6. * Created on 15 May?s 2016 Pazar, 20:25
    7. *
    8. * I2C Slave Device Sample
    9. *
    10. * NOTES:
    11. *
    12. *
    13. */
    14. #define _XTAL_FREQ 16000000
    15.  
    16. #include <xc.h>
    17. #include <stdio.h>
    18. #include <stdlib.h>
    19. #include <string.h>
    20.  
    21. #pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
    22. #pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
    23. #pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
    24. #pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
    25. #pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
    26. #pragma config XINST = OFF // Extended Instruction Set
    27. #pragma config SOSCSEL = DIG // SOSC Power Selection and mode Configuration bits (Digital (SCLKI) mode)
    28.  
    29. int SLAVE_ADDR = 0x10;
    30.  
    31. void init(void);
    32. void delayMS(int ms);
    33.  
    34. void main(void) {
    35.      init();
    36.      delayMS(500);
    37.  
    38.      while (1) {
    39.           // Set activity led pin
    40.           LATJbits.LATJ4 = PORTJbits.RJ4 == 0 ? 1 : 0;
    41.           delayMS(500);
    42.      }
    43.      return;
    44. }
    45.  
    46. void init() {
    47.  
    48.      // OSC
    49.      OSCCONbits.IRCF0 = 1;
    50.      OSCCONbits.IRCF1 = 1;
    51.      OSCCONbits.IRCF2 = 1;
    52.      OSCCONbits.HFIOFS = 1;
    53.      OSCCONbits.SCS1 = 1;
    54.  
    55.      // Disable comparators
    56.      CM1CONbits.CON = 0;
    57.      CM2CONbits.CON = 0;
    58.      CM3CONbits.CON = 0;
    59.  
    60.      // ANALOG INPUTS
    61.      // Enable all analog inputs
    62.      ANCON0 = 0xFF;
    63.      ANCON1 = 0xFF;
    64.      ANCON2 = 0xFF;
    65.  
    66.      // Set conversion as left justified and vref as AVREF+ and AVSS
    67.      ADCON1bits.VCFG = 0b00;
    68.      ADCON1bits.VNCFG = 0b0;
    69.      ADCON2bits.ADFM = 0;
    70.  
    71.      // Set acquisition time for minimum requirements (14 TAD, ~16 TOSC)
    72.      ADCON2bits.ADCS = 0b101;
    73.      ADCON2bits.ACQT = 0b010;
    74.  
    75.      // I2C CONFIGURATION
    76.      // Set as slave (SSPM(3:0) = 0110) as interrupt enabled
    77.      SSP1CON1 = 0b00101110;
    78.  
    79.      // Set stretching enable
    80.      SSP1CON2bits.SEN = 1;
    81.  
    82.      // Configure Input Levels and slew rate as I2C Standard Levels (Slew Rate control (SMP) set for 100kHz)
    83.      SSP1STAT = 0b10000000;
    84.  
    85.      // Set i2c address to SLAVE_ADDR variable. Note that last bit of SSPADD is not used in 7 bit addressing mode
    86.      SSP1ADD = SLAVE_ADDR << 1;
    87.  
    88.      // PORTS
    89.      // Set address switch input port
    90.      TRISB = 0b11111111;
    91.      PORTB = 0;
    92.      LATB = 0;
    93.  
    94.      // Set analog inputs ports. Note that sharp numbers shows input indices as starting from 1
    95.      TRISAbits.TRISA1 = 1; // #1
    96.      PORTA = 0;
    97.      LATA = 0;
    98.  
    99.      TRISF = 0b11111111; // #2, #7, #8, #9, #10, #11, #12
    100.      PORTF = 0;
    101.      LATF = 0;
    102.  
    103.      TRISG = 0b11111111; // #13, #14, #15, #16
    104.      PORTG = 0;
    105.      LATG = 0;
    106.  
    107.      TRISH = 0b11111111; // #3, #4, #5, #6, #17, #18, #19, #20
    108.      PORTH = 0;
    109.      LATH = 0;
    110.  
    111.      // Set digital output ports
    112.      TRISAbits.TRISA4 = 0; // #7
    113.      TRISAbits.TRISA5 = 0; // #8
    114.      PORTA = 0;
    115.      LATA = 0;
    116.  
    117.      TRISCbits.TRISC1 = 0; // #6
    118.      TRISCbits.TRISC2 = 0; // #4
    119.      TRISCbits.TRISC7 = 0; // #5
    120.      PORTC = 0;
    121.      LATC = 0;
    122.  
    123.      TRISDbits.TRISD4 = 0; // #9
    124.      TRISDbits.TRISD5 = 0; // #10
    125.      TRISDbits.TRISD6 = 0; // #11
    126.      TRISDbits.TRISD7 = 0; // #12
    127.      PORTD = 0;
    128.      LATD = 0;
    129.  
    130.      TRISEbits.TRISE4 = 0; // #20
    131.      TRISEbits.TRISE5 = 0; // #19
    132.      TRISEbits.TRISE6 = 0; // #18
    133.      TRISEbits.TRISE7 = 0; // #17
    134.      PORTE = 0;
    135.      LATE = 0;
    136.    
    137.      TRISJ = 0b00000000; // #1, #2, #3, #13, #14, #15, #16 and activity led pin
    138.      PORTJ = 0;
    139.      LATJ = 0;
    140.  
    141. }
    142. void delayMS(int ms){
    143.      while(ms-- >= 0){
    144.           __delay_ms(1);
    145.      }
    146. }
    Mod edit: code tags
     
    Last edited by a moderator: Aug 31, 2016
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,949
    385
    This program compiles and works correctly in the simulator, so it would seem to be some hardware issue. However, it might be worth trying these changes to see if either affects the outcome.
    Change LATJbits.LATJ4 = PORTJbits.RJ4 == 0 ? 1 : 0; to LATJbits.LATJ4 = LATJbits.LATJ4 == 0 ? 1 : 0;

    Replace OSCCONbits.SCS1 = 1;
    With OSCCONbits.SCS0 = 0; OSCCONbits.SCS1 = 0;
     
  3. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0

    Yep, it works perfect with the simulator but real. Sometimes a tricky difference occuring between the simulator and physical. So I thought it might be configuration trick as I had an experience before with different mcu. I will notice you after trying your advices. Thanks
     
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,810
    362
    Try to turn off as many fuse setting as possible, at least initially, and specify all fuse settings.

    The typical traps are lvp, mclre, and debug if so equipped.
     
  5. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,949
    385
    Also make sure that /MCLR has a pull-up resistor.
     
  6. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    I checked MCLRE but I 'm not sure if I did the right thing, I tied MCLR pin to VDD for normal operation with 10K parallel (to ground) and 470 ohm in series, I will try the others, I could not find low voltage programming bit, there is even no LVP word in the documentation, strange. Thanks
     
  7. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    I did actually, likewise in the documentation, please see the attachment (10K resistor). Should I do anything different or extra?
     
  8. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    I changed OSCCON bits as you said and eventually I 'm getting above 1 volt on the output pin, but blinking still not working. I tried blinking status with a variable to take it guarentee but nothing changed. Any additional suggestion? Thanks a lot
     
  9. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,949
    385
    You can use the PICKIT3 to debug the program. Step through the program, view the PIC registers, and measure the voltage on J4.
    This may let you see what is going on.
    As you do not have 0V or supply voltage on J4 I suspect that pin is actually going up and down but perhaps too fast - I don't know why that should be.
     
    zaferaltun likes this.
  10. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    Yes, exactly as you said, it is so fast, looking for why.. Thanks a lot Albert, you really helped. It is running in normal speed when debugging, but normal running..
     
  11. zaferaltun

    Thread Starter New Member

    Aug 31, 2016
    27
    0
    It is my fault, there is a jumper for capacitor tied to MCLR pin and it must be open while programming and close during normal operation. I plug the jumper and everything is fine! Thanks a lot.
     
Loading...