Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Error when trying to program with PicKit3

Status
Not open for further replies.

Cantafford

Member
Hello,

I'm having the following problem:

I'm trying to upload some code into PIC18F2431 with PICKIT3. I'm using MPLAB IPE v2.05 and whenever I try putting the code into the pic I get the following error:

Code:
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3b4f
configuration memory

Programming...
program memory
Address: 0 Expected Value: 52 Received Value: 64
Failed to program device
2016-07-05T22:39:37+0300- Programming failed
Pass Count: 0000

I tried reinstalling the compiler, trying on another computer, turning LVP on and off, putting another same-kind microcontroller...nothing...still getting that error. I think once I have managed to program the microcontroller. I just can't get any code to load on the PIC.
Here is the code I'm trying to upload along with the configuration bits. Please help me fix this. Thank you.

Code:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
#include <plib/delays.h>
#include <plib/xlcd.h>

#define FCY 6250000

int HALL;
int HALLvalue[6] = { 0b00000001, 0b00000101, 0b00000100, 0b00000110, 0b00000010, 0b00000011 };
//int HALLvalue[6] = { 0b00000001, 0b00000011, 0b00000010, 0b00000110, 0b00000100, 0b00000101 };
unsigned int dch;
unsigned int dcl;

float timercount;
float speed, actualspeed;
unsigned char buf[20];

float RPMconstant = 60 * 6250000 / 256;
int long timer5;

// Capture Interrupt Service Routine


void init_XLCD(void);       //Initialize LCD display
void DelayFor18TCY( void ); //18 cycles delay
void DelayPORXLCD (void);   // Delay of 15ms
void DelayXLCD (void);       // Delay of 5ms
void CW();
void CCW();
void ConfigureADC();
void CalculateSpeed();
void SetSpeed();
void DisplaySpeed();
void ConfigureInterrupts();
void ConfigureInputCapture();
void ConfigurePCPWM();
void ConfigureTIMER5();
void Start();

void interrupt CheckHallValue()
{
  if(IC1IF==1)
  {
   CalculateSpeed();
   SetSpeed();
  }

  IC2QEIF = 0; IC3DRIF = 0;

  HALL = (PORTA >> 2) & 0x7; // read the capture pins to get hall sensor state
  CW();

}



void main()
{
 ConfigureADC();
 SCS1 = 0; // use primary
 SCS0 = 0; // oscillator
 TRISA = 0b11111111; // PORTA is input(CAP's + POT)
 TRISB = 0b11000000;
 TRISCbits.RC7 = 0;
 LATCbits.LATC7 = 0; // led initially off

 ConfigureInterrupts();
 ConfigureInputCapture();
 ConfigurePCPWM();
 ConfigureTIMER5();

 init_XLCD();                    //Call the Initialize LCD display function

 //OVDCOND = 0b00100100; // start(needs a change on input capture)
 //PDC2H = 0xFF; PDC2L = 0xFF; PDC1H = 0xFF; PDC1L = 0xFF;
 OVDCONS = 0;

 while(1)
 {
     Start();
     ADON = 1;
     DisplaySpeed();
 }

}

void init_XLCD(void)                //Initialize LCD display
{
OpenXLCD(FOUR_BIT&LINES_5X7);       //configure LCD in 4-bit Data Interface mode
                                    //and 5x7 characters, multiple line display
while(BusyXLCD());                  //Check if the LCD controller is not busy
                                    //before writing some commands
WriteCmdXLCD(0x06);                 //Move cursor right, don't shift display
WriteCmdXLCD(0x0C);                 //Turn display on without cursor
}

void DelayFor18TCY( void )         //18 cycles delay
{
Delay10TCYx(20);
}
void DelayPORXLCD (void)           //Delay of 15ms
{
Delay1KTCYx(30);
}
void DelayXLCD (void)              //Delay of 5ms
{
Delay1KTCYx(10);
}

void CW()
{
  if(HALL == HALLvalue[0]) { OVDCOND = 0x09; PDC2H = dch; PDC2L = dcl; PDC1H = dch; PDC1L = dcl; }
  else if(HALL == HALLvalue[1]) { OVDCOND = 0x18; PDC1H = dch; PDC1L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[2]) { OVDCOND = 0x12; PDC2H = dch; PDC2L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[3]) { OVDCOND = 0x06; PDC2H = dch; PDC2L = dcl; PDC1H = dch; PDC1L = dcl; }
  else if(HALL == HALLvalue[4]) { OVDCOND = 0x24; PDC1H = dch; PDC1L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[5]) { OVDCOND = 0x21; PDC2H = dch; PDC2L = dcl; PDC0H = dch; PDC0L = dcl; }
}

