Author Topic: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)  (Read 4180 times)

0 Members and 1 Guest are viewing this topic.

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Hello,
The following XC8 C code for PIC18F26K20 Builds successfully but doesn’t work. :-//
Please help make it work? :scared:
All it is supposed to do  is output on RC2 what it reads  as an input on RB0. (They are both Digital I/O ports)
I cannot see why it wont work. |O
The following is the crux of the entire code………..

while(1) {
    if (PORTBbits.RB0 == 0) {PORTCbits.RC2 = 0;}
    __delay_ms(1);
    if (PORTBbits.RB0 == 1) {PORTCbits.RC2 = 1;}
    __delay_ms(1);
    }
…..surely there is nothing wrong with this? :-//
Here is the complete code listing……

Code: [Select]
/*
 * File:   main.c
 */

//This file is very simple code.
//It simply puts out on RC2 what it reads on RB0. (HIGH or LOW)
/*
  Generated Main Source File
  File Name:
    main.c

  Description:
 *     Generation Information :
        Device            :  PIC18F26K20
    The generated drivers are tested against the following:
        Compiler          :  XC8 v1.38
        MPLAB             :  MPLAB X IDE v3.61
 */


#include <xc.h>
#include <stdint.h>
#include <stdlib.h>

#define _XTAL_FREQ  16000000

// CONFIG1H
#pragma config FOSC = INTIO67   // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 18        // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = OFF     // HFINTOSC Fast Start-up (The system clock is held off until the HFINTOSC is stable.)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (00C000h-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

   
void main(void)
{
   
    OSCCON = 0x72;          // 16MHz
    OSCTUNE = 0x00;
   
    //Set up Special Function  Registers
    ANSEL = 0x00;   //Enable all input buffers (not using analog functions)
    ANSELH = 0x00;  //Enable all input buffers (not using analog functions)
   
    ADCON0 = 0x00;   //ADC disabled
   
    CM2CON0 = 0x00; //Comparator 2 disabled
    CM1CON0 = 0x00;  //Comparator 1 disabled
   
    CCP2CON = 0x00;  //Disable PWM and capture compare modules.
   
    CVRCON = 0x00;  //Disable internal voltage reference (of comparator))
   
    TRISA = 0b00000110;     //AJM checked
    TRISB = 0b00001111;      //AJM checked       //RB0 is input.
    TRISC = 0b00000000;     //AJM checked.      //RC2 = Output.
   
    INTCON = 0x00;  //Disable all interrupts
    INTCON2 = 0x80; //Disable PORTB pullups
 
    SLRCON = 0x00;     //All PORTB outputs slow at standard rate.
   
    PORTCbits.RC2 = 0;      //LEDs OFF
   
    while(1) {
    if (PORTBbits.RB0 == 0) {PORTCbits.RC2 = 0;}
    __delay_ms(1);
    if (PORTBbits.RB0 == 1) {PORTCbits.RC2 = 1;}
    __delay_ms(1);
    }

    return;
}

 ;)
« Last Edit: June 08, 2017, 08:32:15 pm by treez »
 

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 241
  • Country: us
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #1 on: June 08, 2017, 09:12:24 pm »
I'm not that advanced in programming but something that needs adjusting is:

"PORTCbits.RC2 = " --> Use LATx instead (this is better practice in general).
 
The following users thanked this post: ocset

Offline aiq25

  • Regular Contributor
  • *
  • Posts: 241
  • Country: us
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #2 on: June 08, 2017, 09:15:47 pm »
Typically I use something like this:

Code: [Select]
// Setup outputs --> RA0
TRISAbits.TRISA0 = 0; // RA0 is output
ANSELAbits.ANSA0 = 0; // set for digital
LATAbits.LATA0 = 0; // initialize RA0
 
The following users thanked this post: ocset

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #3 on: June 09, 2017, 05:26:09 am »
Thanks, so   we should  always write to the 'LATCH', and read to the 'PORT'?
 

Online jaromir

  • Supporter
  • ****
  • Posts: 338
  • Country: sk
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #4 on: June 09, 2017, 05:35:23 am »
Thanks, so   we should  always write to the 'LATCH', and read to the 'PORT'?
Yes.
Your example should look like
Code: [Select]
while(1) {
    if (PORTBbits.RB0 == 0) {LATCbits.LATC2 = 0;}
    __delay_ms(1);
    if (PORTBbits.RB0 == 1) {LATCbits.LATC2 = 1;}
    __delay_ms(1);
    }
 
