为什么此PIC代码无法点亮我的LED? [英] Why won't this PIC code light up my LEDs?

查看:131
本文介绍了为什么此PIC代码无法点亮我的LED?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码不会将我的PIC18F14K50上的任何引脚设置为高电平,但是再简单不过了!

The following code won't set any of the pins high on my PIC18F14K50, yet it couldn't be simpler!

#include <pic18.h>
#include <htc.h>

void main(void)
{
  // Set ALL pins to output:
  TRISA = 0;
  TRISB = 0;
  TRISC = 0;

  // Set ALL pins to high:
  LATA = 0b11111111;
  LATB = 0b11111111;
  LATC = 0b11111111;

  // Leave pins high and wait forever:
  while (1);
}

我正在使用MPLAB v8.43和高科技ANSI C编译器.

I'm using MPLAB v8.43 and the Hi-Tech ANSI C Compiler.

一个逻辑探针显示除了VUSB和MCLR之外,没有其他引脚为高电平.

A logic probe shows none of the pins high except the VUSB and the MCLR.

有什么想法吗?

推荐答案

至少某些引脚可以配置为模拟输入.

At least some of the pins may be configured as Analog Inputs.

从此设备的数据表中

通过设置ANS3来选择RA4引脚作为模拟引脚的操作 ANSEL寄存器中的位,它是在设置后的默认设置 上电复位.

The operation of pin RA4 as analog is selected by setting the ANS3 bit in the ANSEL register which is the default set-ting after a Power-on Reset.

如果未设置ANSEL寄存器,则该引脚将被配置为模拟输入,因此无法用作输出.

If you do not set the ANSEL register the pin cannot be used as output as it is configured as an analog input.

这适用于所有可以用作A/D输入的引脚,但不包括您拥有的所有引脚.

This applies to all the pins that can be A/D inputs, which does not cover all the pins you have.

然后我在您的代码中再也看不到任何配置位设置.该设备例如有2种不同的指令集,您至少必须在配置位中指定要使用的指令集.

Then again I do not see any configuration bit setup in your code. That device e.g. has 2 different instruction sets and you have to at the very least specify which instruction set you are using in the configuration bits.

您可以尝试在包含之后紧随其后将其添加到代码顶部:

You may try adding this to the top of your code just after the includes :

// Configuration BITS setup
__CONFIG(1, FOSC_INTIO2 & XINST_OFF);
__CONFIG(2, WDTEN_OFF & PWRTEN_ON);
__CONFIG(3, MCLRE_OFF);

这篇关于为什么此PIC代码无法点亮我的LED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