void CCW()
{
  if(HALL == HALLvalue[0]) { OVDCOND = 0x06; PDC1H = dch; PDC1L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[1]) { OVDCOND = 0x12; PDC2H = dch; PDC2L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[2]) { OVDCOND = 0x18; PDC2H = dch; PDC2L = dcl; PDC1H = dch; PDC1L = dcl; }
  else if(HALL == HALLvalue[3]) { OVDCOND = 0x09; PDC1H = dch; PDC1L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[4]) { OVDCOND = 0x21; PDC2H = dch; PDC2L = dcl; PDC0H = dch; PDC0L = dcl; }
  else if(HALL == HALLvalue[5]) { OVDCOND = 0x24; PDC2H = dch; PDC2L = dcl; PDC1H = dch; PDC1L = dcl; }
}

void SetSpeed()
{
   while(GODONE); // wait untill conversion is over
   dch = ADRESH >> 2; // set the
   dcl = (ADRESL<<6) | ((ADRESL>>2) & 0x00) ; // duty cycle with the pot
   IC1IF = 0;
   ADON = 0;
}

void CalculateSpeed()
{
   timercount = TMR5;                               // get
   speed = ( 6250000 / (timercount*8*6) ) * 60 / 8.5;     // the
   TMR5 = 0;
   if(dch==0 && dcl==0) {speed = 0; LATCbits.LATC7 = 0; }
}

void DisplaySpeed()
{
   putrsXLCD("Speed: ");                     //Display "Temp is" on the screen
   sprintf(buf, "%g", speed );        //Convert temperature float value to string
   putsXLCD(buf);                             //Display the temperature on the screen
   putrsXLCD(" ");                            // Display "C" for Celsius
   putrsXLCD("RPM");                            // Clear after comma
   WriteCmdXLCD(0x02);                        //Home position on LCD
}

void ConfigureADC()
{
  ADCON0 = 0b00000000; // single shot mode, single channel mode
  VCFG1 = 0; // VDD and VSS as ref
  VCFG0 = 0;
  ADCON2 = 0b00001000; // left justified, 2TAD, conversion clock: Fosc/2
  ADCON3 = 0b11000100;  // input capture 1 starts the a/d sequence!!(check here the first two bits)
  GASEL1 = 0; // select AN0
  GASEL0 = 0;
  ANSEL0 = 0b00000001;
}

void ConfigureInterrupts()
{
 GIE = 1; // enable global interrupts
 GIEH = 1;
 PEIE = 1;
}

void ConfigureInputCapture()
{
 // Initialize the Input Capture Module
 CAP1CON = 0b00000000; // disable input capture 1 module
 CAP1CON = 0b00001000; // enable input capture 1 module; interrupt on every state change

 CAP2CON = 0b00000000; // disable input capture 2 module
 CAP2CON = 0b00001000; // enable input capture 2 module; interrupt on every state change

 CAP3CON = 0b00000000; // disable input capture 3 module
 CAP3CON = 0b00001000; // enable input capture 3 module; interrupt on every state change

 // Enable Capture Interrupt and configure TMR5
 IC1IE = 1; // enable IC1 interrupt
 IC1IP = 1; // IC1 interrupt on high priority
 IC1IF = 0; // clear IC1 interrupt status flag

 IC2QEIE = 1; // enable IC2 interrupt
 IC2QEIP = 1; // IC2 interrupt on high priority
 IC2QEIF = 0; // clear IC2 interrupt status flag

 IC3DRIE = 1; // enable IC3 interrupt
 IC3DRIP = 1; // IC3 interrupt on high priority
 IC3DRIF = 0; // clear IC3 interrupt status flag
}

void ConfigurePCPWM()
{
 PTCON0 = 0b00000000; // 1:1 postscale, 1:1 prescale, PWM in free-running mode
 PTCON1 = 0b10000000; // pwm time base is on, the time base counts up
 PWMCON0 = 0b01001111; // pwm0-5 configured as pwm output in independent mode
 SEVOPS3 = 0; // 1:1 postscale
 SEVOPS2 = 0;
 SEVOPS1 = 0;
 SEVOPS0 = 0;
 OSYNC = 1; //Output overrides via the OVDCON register are synchronized to the PWM time base(NOT SURE HERE)

 PTPERH = 0x0F; // frequency is 1.5Khz
 PTPERL = 0xFF; // 0xFF here
}

void ConfigureTIMER5()
{
 T5CON = 0b00011001; // tmr 5 prescaler = 1:8
 PR5H = 0xFF;
 PR5L = 0xFF;
}

void Start()
{
 if(PORTAbits.RA1==0)
     {
      PDC2H = 0xFF; PDC2L = 0xFF; PDC1H = 0xFF; PDC1L = 0xFF;
      LATCbits.LATC7 = 1; // turn on LED
     }
}

