-1

I'm developing a firmware to control a PIC18F45k80 pinout on a customized board. Before loading and programming this pic with the final version I was testing my program/debug environment (MPLABX IDE + Pickit3) with the simplest user code: toggle some portD outputs with a 50 ms period.

3 pins of them works propperly (RD6, RD5, RD4) but it's not the case of RD3 and R2. They have no signal, they never turn on. The pin stills with 0 all the execution time. All the pins are configured and activated with the same way at the same time, as you can see in the next code:

main.c file:

//C libraries
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include <pic18f45k80.h>
#include "18f45k80_mainconfig.h"
#include <xc.h>

 //Application dependent libraries
 #include "gold_whyl_task.h"

 /*outputs defines*/
 #define CADENZA     PORTDbits.RD2 //problem with this bit
 #define CAPW        PORTDbits.RD3 //problem with this bit
 #define FREQFISSA   PORTDbits.RD4
 #define FISSAWAIL   PORTDbits.RD5
 #define COMCICLOSIR PORTDbits.RD6

 /*inputs - debug*/
 #define PGC         PORTBbits.RB6
 #define PGD         PORTBbits.RB7

 int main() 
  {

    TRISDbits.TRISD0=1;//input ACTIVACIOn 
    TRISDbits.TRISD1=1;//input CLACSON  
    TRISBbits.TRISB6=1;//pdg
    TRISBbits.TRISB7=1;//pdc

    /*outputs*/   
    TRISDbits.TRISD2=0;//output CADENZA //problem with this
    TRISDbits.TRISD3=0;//output CAPW  //problem with this

    TRISDbits.TRISD4=0;//output FREQFIJA   
    TRISDbits.TRISD5=0;//output FIJAWAIL 
    TRISDbits.TRISD6=0;//output COMCICLOSIR   

    while(1)
      { 
            COMCICLOSIR=0;
            FISSAWAIL=0;
            CAPW=0;
            CADENZA=0;
            FREQFISSA=0;

            __delay_ms(50);
            COMCICLOSIR=1;
            FISSAWAIL=1;
            CAPW=1; //this assignment has no effect --> it stills 0
            CADENZA=1;//this assignment has no effect--> it stills 0     
            FREQFISSA=1;
            __delay_ms(50);
      }
 }

What can be happening? Is there something wrong with defines, port configuration, etc?

LPs
  • 16,045
  • 8
  • 30
  • 61
Suvi_Eu
  • 255
  • 1
  • 3
  • 16
  • We have a separate exchange site for microcontrollers and electronics: http://electronics.stackexchange.com/ – arminb Apr 06 '17 at 12:09
  • what do you mean? I followed the instructions, I always configure inputs and outputs in the same way, but I have never had only one part of the port working and not working the rest one, even when configuring the whole port exactly with the same way. What kind of information can a datasheet give me about this behaviour? – Suvi_Eu Apr 06 '17 at 12:12
  • What about those pin, HW speaking? Are all circuits the same? Maybe you must enable pull-ups with register `PADCFG1`. – LPs Apr 06 '17 at 12:42
  • We can't help with this. For all we know, those pins are elecrically shorted to ground. – ThingyWotsit Apr 06 '17 at 12:56

2 Answers2

3

You should always check datasheet

Your mcu has an A/D port and, unfortunately for you, by default it uses RD2 and RD3.

You can see at page 364, ADCON1 reg that enables those pins as analog.

enter image description here

At page 92 you can see configuration at ADCON1 register at the startup: -111 1111

enter image description here

This means that at powerup/browout/WDT/reste... RD2 and RD3 are set to be analog inputs.

You must disable pins for A/D converter to use those pins as I/O.

I don't have Microchip SDK but you must do something like

ADCON1 &= 0x9F;

To set bit 6 and bit 5 to 0 and enable RD2 and RD3 as I/O

LPs
  • 16,045
  • 8
  • 30
  • 61
  • I always thought it strange Microchip decided to default some ports to being analogue, I guess it makes it high Z though. – Colin Apr 06 '17 at 14:01
-1

All of them are identically connected, internal pull up connected with these pins but turned off automatically when output is configured. But now I have found the kit of the question. PORTDbits.RD2 allows you to read from pin and maybe write on it, but without guarantees (this is the reason why some pins works and not the other). Instead of this I have used LATD option that writes the pin when pin is configured as ouptut. Now it works.

Suvi_Eu
  • 255
  • 1
  • 3
  • 16
  • @LPs I have configured ANCON1 = 0x00 (I have it in other file included); before writing the first post with the problem I have commented. I have read once again datasheet and other resources, in which it's talked about differences between LAT and PORT. They suggests always to use LAT for write output pins. I was trying to write a digital output with PORT. – Suvi_Eu Apr 06 '17 at 15:56