Menu

pic18f4680 program not running

k p
2014-01-30
2014-01-31
  • k p

    k p - 2014-01-30

    having problems getting program to run

    stage 1 - compile modified bootloader with mplabx 1.95, program using pickit3 - OK
    stage 2 - use 'tinybldlin' to interact with bootloader - OK
    stage 3 - use program to check PIC it responds with found - OK
    Connected to /dev/ttyUSB0 at 19200
    Searching for PIC ...
    Found:18F 2620/4620
    stage 4 - Write Flash, blue bar scrolls with this responce
    Connected to /dev/ttyUSB0 at 19200
    HEX:01 min old,INX32M,18Fcode+cfg,total=1318 bytes.
    Searching for PIC ...
    Found:18F 2620/4620
    Write OK at 19:24 time: 1.352 sec
    stage 5 - open terminal 'GTKterm' to communicate via uart - bad no response

    it appears as though the program does not start
    i know i am missing something to allow the program to run but i am not sure what it is can anyone point me in the right direction?

    the program works if i restore the config fuses (commented out) and program directly via pickit3

    program code
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    //#*******************************
    //#
    //#     use MPLABX 1.95
    //#     use xc8 compiler 1.21
    //#     PIC18F4680
    //#     rom 0-0xff00 for program (XC8 linker -> Memory model -> ROM ranges)
    //#
    //#     iss 13 - uses TinyBootloader
    //#
    //#*******************************
    
    #include <xc.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    #include "defines.h"
    #include "rs232.h"
    
    // fuses
    /*
    #pragma config IESO = OFF, FCMEN = OFF, OSC = XT
    #pragma config BOREN = BOHW, PWRT = ON, BORV = 2 // 2.8v        BORV = 1 // 4.3V
    #pragma config WDTPS = 2048, WDT = ON
    #pragma config PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
    #pragma config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON, BBSIZ = 4096
    #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
    #pragma config CPD = OFF, CPB = OFF
    #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
    #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    #pragma config EBTRB = OFF
    */
    //__EEPROM_DATA(0, 1, 2, 3, 4, 5, 6, 7);
    //__EEPROM_DATA(0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07);
    //__EEPROM_DATA(0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F);
    
    // global variables
    static unsigned char rs232temp;
    
    void interrupt isr(void)
    {
        if (INT0IE && INT0IF)                                               // used to calc RPM interval
        {
            INT0IE = 0;
        INT0IF = 0;
        return;
        }
        if (TMR1IE && TMR1IF)                                           // 0.25second interval
        {
            TMR1IE = 0;
            TMR1IF = 0;
            return;
        }
        if(RCIF && RCIE)                                                    // rs232
        {
            clear_usart_errors_inline;
            rs232temp = getch();
            if (rs232temp == 'r')   // 0x1B = 'escape' character
            {
                putst("rebooting...\n");
                RESET();
            }
            else
            {
                putst(" ? \n");
            }
            RCIF = 0;   /* finished */
            return;
        }
        if(TXIF && TXIE)                                                     // switch OFF TXIE
        {
            clear_usart_errors_inline;
            TXIF = 0;   /* finished */
            TXIE = 0;   /* finished */
            return;
        }
        if(TMR2IF && TMR2IE)
        {
            TMR2IE = 0;
            TMR2IF = 0;
            return;
        }
        if(TMR3IF && TMR3IE)
        {
            TMR3IE = 0;
            TMR3IF = 0;
            return;
        }
    }
    
    //=================================================================================
    //******************* Main Function *********************
    void main(void)
    {
        init();
        Reset_reason();
        SWDTEN = 1;
        CLRWDT();
    
        putst("uses TinyBootloader\n");
    
        do
        {
            CLRWDT();
    
        }while(true);
    }
    
    void Reset_reason(void)
    {
        putst("\n");
        if ((RCON & 0b00000001) == 0) putst("Brown out reset\n");
        if ((RCON & 0b00000010) == 0) putst("Power on reset\n");
        if ((RCON & 0b00000100) == 0) putst("Power down or sleep\n");
        if ((RCON & 0b00001000) == 0) putst("Watchdog timeout\n");
        if ((RCON & 0b00010000) == 0) putst("Reset\n");
        if ((STKPTR & 0x40) == 0x40) putst("stack underflow\n");
        if ((STKPTR & 0x80) == 0x80) putst("stack overflow\n");
    }
    
    //*******************************************************************************************
    //--- initialisation
    void init(void)
    {
    //;------------------------------------------------------------------------------
    //; Set the state of the configuration bits 
    //;------------------------------------------------------------------------------
    //  OSCCON = 0b01111100; //internal oscillator 8MHz
    
    // OSCCON
      OSCCON = 0b00000000;  //bit 6-4 8MHz, bit3 device running int source, bit2 frequency is stable, bit1-0 Freq set by 
    // OSCTUNE
      OSCTUNE = 0b00000000; // Centred frequency tuned to calibrated freq
    
    //  PORTA = 0b00000000;
    //  PORTB = 0b00000001;
    //  PORTC = 0b00000111;
    
      LATA = 0x00;
      LATB = 0x00;
      LATC = 0x00;
      LATD = 0x00;
      LATE = 0x00;
    
      TRISA = 0b11101011;
      TRISB = 0b11001111;
      TRISC = 0b11011000;
      TRISD = 0b10001111;
      TRISE = 0b00001111;
    
    //RS232 - BRGH BRG16
      TXSTA = 0b00100110;       //8bit, Async, BRGH = hi
      RCSTA = 0b10010000;       //SPEN = pins, CREN = enable receive
      BAUDCON = 0b00001010;     // Wakeup on receive, BRG16
      //SPBRG = 16;         // 115200baud
      SPBRG = 34;           // 57600baud
      //SPBRG = 103;                // 19200baud
      //SPBRG = 207;            // 9600baud
    
    //ADC
      ADCON0 = 0b00000000;
      ADCON1 = 0b00011110;      // Vref = AN3 to Vss, AN0 only
      ADCON2 = 0b10111110;      // right justified, 20Tad, Fosc/64
    
    // I2C
      SSPSTAT = 0b10000000;     // Disabled
      //SSPCON1 = 0b00000000;       // reset before start
      SSPADD = 0x04;            // 400kHz
      SSPCON1 = 0b00101000;     // Enabled, Master mode clock = Fosc/(4*SSPADD+1)
      SSPCON2 = 0b00000000;     // 
    
    // Timer1 - set to 0.25sec
    
    //  TMR1IE = 1;     // enable timer 1 interrupt
    
        T1CON = 0b00110001; // 1:8 prescale
        TMR1L = 0xDB;
        TMR1H = 0x0B;       // (((osc /4) /4) /prescaler) = 62500 (FFFF - 62500 = BDB)
    
        // Timer2
    
    //  TMR1IE = 1;     // enable timer 1 interrupt
    
        T2CON = 0b01111011; // 1:16 PostScale 1:16 prescale ( = 32.768ms) per tick
        PR2 = 0xFF;
    
    //
    // Timer3 - configured as a 16bit counter
    //  TMR1IE = 1;     // enable timer 1 interrupt
    
        T3CON = 0b00110000; // 1:8 prescale
        TMR3L = 0x0000;
        TMR3H = 0x0000;
    //
      PIE1 = 0b00100001;        //enabled RCIE, TMR1
      PIE2 = 0b00000010;        //enabled TMR3
      PIE3 = 0b00000000;        //enabled 
    
      INTCON = 0b11010000;      // enables gie, peie, INT0
      INTCON2 = 0b10000000;     // 
      INTCON3 = 0b00000000;     // 
    }
    

    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    bootloader code
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

        radix DEC
    
        ; change these lines accordingly to your application    
    #include "p18f4680.inc"
    IdTypePIC = 0x4F            ; must exists in "piccodes.ini"
    #define max_flash 0x10000   ; in BYTES!!! (= 'max flash memory' from "piccodes.ini")
    xtal EQU 8000000        ; you may want to change: _XT_OSC_1H  _HS_OSC_1H  _HSPLL_OSC_1H
    baud EQU 19200          ; the desired baud rate
        ; The above 5 lines can be changed and buid a bootloader for the desired frequency (and PIC type)
    
        ;********************************************************************
        ;   Tiny Bootloader     18F series      Size=100words
        ;   claudiu.chiculita@ugal.ro
        ;   http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
        ; 
        ; modified by Edorul:
        ; EEPROM write is only compatible with "Tiny PIC Bootloader+"
        ; http://sourceforge.net/projects/tinypicbootload/
        ;********************************************************************
    
        #include "../spbrgselect.inc"   ; RoundResult and baud_rate
    
            #define first_address max_flash-200     ;100 words
    
        config IESO = OFF, FCMEN = OFF, OSC = XT
        config BOREN = BOHW, PWRT = ON, BORV = 2 ;// 2.8v        BORV = 1 // 4.3V
        config WDTPS = 2048, WDT = OFF
        config PBADEN = OFF, LPT1OSC = OFF, MCLRE = ON
        config DEBUG = OFF, XINST = OFF, LVP = OFF, STVREN = ON, BBSIZ = 2048
        config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
        config CPD = OFF, CPB = OFF
        config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
        config WRTB = OFF, WRTC = OFF, WRTD = OFF
        config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
        config EBTRB = OFF
    
    ;----------------------------- PROGRAM ---------------------------------
        cblock 0
        crc
        i
        cnt1
        cnt2
        cnt3
        counter_hi
        counter_lo
        flag
        endc
        cblock 10
        buffer:64
        endc
    
    SendL macro car
        movlw car
        movwf TXREG
        endm
    
    ;0000000000000000000000000 RESET 00000000000000000000000000
    
            ORG     0x0000
            GOTO    IntrareBootloader
    
    ;view with TabSize=4
    ;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
    ;----------------------  Bootloader  ----------------------
    ;PC_flash:      C1h             U       H       L       x  ...  <64 bytes>   ...  crc   
    ;PC_eeprom:     C1h             40h   EEADRH  EEADR     1       EEDATA  crc                 
    ;PC_cfg         C1h         U OR 80h    H       L       1       byte    crc
    ;PIC_response:     type `K`
    
        ORG first_address       ;space to deposit first 4 instr. of user prog.
        nop
        nop
        nop
        nop
        org first_address+8
    
    IntrareBootloader
                                ;init serial port
        movlw b'00100100'
        movwf TXSTA
        movlw spbrg_value
        movwf SPBRG
        movlw b'10010000'
        movwf RCSTA
                                ;wait for computer
        rcall Receive           
        sublw 0xC1              ;Expect C1h
        bnz way_to_exit
        SendL IdTypePIC         ;send PIC type
    MainLoop
        SendL 'K'               ; "-Everything OK, ready and waiting."
    mainl
        clrf crc
        rcall Receive           ;Upper
        movwf TBLPTRU
            movwf flag          ;(for EEPROM and CFG cases)
        rcall Receive           ;Hi
        movwf TBLPTRH
            movwf EEADRH        ;(for EEPROM case)
        rcall Receive           ;Lo
        movwf TBLPTRL
            movwf EEADR         ;(for EEPROM case)
    
        rcall Receive           ;count
        movwf i
        incf i
        lfsr FSR0, (buffer-1)
    rcvoct                      ;read 64+1 bytes
            movwf TABLAT        ;prepare for cfg; => store byte before crc
        rcall Receive
        movwf PREINC0
        btfss i,0       ;don't know for the moment but in case of EEPROM data presence...
            movwf EEDATA        ;...then store the data byte (and not the CRC!)
        decfsz i
        bra rcvoct
    
        tstfsz crc              ;check crc
        bra ziieroare
            btfss flag,6        ;is EEPROM data?
            bra noeeprom
            movlw b'00000100'   ;Setup eeprom
            rcall Write
            bra waitwre
    noeeprom
    ;----no CFG write in "Tiny PIC Bootloader+"
    ;       btfss flag,7        ;is CFG data?
    ;       bra noconfig
    ;       tblwt*              ;write TABLAT(byte before crc) to TBLPTR***
    ;       movlw b'11000100'   ;Setup cfg
    ;       rcall Write
    ;       bra waitwre
    ;noconfig
    ;----
                                ;write
    eraseloop
        movlw   b'10010100'     ; Setup erase
        rcall Write
        TBLRD*-                 ; point to adr-1
    
    writebigloop    
        movlw 8                 ; 8groups
        movwf counter_hi
        lfsr FSR0,buffer
    writesloop
        movlw 8                 ; 8bytes = 4instr
        movwf counter_lo
    writebyte
        movf POSTINC0,w         ; put 1 byte
        movwf TABLAT
        tblwt+*
        decfsz counter_lo
        bra writebyte
    
        movlw   b'10000100'     ; Setup writes
        rcall Write
        decfsz counter_hi
        bra writesloop
    waitwre 
        ;btfsc EECON1,WR        ;for eeprom writes (wait to finish write)
        ;bra waitwre            ;no need: round trip time with PC bigger than 4ms
    
        bcf EECON1,WREN         ;disable writes
        bra MainLoop
    
    ziieroare                   ;CRC failed
        SendL 'N'
        bra mainl
    
    ;******** procedures ******************
    
    Write
        movwf EECON1
        movlw 0x55
        movwf EECON2
        movlw 0xAA
        movwf EECON2
        bsf EECON1,WR           ;WRITE
        nop
        ;nop
        return
    
    Receive
        movlw xtal/2000000+1    ; for 20MHz => 11 => 1second delay
        movwf cnt1
    rpt2                        
        clrf cnt2
    rpt3
        clrf cnt3
    rptc
            btfss PIR1,RCIF         ;test RX
            bra notrcv
            movf RCREG,w            ;return read data in W
            addwf crc,f             ;compute crc
            return
    notrcv
        decfsz cnt3
        bra rptc
        decfsz cnt2
        bra rpt3
        decfsz cnt1
        bra rpt2
        ;timeout:
    way_to_exit
        bcf RCSTA,  SPEN            ; deactivate UART
        bra first_address
    ;*************************************************************
    ; After reset
    ; Do not expect the memory to be zero,
    ; Do not expect registers to be initialised like in catalog.
    
                END
    

    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

     
  • Dan

    Dan - 2014-01-31

    Hi,k p

    ;*********
    ; Tiny Bootloader 18F series Size=100words
    ; claudiu.chiculita@ugal.ro
    ;
    ;http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
    ;
    ; modified by Edorul:
    ; EEPROM write is only compatible with "Tiny PIC Bootloader+"
    ; http://sourceforge.net/projects/tinypicbootload/
    ;
    *********

    Not compatible with tinybldlin / tinybldwin this firmware.

    Write Format EEPROM has changed.

    If you were using the tinybldlin, please use the tinybld18F4620_zigbee.asm that is attached to the tinybld198.

     
  • k p

    k p - 2014-01-31

    no success. i downloaded modified and programmed the file you suggested but i still had the same result.

    even if i used 'Tiny_Avr-Pic_Bootloader+.exe' with the file above it still would not program and the program would freeze.

    in the meantime i got ds30loader to work it does have a larger footprint though. i will return one day to figure out tiny pic bootloader.

    thank you for your help

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.