由于Mike 已在 11 年前编辑了这个原始问题,因此原始海报是否需要一个全面的答案似乎值得怀疑。事实上,在过去的 9 年里,OP 似乎只询问或回应了 2 个有关微控制器的话题。
考虑到这一切,看看 PIC18F 控制器可以通过哪些方式使用 Hi-Tech C 或 XC8 编译的代码从复位向量开始执行可能会有所帮助,因为它现在由 Microchip 调用。
此代码已使用 MPLABX v5.25、XC8 v2.05 和 PIC18F45K20 控制器进行测试。
/*
* File: main.c
* Author: dan1138
* Target: PIC18F45K20
* Compiler: XC8 v2.05
*
* PIC18F46K20
* +---------+ +---------+ +----------+ +----------+
* <> 1 : RC7/RX : -- 12 : NC : <> 23 : RA4 : -- 34 : NC :
* LED4 <> 2 : RD4 : -- 13 : NC : <> 24 : RA5 : 32.768KHz -> 35 : RC1/SOSI :
* LED5 <> 3 : RD5 : <> 14 : RB4 : <> 25 : RE0 : <> 36 : RC2 :
* LED6 <> 4 : RD6 : <> 15 : RB5/PGM : <> 26 : RE1 : <> 37 : RC3 :
* GND -> 5 : VSS : PGC <> 16 : RB6/PGC : <> 27 : RE2 : LED0 <> 38 : RD0 :
* 3v3 -> 6 : VDD : PGD <> 17 : RB7/PGD : 3v3 -> 28 : VDD : LED1 <> 39 : RD1 :
* SW1 <> 7 : RB0/INT : VPP -> 18 : RE3/VPP : GND -> 29 : VSS : LED2 <> 40 : RD2 :
* <> 8 : RB1 : POT <> 19 : RA0/AN0 : 4MHz -> 30 : RA7/OSC1 : LED3 <> 41 : RD3 :
* <> 9 : RB2 : <> 20 : RA1 : 4MHz <- 31 : RA6/OSC2 : <> 42 : RC4 :
* <> 10 : RB3 : <> 21 : RA2 : 32.767KHz <- 32 : RC0/SOSO : <> 43 : RC5 :
* LED7 <> 11 : RD7 : <> 22 : RA3 : -- 33 : NC : <> 44 : RC6/TX :
* +---------+ +---------+ +----------+ +----------+
* TQFP-44
*
*
* Created on December 21, 2019, 2:26 PM
*/
/* Target specific configuration words */
#pragma config FOSC = INTIO67, FCMEN = OFF
#pragma config IESO = OFF, PWRT = OFF, BOREN = SBORDIS, BORV = 18
#pragma config WDTEN = OFF, WDTPS = 32768, CCP2MX = PORTC, PBADEN = OFF
#pragma config LPT1OSC = ON, HFOFST = ON
#pragma config MCLRE = ON, STVREN = ON, LVP = OFF, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
/* Target specific definitions for special function registers */
#include <xc.h>
/* Declare the system oscillator frequency setup by the code */
#define _XTAL_FREQ (4000000UL)
/* reset instruction */
void ResetMethod_1(void)
{
asm(" reset");
}
/* long jump to absolute address zero */
void ResetMethod_2(void)
{
INTCON = 0;
asm(" pop\n ljmp 0");
}
/* return to absolute address zero */
void ResetMethod_3(void)
{
INTCON = 0;
asm(" clrf TOSU\n clrf TOSH\n clrf TOSL\n");
}
/* provoke stackoverflow reset */
void ResetMethod_4(void)
{
INTCON = 0;
while (1)
{
asm(" push\n");
}
}
/* provoke stackunderflow reset */
void ResetMethod_5(void)
{
INTCON = 0;
STKPTR = 0;
}
/* clear the program counter */
void ResetMethod_6(void)
{
INTCON = 0;
asm(" clrf PCLATU\n clrf PCLATH\n clrf PCL\n");
}
void main(void)
{
INTCON = 0; /* Disable all interrupt sources */
PIE1 = 0;
PIE2 = 0;
INTCON3bits.INT1IE = 0;
INTCON3bits.INT2IE = 0;
OSCCON = 0x50; /* set internal oscillator to 4MHz */
OSCTUNEbits.TUN = 0; /* use factory calibration of internal oscillator */
ANSEL = 0;
ANSELH = 0;
if(!RCONbits.nPOR)
{
RCONbits.nPOR = 1;
LATD = 0;
}
TRISD = 0;
/*
* Application loop
*/
while(1)
{
__delay_ms(500);
if (LATDbits.LD0 == 0)
{
LATDbits.LD0 = 1;
ResetMethod_1();
}
if (LATDbits.LD1 == 0)
{
LATDbits.LD1 = 1;
ResetMethod_2();
}
if (LATDbits.LD2 == 0)
{
LATDbits.LD2 = 1;
ResetMethod_3();
}
if (LATDbits.LD3 == 0)
{
LATDbits.LD3 = 1;
ResetMethod_4();
}
if (LATDbits.LD4 == 0)
{
LATDbits.LD4 = 1;
ResetMethod_5();
}
if (LATDbits.LD5 == 0)
{
LATDbits.LD5 = 1;
ResetMethod_6();
}
}
}