0

I am using MPLABX to write a simple program for PIC 18LF46K42 Microcontroller that would turn LEDs on and off after a certain delay. The compiler does not recognize "TRISB" and "LATB" registers. It looks like that I am missing some header files. I am not sure what the issue is. I am using a CCS C Compiler. Here is my code:

include <18LF46K42.h>
#include <stdio.h>
#include <stdlib.h>

#define SCL_PIN PIN_C3
#define SDA_PIN PIN_C4

// I2C declaration code
#pin_select SCL1OUT = SCL_PIN
#pin_select SCL1IN = SCL_PIN
#pin_select SDA1OUT = SDA_PIN
#pin_select SDA1IN = SDA_PIN 

#use delay(internal=16Mhz)
#use rs232(baud=4800, stream=RS232_2 , xmit=PIN_E1, rcv=PIN_E0, timeout=2000)
#use i2c(I2C1,stream=IICBUS, master, FORCE_HW)

void delay_ms ( int delay );
 
void delay_ms ( int delay )
{
int ms, i;
 
for ( ms = 0; ms < delay; ms ++ )
for ( i = 0; i < 5; i ++ );
}

void main()
{
TRISB = 0x00; // Set PORTB as output PORT
LATB = 0xFF;     // Set PORTB high initially (All LEDs on)
 
while ( 1 )
{
  LATB = ~LATB;      // Toggle the value of PORTB
         delay_ms ( 1000 );   // Delay of 1 sec
}
}

I get the following errors:

C:\MicrocontrollerTest\PIC18Test.X\main.c:39:4:  Error#12  Undefined identifier   TRISB:
C:\MicrocontrollerTest\PIC18Test.X\main.c:40:4:  Error#12  Undefined identifier   LATB:
C:\MicrocontrollerTest\PIC18Test.X\main.c:44:4:  Error#12  Undefined identifier   LATB:

I am not sure if I need to use a different compiler or am I missing any header file.

user3723326
  • 115
  • 2
  • 7
  • Locate the register definition in your compiler's header files. Generate a preprocessor output for your source code. Check, why the header file with `TRISB` and `LATB` are not included. Probably a `#if` inhibits the visibility of the register definition. Probably you are just missing the right `#include`. – harper Dec 16 '21 at 20:25
  • 3
    Why not use the standard XC8 compiler? Anyway, had a quick look in the CCS C Compiler reference and it seems like you should use `set_tris_b(0x00)` and `output_b(0xff)` instead of direct register access. – Klas-Kenny Dec 16 '21 at 20:56
  • Your code will be much easier to read if you indent it!!! – Mike Dec 17 '21 at 09:41

1 Answers1

0

You havn't include the header correctly. A # is missing: Should be:

#include <18LF46K42.h>
Mike
  • 4,041
  • 6
  • 20
  • 37