Set I/O pins with LAT
Posted: 17 Aug 2021 19:41
Goodday Everyone,
I'm new to PIC programming and assembly language. I have a bit of knowledge of the C language and also programming with Arduino boards.
Im currently in the struggle of setting my output pins to preform correctly.
Let me state what I'm trying to do.
My end product will be a 6 LED traffic light circuit for a Lab project. But obviously one needs to start from the beginning. Know in my case I just want to turn on 6 LEDs individually in accordance with what I'm telling them to do, just as a basic start. Now from my knowledge of C++ in the Arduino IDE, it is fairly simple, one just initializes the Pin the led is connected and makes it HIGH.
OK, let's get to the problem I'm dealing with. I am using a PIC18 family (PIC18F45K22) and what I have learned is LAT is the way to go. Now I cant seem to figure out how to set the LAT to the port I want it to write to. for instance, if I simulate it on proteus led at port RB0 turns on, Led at RB1 turns on. it "skips " a line in my code and turns on Led at RB2 on but in my code, I set it to turn on RB3 (it sound confusing but if you see my code I guess you clever people will understand).
Oh, and the reason I'm saying it is jumping a line of code is I have delays in between each turn on and I could see it take 2secs longer to the next action and then turns on the wrong Led.
CODE
Any help would be greatly appreciated.
I'm new to PIC programming and assembly language. I have a bit of knowledge of the C language and also programming with Arduino boards.
Im currently in the struggle of setting my output pins to preform correctly.
Let me state what I'm trying to do.
My end product will be a 6 LED traffic light circuit for a Lab project. But obviously one needs to start from the beginning. Know in my case I just want to turn on 6 LEDs individually in accordance with what I'm telling them to do, just as a basic start. Now from my knowledge of C++ in the Arduino IDE, it is fairly simple, one just initializes the Pin the led is connected and makes it HIGH.
OK, let's get to the problem I'm dealing with. I am using a PIC18 family (PIC18F45K22) and what I have learned is LAT is the way to go. Now I cant seem to figure out how to set the LAT to the port I want it to write to. for instance, if I simulate it on proteus led at port RB0 turns on, Led at RB1 turns on. it "skips " a line in my code and turns on Led at RB2 on but in my code, I set it to turn on RB3 (it sound confusing but if you see my code I guess you clever people will understand).
Oh, and the reason I'm saying it is jumping a line of code is I have delays in between each turn on and I could see it take 2secs longer to the next action and then turns on the wrong Led.
CODE
Code: Select all
void main() {
TRISB = 0;
LATB |= 1; // writing to RB0 , to be High
delay_ms(1000);
LATB |= 2; // writing to RB1 , to be High
delay_ms(1000);
LATB |= 3; // writing to RB2 ,"
delay_ms(1000);
LATB |= 4; // writing to RB3 ,''
delay_ms(1000);
LATB |= 5; // writing to RB4 , ''
delay_ms(1000);
LATB |= 6; // writing to RB5 , ''
}