📄 f_10_12_txfifo.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"
/*
'getch' uses interrupt driven input to read the values from
the serial port.
'putch' also uses interrupt driven IO
This program reads decimal number, computers the
floating point square root, and prints it out.
Will be used to illustrate buffering in interrupt driven
input
*/
char getch (void);
void putch (char c);
void pcrlf (void);
char getche (void);
#define BUFMAX 32
volatile unsigned char txbuf[BUFMAX];
volatile unsigned char txhead, txtail;
void putch (char c)
{
char tmp;
// must use tmp because we do not want ISR thinking
// that buffer is empty
tmp = txhead;
tmp++;
if (tmp == BUFMAX) tmp = 0;
while(tmp == txtail); // wait until buffer empties
txbuf[tmp] = c;
txhead = tmp;
// enable interrupt
TXIE = 1;
}
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;
while (head == tail) {};
tail = tail + 1;
if (tail == BUFMAX) tail = 0;
c = ibuf[tail];
return(c);
}
// needed by scanf library call,
// get character and echo
char getche (void)
{
unsigned 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
#if defined(HI_TECH_C)
void interrupt pic_isr(void)
#endif
#if defined(__18CXX)
#pragma interrupt pic_isr
void pic_isr(void)
#endif
{
// see if this interrupt was generated by a receive character
if (RCIF) { // check RCIF bit
head = head + 1;
if (head == BUFMAX) head = 0;
// reading this register clears interrupt bit
ibuf[head] = RCREG;
}
if (TXIF) { //check TXIF bit
if (txtail == txhead) { // buffer is empty, disable
TXIE = 0;
} else {
// get character
txtail = txtail + 1;
if (txtail == BUFMAX) txtail = 0;
TXREG = txbuf[txtail];
}
}
}
unsigned ivalue,root;
double temp_fp, root_fp;
void main(void)
{
// 19200 in HSPLL mode, crystal = 7.3728 MHz
serial_init(95,1);
// enable received character interrupt
IPEN = 0; RCIE = 1; PEIE = 1; GIE = 1;
printf("Has software tx/rx fifo buffer."); 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 + -