PIC MCU circuit problem - Page 1
Forum section: electronics / beginners

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 23, 2013, 01:21:02 pm
content: I have been learning PIC programming & decided to hook up a PIC18F4550 and have fun with it. But its not working. I am using a genuine PICkit 2. The PICkit is detecting & programming the MCU fine but when I try to run it, it does not work at all. I have a tried a simple LED blink program & a dimming program. The weird thing is that when i touch the pin (CKI/OSC1) connected to the crystal, the LED just turns on & stays on. I tried switching circuit components & even tried a different PIC18 MCU but the same problem remains. I am using the circuit on a breadboard. Please tell me what I have been doing wrong. I have 100nF capacitors between power & ground pins. I have enclosed the code & schematic. The 2nd code is written for a PIC18F14K50.Blinking LED code-#include<P18F4550.h>void delay(unsigned int x);void main(){while(1){TRISD=0xFE; //RD0 is output//toggle RD0 every 1sPORTDbits.RD0=1;delay(1000);PORTDbits.RD0=0;delay(1000);}}void delay(unsigned int x){unsigned int i;unsigned char j;for(i=0; i<x; i++){for(j=0; j<165; j++);}}LED dimming code-LIST P=18F14K50, MM=OFF, R=HEX#include<P18F14K50.inc>org 0000HCLRF TRISC, 0; PORTC IS USED FOR OUTPUTBSF T0CON, T08BIT, 0; TIMER IN 8-BIT MODEBCF T0CON, T0CS, 0; USE AS TIMERBCF T0CON, PSA; USE PRESCALERBCF T0CON, T0PS1, 0; PRESCALE VALUE=1:8BCF T0CON, T0PS2, 0;BCF T0CON, T0PS0, 0;MOVLW 9EH; INITIAL COUNT VALUEREPEAT: MOVWF TMR0L, 0BTG PORTC, 0, 0; SQUARE WAVE ON RC0BSF T0CON, TMR0ON, 0; START TIMERAGAIN: BTFSS INTCON, TMR0IF, 0; CHECK FOR OVERFLOWBRA AGAINBCF T0CON, TMR0ONBCF INTCON, TMR0IF, 0BRA REPEATend

