first led on pic program

Thread Starter

zak9000

Joined Jan 1, 2012
20
hi

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
 

Attachments

nerdegutta

Joined Dec 15, 2009
2,684
Hi.

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. :)
 

MrChips

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

Rich (BB code):
void main() {

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

    while(1);

}
 

Thread Starter

zak9000

Joined Jan 1, 2012
20
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
 

Thread Starter

zak9000

Joined Jan 1, 2012
20
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.
 

nerdegutta

Joined Dec 15, 2009
2,684
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.
Some uC need that and some don't. However, when drawing a schematic and making code - the datasheet is your best friend. :)
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

Markd77

Joined Sep 7, 2009
2,806
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.
 
Top