【发布时间】:2021-03-02 19:26:17
【问题描述】:
我现在尝试了几个小时在按下按钮时点亮一个简单的 LED,但没有运气。由于某种原因,引脚似乎随机变低和变高。
我正在使用 MPLAB X IDE v5.54 和 PIC12F1822。 谁能发现我的错误?
代码如下:
// CONFIG1
#pragma config FOSC = ECH // Oscillator Selection (ECH, External Clock, High Power Mode (4-32 MHz): device clock supplied to CLKIN pin)
#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)
#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 = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#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)
#include <xc.h>
#define _XTAL_FREQ 8000000 // SET frequency as 8000000 bit because of the 8 MHz clock
int main(int argc, char** argv) {
OSCCON = 0xF0; // Internal oscillator 8MHz and PLL enabled
ADCON0 = 0; // Disable ADC module
ANSELAbits.ANSA2 = 0; // SET LED PIN to Digital
ANSELAbits.ANSA4 = 0; // SET Button pin to Digital
TRISAbits.TRISA2 = 0; // LED bit is output
TRISAbits.TRISA4 = 1; // Button bit is input
PORTAbits.RA4 = 0; // Button bit low initial
PORTAbits.RA2 = 0; // LED bit low initial
while (1) {
while (PORTAbits.RA4 == 0); // Wait until button is pressed
__delay_ms(100); // wait 100ms
PORTAbits.RA2 = 1; // Light up the LED
__delay_ms(100); // wait 100ms
while (PORTAbits.RA4 == 1); // Wait until button release
__delay_ms(100); // wait 100ms
PORTAbits.RA2 = 0; // Turn off the LED
__delay_ms(100); // wait 100ms
}
return (EXIT_SUCCESS);
}
我尝试了很多东西,例如:
- 天知道调整了多少次配置
- 将所有引脚一一对应为输入、输出、数字、模拟等。
我想不出别的了。
任何帮助将不胜感激。
编辑 1
这是我拥有的最新代码。这个随机打开和关闭 LED。我还禁用了看门狗定时器:
// CONFIG1
#pragma config FOSC = ECH // Oscillator Selection (ECH, External Clock, High Power Mode (4-32 MHz): device clock supplied to 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 = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#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)
#include <xc.h> // include standard header file
// Definitions
#define _XTAL_FREQ 16000000 // this is used by the __delay_ms(xx) and __delay_us(xx) functions
void main() {
// set up oscillator control register
OSCCONbits.SPLLEN = 0; // PLL is disabled
OSCCONbits.IRCF = 0x0F; //set OSCCON IRCF bits to select OSC frequency=16Mhz
OSCCONbits.SCS = 0x02; //set the SCS bits to select internal oscillator block
ANSELAbits.ANSA0 = 0; // Set to digital
ANSELAbits.ANSA1 = 0; // Set to digital
ANSELAbits.ANSA2 = 0; // Set to digital
ANSELAbits.ANSA4 = 0; // Set to digital
// Set up I/O pins
// PORT A Assignments
TRISAbits.TRISA0 = 0; // RA0 = Digital Voltage out
TRISAbits.TRISA1 = 0; // RA1 = Digital Voltage out
TRISAbits.TRISA2 = 0; // RA2 = Digital Voltage out
TRISAbits.TRISA3 = 0; // RA3 = Digital Voltage out
TRISAbits.TRISA4 = 1; // RA4 = Digital Voltage in
TRISAbits.TRISA5 = 0; // RA0 = Digital Voltage out
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 0;
PORTAbits.RA2 = 0;
PORTAbits.RA3 = 0;
PORTAbits.RA4 = 0;
PORTAbits.RA5 = 0;
// Set up ADC
ADCON0 = 0; // ADC is off
for (;;) {
__delay_ms(100); // wait 100ms
if (PORTAbits.RA4 == 0) {
LATAbits.LATA2 = 0;
} else {
LATAbits.LATA2 = 1;
}
}
}
好的,这就是我观察到的。当按钮被按下时,LED 永远不会关闭,但当按钮未被按下时,它会随机打开或关闭。
【问题讨论】:
-
首先,删除按钮代码并实现一个闪烁代码,例如 500 ms on & 500 ms off。这始终是第一步。确认它正常工作后,您可以寻找其他问题。我怀疑浮动输入。您的按钮输入需要下拉或上拉电阻。
-
当然还要关掉看门狗……
-
你们提供了下拉电阻吗?您的第一个代码告诉我们按钮连接到 VDD。
-
您也可以启用内部上拉电阻。见
OPTION_REG -
嗯,@Tagli 是第一个有这个提示的人......
标签: microcontroller pic microchip