Maker Pro
Maker Pro

pic 16f870 programming

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

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
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!
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
@steve but then whats the purpose of microcontroller :)

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