Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Now let’s try to turn an LED on and off in response to the button! We’ve already got the LED control sorted, so all we need to do is to figure out how to read the digital value of the button pin! If you feel confident, The Datasheet sections we just went over contain all the information you need to get this working. However, there are a few subtle tripping points I’ll point out below.

  2. Right off the bat, we need to know which register we should be reading from. Read through the first page of section 11 and try to figure it out.
    Hint: What’s the difference between reading from TRISx LATx and PORTx?

  3. Go ahead and code up your solution based on the register you found above. I’ll spoil the surprise a bit and let you know that it won’t work yet, but its good to have something we can tweak and test with.

  4. Now it’s time to debug! Let’s start by using a multimeter to probe the voltage on the pin, to make sure it’s not a hardware issue (as it too often is 😧). This is where pulling up the PCB in KiCad can be helpful - if you have both the schematic and the PCB open and you click on something on the schematic it will select it for you in the PCB! This is very helpful for finding where to probe.
    Tip: Remember to be very careful not to short two pins together when probing. Feel free to call someone over to help you figure out the multimeter and how best to probe.

  5. Now, you should find that when you press and release the button, the voltage on the pin doesn’t change! First of all make sure you are using the multimeter correctly by probing something you know is +3V3 (eg the +3V3 pad on C1 or R2), and once you’ve confirmed that, think about why you’re not reading something different when the button is pressed.

  6. Take a look at the schematic. You’ll notice that the button merely connects the pin to ground when pressed, and when released the pin isn’t actually connected to anything! This is called “floating”, and if you try to read the value of a floating pin you will get a random value that depends on things like electromagnetic interference and the specific internals of the PIC. To fix this, we would typically add a pull-up resistor between the pin and +3V3. As it turns out, I didn’t need to include one on this board because the PIC has its own internal pull-ups that we can enable! Unfortunately the only info on the internal pull-ups is a brief mention in the first page and the corresponding register definition! Go ahead and give enabling the pull-up a try, remember to look at the bit description and notes in the register definition. If you’ve done it correctly you should now see the pin go to +3V3 when the button is released.

  7. So now we’ve fixed the “hardware” issue (which was really still a software issue), but why is it still not working? For this, I point you to the last paragraph in section 11.0 and all of section 11.3.3. For some godforsaken reason the PIC designers thought it would be a good idea for ANSEL to be enabled by default, and you just read about what that does. If your code does not work after fixing this last issue, call someone over to give you a hand.