打印
[PIC®/AVR®/dsPIC®产品]

在使用PIC16F18326时程序不能正常运行的问题

[复制链接]
8118|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cr315|  楼主 | 2025-2-19 16:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
想在PIC16F18326中写一个简单的IO控制测试程序,编译和烧写都正常,但是用万能表检测IO电平时无论怎么写代码都不能出正常的效果
同样的代码在PIC16f1829中正常运行(除了一些配置位不同)

环境为:MPLAB X IDE V3.55 + XC81  V1.37


附源码:

#include
#include

// PIC16F18326 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = XT     // FEXTOSC External Oscillator mode Selection bits (XT (crystal oscillator) from 100 kHz to 4 MHz)
#pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = ON        // Watchdog Timer Enable bits (WDT enabled, SWDTEN is ignored)
#pragma config LPBOREN = OFF    // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored)
#pragma config BORV = LOW       // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = ON     // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
#pragma config DEBUG = OFF      // Debugger enable bit (Background debugger disabled)

// CONFIG3
#pragma config WRT = OFF        // User NVM self-write protection bits (Write protection off)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.)

// CONFIG4
#pragma config CP = OFF         // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF        // Data NVM Memory Code Protection bit (Data NVM code protection disabled)

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

#include
/*
*
*/
int main(int argc, char** argv) {

    TRISCbits.TRISC2 = 0;
    LATC2 = 1;

    while(1){
    }

    return (EXIT_SUCCESS);
}


PS:其他配置位也尝试过了
// CONFIG1
#pragma config FEXTOSC = HS     // FEXTOSC External Oscillator mode Selection bits (HS (crystal oscillator) above 4 MHz)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config MCLRE = OFF      // Master Clear Enable bit (MCLR/VPP pin function is digital input; MCLR internally disabled; Weak pull-up under control of port pin's WPU control bit.)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config WDTE = OFF       // Watchdog Timer Enable bits (WDT disabled; SWDTEN is ignored)
#pragma config LPBOREN = OFF    // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored)
#pragma config BORV = LOW       // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = ON     // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
#pragma config DEBUG = ON      // Debugger enable bit (Background debugger disabled)

// CONFIG3
#pragma config WRT = OFF        // User NVM self-write protection bits (Write protection off)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (High Voltage on MCLR/VPP must be used for programming.)

// CONFIG4
#pragma config CP = OFF          // User NVM Program Memory Code Protection bit (User NVM code protection enabled)
#pragma config CPD = OFF         // Data NVM Memory Code Protection bit (Data NVM code protection enabled)

使用特权

评论回复
沙发
锵才才| | 2025-2-20 11:05 | 只看该作者
RC2脚接了什么设备,有没有和地短路?

使用特权

评论回复
板凳
发送到工地施工| | 2025-2-22 23:52 | 只看该作者
这可能是由于 芯片配置位、引脚映射 或 硬件连接 的差异导致的。

使用特权

评论回复
地板
gra22ce| | 2025-3-11 17:10 | 只看该作者
PIC16F18326 和 PIC16F1829 的配置位可能有所不同,尤其是时钟和引脚功能配置。

使用特权

评论回复
5
清芯芯清| | 2025-3-11 18:15 | 只看该作者
检查并确保配置位正确设置,特别是以下部分:时钟源配置:确保 FEXTOSC 和 RSTOSC 与实际硬件匹配。MCLRE 配置:如果使用 MCLR 引脚作为复位引脚,确保 MCLRE = ON。LVP 配置:如果使用低压编程,确保 LVP = ON。

使用特权

评论回复
6
pe66ak| | 2025-3-11 19:21 | 只看该作者
PIC16F18326 的引脚功能可能与 PIC16F1829 不同,尤其是 PPS(外设引脚选择) 功能。

使用特权

评论回复
7
ewyu| | 2025-3-11 20:32 | 只看该作者
建议你查查并配置 PPS 寄存器,确保 IO 引脚功能正确。

使用特权

评论回复
8
miltk| | 2025-3-11 22:40 | 只看该作者
如果使用 PPS,确保在代码中正确初始化 PPS

使用特权

评论回复
9
wamed| | 2025-3-12 07:55 | 只看该作者
时钟配置错误可能导致 MCU 无法正常运行,进而影响 IO 控制。

使用特权

评论回复
10
yuliangren| | 2025-3-12 09:18 | 只看该作者
如果使用外部晶振,检查晶振和负载电容是否正确连接。如果使用内部振荡器,确保 RSTOSC 配置正确

使用特权

评论回复
11
tiakon| | 2025-3-12 11:56 | 只看该作者
硬件连接不良可能导致 IO 引脚无法正常工作

使用特权

评论回复
12
gongqijuns| | 2025-3-12 13:08 | 只看该作者
使用万用表测量 RC2 引脚的电压,确认电平是否正确。

使用特权

评论回复
13
nqty| | 2025-3-12 14:15 | 只看该作者
检查电源和地线连接,确保 MCU 供电正常。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1376

主题

4321

帖子

0

粉丝