Set I/O pins with LAT

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
StephCode
Posts: 1
Joined: 17 Aug 2021 19:11

Set I/O pins with LAT

#1 Post by StephCode » 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

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 , ''
}
Any help would be greatly appreciated.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Set I/O pins with LAT

#2 Post by filip » 02 Sep 2021 13:37

Hi,

Try also to set TRISB register to zero, i.e. making PORTB as output.

Regards,
Filip.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Set I/O pins with LAT

#3 Post by janni » 02 Sep 2021 16:49

Try this

Code: Select all

void main() {

  TRISB = 0;
  LATB = 0;
  
  LATB |= 1;            // set RB0
        delay_ms(1000);
  LATB |= 2;            // set RB1 
         delay_ms(1000);
  LATB |= 4;            // set RB2
        delay_ms(1000);
  LATB |= 8;            // set RB3 
         delay_ms(1000);
  LATB |= 16;           // set RB4 
        delay_ms(1000);
  LATB |= 32;           // set RB5 
}
The following may be less portable and thus not a C-approved way, but it produces less code:

Code: Select all

void main() {

  TRISB = 0;
  LATB = 0;
  
  LATB.B0= 1;            // set RB0 
        delay_ms(1000);
  LATB.B1= 1;            // set RB1
         delay_ms(1000);
  LATB.B2 = 1;            // set RB2 
        delay_ms(1000);
  LATB.B3 = 1;            // set RB3
         delay_ms(1000);
  LATB.B4 = 1;            // set RB4
        delay_ms(1000);
  LATB.B5 = 1;            // set RB5 
}

Post Reply

Return to “PIC PRO Compilers”