Author Topic: PIC MCU circuit problem  (Read 11101 times)

0 Members and 1 Guest are viewing this topic.

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
PIC MCU circuit problem
« on: November 23, 2013, 01:21:02 pm »
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 1s
      PORTDbits.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 0000H
CLRF TRISC, 0;              PORTC IS USED FOR OUTPUT
BSF T0CON, T08BIT, 0;       TIMER IN 8-BIT MODE
BCF T0CON, T0CS, 0;         USE AS TIMER
BCF T0CON, PSA;             USE PRESCALER
BCF T0CON, T0PS1, 0;        PRESCALE VALUE=1:8
BCF T0CON, T0PS2, 0;       
BCF T0CON, T0PS0, 0;
MOVLW 9EH;                  INITIAL COUNT VALUE
REPEAT: MOVWF TMR0L, 0
BTG PORTC, 0, 0;            SQUARE WAVE ON RC0
BSF T0CON, TMR0ON, 0;       START TIMER
AGAIN: BTFSS INTCON, TMR0IF, 0;    CHECK FOR OVERFLOW
BRA AGAIN
BCF T0CON, TMR0ON
BCF INTCON, TMR0IF, 0
BRA REPEAT
end
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC MCU circuit problem
« Reply #1 on: November 23, 2013, 01:47:58 pm »
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.

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #2 on: November 23, 2013, 01:52:58 pm »
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?
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC MCU circuit problem
« Reply #3 on: November 23, 2013, 02:02:53 pm »
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.

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: PIC MCU circuit problem
« Reply #4 on: November 23, 2013, 02:06:18 pm »
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.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #5 on: November 23, 2013, 02:11:03 pm »
I have enclosed the configuration bit settings that I used.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #6 on: November 23, 2013, 02:12:33 pm »
Crystal is right next to the OSC pins...   :(
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: PIC MCU circuit problem
« Reply #7 on: November 23, 2013, 02:13:30 pm »
See "oscillator" setting, it's XT. See the datasheet, for 8 MHz it should be HS.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #8 on: November 23, 2013, 02:17:51 pm »
Okay I will try with the "HS" setting and get back to you tomorrow. Hope it works.
 

Offline trackman44

  • Regular Contributor
  • *
  • Posts: 67
  • Country: ca
Re: PIC MCU circuit problem
« Reply #9 on: November 23, 2013, 02:37:06 pm »
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
How 'bout them Maple Leafs?
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC MCU circuit problem
« Reply #10 on: November 23, 2013, 02:50:40 pm »
In C18 and in XC8, an unsigned int is 16 bit and an unsigned char is 8 bit. The code is fine.

Offline Sasja

  • Contributor
  • Posts: 49
  • Country: 00
Re: PIC MCU circuit problem
« Reply #11 on: November 23, 2013, 09:07:47 pm »
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  :o...
anyone any experience with this feature? am i right or just paranoid?  ;D
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  |O, first time this happens.
 

Offline Maxlor

  • Frequent Contributor
  • **
  • Posts: 565
  • Country: ch
Re: PIC MCU circuit problem
« Reply #12 on: November 23, 2013, 11:58:21 pm »
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  :o...
anyone any experience with this feature? am i right or just paranoid?  ;D
You're paranoid  ;) To 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.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #13 on: November 24, 2013, 03:37:56 am »
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.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #14 on: November 24, 2013, 03:45:25 am »
@Sasja
Please 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).
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #15 on: November 24, 2013, 04:01:38 am »
 :clap: 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. :(
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC MCU circuit problem
« Reply #16 on: November 24, 2013, 04:05:28 am »

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.

Did 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 using Tapatalk
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #17 on: November 24, 2013, 04:08:21 am »
@WarSim
After 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.
 

Offline Sasja

  • Contributor
  • Posts: 49
  • Country: 00
Re: PIC MCU circuit problem
« Reply #18 on: November 24, 2013, 07:13:07 am »
You're paranoid  ;) To 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.
thx for the reassurance :)

Also please explain what exactly the "divide by 2" setting would do.
Just 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.

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.
have 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.

@Sasja
Please 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).
of course, dont know why i forgot to do that...
bt the way i used a 20MHz crystal as i didnt have any 8MHz on hand
and i dont use the UI tool so the configuration that did it for me is just this pasted after the #includes:
Code: [Select]
#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 paranoid :)

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. :(
more power to you!
« Last Edit: November 24, 2013, 07:16:20 am by Sasja »
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #19 on: November 24, 2013, 07:22:45 am »
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  :-+
 

Offline Sasja

  • Contributor
  • Posts: 49
  • Country: 00
Re: PIC MCU circuit problem
« Reply #20 on: November 24, 2013, 08:43:17 am »
i guess microchip doesnt sample where you live then :(

i 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.

I am unable to find any official documentation from Microchip regarding the config settings though for the 18F14K50
i 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.html
along with the datasheet you should be able to figure it out.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #21 on: November 24, 2013, 08:48:35 am »
Yeah 18F4550 is a much higher end MCU. It also has more config settings options to deal with  :scared:
 

Offline Sasja

  • Contributor
  • Posts: 49
  • Country: 00
Re: PIC MCU circuit problem
« Reply #22 on: November 24, 2013, 09:12:08 am »
Yeah 18F4550 is a much higher end MCU. It also has more config settings options to deal with  :scared:
exactly! 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: [Select]
#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 ONLY

but for quick experiments you can get away with minimal settings like for this 18F4550 blinker i only have:
Code: [Select]
#pragma config FOSC = HS
#pragma config CPUDIV = OSC1_PLL2
« Last Edit: November 24, 2013, 09:14:04 am by Sasja »
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC MCU circuit problem
« Reply #23 on: November 24, 2013, 09:26:38 am »
There's no need to be so selective, though. MPLAB X makes it trivially easy to generate these settings:

Window > PIC Memory Views > Configuration Bits

Set the config settings you want from a drop-down menu

Click "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.

Offline Sasja

  • Contributor
  • Posts: 49
  • Country: 00
Re: PIC MCU circuit problem
« Reply #24 on: November 24, 2013, 09:39:18 am »
thanks for bringing that config tool to my attention!
i was doing that stuff by hand :palm:
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC MCU circuit problem
« Reply #25 on: November 24, 2013, 10:55:27 am »
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 :palm:

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: PIC MCU circuit problem
« Reply #26 on: November 24, 2013, 10:58:23 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf