📄 f_10_6_root.c
字号:
#include "delay.h"
// 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"
/*
This program reads decimal number, computes the
floating point square root, and prints it out.
*/
char getch (void);
void putch (char c);
void pcrlf (void);
char getche (void);
void pcrlf (void)
{
putch(0x0a); putch(0x0d);
}
void putch (char c)
{
/* wait until transmit reg empty */
while (!TXIF);
TXREG = c;
}
// use -lf option when compiling this program
char getch (void){
// check RCIF bit
while (!RCIF);
return(RCREG);
}
// 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
unsigned int ivalue, root;
float temp_fp, root_fp;
void main(void){
// 19200 in HSPLL mode, crystal = 7.3728 MHz
serial_init(95,1);
printf("No buffering."); pcrlf();
printf("Hit any key to start..."); pcrlf();
getch();
while(1) {
#if defined(HI_TECH_C)
// read integer for input using scanf
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);
//convert to nearest integer as demo compiler does not support
// floats in printf() statements.
root = (unsigned int)root_fp;
printf("Square root of %d is: %d ",ivalue,root); pcrlf();
DelayMs(5); // tuneable delay for USART overrun
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -