单片机 > 单片机程序设计 > 详情

PIC18F14k50 中断

发布时间:2023-08-14 发布时间:
|

//mplab v1.7 mcc18 串口接收中断示例


#include


#include

#define u8 unsigned char

#define u16 unsigned int



#pragma config CPUDIV = NOCLKDIV

#pragma config USBDIV = OFF

#pragma config FOSC = HS

#pragma config PLLEN = ON

#pragma config FCMEN = OFF

#pragma config IESO = OFF

#pragma config PWRTEN = OFF

#pragma config BOREN = OFF

#pragma config BORV = 30

#pragma config WDTEN = OFF

#pragma config WDTPS = 32768

#pragma config MCLRE = OFF

#pragma config HFOFST = OFF

#pragma config STVREN = ON

#pragma config LVP = OFF

#pragma config XINST = OFF

#pragma config BBSIZ = OFF

#pragma config CP0 = OFF

#pragma config CP1 = OFF

#pragma config CPB = OFF

#pragma config WRT0 = OFF

#pragma config WRT1 = OFF

#pragma config WRTB = OFF

#pragma config WRTC = OFF

#pragma config EBTR0 = OFF

#pragma config EBTR1 = OFF

#pragma config EBTRB = OFF



#define Firmware_led LATBbits.LATB4

#define InitFirmwareLed() LATB |= 1<<4; TRISB &= ~(1<<4)

#define Fir_Led_on() Firmware_led = 0

#define Fir_Led_Off() Firmware_led = 1



#define InitSwitch() ANSEL = 0;

ANSELH = 0;

LATC |= 0xF9;

TRISC |= 0xF9;

#define SW_MENU PORTCbits.RC0

#define SW_VGA2 PORTCbits.RC4



#define DelayMs(x)

{

unsigned long _dcnt;

unsigned long _ms;

_ms = x;

while (_ms)

{

_dcnt=((unsigned long)(0.001/(1.0/2000000)/6));

while(_dcnt--);

_ms--;

}

}



void UART_Init(void)

{

//Fir_Led_on();

TRISBbits.TRISB5 = 1;

ANSELHbits.ANS11 = 0;



TXSTAbits.TX9 = 0;

TXSTAbits.TXEN = 1;

TXSTAbits.BRGH = 1;



RCSTAbits.RX9 = 0;

RCSTAbits.CREN =1;

TXSTAbits.SYNC = 0;

RCSTAbits.SPEN = 1;



BAUDCONbits.BRG16 = 0;

SPBRG = 25;



RCONbits.IPEN = 1;

PIE1bits.RCIE = 1;

INTCONbits.PEIE = 1;

INTCONbits.GIE = 1;

Fir_Led_on();

}



//void InterruptHandlerLow(void);

//#pragma code InterruptVectorLow = 0x18

//void InterruptVectorLow(void)

//{

// _asm

// goto InterruptHandlerLow

// _endasm

//}

//#pragma code

//

//#pragma interruptlow InterruptHandlerLow

//void InterruptHandlerLow(void)

//{

// unsigned char data;

// if(PIR1bits.RCIF)

// {

// PIR1bits.RCIF = 0;

// data = RCREG;

// Fir_Led_on();

// }

//}



void InterruptHandlerHigh(void);

#pragma code InterruptVectorHigh = 0x08

void InterruptVectorHigh(void)

{

_asm

goto InterruptHandlerHigh

_endasm

}

#pragma code



#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh(void)

{

unsigned char data;

if(PIR1bits.RCIF)

{

PIR1bits.RCIF = 0;

data = RCREG;

Fir_Led_on();

}

}



void main(void)

{

unsigned char data;

InitSwitch();

InitFirmwareLed();

UART_Init();

while(1)

{

if(!SW_MENU)

{

Fir_Led_Off();

}

else if(!SW_VGA2)

{

Fir_Led_on();

}

// else if(PIR1bits.RCIF)

// {

// //PIR1bits.RCIF = 0;

// data = RCREG;

// Fir_Led_on();

// }

}

}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
MSP430低功耗模式-while循环失效