first led on pic program

Discussion in 'The Projects Forum' started by zak9000, Nov 28, 2012.

  1. zak9000

    Thread Starter New Member

    Jan 1, 2012

    i am interested in pic microcontrollers and have decided to learn about programming with them. so i downloaded a copy of microC PRO for programming in c and bought a simple usb pic programmer, i bought a pic16f84a as my first micrcontroller and after following some tutorials on youtube i wrote my first program as shown below:

    void main() {

    TRISB=0x00; //make portb output
    PORTB=0x10; //make portb4 high


    i also used a mcu simulator as shown in image one. as you can see the simulator shows that port b4 is pulled high. so then i connected a led to circuit as shown in image 2. after programming my miccontroller and setting up the set up as shown in image 2 i expected the pin b4 to go high as soon as power was supplied to the micro and the led would light up however in reality after hooking up the circuit the led never turned on.

    i only reason i can think of this happening is because i have not used an external oscillator otherwise i dont know what i have done wrong
  2. nerdegutta


    Dec 15, 2009

    I believe you need a resitor from MCLR/PIN4 to VDD. 10K-12K Ohm.

    About the external oscillator. I think you can set that to be internal, but now I don't remember if the F84 has an internal oscillator...:rolleyes: Check the datadsheet. :)
  3. MrChips


    Oct 2, 2009
    You need to create an endless loop within main()
    i.e. give your program somewhere to go.

    Code ( (Unknown Language)):
    2. void main() {
    4.     TRISB=0x00; //make portb output
    5.     PORTB=0x10; //make portb4 high
    7.     while(1);
    9. }
  4. zak9000

    Thread Starter New Member

    Jan 1, 2012
    nerdegutta thanks for your reply , the 84a does not have an internal oscillator but the question is do i need an oscillator if all i want is only turn on an led.

    about the mclr pin i will add a resistor but i am not sure why i need to add one

    mr chips: ok will add it in my code
  5. zak9000

    Thread Starter New Member

    Jan 1, 2012
    thanks nerdgutta by using your solution and adding that 10k resistor between vdd and mclr my controller is operating as intended but im not sure why i needed the extra resistor at this location. does that mean i always need a 10k resistor between the mclr pin and the supply voltage for the controller to work, i will do some reading on the mclr pin.
  6. nerdegutta


    Dec 15, 2009
    Some uC need that and some don't. However, when drawing a schematic and making code - the datasheet is your best friend. :)
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    MCLR is the Master CLeaR pin, and it will reset your PIC when low and restart the program as it goes high. So you should always drive MCLR, a resistor to Vdd works real good: don't use a wire as you need to drive this pin when programming.

    Now while *every* PIC has a MCLR pin the newer ones allow you to change the function to an I/O input, handy when you are really pushing to use every I/O pin.

    I cannot explain how this worked without an oscillator.

    The configuration bit setting are also undefined. You may have done this and not included it in your post.
  8. Markd77

    Senior Member

    Sep 7, 2009
    Without an oscillator it shouldn't run at all. There must be some kind of fluke where noise or the capacitance between the pins has let it run. Don't expect it to continue working or run at any predictable rate.
    You can use a capacitor and resistor as the oscillator, which although not very accurate, works fine. See the oscillator section of the datasheet. You need to set the oscillator mode in the configuration word. Search for:
    setting configuration in code pic16F84 and the language you are using.
  9. Dodgydave

    AAC Fanatic!

    Jun 22, 2012
    clrf TRISB,4 ; make port B4 output
    Bsf PORTB,4 ; this will make the bit high
    goto $-1 ; repeat