Trouble in PWM Configuration with ICD 3 in PIC12F615?

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

Trouble in PWM Configuration with ICD 3 in PIC12F615?

Postby thanhvu94 » Thu Jun 01, 2017 5:55 pm

I'm working with a self-made PCB made by an electronic technician in my lab (have a PIC12F615 SMD type on it), and I need to generate PWM signal of 17kHz and 50% duty cycle to the CCP1 pin using ICD 3 Debugger with MPLAB XC8. Here is the code:

Code: Select all
#include <xc.h>

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config IOSCFS = 4MHZ    // Internal Oscillator Frequency Select (4 MHz)
#pragma config BOREN = OFF      // Brown-out Reset Selection bits (BOR disabled)

int main(void) {
ANSEL = 0x00;   // All IO pins are configured as digital
GPIO = 0;      // Reset all GPIO
TRISIO = 0b00100000;   //All GPIO pins to output

CCP1CON = 0x00;    //disable PWM pin CCP1

//PWM frequency
PR2  = 58;    //Frequency = 17 kHz (with clock 4MHz)

//PWM duty cycle (50%)
CCPR1L = 29;     //PR2*0.5

//PWM Enable
CCP1CON = 0x0C ;    //PWM enable, P1A output active-high

//Timer2 (Prescaler = 1:1)
TMR2 = 0;      //clear TM2 counter

while(1)
{
PIR1 &= ~(0x01 << 1);      //clear Timer2 Flag on TMR2IF
TMR2ON |= (0x01 <<2);   //Enable Timer2
while(PIR1.TMR2IF == 0);   //wait until TM2IF is high
}

return 0;
}


The compilation has no error. However, when I tried to load the code into my PIC, there is an error like this:
Code: Select all
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3ff
configuration memory
program memory
Address: 0  Expected Value: 2bfe  Received Value: 280e
Fail to program device


When this program fails, I replace my current PIC with a new one. First, I load the empty program, it could load the program inside. Then, I tried again with my program, the same error happens. After that, I tried again with empty program, it is no longer possible to load the program inside. Thus, I suppose my error doesn't come from the problem of the PIC or the ICD 3, but maybe the software program itself. Did I do something wrong in my program that kills the chip?

Here are the images for the PCB, schematics of the PCB and my hardware connection (The image is large so I cannot upload directly here). The PCB is attached into the Crazyflie and get the voltage supply from the Crazyflie quadcopter (3-4V).
Link: https://drive.google.com/file/d/0B6FAxuaQs3YtQmVmZkRaSGlPdnM/view?usp=sharing

Thank you for your help.
thanhvu94
 
Posts: 5
Joined: Tue May 30, 2017 9:24 am

Re: Trouble in PWM Configuration with ICD 3 in PIC12F615?

Postby ric » Thu Jun 01, 2017 11:07 pm

You're sharing the PGC pin with the U2 switch input, so this chip will be switching rapidly when you are programming the chip, which could cause problems.
Can you lift pin 6 of that chip and see if the problem persists?

I also think the area around U2 and the MOSFETs is badly designed. The gate of the not selected MOSFET is floating, and you have no pulldown resistor to force it off.
A demux, like a 74HC139 would have worked better.

If you're able to redesign, you could change your pushbutton to pull down rather than up, and connect it to GP3 with MCLR disabled, so you could move the U2-IN control from GP1 to GP5 to free up PGC.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 454
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Trouble in PWM Configuration with ICD 3 in PIC12F615?

Postby thanhvu94 » Fri Jun 02, 2017 8:56 am

ric wrote:You're sharing the PGC pin with the U2 switch input, so this chip will be switching rapidly when you are programming the chip, which could cause problems.
Can you lift pin 6 of that chip and see if the problem persists?

I also think the area around U2 and the MOSFETs is badly designed. The gate of the not selected MOSFET is floating, and you have no pulldown resistor to force it off.
A demux, like a 74HC139 would have worked better.

If you're able to redesign, you could change your pushbutton to pull down rather than up, and connect it to GP3 with MCLR disabled, so you could move the U2-IN control from GP1 to GP5 to free up PGC.



Thank you so much for your reply.

I'm quite new to the programming of PIC, what exactly do you mean by LIFTING pin 6? And why PGC pin switching rapidly might make the chip dead when it shares the pin with U2 switch? By the way, is there any pin configuration in my program that could damage the chip?

I'm not the one who designs this PCB, it is a technician in the lab who did, and unfortunately he was on holiday until next week. Thus, I'm not able to redesign the PCB again, is it possible to still make it work without redesigning for the moment?
thanhvu94
 
Posts: 5
Joined: Tue May 30, 2017 9:24 am

Re: Trouble in PWM Configuration with ICD 3 in PIC12F615?

Postby ric » Sun Jun 04, 2017 11:02 pm

what exactly do you mean by LIFTING pin 6?

I'm talking about disconnecting the pin from the circuit. The easiest way is to unsolder the pin and lift it off the pad (assuming it is a surface mount package).

And why PGC pin switching rapidly might make the chip dead when it shares the pin with U2 switch?

It is a very bad practice to share the programming pins with any other function, and you must be very careful if it's unavoidable.
In your case, the PGC pin is selected to the control input on your multiplexer, so the multiplexer will be switching rapidly when the chip is being programmed, which could cause all sorts of other unexpected effects.
This may not be your immediate problem, but it's something that can't be ruled out.

Personally, I would remove the U2 chip, and see if the PIC can be programmed and tested first, without that chip there.
When your designer returns, you need to review the MOSFET switching design. As I mentioned, there is nothing to force them to switch off quickly when you remove drive from the gate, so no way will they switch at 17kHz.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 454
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Trouble in PWM Configuration with ICD 3 in PIC12F615?

Postby thanhvu94 » Mon Jun 05, 2017 7:59 pm

ric wrote:
what exactly do you mean by LIFTING pin 6?

I'm talking about disconnecting the pin from the circuit. The easiest way is to unsolder the pin and lift it off the pad (assuming it is a surface mount package).

And why PGC pin switching rapidly might make the chip dead when it shares the pin with U2 switch?

It is a very bad practice to share the programming pins with any other function, and you must be very careful if it's unavoidable.
In your case, the PGC pin is selected to the control input on your multiplexer, so the multiplexer will be switching rapidly when the chip is being programmed, which could cause all sorts of other unexpected effects.
This may not be your immediate problem, but it's something that can't be ruled out.

Personally, I would remove the U2 chip, and see if the PIC can be programmed and tested first, without that chip there.
When your designer returns, you need to review the MOSFET switching design. As I mentioned, there is nothing to force them to switch off quickly when you remove drive from the gate, so no way will they switch at 17kHz.


OK I understand. But can you check if the code I write is a correct one to produce the PWM signal? Or is there anything in my program that may cause the problem?

One more thing, I don't want to switch the U2 at 17kHz, I just want to use the pin CCP1 to produce a PWM signal with 17kHz frequency (the switch is just to choose which LED I will export the PWM to). I think it's possible?
thanhvu94
 
Posts: 5
Joined: Tue May 30, 2017 9:24 am


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 26 guests