The following users thanked this post: ocset

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #5 on: June 09, 2017, 06:05:34 am »
Thanks, so   we should  always write to the 'LATCH', and read to the 'PORT'?

yes. even written in the datasheet.
well actually bit manipulation are converted to BCF and BSF instructions so there is no real harm in this case, but it's still bad practice.

does it work now?
if it doesn't try sampling porta first, i tend to have a lot of problems reading pin states this way, i prefer to sample to ports first (also help with debugging)

Code: [Select]
uint8_t portb_state

...

portb_state = PORTB
if (portb_state & 1) { pressed } else { not pressed }
[code]

if you single stepped you would see if the pic is getting the correct data from the port.

Also, the 26k20 like all modern pics has the ANSELx register to make an input pin digital/analog. so you should also add ANSELBbits.ANSB0 = 0
 
The following users thanked this post: ocset

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • Country: ca
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #6 on: June 09, 2017, 06:32:01 am »
If you are keeping the programmer plugged in, did you release the PIC from reset state?
Are running in debug mode?
Is the PIC cpu clock truly oscillating?
EG, get rid of the IF part & delay and just set and clear PortRC, see if it is going high and low all on it's own.
 
The following users thanked this post: ocset

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #7 on: June 11, 2017, 12:44:10 pm »
Hello,
I have now enabled the portb pullup on RB3 too, though please could you check the order in which i setup the special function registers?.. ;)

I mean i know that  ANSEL must be set before TRIS, but what about INTCON  etc, should you set that before or after setting TRIS? :-//

Code: [Select]
/*
 * File:   main.c
 */

//This file is very simple code.
//All it does is mimick the input on pin 18 (RB0) and put it out on pin 10 (RC2))
/*
  Generated Main Source File
  File Name:
    main.c

  Description:
 *     Generation Information :
        Device            :  PIC18F26K20
    The generated drivers are tested against the following:
        Compiler          :  XC8 v1.38
        MPLAB             :  MPLAB X IDE v3.61
 */


#include <xc.h>
#include <stdint.h>
#include <stdlib.h>

#define _XTAL_FREQ  16000000

// CONFIG1H
#pragma config FOSC = INTIO67   // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 18        // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = OFF     // HFINTOSC Fast Start-up (The system clock is held off until the HFINTOSC is stable.)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (00C000h-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

   
void main(void)
{
   
    OSCCON = 0x72;          // 16MHz
    OSCTUNE = 0x00;
   
    //Set up Special Function  Registers
    ANSEL = 0x00;   //Enable all input buffers (not using analog functions)
    ANSELH = 0x00;  //Enable all input buffers (not using analog functions)
   
    ADCON0 = 0x00;   //ADC disabled
   
    CM2CON0 = 0x00; //Comparator 2 disabled
    CM1CON0 = 0x00;  //Comparator 1 disabled
   
    CCP2CON = 0x00;  //Disable PWM and capture compare modules.
   
    CVRCON = 0x00;  //Disable internal voltage reference (of comparator))
   
    TRISA = 0b00000110;     //AJM checked
    TRISB = 0b00001111;      //AJM checked       //RB0 is input.
    TRISC = 0b00000000;     //AJM checked.      //RC2 = Output.
   
    WPUB = 0x08;

    INTCON = 0x00;  //Disable all interrupts
    INTCON2 = 0x00; //Enable PORTB pullups (depends now on WPUB)
    INTCON3 = 0x00;
 
    SLRCON = 0x00;     //All PORTB outputs slow at standard rate.
   
    LATCbits.LATC2 = 0;      //LEDs OFF
   
    while(1) {
    if (PORTBbits.RB0 == 0) {LATCbits.LATC2 = 0;}
    __delay_ms(1);
    if (PORTBbits.RB0 == 1) {LATCbits.LATC2 = 1;}
    __delay_ms(1);
    }

    return;
}

 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #8 on: June 11, 2017, 04:36:57 pm »
LATCbits.LATC2 = 0;      //LEDs OFF
 ...
