[英]How to configure correctly a PIC18 port D for output direction?
I'm developing a firmware to control a PIC18F45k80 pinout on a customized board. 我正在开发一种固件来控制定制板上的PIC18F45k80引脚。 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.
在以最终版本加载和编程此图片之前,我正在用最简单的用户代码测试我的程序/调试环境(MPLABX IDE + Pickit3):切换一些portD输出,间隔为50 ms。
3 pins of them works propperly (RD6, RD5, RD4) but it's not the case of RD3 and R2. 它们的3个引脚正常工作(RD6,RD5,RD4),但RD3和R2并非如此。 They have no signal, they never turn on.
他们没有信号,他们永远不会打开。 The pin stills with 0 all the execution time.
引脚在所有执行时间内均保持为0。 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: main.c文件:
//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?
定义,端口配置等是否有问题?
You should always check datasheet 您应该经常检查数据表
Your mcu has an A/D port
and, unfortunately for you, by default it uses RD2
and RD3
. 您的单片机具有
A/D port
,不幸的是,默认情况下,它使用RD2
和RD3
。
You can see at page 364
, ADCON1
reg that enables those pins as analog. 您可以在
page 364
看到的ADCON1
reg使这些引脚成为模拟引脚。
At page 92
you can see configuration at ADCON1
register at the startup: -111 1111
在
page 92
您可以在启动时看到ADCON1
寄存器上的配置: -111 1111
This means that at powerup/browout/WDT/reste... RD2
and RD3
are set to be analog inputs. 这意味着在上电/断电/ WDT /休息时...
RD2
和RD3
设置为模拟输入。
You must disable pins for A/D
converter to use those pins as I/O
. 您必须禁用
A/D
转换器的引脚才能将这些引脚用作I/O
I don't have Microchip SDK but you must do something like 我没有Microchip SDK,但是您必须执行以下操作
ADCON1 &= 0x9F;
To set bit 6
and bit 5
to 0
and enable RD2
and RD3
as I/O
将
bit 6
和bit 5
为0
并启用RD2
和RD3
作为I/O
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).
PORTDbits.RD2允许您从引脚读取数据,也可以在引脚上写入数据,但并不能保证(这是某些引脚起作用而其他引脚不能起作用的原因)。 Instead of this I have used LATD option that writes the pin when pin is configured as ouptut.
取而代之的是,我使用了LATD选项,当引脚配置为ouptut时会写入引脚。 Now it works.
现在可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.