Connect with us

pic 16f870 programming

Discussion in 'Microcontrollers, Programming and IoT' started by gurjant.sandhu33, Feb 25, 2014.

Scroll to continue with content
  1. gurjant.sandhu33

    gurjant.sandhu33

    51
    0
    Dec 10, 2013
    i am new to programming in mikro c, altough i know some basics
    i want to know how to programme a microcontroller such that i can operate two switces at same time
    like -
    if i press button1 light 1 glow
    if i press button2 light 2 glow and
    if i press button1 and 2 simuntaneously then light 1 and 2 glow

    a programme would help:)
     
  2. kpatz

    kpatz

    330
    88
    Feb 24, 2014
    Inside a loop, check button1, if pressed turn on LED 1, if not turn off LED 1.
    check button2, if pressed turn on LED 2, if not turn off LED 2

    The loop will keep it checking the buttons and updating the LEDs.

    The loop itself should be doable in about 5 lines of code, and that's if you put the curly brackets on their own lines.

    You'll need code before the loop to initialize some registers, especially the TRIS ones to select what pins are inputs and what ones are outputs. You may need to set the pins to digital as well via an ANSEL register. The datasheet for the PIC you're using will tell all.

    Do you know C at all?
     
  3. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    How about connecting the LEDs to the same input that senses the button state>

    Then you can even remove the microcontroller and it will still work!
     
  4. gurjant.sandhu33

    gurjant.sandhu33

    51
    0
    Dec 10, 2013
    @kpatz i have tried that but it wont work
     
  5. gurjant.sandhu33

    gurjant.sandhu33

    51
    0
    Dec 10, 2013
    @steve but then whats the purpose of microcontroller :)
     
  6. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    To do something totally unnecessary.

    Your problem is solved best without the microcontroller.

    If kpatz's solution doesn't work then your code is wrong.

    Try posting your code that follows kpatz's suggestion.
     
  7. gurjant.sandhu33

    gurjant.sandhu33

    51
    0
    Dec 10, 2013
    yes my code was slightly wrong but now i have solved the problem

    void main()
    org 0x10
    {
    trisb=0x00;
    trisc=0xff;
    while(1)
    {
    if(portc.f0==0)
    {
    portb.f0=1;
    }
    if(portc.f0==1)
    {
    portb.f0=0;
    }
    if(portc.f1==0)
    {
    portb.f1=1;
    }
    if(portc.f1==1)
    {
    portb.f1=0;
    }
    }
    }

    thanx everyone for help
    regards
     
  8. kpatz

    kpatz

    330
    88
    Feb 24, 2014
    You can shorten that code to:
    Code:
    void main()
    org 0x10
    {
      trisb=0x00;
      trisc=0xff;
      while(1)
      {
        if(portc.f0==0)
           portb.f0=1;
        else
           portb.f0=0;
    
        if(portc.f1==0)
           portb.f1=1;
        else
           portb.f1=0;
      }
    }
    
    Or even shorter:
    Code:
    void main()
    org 0x10
    {
      trisb=0x00;
      trisc=0xff;
      while(1)
      {
        portb.f0 = !portc.f0;  // set RB0 to inverse of RC0
        portb.f1 = !portc.f1;  // set RB0 to inverse of RC0
      }
    }
    
     
    Last edited: Feb 26, 2014
  9. gurjant.sandhu33

    gurjant.sandhu33

    51
    0
    Dec 10, 2013
    thanx kpatz
     
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

-