while(1) {


I'd put a 200ms LED flash between those two lines. If you don't see the LED flash you know the problem is in the setting up, if you do see the LED flash you'll know the problem is in the while loop's sensing/setting.
Don't forget to add a 200ms delay after the flash, in case the while loop immediately turns the LED off!
« Last Edit: June 11, 2017, 10:04:41 pm by StillTrying »
.  That took much longer than I thought it would.
 
The following users thanked this post: ocset

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #9 on: June 14, 2017, 09:16:20 pm »
Well what was it that stopped the LED going on and off.  :-\
.  That took much longer than I thought it would.
 
The following users thanked this post: ocset

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #10 on: June 24, 2017, 08:19:58 am »
Thanks, dont know but this did it

Code: [Select]
/*
 * File:   main.c
 */

//This file is very simple code.
//All it does is mimick the input on pin 18 (RB0) and put it out on pin 10 (RC2))
/*
  Generated Main Source File
  File Name:
    main.c

  Description:
 *     Generation Information :
        Device            :  PIC18F26K20
    The generated drivers are tested against the following:
        Compiler          :  XC8 v1.38
        MPLAB             :  MPLAB X IDE v3.61
 */


#include <xc.h>
#include <stdint.h>
#include <stdlib.h>

#define _XTAL_FREQ  16000000

// CONFIG1H
#pragma config FOSC = INTIO67   // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 18        // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT is controlled by SWDTEN bit of the WDTCON register)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = OFF     // HFINTOSC Fast Start-up (The system clock is held off until the HFINTOSC is stable.)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (00C000h-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

   
void main(void)
{
   
    OSCCON = 0x72;          // 16MHz
    OSCTUNE = 0x00;
   
    //Set up Special Function  Registers
    ANSEL = 0x00;   //Enable all input buffers (not using analog functions)
    ANSELH = 0x00;  //Enable all input buffers (not using analog functions)
   
    ADCON0 = 0x00;   //ADC disabled
   
    CM2CON0 = 0x00; //Comparator 2 disabled
    CM1CON0 = 0x00;  //Comparator 1 disabled
   
    CCP2CON = 0x00;  //Disable PWM and capture compare modules.
   
    CVRCON = 0x00;  //Disable internal voltage reference (of comparator))
   
    TRISA = 0b00000110;     //AJM checked
    TRISB = 0b00001111;      //AJM checked       //RB0 is input.
    TRISC = 0b00000000;     //AJM checked.      //RC2 = Output.
   
    WPUB = 0x08;

    INTCON = 0x00;  //Disable all interrupts
    INTCON2 = 0x00; //Enable PORTB pullups (depends now on WPUB)
    INTCON3 = 0x00;
 
    SLRCON = 0x00;     //All PORTB outputs slow at standard rate.
   
    LATCbits.LATC2 = 0;      //LEDs OFF
   
    while(1) {
    if (PORTBbits.RB0 == 0) {LATCbits.LATC2 = 0;}
    __delay_ms(1);
    if (PORTBbits.RB0 == 1) {LATCbits.LATC2 = 1;}
    __delay_ms(1);
    }

    return;
}
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #11 on: June 24, 2017, 08:50:43 pm »
Thanks, dont know but this did it
That's the identical code to your previous post. Compared to the original code, you:-

- Turned LVP OFF

- Explicitly cleared INT3
 
- Enabled pullup on RB3

- changed PORTC to LATC

Though you should set PORTC bits using LATC to avoid disturbing other output bits, this should not have affected the bit you were setting. Perhaps having LVP on was preventing the code from running.
 
The following users thanked this post: ocset

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #12 on: June 24, 2017, 09:05:23 pm »
Perhaps having LVP on was preventing the code from running.

LVP enables the PGM pin. If the PGM pin is left unconnected, the PIC will reset from time to time randomly. Disabling LVP alleviates this problem.
 
The following users thanked this post: ocset

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #13 on: June 25, 2017, 02:25:22 pm »
Perhaps having LVP on was preventing the code from running.

But as you noted that was already done by Reply#7, and still no worky, so it must have been something else.
.  That took much longer than I thought it would.
 
The following users thanked this post: ocset

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Simple C code builds but doesnt work...PIC18F26K20 (XC8 compiler)
« Reply #14 on: June 25, 2017, 05:54:38 pm »
But as you noted that was already done by Reply#7, and still no worky, so it must have been something else.
I don't see where in reply#7  treez says it is still no worky, and since the 'this did it' code in reply#10 is identical I can only presume that it was already working in reply#7.

If it was me, I would have undone the changes one at a time until the fault reappeared, then I would know what caused it. But apparently treez doesn't care...

 


 
 
The following users thanked this post: ocset


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf