Led not blinking.

Thread Starter

lemario

Joined Jan 31, 2016
22
Hello. This is my first time messing with a microcontroller. I am trying this tutorial:

CIRCUIT: http://postimg.org/image/lasta61k1/
CHIP: 12f683
COMPILER: mikroC PRO for PIC v.6.6.1


I write the program just fine but the led does not work.
I might be doing some stupid mistakes but its my first time so don't shop my head of please.

code:
C:
void main(void) {
      TRISIO.B2 = 0;
      while(1)
      {
              GPIO.B2 = 1;
              Delay_ms(1000);
              GPIO.B2 = 0;
              Delay_ms(1000);
      }
}
Mod edit: added code tags
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
Never mind about the while loop. Of course you have one. I posted that too early in the morning. Too early to think.

You need to set up configuration.

Here is an example from my compiler and my chip. Yours will be different. Your may be very different.

#pragma config FOSC = INTIO67 // Oscillator Selection bits (Internal oscillator block)
#pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator used directly)
#pragma config PRICLKEN = OFF // Primary clock enable bit (Primary clock enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)


What it does is give the starting settings for the chip. If you look at the first line it tells the compiler that you want to use the internal oscillator.

You need to look at your datasheet to determine the configuration bits. Some programming environments like MPLab and MPLabX have helper programs to create the configuration bit settings based off your code.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
You also need to configure the oscillator for the speed if using the internal oscillator. And regardless of using internal or external oscillator, you will need to tell the Delay_ms the speed of your mcu. This is usually done with a #define.
 

spinnaker

Joined Oct 29, 2009
7,830
I don't use MikroC. It looks like everything is done for you when you configure the project. So I think you are doing everything right so far.

I found a similar tutorial here.

https://electrosome.com/led-blinking-pic/


So I would start with the basics.

Are you certain the chip is being programmed?

Can you test the pin with a scope or voltmeter to see if the output of B2 is changing? It could be your LED wiring.


Also post a schematic.
 

stefan.54

Joined Dec 26, 2015
28
You need to set up the osccon register before the while loop; try something like: osccon=0b00000010; which is 31khz.
Next, press ctrl+shift+e (inside mikroc) and make sure:
-the selected mcu matches the one you use
-the osc is set to Internal
-the frequency matches the osccon register (in this case you should enter 0.031 mhz)

Hope this helps.
 

Thread Starter

lemario

Joined Jan 31, 2016
22
In your code above you set TRISIO.B2 = 0; which is an output. In your screen shot you set it to 1 which is an input. Which do you actually have in your code?
It was 1. I changed it and still does not work. :/

I think is something with the resistors. In the turorial he is using some. I am not.
 

Thread Starter

lemario

Joined Jan 31, 2016
22
You need to set up the osccon register before the while loop; try something like: osccon=0b00000010; which is 31khz.
Next, press ctrl+shift+e (inside mikroc) and make sure:
-the selected mcu matches the one you use
-the osc is set to Internal
-the frequency matches the osccon register (in this case you should enter 0.031 mhz)

Hope this helps.
I did that and it does not work still. I belive its because i am not using any resistors.
 

JohnInTX

Joined Jun 26, 2012
4,787
A couple of things:

You are not configuring ANSEL. You need ANSEL = 0; to set the port to a digital port.
You have to use the LED resistors on this chip - if you do not, you will run afoul of the notorious read-modify-write problem. A better way to do your code would be to use an unsigned char variable as a port image, flip bits in that then write the whole byte to the port.
You should initialize the TRISIO register by writing a complete byte for all the port bits instead of using a single bit operation for several reasons 1) you should initialize ALL port bits, not just the ones you are using - init any unused ones to output 0 and 2) accessing individual bits on any TRIS register is not recommended by Microchip for lots of reasons.
As others have said, OSCCON must be set to agree with the clock speed you specified in the Project window AND what is set in the CONFIGURATION bits.

