【发布时间】:2021-12-16 19:34:59
【问题描述】:
我正在使用 MPLABX 为 PIC 18LF46K42 微控制器编写一个简单的程序,该程序将在一定延迟后打开和关闭 LED。编译器无法识别“TRISB”和“LATB”寄存器。看起来我缺少一些头文件。我不确定问题是什么。我正在使用 CCS C 编译器。这是我的代码:
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
}
}
我收到以下错误:
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:
我不确定是否需要使用不同的编译器,或者我是否缺少任何头文件。
【问题讨论】:
-
在编译器的头文件中找到寄存器定义。为您的源代码生成预处理器输出。检查,为什么不包含带有
TRISB和LATB的头文件。#if可能会抑制寄存器定义的可见性。可能你只是错过了正确的#include。 -
为什么不使用标准的 XC8 编译器?无论如何,快速浏览一下 CCS C 编译器参考,您似乎应该使用
set_tris_b(0x00)和output_b(0xff)而不是直接访问寄存器。 -
缩进代码会更容易阅读!!!
标签: c microcontroller pic18