菜鸟PIC16按键LED不亮,求解

2020-02-06 09:57发布

本帖最后由 wang479871 于 2014-3-1 19:42 编辑

//我想通过PIC16F883单片机的RA1管脚按键输入,RB6管脚接LED做指示灯
//我已经用万用表测了RA1管脚信号电压不管是0V还是5V,RB6LED都不亮,如果PORTB=0xFF,则LED亮
//编译环境为 MPLAB IDEv8.7,编译器用HI-TECH C,下面这是单片机配置位
配置位.png (34.8 KB, 下载次数: 0) 下载附件 2014-3-1 19:40 上传
#include <pic.h>
#define uchar unsigned char
#define uint  unsigned int

void Port_Init()
{
        TRISA |= 0x02;        //RA1管脚配置输入
        TRISB &= ~0x40;        //RB6管脚配置输出
        INTCON = 0x00;   //关闭所有中断
}


void main()
{
        Port_Init();
        while(1)
        {
                if(PORTA & 0x02)
                        PORTB |= 0x40;
                else
                        PORTB &= ~0x40;        
        }
}

//请教LED不亮的原因是程序不对,还是配置位不对,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
14条回答
石进良
1楼-- · 2020-02-07 15:02
// PIC18F25K80 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = DIG    // SOSC Power Selection and mode Configuration bits (Digital (SCLKI) mode)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = EC2     // Oscillator (Internal RC oscillator, CLKOUT function on OSC2)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = SWDTDIS  // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define INT8U  unsigned char
#define _XTAL_FREQ 1000000UL

#define uchar unsigned char
#define uint  unsigned int

void Port_Init()
{
        TRISA |= 0x02;        //RA1管脚配置输入
        TRISB &= ~0x40;        //RB6管脚配置输出
        INTCON = 0x00;   //关闭所有中断
}


void main()
{
        Port_Init();
        while(1)
        {
                if(PORTA & 0x02)
                        PORTB |= 0x40;
                else
                        PORTB &= ~0x40;         
        }
}

//请教LED不亮的原因是程序不对,还是配置位不对,
K7toubleaz
2楼-- · 2020-02-07 18:59
關類比功能、設定輸入或輸出,做按鍵輸入的腳位要上拉,輸出電晶體的要串電阻,輸出LED的串電阻限流,我PIC大概IO口這樣設定就能工作了,給您參考。

一周热门 更多>