FWIW: MikroC doesn't use #pragmas to set the config bits - it does it in the Project setup window. I don't like that but that's the way it is...
 

Thread Starter

lemario

Joined Jan 31, 2016
22
A couple of things:

You are not configuring ANSEL. You need ANSEL = 0; to set the port to a digital port.
You have to use the LED resistors on this chip - if you do not, you will run afoul of the notorious read-modify-write problem. A better way to do your code would be to use an unsigned char variable as a port image, flip bits in that then write the whole byte to the port.
You should initialize the TRISIO register by writing a complete byte for all the port bits instead of using a single bit operation for several reasons 1) you should initialize ALL port bits, not just the ones you are using - init any unused ones to output 0 and 2) accessing individual bits on any TRIS register is not recommended by Microchip for lots of reasons.
As others have said, OSCCON must be set to agree with the clock speed you specified in the Project window AND what is set in the CONFIGURATION bits.

FWIW: MikroC doesn't use #pragmas to set the config bits - it does it in the Project setup window. I don't like that but that's the way it is...
Can u contact me at<snipped email>please? Or by some kind of direct chat here on the forum?
Mod edit: snipped email to deter spammers.
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Doing things on a back channel is restricted. The idea is to keep it public so that others can look for errors. Also, when you have success, it will provide guidance for the next person who may encounter your problem

Do the steps I suggested then post your results. I ran up the code in MikroC simulator and it toggles the LED bit. Your problem is in getting the actual chip to run. That is not unusual.
Can you post a screen shot of the Project Properties window?
What are you using to program the PIC chip itself?
 

Thread Starter

lemario

Joined Jan 31, 2016
22
Doing things on a back channel is restricted. The idea is to keep it public so that others can look for errors. Also, when you have success, it will provide guidance for the next person who may encounter your problem

Do the steps I suggested then post your results. I ran up the code in MikroC simulator and it toggles the LED bit. Your problem is in getting the actual chip to run. That is not unusual.
Can you post a screen shot of the Project Properties window?
What are you using to program the PIC chip itself?
I understand.. The problem is that i am so noob that most times i dont even understand what ppl saying.


I am using pickit2.
"LED resistors on this chip" - The problem is this i think. I probably am not wiring this correctly. I have resistors here. But don't know witch to use.
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
"LED resistors on this chip" - The problem is this i think. I probably am not wiring this correctly. I have resistors here. But don't know witch to use
You can use anything in the 220-560 ohm range for now. If you have a voltmeter, you don't even need the LEDs, just probe the pin and watch the volts change on 1 second intervals.

Looking at your config, its OK. Note the value of the CONFIG register 0xFD4. That 4 on the end says that you have configured the oscillator for INTERNAL with IO function on GP4 and 5. That's what you want.
Next: MikroC expects that you configure OSCCON to generate the 4MHz internal clock. OSCCON is 4MHz INTERNAL by default so you should be OK - I always specifically load it though..

You still have to configure ANSEL=0 or it won't be able to output anything.

Fix up and post your code with results and we'll take it from there.
 

stefan.54

Joined Dec 26, 2015
28
@lemario You have to use INTOSC, not INTOSCIO which has a different meaning.

Also, make sure you connected the anode to GP2 and the cathode to GND.

@JohnInTX I, sometimes, neglect the ANSEL register and still get a proper output.

LE: @lemario Have you ever considered using Proteus? It's a virtual simulation machine which enables you to use various components.
 

stefan.54

Joined Dec 26, 2015
28
Did you check the wiring?

If correctly set up, I would try the following code:

Code:
void main(){
TRISIO.B2=0;
ANSEL=0;
osccon=0b00000010; //31 khz
while(1){
GPIO.B2=1;
delay_ms(500); /0.5 ms delay
GPIO.B2=0;
delay_ms(500);
}
}
Again, make sure you choose the right settings in the project editor.
 
Top