我正在使用带有mplabv8.70的pic16F690,一个pickit 2和高科技的PICC PRO c编译器。
我的问题是这个中断是否能够使用其中的getch
函数,因为getch
函数使用RCIF标志,该标志也是用于触发EUSART中断的标志。
她是我的代码,很遗憾很长,但是这里的人似乎不喜欢放置片段/部分而不是整个事情。它复用了一个4位7段LED显示屏并获得了RPM值的EUSART信号,但是我希望它能够继续显示,即使它没有接收到EUSART,然后有效地更新'因此它确实是中断和全局变量。如果有一个明显更好的方式,那么我很高兴被告知我的方式是错误的。
//#include _LEGACY_HEADERS //Added for compiler versions 9.81+
#include <stdio.h> //For the printf function
#include <htc.h> //Compiler header file
//#include "usart.h" //USART header file
//#include "pause.h"
//#include "SerialIn.h"
//#include "Display.h"
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS); //Configure the PIC with generic set up
#define BAUD 2400
#define FOSC 4000000L
#define baudsetting ((int)(FOSC/(64UL * BAUD) -1))
#define RX_PIN TRISB5
#define TX_PIN TRISB7
#define dig0 0b01111110
#define dig1 0b00110000
#define dig2 0b01101101
#define dig3 0b01111001
#define dig4 0b00110011
#define dig5 0b01011011
#define dig6 0b01011111
#define dig7 0b01110000
#define dig8 0b01111111
#define dig9 0b01111011
unsigned char byte1;
unsigned char byte2;
unsigned char byte3;
unsigned char byte4;
unsigned char byte5;
unsigned char byte6;
unsigned char byte7;
unsigned char byte8;
unsigned char byte9;
unsigned RPMleft;
unsigned RPMright;
#define units 0b000001
#define tens 0b000010
#define hundreds 0b000100
#define thousands 0b001000
unsigned char RPM_unit;
unsigned char RPM_ten;
unsigned char RPM_hund;
unsigned char RPM_thou;
void interrupt isr(void);
void display_digit(unsigned char digit);
void multiplex(unsigned RPM);
void pause(unsigned usvalue);
unsigned char getch(void);
void init_comms(void);
void interrupt isr(void)
{
if (RCIF == 1)
{
byte2 = getch(); //Receive 8 bytes from the PICAXE
byte3 = getch();
byte4 = getch();
byte5 = getch();
byte6 = getch();
byte7 = getch();
byte8 = getch();
byte9 = getch();
byte2 = byte2 - 30; //Convert the ASCII to equivilent integer
byte3 = byte3 - 30;
byte4 = byte4 - 30;
byte5 = byte5 - 30;
byte6 = byte6 - 30;
byte7 = byte7 - 30;
byte8 = byte8 - 30;
byte9 = byte9 - 30;
////////Depending on which PIC is being used comment one of the following lines///////////////////////////////
RPMleft = byte2*1000 + byte3*100 + byte4*10 + byte5; //Save the RPM of the left prop shaft
// RPMright = byte6*1000 + byte7*100 + byte8*10 + byte9; //Save the RPM of the right prop shaft
}
}
void main(void)
{
/* General Setup */
PORTA = 0; //Clear PortA
PORTB = 0; //Clear PortB
PORTC = 0; //Clear PortC
TRISA = 0; //All PortA outputs
TRISB = 0xFF; //All PortB inputs
TRISC = 0; //All PortC outputs
CM1CON0 = 0; //Comparators off
CM2CON0 = 0;
ANSEL = 0; //A/D module off
INTCON = 0b11000000; //Enable interrupts GIE = 1 (global interrupts), PEIE = 1 (periphaeral interrupts)
PIE1 = 0b00100000; //Enable bit 5 RCIE = 1 (EUSART receive interrupt enable bit)
init_comms(); //Set up the USARTh
while(1==1) //Loop Forever
{
////////Depending on which PIC is being used comment one of the following lines////////////////////////////////////
multiplex(RPMleft);
// multiplex(RPMright);
}//End while
}//End main
unsigned char getch(void) {
/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}
void init_comms(void)
{
RX_PIN = 1;
TX_PIN = 0;
SPBRG = baudsetting;
//Continuous 8 bit asynchronous non inverted low speed communication
RCSTA = 0x90; // SPEN and CREN bit = 1, RX9EN = 0
TXSTA = 0x20;//TXEN = 1, BRGH, SYNC = 0
BAUDCTL = 0; //BRG16 = 0
}
void multiplex(unsigned RPM)
{
RPM_unit = RPM / 1000; //Split the Left RPM value into 4 digits
RPM_ten = (RPM / 100) % 10;
RPM_hund = (RPM / 10) % 10;
RPM_thou = RPM % 10;
//Start Multiplexing
PORTA = thousands;
display_digit(RPM_thou);
PORTA = hundreds;
display_digit(RPM_hund);
PORTA = tens;
display_digit(RPM_ten);
PORTA = units;
display_digit(RPM_unit);
}
void display_digit(unsigned char digit)
{
switch (digit)
{
case 0:
PORTC = dig0; //zero
break;
case 1:
PORTC = dig1; //one
break;
case 2:
PORTC = dig2; //two
break;
case 3:
PORTC = dig3; //three
break;
case 4:
PORTC = dig4; //four
break;
case 5:
PORTC = dig5; //five
break;
case 6:
PORTC = dig6; //six
break;
case 7:
PORTC = dig7; //seven
break;
case 8:
PORTC = dig8; //eight
break;
case 9:
PORTC = dig9; //nine
break;
}
}
答案 0 :(得分:5)
在PIC16架构中,中断不能自行中断。即使你提前清除了旗帜,ISR也会继续运行直到它返回。
如果需要嵌套中断,请切换到PIC18。
答案 1 :(得分:1)
我希望尽可能缩短中断服务程序,避免延迟或代码花费太长时间来处理它们。这样,程序就可以尽快处理新的中断。
如果在处理另一个中断时发生中断,则自中断标志置位后,微处理器将在退出ISR后再次重新进入ISR。
在您的代码中,我认为您可以在读取第一个字符后退出ISR,然后继续执行,直到下一次设置RCIF标志。因为您可以执行大量代码,直到通过USART发送另一个字节。 如果你创建一个像byte [9]这样的缓冲区和一个静态指针来记住你在ISR调用之间接收的字符,这应该可行。
以下是一个例子:
void interrupt isr(void)
{
static char pointer = 0;
if (RCIF == 1)
{
byte[pointer]=getch(); // stores the incoming data in byte[0]-byte[8]
pointer++;
if(pointer==9)
{
pointer=0; // Reset pointer
// Other operations or flags that signal the expected data was received
}
}
}
编辑:我还认为您需要清除软件中的RCIF标志,检查数据表以查看是否需要手动执行此操作,或者如果缓冲区为空则自动清除标记。