问题描述
我正在使用宽度为pickit3和mplab x ide(以及x8 c编译器)的 pic12f1840 。这可能真的很容易,但是我不知道如何读取引脚的值!
void main(void) {
//setting up TESA
TRISA = 0b111111;
TRISA5 = 0; //pin 5 is output
TRISA1 = 1; //pin 1 in input
for (;;) {
RA5 = RA1;
}
}
这是我目前的代码(我省略了配置并包含了)。我有一个连接到引脚5的LED灯,还有一个连接到引脚1的按钮(带有下拉电阻)。整个设备的电压为3.3伏。
解决方法
在处理像PIC这样的Microchip控制器时,发布完整的代码是一个不错的主意。根据我的经验,真正的问题几乎总是在原始海报遗漏的地方。
此代码在模拟器中对我有效:
/*
* File: main.c
* Author: dan1138
* Compiler: XC8 v2.20
* IDE: MPLABX v5.40
*
* Created on September 29,2020,1:24 PM
*/
// PIC12F1840 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor),low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
void main(void) {
//setting up TESA
TRISA = 0b111111;
ANSELA = 0; /* make all of PORTA digital I/O */
TRISAbits.TRISA5 = 0; //pin 5 is output /* use Microchip suggested syntax */
TRISAbits.TRISA1 = 1; //pin 1 in input /* use Microchip suggested syntax */
for (;;) {
LATAbits.LATA5 = PORTAbits.RA1; /* use Microchip suggested syntax */
}
}
在您的情况下,似乎有两件事:
- 不知道您需要为数字操作配置GPIO引脚。
- 简单的语法错误。
我的示例代码使用了大约10年以来几乎所有版本的XC8编译器通常都支持的语法。
您使用的较短格式可能并不总是适用于您可以定位的每个控制器。