Reading switches with PIC – MikroC

Home Forums MikroC Pro for PIC Tutorial Discussions Reading switches with PIC – MikroC

Tagged: , , ,

Viewing 3 reply threads
  • Author
    Posts
    • #4896
      Geotron
      Participant

      Hi there,

      I am beginner with PIC microcontrollers and discovered Student Companion recently. Many thanks for the good work.

      I worked with your tutorial about reading switches with PIC – MikroC (see it here: https://www.studentcompanion.co.za/reading-switches-with-pic-microcontroller-mikroc/)

      I modified it to work with 16F887 and works fine, but there is one part I cannot understand.

      The end of the code is as follows:

      if(PORTC == 0b00) //If both switches are pressed
      {
      Delay_ms(100); //Switch Debounce
      if(PORTC == 0b00)//If both switches are still pressed
      {
      LATB.F0 = 1; //LED 1 ON
      LATB.F1 = 1; //LED 2 ON
      }
      }
      if(PORTC == 0b11) //If both switches are pressed
      {
      Delay_ms(100); //Switch Debounce
      if(PORTC == 0b11)//If both switches are still pressed
      {
      LATB.F0 = 0; //LED 1 ON
      LATB.F1 = 0; //LED 2 ON
      }

      The two “ifs” are opposite (0b00 – 0b11) but are considered as pressed switch.
      Then the four “LATB.s” are opposite (0 – 1) but considered as LED ON.

      Could somebody explain it?

      Thank you in advance.

    • #4899
      Bitahwa Bindu
      Keymaster

      Hi, Thanks for following StudentCompanion.

      The 2nd “If statements” comments are incorrect but the code is correct as you can see it’s working. any word or text after // is considered comment is C language to explain what the code is doing, in our case, I think it was a copy and paste error, we forgot to change the second statement. Thanks for notifying us, we have rectified the article.

    • #4900
      Geotron
      Participant

      Yes! If I ignore the commends I can see it. Heh, I am beginner and mixed me the fact that in a pull-up switch  condition “one” means that the switch is open.  Thank you again for the good work.

    • #4901
      Bitahwa Bindu
      Keymaster

      Yes, if you are using a pull-up resistor. We explained it very well and show the current flow when the switch is Open and when is closed in this video: https://youtu.be/X4KN8_irJ6s

       

Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Select your currency
EUR Euro
USD United States (US) dollar