读取 pic12f1840 的引脚
Reading pins of a pic12f1840
我正在使用 pic12f1840 宽度的 pickit3 和 mplab x ide(和 x8 c 编译器)。这可能真的很容易,但我不知道如何读取引脚的值!
void main(void) {
//setting up TESA
TRISA = 0b111111;
TRISA5 = 0; //pin 5 is output
TRISA1 = 1; //pin 1 in input
for (;;) {
RA5 = RA1;
}
}
这是我目前的代码(我省略了配置和包含)。我有一个 LED 连接到引脚 5,一个按钮(带有下拉电阻)连接到引脚 1。我 运行 整个电压为 3.3 伏。
在处理像 PIC 这样的 Microchip 控制器时,post 构建一个完整的代码是一个真正的好主意。根据我的经验,真正的问题几乎总是在原始发帖人遗漏的内容中。
此代码在模拟器中适用于我:
/*
* 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 引脚。
- 简单的语法错误。
我的示例代码使用的语法通常已被几乎所有版本的 XC8 编译器支持大约 10 年了。
您使用的较短形式可能并不总是适用于您可以定位的每个控制器。
我正在使用 pic12f1840 宽度的 pickit3 和 mplab x ide(和 x8 c 编译器)。这可能真的很容易,但我不知道如何读取引脚的值!
void main(void) {
//setting up TESA
TRISA = 0b111111;
TRISA5 = 0; //pin 5 is output
TRISA1 = 1; //pin 1 in input
for (;;) {
RA5 = RA1;
}
}
这是我目前的代码(我省略了配置和包含)。我有一个 LED 连接到引脚 5,一个按钮(带有下拉电阻)连接到引脚 1。我 运行 整个电压为 3.3 伏。
在处理像 PIC 这样的 Microchip 控制器时,post 构建一个完整的代码是一个真正的好主意。根据我的经验,真正的问题几乎总是在原始发帖人遗漏的内容中。
此代码在模拟器中适用于我:
/*
* 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 引脚。
- 简单的语法错误。
我的示例代码使用的语法通常已被几乎所有版本的 XC8 编译器支持大约 10 年了。
您使用的较短形式可能并不总是适用于您可以定位的每个控制器。