Led not blinking.

Discussion in 'Embedded Systems and Microcontrollers' started by lemario, Jan 31, 2016.

  1. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    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
    [​IMG]

    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:
    Code (C):
    1. void main(void) {
    2.       TRISIO.B2 = 0;
    3.       while(1)
    4.       {
    5.               GPIO.B2 = 1;
    6.               Delay_ms(1000);
    7.               GPIO.B2 = 0;
    8.               Delay_ms(1000);
    9.       }
    10. }
    Mod edit: added code tags
     
    Last edited by a moderator: Jan 31, 2016
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    What chip are you using?

    What compiler?

    Where are your configuration bit settings?

    Where is your while loop?
     
  3. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    Sorry for the bad question making.
    I added the first to. What do you mean by:

    "Where are your configuration bit settings?

    Where is your while loop?"

    http://postimg.org/image/gv4fbamu7/
     
    Last edited: Jan 31, 2016
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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: Jan 31, 2016
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
    lemario likes this.
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Read the datasheet about how to set up the pins for gpio operations.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015

    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?
     
  9. stefan.54

    New Member

    Dec 26, 2015
    28
    4
    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.
     
    lemario likes this.
  10. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    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.
     
  11. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    I did that and it does not work still. I belive its because i am not using any resistors.
     
  12. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    I am whiling to pay someone that can skype me and fix this problem..
     
  13. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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...
     
    lemario likes this.
  14. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    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: Jan 31, 2016
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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?
     
  16. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
    I understand.. The problem is that i am so noob that most times i dont even understand what ppl saying.

    [​IMG]
    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: Jan 31, 2016
  17. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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.
     
  18. stefan.54

    New Member

    Dec 26, 2015
    28
    4
    @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.
     
  19. lemario

    Thread Starter New Member

    Jan 31, 2016
    22
    3
  20. stefan.54

    New Member

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

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

    Code (Text):
    1. void main(){
    2. TRISIO.B2=0;
    3. ANSEL=0;
    4. osccon=0b00000010; //31 khz
    5. while(1){
    6. GPIO.B2=1;
    7. delay_ms(500); /0.5 ms delay
    8. GPIO.B2=0;
    9. delay_ms(500);
    10. }
    11. }
    12.  
    Again, make sure you choose the right settings in the project editor.
     
Loading...