Hello and in this video I'll be showing how to blink an LED on a PIC
microcontroller. First what you want to do is include your PIC specific header
in my case a p18f4520. Then we add an infinite loop while 1. Then we set our
PORTB as output using TRISB equals 0.
Then we set LATB equal to 1 and then we program the PIC.
Now indicated by the white LED. RB0 on PORTB is equal to 1. Which is this pin
on the PIC. Now to blink the LED we set LATB to 0 add a delay by using double
underscore delay (using controle space for intellisense) delay ms. We add 250
milliseconds and setting LATB equal to 1 again. Adding a delay underneath
LATB equal to 1. Now we program the PIC again.
Now I'm going to blink a different LED on PORTB. This can be done by setting
LATB equal to 0X02. This will now set RB1 on PORTB.
Now we program again.
As indicated by the blue LED flashing we have now set RB1. What TRISB does is
set the entire PORT as output. Then LAT is used to drive an output to either
high or low. Indicated you can set any TRIS register to input or output
according to the port letter. Which can be found on the datasheet. As this one
will be the entire B PORT. If you want to set... say this pin to also drive an output
you'll use TRIS A instead of B. Then we set the entire port low by writing to
LATB and then we drive or RB1 high by writing hexadecimal 2 the register.
In essence what you got is this entire set as 0 B 1 2 3 4 5 6 7 8 so the entire
register as output. Next what you got is sitting your output driver all too low
which is also 1 2 3 4 5 6 7 8, 8 zeros and when you drive them high you'll have...
you'll have 1 2 3 4 5 6 7 8 and then a 1 located here. If I write another 1 to
this register that will translate these 3 in hexadecimal. We'll blink or RB1 and RB0.
That is the wrong thing. Program.
And as indicated both RB1 and RB0 is blinking. Now I'm going to do an example
of how to do running LEDs. So we'll declare a global variable of size char
we'll call it run and that is initialized as one. We'll set LATB equal to run. Now to
reset the variable we'll say if run greater or equals to 0x80. Run is
equals to 1 else run is times 2. Now we can program again
Now note our RB5 is dead on this PIC. Had an accident while wiring it up.
Now how this running led code works is. By having if run is greater than this
binary value. Which is the equivalent of 80 hex. Reset our run variable to
1 , else multiplied by 2. So we'll first have... When it enters it will take 1
multiplied by 2 which is that corresponding binary value and on the
next pass, will have a 1 there and so on. Until we reach this binary value which
is then reset and then we have 1. Now if you want to sit individual pins you can
use TRISBbits.RB0 equals to 0. Then we repeat the process of
LATBbits.LB0 (we can copy that over to here). This will Now blink the white LED
connected to RB0.
and that's pretty much it too blinking LEDs with a PIC microcontroller. Any code
that I've written will be linked down in the description to my GIt. Any parts that
I have used I will have links in the description for that (removed). Thank you have a
nice day