p10f200 Turn On LED

Building on the Microcontrollers and Assembly Code post this is my first project based on the amazing work from circuitbread.com.

Turning on a LED (Hello World)

Diagram from circuitbread.com

Turning on a LED (Hello World)

1
2
3
4
5
6
7
8
9
10
11
#include "p10f200.inc"
__CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF
ORG 0x0000
INIT
MOVLW ~(1 << T0CS) ; enable GPIO2
OPTION
MOVLW ~(1 << GP2) ; set and GP2 as an output
TRIS GPIO
LOOP
GOTO LOOP ; loop forever
END
  • #include "p10f200.inc" tells the IDE & compiler there are parameters already pre-defined for the chip that we can include, things like the GP2 which would reference the bit in the register for us
  • ; CONFIG this is comment telling the human we are going to do the configuration
  • __CONFIG setup the micro controller (p10f200)
    • _WDT_OFF watch dog timer off (else it will reset itself every once in a while)
    • _CP_OFF code protection off
    • _MCLRE_OFF means we can use pin 8 as a GP input and not a reset pin
  • ORG 0x0000 origin, this is the begin of the program to execute
  • INIT ; We are initializing the microcontroller over the next three lines. - this is a INIT is a label telling us where we will initialize / setup the controller anything after the ; is a human readable comment
  • MOVLW ~(1 << GP2)
    • MOVLW is an instruction which moves the literals you pass it to the working register
    • the tilda ~ is a bitwise operations that inverts 1 and 0s. IE: 1 becomes 0 and 0 becomes 1
    • the left shit << takes what we have and shifts it to the left
1
2
3
4
5
         bits 7 6 5 4 3 2 1 0
-----------------------------
1 0 0 0 0 0 0 0 1
1 << GP2 0 0 0 0 0 0 1 0
~(1 << GP2) 1 1 1 1 1 1 0 1
  • TRIS GPIO the TRIS instruction always takes GPIO as an operand. It means TriState so the GPIO can be high voltage, low voltage or high impedance (OFF). This means take the values we put in the register and load them into the TRISGPIO. So GP2 is an output because its value is 0
  • BSF GPIO, GP2 - set the bit in the register, this is setting GP2 to 1 / HIGH turning on the LED
  • LOOP - just a lable
  • GOTO LOOP - go back to the loop above, which will jump down to GOTO LOOP, so this is an infinite loop
  • END - the end of our program