Program not working on PIC16F887 TQFN package - Page 1
Forum section: electronics / general

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 09, 2023, 01:23:35 am
content: I thought I would get fancy and move the code I had flashed from my DIP version of PIC16F887 to TQFN package. After making necessary pin re-adjustments and flashing the PIC I noticed the program I transferred over wasn't executing, so I wrote a simple one to flash an LED connected to port RC7.What I'm seeing is that in the program loop, only the first line of code gets executed.Code:// CONFIG1#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/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 = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)// CONFIG2#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)#include <xc.h>#define _XTAL_FREQ 8000000#include <stdio.h>void main(void){OSCCON = 0X70;TRISCbits.TRISC7 = 0; // LEDwhile(1){/* TODO <INSERT USER APPLICATION CODE HERE> */RC7 = 0;__delay_ms(1);RC7 = 1;}}When PIC executes this, the LED doesn't toggle and stays off. If I swap the instructions and make RC=1 first, it will stay on forever.My PICKit 4 does identify the PIC as PIC16F887 but I wonder if it's counterfeit (maybe just a PIC16F778A in sheep's clothes) as I purchased a bunch of them for cheap from a 3rd party seller on Amazon (branded Bestol) instead of a reputable source.The same behavior occurs if I try another PIC from this batch.Any thoughts?

author: MarkS
author_rank: Unknown
timestamp: January 09, 2023, 01:34:04 am
content: Have you tried a longer delay? 1ms is awfully short. Your brain takes ~80ms just to process input.As the code is written, the LED is off, a 1ms delay and the the LED lights immediately before being turned back off.

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 09, 2023, 01:54:54 am
content: Quote from: MarkS on January 09, 2023, 01:34:04 amPasted from the wrong experiment. I initially started with 1000ms, then lowered 500ms and just for fun to 1ms, same behavior.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 09, 2023, 02:15:12 am
content: INTRC? Wrong oscillator, should be INTOSC / INTOSCIO (Use clock pins as IO).Write 0x71 to OSCCON to set SCS bit to 1 (Use internal oscillator block).Ultimately, check what is _delay() doing, could be hanging.Make your own to make sure:Code:void delayms(unsigned int ms){while(ms--){for(volatile int i=0; i<1000; i++); //Not calibrated, just for the test}}

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 09, 2023, 04:19:41 am
content: [ Specified attachment is not available ] [ Specified attachment is not available ]Isn'tFOSC = INTRC_NOCLKOUTactually INTOSCIO?This program is copy and paste from my PIC16F887 DIP where it works.LE: Really strange, if I add an additional port change the LED toggles.Code:while(1){/* TODO <INSERT USER APPLICATION CODE HERE> */RC7 = 1;__delay_ms(1000);RC7 = 0;__delay_ms(1000);RC7 = 1;}Could it be the length of the wires between the TQFN adapter and the breadboard/signal integrity issue? Each wire is 7.8".

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 09, 2023, 10:22:21 am
content: 7.8" without any capacitor near the pic?Sure, that could be a issue.Try soldering a 10uf cap in the tqfp adapter pins.

author: Salome2
author_rank: Regular Contributor
timestamp: January 09, 2023, 10:57:08 am
content: Could be the value of the resistor in series with the LED. Too small ohms and you are in trouble.It could also be that your pic is entering program mode on power-up because you connect the res-LED to ground to bbit7 instead of cbit7. (check to see if your LED wiring and pin use are correct).As already been stated:1)Ensure you have Vdd-Vss bypass cap.2)Set delay ms value back to 1000.3)SCS is correct value.4)Make sure you have a pullup res on Reset pin 1.

author: H.O
author_rank: Frequent Contributor
timestamp: January 09, 2023, 01:24:25 pm
content: QuoteI don't read C very well but as far as I see it's not the addition of the extra port toggle but the addition of the extra delay that does it, which I believe is what MarkS tried to convey in the very first reply. Your original code:Code:while(1){/* TODO <INSERT USER APPLICATION CODE HERE> */RC7 = 0;__delay_ms(1);RC7 = 1;}RC7 is set low.Then a delay of whatever time, be it 1ms or 1h.Then RC7 is set high.Then IMMIDEATELY the loop starts over setting RC7 low again, effectively leaving it high for just a couple of cycles.You need to have a delay after setting RC7=1 as well - which is what you got when added the second toggle.

author: MarkS
author_rank: Unknown
timestamp: January 09, 2023, 05:23:22 pm
content: Quote from: H.O on January 09, 2023, 01:24:25 pmYES! Thank you. Words hard.

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 13, 2023, 10:12:33 pm
content: Quote from: H.O on January 09, 2023, 01:24:25 pmIndeed, I misinterpreted my own codeIt makes sense why it didn't work.Initially I was adamant that I had the same code running in my other project but I was wrong.Sorry for the thrash everyone and thank you all for your replies!As for the TQFN chip, I still can't get other stuff to work on it like rotary encoder program so I gave up on it and went back to the THT version which works fine.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 14, 2023, 12:51:07 pm
content: Did you try the encoder program I postedin your thread?It works perfect and it's pretty simple.

author: 741
author_rank: Frequent Contributor
timestamp: January 14, 2023, 06:45:11 pm
content: Just a guess... For some PICs, the TQFP pinout differs from the QFN pinout, could it be you looked at the wrong table?"PIC16F884/887 44-PIN SUMMARY (QFN)" vs "PIC16F884/887 44-PIN SUMMARY (TQFP)"

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 15, 2023, 03:57:34 am
content: Quote from: DavidAlfa on January 14, 2023, 12:51:07 pmI did not try it on the tqfn package, but my version works fine on the DIP and doesn't work at all on the TQFN.

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 15, 2023, 04:00:31 am
content: Quote from: 741 on January 14, 2023, 06:45:11 pmYeah, it's very possible. I did notice the pin out difference and think I adjusted it properly but there's always a chance I messed up. It's a total rat's nest with that many pins and wires close to each other.

author: newtekuser
author_rank: Frequent Contributor
timestamp: January 21, 2023, 06:32:29 pm
content: Update: soldered the SMD chip directly onto a SMD-DIP PCB adapter and everything is working fine. Wires are a lot shorter now too.