Connect with us

Design Simple microcontroller circuit

Discussion in 'Microcontrollers, Programming and IoT' started by sid2286, Aug 31, 2016.

Scroll to continue with content
  1. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    i am Trying to build a very basic circuit, the working is as follows:

    operating voltage is 3.3Vdc

    1. Press switch 1- LED starts to Toggle
    2. Press Switch 2- Stop the toggle
    3. Long press Switch 2- LED blink 5 times.

    i used 18F4520 and it worked (But a costly solution) so i switched to 12F510 but not able to program., not able to figure out the issue.

    Can someone kindly help me with the code or any analog circuit.


    #include <18f4520.h>
    #fuses INTRC,NOWDT,MCLR //,PROTECT
    #use delay(clock=8000000)

    void main()
    {
    while(True)
    {
    if ( input(PIN_B2) == 0)
    {
    while(True)
    {
    output_toggle(Pin_B0);
    delay_ms(500);
    if( input(Pin_B1) == 0)
    break;
    }
    }
    output_low(Pin_B0);

    if(input(Pin_B1) == 0 && delay_ms(3000))
    {

    output_high(Pin_B0);
    delay_ms(100);
    output_low(Pin_B0);
    delay_ms(100);
    output_high(Pin_B0);
    delay_ms(100);
    output_low(Pin_B0);
    delay_ms(100);
    output_high(Pin_B0);
    delay_ms(100);
    output_low(Pin_B0);
    delay_ms(100);
    }
    }
    }
     
    Last edited: Aug 31, 2016
  2. Sjoert de Boer

    Sjoert de Boer

    3
    0
    Aug 31, 2016
    Hello!
    To make this circuit in analog components solely, it would be rather costly.
    For the C-code, its nice to use functions (in combination with hardware interrupts.) For switch 1,2:
    the switch not being debounced might give problems when using a hw interrupt.

    bool sw1 = 'value switch 1';
    bool sw2 = 'value switch 2' ;

    //LED blink as long as sw1 is engaged
    void toggle_sw1( sw1 && !sw2){
    led =! led;
    delay_ms(1000);
    }
    // a do while loop can also be used within main or some function
    do
    led = ! led;
    delay_ms(1000);
    while(sw1 & !sw2);

    For switch 5 you can make a counter with for example a for -loop.

    for(int j= 0; j < 6; J++){
    J++;
    delay_ms(1000);
    if(j == '5'){
    blink_five_times();
    }
    }


    void blink_five_times(){
    for(int i=0; i <6; i++){
    led=!led;
    delay_ms(1000);
    }
    }

    I have not personally test this code, but I hope it might point in the good direction :). I have included two ways to debounce switches. It is also possible to use a software interrupt.
    you can for example read the state of the switch a few times, and have it run trough a 500 ms loop. and look for a stable reading.
     

    Attached Files:

  3. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    651
    May 8, 2012
    This topic needs to be relocated to its pertinent forum. That would be Microcontrollers & Programming.

    Chris
     
    davenn likes this.
  4. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Hi sid, can you elaborate on what specifically happened. You said that you are not able to program the 12F510 - are you having issues getting the chip configured? or getting any output from the chip?
     
  5. davenn

    davenn Moderator

    13,722
    1,913
    Sep 5, 2009

    done
     
  6. OBW0549

    OBW0549

    159
    118
    Jul 5, 2016
    You say you switched from the 18F4520 to the 12F510, but the code you posted still uses the #include directive for the 18F4520. That might be the problem.
     
    chopnhack likes this.
  7. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Exactly. The pin outs are probably not even the same. Hopefully sid will be back on to elaborate on the exact problem.
     
    OBW0549 likes this.
  8. OBW0549

    OBW0549

    159
    118
    Jul 5, 2016
    Definitely not! The 12F510 is an 8-pin device, and the 18F4520 is 40/44 pins.
     
  9. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    Hello,

    I did make the necessary changes such as required header file to #include <18f4520.h> , i used the 8 pin IC and made the necessary cirucit as per the pinout of the IC.

    while i was programming using pickit3 i was getting an error for OSSCAL but ignoring that i still programmed and it did program successfully.
    i used http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit3%20Programmer%20Application%20v3.10.zip
    programming.

    i tried to simulate the same on proteus as well but still didnt work.

    i guess the IC is not getting programmed.

    Request you to help.

    thanks,
    SId
     
  10. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Yes, but didn't you say that you changed from the 18f to the smaller 12f pic? If you did then you would have to call the header for the 12f pic and not the 18f. They are not compatible, the setup for each chip is separate.
     
  11. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    This is the error I am getting, everytime I am trying to program.
     

    Attached Files:

    • IMG1.JPG
      IMG1.JPG
      File size:
      176.2 KB
      Views:
      92
  12. davenn

    davenn Moderator

    13,722
    1,913
    Sep 5, 2009
    can you please respond to chopnhack's comments in post #10
    you have not stated that you understood his comment and acted on it
     
  13. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    Yes Sorry I completely skipped this one, Yes i did change the header file to 12F510 , while programming.

    I posted the code which was working for 18F4520. so yes i did change the header file.
     
  14. Amar Dhore

    Amar Dhore

    129
    12
    Dec 2, 2015
    I do not see the pin assignment.

    You have to define the direction of the pin (IN/OUT) and then clear any Analog (ANSEL) registers associated with the Pins.
     
  15. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Ok, good. Now with the pic12's the oscal value can be lost during programming - especially if you are not using mplabs ide. What programmer and ide are you using?
     
  16. NorthGuy

    NorthGuy

    53
    16
    Mar 24, 2016
    Do you really want to battle these OSCCAL (and other) problems with 10-year old chips? Why not to try PIC12F1501?
     
  17. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    Initially i was trying MPLAB IDE but since it was not working i used, pickit3 standalone software which i had shared previously.
    i read multiple forums wherein most of them suggest pickit 3 is a problem while using microchips baseline controllers.

    i am clueless on how to go about with it now. :(
     
  18. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    Thanks Amar, the CCS complier internally handles the direction.
     
  19. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    There is your issue - when using mplabs, osscal is preserved. Third party programs do not, AFAIK. The chip might be wasted at this point. Do a web search for resetting osscal and see what comes up.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-