|
用PIC做scientific calculator
[复制链接]
|
|
发表于 1-9-2008 11:33 AM
|
显示全部楼层
原帖由 extreme2550 于 31-8-2008 05:46 PM 发表
哦。。。但是要怎样才能知道要加到几个小数点?
我的意思是code里要怎样写?
float x;
x= 22 / 7 ; //(3.1428571)
printf ("%f",x);
但是如果像cos 60,答案因该是0.5,可是我算出来的是0.50000014 。。。为什么???
我不会, 很少用float,没有什么心得, 可能是mantisa 转换造成?
[ 本帖最后由 pic 于 2-9-2008 08:10 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 1-9-2008 07:15 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 2-9-2008 08:23 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 2-9-2008 06:56 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 2-9-2008 07:15 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 2-9-2008 09:33 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 2-9-2008 11:02 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 3-9-2008 09:31 AM
|
显示全部楼层
原帖由 extreme2550 于 2-9-2008 11:02 PM 发表
我没完整的抄啊。。。毕竟他只是简单的calculator,我有改掉很多地方和加进一些extra的功能,如trigo, log, ln, square,square root, x raise to the power of y etc(也多加了一个4X4 keypad )。想问,cube root 要怎样写function???
mikroC 没有printf,但有FloatToStr,所以能够把得到的result直接convert去string,然后在LCD显示。过后我发现,其实PIC18本身算出来的答案就不是很准。。。可能是你所说的mantisa问题 。至于要怎样解决,暂时还没有头绪,有没有人懂???
其实不是PIC18本身算出来的答案不准, 而是一些小数点被拿掉, 还有就是pi 。
pi 3.14
pi 3.14159265
pi 3.1415926535897932384626433832795
都会影响结果。
float 是32bit, 他不会把所有的小数点也包含, 一定会有truncated 。
你如很执作要非常精确, 可能要考虑dsPIC, 写自己的float 128bit 。
COS(60)= 0.50
MCU 算出0.5000004 已经很不简单了。。。7位数,还想怎样啊?
下面是CCS C算出来的Sin(60) 和cos(60)
=====================================
Cos(60)= 0.50 [Float with truncated decimal]
Cos(60)= 0.50 [Float with rounded decimal]
Cos(60)= 5.000004E-01 [Float in exponential format]
==================================
Sin(60)= 0.86 [Float with truncated decimal]
Sin(60)= 0.87 [Float with rounded decimal]
Sin(60)= 8.660254E-01 [Float in exponential format]
==========================================
CCS C 源码
如果要显示在LCD, 只要加lcd_putc
printf(lcd_putc, "\n\rCos(60)= %f",result);
- // Program by: pIc@cAri
- // Date: 3 Sep 2008
- #include <18F4520.h>
- #fuses HS, NoPROTECT,WDT,put,brownout,nolvp
- #use delay(clock=20000000)
- #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
- #include <math.h>
- #define pi 3.14159265
- float result;
- float rad;
- float deg;
- void main()
- {
- // %f Float with truncated decimal
- // %g Float with rounded decimal
- // %e Float in exponential format
- deg=60;
- rad=(deg*pi)/180;
- result= cos(rad);
- printf("Cos(60)= %f",result);
- printf("\n\rCos(60)= %f [Float with truncated decimal]",result);
- printf("\n\rCos(60)= %g [Float with rounded decimal]",result);
- printf("\n\rCos(60)= %e [Float in exponential format]",result);
- printf("\n\r==================================");
- result= Sin(rad);
- printf("\n\rSin(60)= %f [Float with truncated decimal]",result);
- printf("\n\rSin(60)= %g [Float with rounded decimal]",result);
- printf("\n\rSin(60)= %e [Float in exponential format]",result);
- }
复制代码
Proteus 的结果:
|
|
|
|
|
|
|
|
楼主 |
发表于 14-9-2008 10:54 AM
|
显示全部楼层
想问,如果把原本我现在用的4mhz crystal换去20Mhz 或 40Mhz的,会有什么影响?会算得比较快对吗?18F452 是不是最高只支持到20Mhz? |
|
|
|
|
|
|
|
发表于 14-9-2008 02:41 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 14-9-2008 03:00 PM
|
显示全部楼层
原帖由 pic 于 14-9-2008 02:41 PM 发表
18FXXXX 都可以去到40Mhz。
你可以用10MHZ 的crystal, 然后set fuse HS+ PLL , 10Mhz x4, 变成40Mhz。
frequency越高,是不是代表越快(处理command & calculation)? |
|
|
|
|
|
|
|
发表于 14-9-2008 09:37 PM
|
显示全部楼层
原帖由 extreme2550 于 14-9-2008 03:00 PM 发表
frequency越高,是不是代表越快(处理command & calculation)?
对, 40Mhz 可以有10MIPS 的速度。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|