⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f_10_15_intfsm.c

📁 * Use 10 MHz crystal frequency. * Use Timer0 for ten millisecond looptime. * Blink "Alive" LED e
💻 C
字号:

#include "config.h"
#include "delay.h"
#include "serial.c"
#include "serio.c"

#define START_STATE 0
#define BLINK_STATE 1
#define ON_STATE 2

void print_debug(void);

volatile unsigned char state, led_blink;
volatile unsigned char led_on, int_flag;

#if defined(HI_TECH_C)
void interrupt pic_isr(void)
#endif
#if defined(__18CXX)
#pragma interrupt pic_isr
void pic_isr(void)
#endif
{
 if (INT0IF) {
   ISR_DelayMs(30); //debounce
  INT0IF = 0; int_flag = 1;
  switch(state) {
   case START_STATE:
    state = BLINK_STATE; led_blink = 1;
    break;
   case  BLINK_STATE:
    if (!RB7) {
     led_blink = 0; led_on = 1;
     state = ON_STATE;
    }
    break;
   case ON_STATE:
    led_on = 0; state = START_STATE;
    break;
  }
 }
}

void print_debug(void){
     printf("State: %d, Led_on: %d, led_blink: %d", state,led_on,led_blink); pcrlf();
}

unsigned char last_state;
void main(void){
  // 19200 in HSPLL mode, crystal = 7.3728 MHz 
  serial_init(95,1);
  RBPU = 0; // weak pullups enabled
  // set RB0 for input, rising edge interrupt initially
  TRISB = 0xEF;  //RB4 is output, others inputs
  INTEDG0 = 0; // falling edge
  RB4 = 0; // turn LED off
  printf("INT0 FSM started (with debug).");pcrlf();
  state = START_STATE; led_blink = 0; led_on = 0;
  // enable interrupts
  IPEN = 0; INT0IF = 0; INT0IE = 1; PEIE = 1; GIE = 1;  
  print_debug();
  while(1) {
    if (int_flag) {
        print_debug(); int_flag = 0;
    }
    if (led_blink) {
      //LED toggle, delay
      if (LATB4) RB4 = 0; else RB4 = 1;
      DelayMs(250); DelayMs(250); 
    } 
    else if (led_on) RB4 = 1;
    else RB4  = 0;    
  }
}

//for MCC18, place the interrupt vector goto
#if defined(__18CXX)
#if defined(HIGH_INTERRUPT)
#pragma code HighVector=HIGH_INTERRUPT
#else
#pragma code HighVector=0x0008  
#endif
void HighVector (void)
{
    _asm goto pic_isr _endasm
}
#pragma code
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -