Why won't this PIC code light up my LEDs?

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:31:32

问题


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);
}

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

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

Any ideas?


回答1:


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

From the Datasheet for this device

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.

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

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

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);



回答2:


I suppose that you didn't configure the MCPU oscillator, try to define:

;   Oscillator:
    config  FOSC = INTIO2           ;Internal RC oscillator
;
;   PLL x4 Enable bit:
    config  PLLCFG = OFF

and

;Define oscillator frequency 
;{
    movlw   b'01100000'
    movwf   OSCCON
    movlw   b'01000000'
    movwf   OSCTUNE
;};

This directives are for MPLAB asm and not for Hi-Tech, but file registers should have the same names.



来源:https://stackoverflow.com/questions/9937605/why-wont-this-pic-code-light-up-my-leds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!