author: AndyC_772
author_rank: Super Contributor
timestamp: November 23, 2013, 01:47:58 pm
content: How do you have the configuration fuses set? Did you use MPLABX to generate all the #pragma config settings? It sounds like you may have the system clock source configured wrong.It may also be worth writing to PORTDbits.LATD0 instead of RD0.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 23, 2013, 01:52:58 pm
content: Will try with the PORTDbits.LATD0 (I wrote the 2nd code in assembly so I don't think the code would be faulty). I use MPLAB 8.3. And yes I set the configuration bits using the UI. I did not write the code for it. Should I post the settings which I used?

author: AndyC_772
author_rank: Super Contributor
timestamp: November 23, 2013, 02:02:53 pm
content: When you touch the pin, I'd bet XTALI is picking up 50Hz noise from your finger, and clocking the MCU at that rate. Try removing your delay loops, and I'd bet the LED blinks visibly.It sounds like one of the config settings is wrong, or perhaps the layout of your circuit has a problem which means the crystal won't oscillate.

author: FrankBuss
author_rank: Unknown
timestamp: November 23, 2013, 02:06:18 pm
content: The C code looks good.Did you configure "HS" oscillator mode? Sounds like it doesn't oscillate. Could be a problem with the capacitors for the crystal. A breadboard has already some capacitance, so maybe lower the capacitors, and use as short as possible connections from the PIC to the crystal. Or try to increase the capacitors. And you could try the series resistor, which is mentioned on page 27 of the datasheet.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 23, 2013, 02:11:03 pm
content: I have enclosed the configuration bit settings that I used.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 23, 2013, 02:12:33 pm
content: Crystal is right next to the OSC pins...

author: FrankBuss
author_rank: Unknown
timestamp: November 23, 2013, 02:13:30 pm
content: See "oscillator" setting, it's XT. See the datasheet, for 8 MHz it should be HS.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 23, 2013, 02:17:51 pm
content: Okay I will try with the "HS" setting and get back to you tomorrow. Hope it works.

author: trackman44
author_rank: Regular Contributor
timestamp: November 23, 2013, 02:37:06 pm
content: Your counting up to 1000 with both j and i, an 'unsigned interger' is 1 byte, which is 8bits wide and only counts to 255. Change it to 'unsigned word', then your program should work.Will

author: AndyC_772
author_rank: Super Contributor
timestamp: November 23, 2013, 02:50:40 pm
content: In C18 and in XC8, an unsigned int is 16 bit and an unsigned char is 8 bit. The code is fine.

author: Sasja
author_rank: Contributor
timestamp: November 23, 2013, 09:07:47 pm
content: i just rebuilt it on a breadboard and got it to work.So the code is fine and the circuit too,i'd say it's the configuration bits:are you using a 8Mhz crystal as shown on your circuit? Then shouldn't the PLL prescaler setting be set to "divide by 2" instead of "No divide"? Also the FOSC = HS (not XT) is the right one for an 8Mhz crystal as has been pointed out. Have a look in the oscillator configurations chapter of the datasheet if you havent allready. Are you following some kind of tutorial to do this or figuring this out by yourself? My experience is that when you move from baseline pics to these advanced 8 bit pics, you need to take some time to figure out what all the configuration bits do and consider how they might fuck up what you're trying to do.For instance looking at your settings i see you have low voltage programming enabled. Im not sure as i never used that feature but if i read the datasheet right this would mean that you need to add a pullup to RB5 because if that pin would go low (easy thing if it is left floating) the PIC would go into programming mode...anyone any experience with this feature? am i right or just paranoid?getting to blinky can be surpisingly frustrating, in fact just now i fucked it up myself, shorting my power supply like 5 times trying to fire this circuit up on a breadboard, i had put the pic backwards, first time this happens.

author: Maxlor
author_rank: Frequent Contributor
timestamp: November 23, 2013, 11:58:21 pm
content: Quote from: Sasja on November 23, 2013, 09:07:47 pmYou're paranoidTo enter low voltage programming mode, the programmer needs to transmit a 32bit key on ICSPDAT while clocking ICSPCLK, all while holding the MCU in reset using !MCLR. It's rather unlikely that this would happen accidentally.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 03:37:56 am
content: Tried with the HS mode on the 18F14K50. No luck so far. Forced to use a PIC18F14K50 for now. The 18F4550 got spoiled for some reason. And I followed the Mazidi textbook for PIC microcontrollers for learning about the architecture & programming. For peripherals (timers, serial port, etc) I am planning to learn & figure them out myself using the datasheets & technical reference manuals provided by Microchip. Also please explain what exactly the "divide by 2" setting would do.I haven't used PICs before. Have worked with 8051s though.The 18F4550 will arrive in a few days but I guess I better get the 18F14K50 to work before using the 18F4550 again. These guys are expensive here.I have enclosed the config settings I am using on the 18F14K50. The code is the same as given above. I just changed the library to P18F14K50.h (p18f14k50.inc for the assembly code) & recompiled/assembled both of them. For the C code the LED just blinks once quickly & then stays off. For the assembly code, I am getting straight logic high on the pin (checked using the logic tool on PICkit). It is supposed to be a PWM wave with 50% duty cycle.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 03:45:25 am
content: @SasjaPlease post what config settings you used to get it to work (since you used the same code & circuit, I guess the code & circuit is good then).

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 04:01:38 am
content: Got it working yayy!!!Have enclosed the settings that I have used. I am unable to find any official documentation from Microchip regarding the config settings though for the 18F14K50 (the 18F4550 settings are there in the Config Settings Addendum). Please explain how exactly the "CPU system clock divide by 2" option would affect the working as Sasja said. And also the HFINTOSC fast start up setting & primary clock enable bit settings...how would they affect the working of the MCU?Sorry for asking so many questions but this forum is the only way for me to clear my doubts. In my college, my friends mostly use AVRs or Arduinos while the professors use 8051s (all on easy to use development boards). Very few people know how to program PICs here & don't like implementing MCU circuits on breadboards from scratch. They think its a waste of time.

author: WarSim
author_rank: Frequent Contributor
timestamp: November 24, 2013, 04:05:28 am
content: Quote from: Udayan Sinha on November 24, 2013, 03:37:56 amDid you know that setting the config bits on the config window is only a tool to generate the code to set the config bits?Code is still required to set anything other than default values.I recommend setting the config bits to fosc (internal Osc) first, just to eliminate external Osc problems.Once you know your config bits and code works change to an external Osc.Then you will only have two variables to consider.Sent from my iPad usingTapatalk

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 04:08:21 am
content: @WarSimAfter using the UI to set the config bits, I had used the Export option in the file menu to get the settings in the final hex file which I later uploaded/burned to the MCU.

author: Sasja
author_rank: Contributor
timestamp: November 24, 2013, 07:13:07 am
content: Quote from: Maxlor on November 23, 2013, 11:58:21 pmthx for the reassuranceQuote from: Udayan Sinha on November 24, 2013, 03:37:56 amJust had another look at the datasheet diagram and i guess thats only necessary if you need the usb clock source to work properly or in case you use the 96MHz PLL that only accepts a 4MHz input signal (8MHz divided by 2). But that PLL is bypassed in your case anyway i think.Quote from: Udayan Sinha on November 24, 2013, 03:37:56 amhave you tried getting the free samples from the microchip website yet? You can get up to three of any chip delivered to your doorstep for free. You might have to be a bit creative when filling in the forms about your develoment work and such but it always worked for me. A good sounding email adres probably helps too... give it a try.Quote from: Udayan Sinha on November 24, 2013, 03:45:25 amof course, dont know why i forgot to do that...bt the way i used a 20MHz crystal as i didnt have any 8MHz on handand i dont use the UI tool so the configuration that did it for me is just this pasted after the #includes:Code:#pragma config FOSC = HS#pragma config CPUDIV = OSC1_PLL2#pragma config PLLDIV = 5 (try 2 if this doesnt work)anyway i'll do some experimenting on these clock settings today as i'm not very confident about all of them myself, and i'm paranoidQuote from: Udayan Sinha on November 24, 2013, 04:01:38 ammore power to you!

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 07:22:45 am
content: I don't think the "divide by 2" setting is necessary then even if I use PLL. All it does is that it either allows the CPU or USB port to run faster. And about the free samples, I don't have a credit card (they ask for a $7.5 fee). Even I have to tinker around with the settings a little, get to know them better. Cheers

author: Sasja
author_rank: Contributor
timestamp: November 24, 2013, 08:43:17 am
content: i guess microchip doesnt sample where you live theni just tried it and you're right, that PLLDIV setting doesn't matter (assumed 18F4550, FOSC = HS, and dont care about the USB)sorry for adding to the confusion bringing it up. The oscillator circuit seems quite different for 18F4550 and 18F14K50 btw.Quote from: Udayan Sinha on November 24, 2013, 04:01:38 ami like to use the html files that you will find in a similar folder on your computer:C:/Program Files/Microchip/xc8/v1.20/docs/chips/18f14k50.htmlalong with the datasheet you should be able to figure it out.

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 08:48:35 am
content: Yeah 18F4550 is a much higher end MCU. It also has more config settings options to deal with

author: Sasja
author_rank: Contributor
timestamp: November 24, 2013, 09:12:08 am
content: Quote from: Udayan Sinha on November 24, 2013, 08:48:35 amexactly! thats where the paranoia kicks in. for more serious projects i make sure to set ALL the bits manually and comment them in code like: (a pic16F1788)Code:#pragma config CPD = OFF // data memory code protection OFF#pragma config BOREN = ON // brown out reset ON#pragma config IESO = OFF // internal/external switchover mode OFF#pragma config FOSC = INTOSC // INTERNAL OSCILLATOR, CLKIN/RA7 pin IS IO#pragma config FCMEN = OFF // fail-safe clock monitor OFF#pragma config MCLRE = ON // MCLR/Vpp/RE3 pin is MCLRE#pragma config WDTE = OFF // watchdog timer OFF#pragma config CP = OFF // flash program memory code protection OFF#pragma config PWRTE = ON // power-up timer ON#pragma config CLKOUTEN = OFF // RA6/CLKOUT is IO#pragma config LPBOR = OFF // low power brown out reset OFF#pragma config PLLEN = ON // 4xPLL ON#pragma config WRT = OFF // flash memory self-write protection OFF#pragma config STVREN = ON // stack overflow/underflow reset ON#pragma config BORV = LO // brown out reset voltage trip point LOW#pragma config VCAPEN = OFF // VCAP/RA6 pin is IO#pragma config LVP = OFF // HIGH VOLTAGE PROGRAMMING ONLYbut for quick experiments you can get away with minimal settings like for this 18F4550 blinker i only have:Code:#pragma config FOSC = HS#pragma config CPUDIV = OSC1_PLL2

author: AndyC_772
author_rank: Super Contributor
timestamp: November 24, 2013, 09:26:38 am
content: There's no need to be so selective, though. MPLAB X makes it trivially easy to generate these settings:Window > PIC Memory Views > Configuration BitsSet the config settings you want from a drop-down menuClick "Generate Source Code to Output"Copy & paste into your project.For neatness, I normally copy them into a separate file, call it "config.h" and then #include it at the start of main.c.If you're still using an older version of MPLAB, then it's time to migrate. Don't waste any more time learning under the old, discontinued IDE, and move over to MPLAB X. The early bugs are out and it's OK for production use now.

author: Sasja
author_rank: Contributor
timestamp: November 24, 2013, 09:39:18 am
content: thanks for bringing that config tool to my attention!i was doing that stuff by hand

author: AndyC_772
author_rank: Super Contributor
timestamp: November 24, 2013, 10:55:27 am
content: It's quite well hidden, isn't it? I've no idea why they couldn't have put it under a name like Tools > Set configuration

author: Udayan Sinha
author_rank: Regular Contributor
timestamp: November 24, 2013, 10:58:23 am
content: You can do the same thing in MPLAB 8.3 as well. After setting the bits in the UI, go to file & choose export. The settings & the code will be put in a hex file of your choice. In my opinion MPLAB 8.3 is still more reliable & easy to use than MPLAB X. Have tried both.