Code:
// PIC18F2431 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1H
#pragma config OSC = XT         // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = ON        // Internal External Oscillator Switchover bit (Internal External Switchover mode enabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled)
// BORV = No Setting

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDPS = 32768     // Watchdog Timer Postscale Select bits (1:32768)
#pragma config WINEN = OFF      // Watchdog Timer Window Enable bit (WDT window disabled)

// CONFIG3L
#pragma config PWMPIN = OFF     // PWM output pins Reset state control (PWM outputs disabled upon Reset (default))
#pragma config LPOL = HIGH      // Low-Side Transistors Polarity (PWM0, 2, 4 and 6 are active-high)
#pragma config HPOL = HIGH      // High-Side Transistors Polarity (PWM1, 3, 5 and 7 are active-high)
#pragma config T1OSCMX = ON     // Timer1 Oscillator MUX (Low-power Timer1 operation when microcontroller is in Sleep mode)

// CONFIG3H
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Low-Voltage ICSP Enable bit (Low-voltage ICSP enabled)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-000FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (001000-001FFF) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (002000-002FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (003000-003FFFh) not code-protected)

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

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-000FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (001000-001FFF) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (002000-002FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (003000-003FFFh) 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-0001FFh) 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 bit (Block 0 (000200-000FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (001000-001FFF) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (002000-002FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (003000-003FFFh) not protected from table reads executed in other blocks)

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

#define _XTAL_FREQ 25000000
 
Also when I'm trying to erase the device...it says erase complete and when I do "black check" it says:

Code:
Blank Checking...
program memory
Address: 0 Expected Value: ff Received Value: 64
Blank check complete, device is not blank.
2016-07-05T22:45:02+0300- Blank check failed
 
Looks like erase isn't working. One of the possible causes - not enough voltage. It needs at the very least 4.5V. Of course, may be just a bad chip ...
 
It is likely a problem with the target. Voltage, programming header, something is not right.
 
as mentioned..
I would look at the ICSP header to ensure data is flowing without shorts,
or maybe excess draw from the motors, can you disconnect them...
..if possible eject chip from the circuit board and try on a demo board or bread board
 
Hey guys, thank you for answering:

NorthGuy: I don't think it's a voltage problem. If it was the Pickit would not even see the pic. The pickit connects to the microcontroller. Nontheless I tried charging pic from the pickit and also charging pic via separate 5V. Didn't solve the error. Also tried with like 4 pics. Also with 2 pic kits and from different computers.

@DirtyLude: Maybe. As I said I think I did manage to unload the program once into the pic a while back. Not this actuall program but a program with the same header file. I don't remember changing anything at all in the header(configuration bits) so I do not understand what the problem is now :(

Dr_Doggy: What is the ICPS header? I assume it is the "header.h" file from my project. I checked and double checked all the pins for shorts. As mentioned before I used several pic's and two pic kits 3. I'm programming the pic separately on a breadboard there are no motors connected while I'm doing that. It's just the pic on a breadboard. I'm not using a circuit board.
 
To help in solving some doubts as yours I always keep handy several chips of each type.
 
What is the ICPS header? I assume it is the "header.h" file from my project. I checked and double checked all the pins for shorts. As mentioned before I used several pic's and two pic kits 3. I'm programming the pic separately on a breadboard there are no motors connected while I'm doing that. It's just the pic on a breadboard. I'm not using a circuit board.

ICSP header is the actual connection pins from the PK3 to the PIC.

Post a picture of your setup, it sounds like you don't have it connected properly? - personally I'm never impressed with trying to do things on breadboards, they never seem reliable enough to me.
 
I am pleased to see your chip doesn't have an LVP pin (a frequent cause of trouble), but I have experienced this with PICKIT3 if the PIC device decoupling is inadequite, I use a 100nF mlc directly across the pins under the pcb for 18 pin devices. Also a 10K pullup on Mclr/Vdd helps sometimes. They are certainly finicky at times! Also check your fuses weren't blown with something weird that's now causing a problem with onboard circuitry like the dreaded clock out etc! I use a board with nothing else on it as a last ditch programmer.
Roger
 
<I am pleased to see your chip doesn't have an LVP pin> OOPS!! I am sorry it does disguised under the name PGM!! ok make sure its solidly grounded!! I have tried various values of pulldown but the only one that works with all chips is ZERO ohms! This is a real bugbear of there programming system and causes all sorts of frustration and annoyance!! I have had chips that no longer program or exhibit random errors in the target but are recoverable using my standalone programming board that has PGM grounded. Damm Microchip and there LVP nonsense..............
 
Damm Microchip and there LVP nonsense..............

PGM pin is the thing of the past. None of modern chips use it. Although LVP survived well without it. Having to deal with PGM pins is a punishment for the people who live in the past and do not want to use newer PICs. And that's not the only one.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top