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

📄 f_10_13_sfifoslp.c

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

// In Hyperterminal, use File->Properties->Settings->AscII setup
// and ensure that line delay and character delay are 0 milliseconds


#include "config.h"

#include "math.h"  // for sqrt function

#include "serial.c"
char getch (void);
void putch (char c);
void pcrlf (void);
char getche (void);

// Uses INT0 to wake up PIC for received character
// received data is buffered through an a software FIFO

#define BUFMAX 32


void pcrlf (void)
{
  putch(0x0a);
  putch(0x0d);
}

// head points to last character received 
volatile unsigned char ibuf[BUFMAX];
volatile unsigned char head, tail;

// return 8 bit char from Recieve port 
// interrupt driven version 
char getch (void)
{
  unsigned char c;
  // now wait for character 
  // WDT will cause timeout
  // if no characters arrive
  while (head == tail); 
  tail = tail + 1;
  if (tail == BUFMAX) tail = 0;
  c = ibuf[tail];
  return(c);
}

void putch (char c)
{
  // wait until transmit reg empty 
  CLRWDT();  
  while (!TXIF) {
    CLRWDT();  
  }
  TXREG = c;
}

// needed by scanf library call, 
// get character and echo
char getche (void)
{
  char c;
  c = getch();
  putch(c);
  return(c);
}

#if defined(__18CXX)
void my_gets (char *s);

//return a string from the console
void my_gets (char *s){
 char c;
 
 do {
  c = getche();
  *s = c;
  s++;
  } while (c != '\r');
}
#endif


volatile char ignore_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) { 
    // this has successfully woken up the PIC
    // disable, and clear flag
    INT0IF = 0;
    INT0IE = 0;
  }
  // see if this interrupt was generated by a receive character 
  if (RCIF) {  // check RCIF bit 
    CLRWDT();  
    if (!ignore_flag) {
      head = head + 1;
      if (head == BUFMAX) head = 0;
      // reading this register clears interrupt bit 
      ibuf[head] = RCREG;  
    } else ignore_flag = 0;
  }
}

unsigned int ivalue, root;
float temp_fp, root_fp;

void main(void)
{
  // set RB0 for input, falling edge interrupt
  TRISB0 = 1;
  INTEDG0 = 0;
  // 19200 in HSPLL mode, crystal = 7.3728 MHz 
  serial_init(95,1); 
  // enable interrupts
  IPEN = 0;  
  RCIE = 1;   
  PEIE = 1;  
  GIE = 1;  
  if (TO ==0 ) {
    //WDT timer went off waiting for input. 
    // Go to sleep
    // enable RB0 interrupt before falling asleep
    printf("Sleeping...");pcrlf();
    SWDTEN = 0;     // disable watchdog timer
    INT0IF = 0;    
    INT0IE = 1;    // enable RB0 interrupt
    CREN = 0;     // disable receive
    SLEEP();
    CREN = 1;     // enable receive
    ignore_flag = 1; // ignore this character
    SWDTEN = 1;     // enable watchdog timer
    printf("Awake!");pcrlf();
  } else {
    // any other reset, print start message
    SWDTEN = 1;     // enable watchdog timer
    pcrlf();
    printf("Software RX FIFO buff + WDT on input.");  pcrlf();
    printf("Hit any key to start...");  pcrlf();
    getch();
  } 
  while(1) {
	// read an ASCII string in, convert to decimal
#if defined(HI_TECH_C)
    scanf("%d",&ivalue); 
#endif
#if defined(__18CXX)
    {// MCC18 does not have scanf, just get string from console, use atoi
	  char buf[30];
	  my_gets(buf);  // get a string from console
	  ivalue = atoi(buf);  // convert string to integer
	}
#endif  
    // convert integer to floating point value
    temp_fp = ivalue; 
    // use library routine to compute floating point square root
    root_fp = sqrt(temp_fp); 
    root = (unsigned int)root_fp;  // integer square root
    printf("Square root of %d is: %d",ivalue,root); pcrlf();
  }
}


//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 + -