Реклама на сайте English version  DatasheetsDatasheets

KAZUS.RU - Электронный портал. Принципиальные схемы, Datasheets, Форум по электронике

Новости электроники Новости Литература, электронные книги Литература Документация, даташиты Документация Поиск даташитов (datasheets)Поиск PDF
  От производителей
Новости поставщиков
В мире электроники

  Сборник статей
Электронные книги
FAQ по электронике

  Datasheets
Поиск SMD
Он-лайн справочник

Принципиальные схемы Схемы Каталоги программ, сайтов Каталоги Общение, форум Общение Ваш аккаунтАккаунт
  Каталог схем
Избранные схемы
FAQ по электронике
  Программы
Каталог сайтов
Производители электроники
  Форумы по электронике
Помощь проекту

Микроконтроллеры, АЦП, память и т.д Темы касающиеся микроконтроллеров разных производителей, памяти, АЦП/ЦАП, периферийных модулей...

 
Опции темы
Непрочитано 10.06.2005, 10:26  
Raushan
Гость
 
Сообщений: n/a
По умолчанию Не могу понять про float в PicBasicPro

Познакомился с бейсиком для процов. Мне необходимо написать небольшую прогу, а там опирации с дробными значениями, причем чем точнее тем лучше, скачал Мануалку и все равно не могу понять как складывать, умножать и вообще производить все операции вплоть до вычисления интеграла и записи в память, передачей по портам ДРОБНЫХ ЧИСЕЛ. Может это намного проще чем я думаю, просто сам себе выдумал проблему. Если кто знает и подскажет или хотябы пример скинет буду очень признателен bulrash_avantag@mail.ru. Пишу на PIC18LF448, PIC16F877.
Реклама:
 
Непрочитано 10.06.2005, 14:27  
julbu
Прописка
 
Регистрация: 19.08.2004
Сообщений: 156
Сказал спасибо: 0
Сказали Спасибо 4 раз(а) в 4 сообщении(ях)
julbu на пути к лучшему
По умолчанию

PicBASIC Pro Floating Point Routines

--------------------------------------------------------------------------------

The PicBasic Pro Compiler has several built-in data types: bits, bytes and words, along with arrays of each. All of these types are unsigned integers. This means there is no decimal point so real or floating point numbers cannot be represented. There are several workarounds for this, including multiplying each value by 10 or 100 for calculations and dividing back when it is time to display the value.

The routines are accessed in PicBasic Pro by setting up specific integer variables (aint and bint) and performing a GOSUB to a floating point routine. The first routine should convert the integer value (aint) to a floating point value. Generally, floating point operations occur between 2 numbers, so a second integer (bint) should also be converted. Next a GOSUB to the required floating point operation, multiply for example, is performed. Finally, the floating point number is converted back into an integer (aint) so that PicBasic Pro can use it again. These GOSUB routines are created in an additional PicBasic Pro file that must be INCLUDED at the beginning of the program. Below is an example program that demonstrates these steps.

' Floating point test program

include "fp0c.bas" ' Include file for 'F84 (24-bit)
' include "fp20.bas" ' Include file for most other PICmicros (24-bit)
' include "fp0c32.bas" ' Include file for 'F84 (32-bit)
' include "fp2032.bas" ' Include file for most other PICmicros (32-bit)

Pause 500 ' Wait for LCD to start


' Demonstrate floating point multiply
aint = 10
Gosub itofa ' Convert int to float

bint = 20
Gosub itofb ' Convert int to float

Gosub fpmul ' FP multiply (10 * 20)

Gosub ftoia ' Convert float to int

Lcdout $fe, 1, "* ", dec aint
Pause 1000


' Demonstrate floating point divide
Gosub itofa ' Convert int back to float (200)

bint = 3
Gosub itofb ' Convert int to float

Gosub fpdiv ' FP divide (200 / 3)

Gosub ftoia ' Convert float to int

Lcdout $fe, 1, "/ ", dec aint


' Demonstrate floating point remainder
aint = 200
Gosub itofa ' Convert int to float

bint = 3
Gosub itofb ' Convert int to float

Gosub fpdiv ' FP divide (200 / 3)

bint = 100
Gosub itofb ' Convert int to float

Gosub fpmul ' Multiply by 100 to move remainder up 2 places

Gosub ftoia ' Convert float to int

aint = aint // 100 ' Get to the remainder
Lcdout ".", dec2 aint

End

кстати здесь и нашел
julbu вне форума  
Непрочитано 10.06.2005, 14:45  
julbu
Прописка
 
Регистрация: 19.08.2004
Сообщений: 156
Сказал спасибо: 0
Сказали Спасибо 4 раз(а) в 4 сообщении(ях)
julbu на пути к лучшему
По умолчанию

Поиск рулит
https://kazus.ru/programs/viewdownlo..._0/lid_83.html
https://kazus.ru/programs/viewdownlo..._0/lid_81.html
julbu вне форума  
Непрочитано 13.06.2005, 13:57  
DK
Почётный гражданин KAZUS.RU
 
Аватар для DK
 
Регистрация: 10.05.2003
Адрес: Москва
Сообщений: 2,528
Сказал спасибо: 737
Сказали Спасибо 1,927 раз(а) в 799 сообщении(ях)
DK на пути к лучшему
По умолчанию

вот прога, использующая вычисления с плаващей точкой (температура там считается)
Код:
'*************************************************  ***************
'*  Name    : RTC+2TM.BAS                                      *
'*  Author  : Denis Kuznetsov                                   *
'*  Notice  : Copyright (c) 2003 CopyLeft :)                    *
'*          : All Rights Reserved                               *
'*  Date    : 23.11.2003                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'*************************************************  ***************
'Include "MODEDEFS.BAS" 
include "fp20.bas"

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

DS_SCL      VAR PORTC.6 ' I2C clock pin 
DS_SDA      VAR PORTC.7 ' I2C data pin 
RTC         CON %11010000 ' RTC device address (byte addressing) 

I           CON 254 ' Control Byte 
Clr         CON 1 ' Clear the display 
Line1       CON 128 ' Point to beginning of line 1 
Line2       CON 192 ' Point to beginning of line 2 
pr          CON $14
blink       CON $0F

pos         var byte
pos_end     var byte

button1	    VAR	PORTC.0
button2	    VAR	PORTC.1
button3	    VAR	PORTC.2
beep        var PORTC.3

SecReg      CON $00 ' seconds address (00 - 59) 
' MSB of SecReg must be set to a 0 to enable RTC 
MinReg      CON $01 ' minutes address (00 - 59) 
HourReg     CON $02 ' hours address (01 - 12) or (00 - 23) 
DayReg      CON $03 ' day address (1 - 7) 
DateReg     CON $04 ' date address (01 - 28/29, 30, 31) 
MonthReg    CON $05 ' month address (01 - 12) 
YearReg     CON $06 ' year address (00 - 99) 
ContReg     CON $07 ' control register 
RTCflag     CON 0 ' RTC flag (location 0 of internal EEPROM) 
RTCset      VAR BIT ' bit to check if RTC has been set 
cntrl       CON %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low 

' The variable below holds the values entered: 
' entered by the user 

_sec        VAR BYTE ' $00 set seconds 
_min        VAR BYTE ' $56 set minutes 
_hr         VAR BYTE ' $23 set hour 
_day        VAR BYTE ' $06 set day 
_date       VAR BYTE ' $28 set date 
_mon        VAR BYTE ' $02 set month 
_yr         VAR BYTE ' $03 set year 

Set         var byte(8)

            
sec         VAR BYTE ' seconds 
MINs        VAR BYTE ' minutes 
hr          VAR BYTE ' hours 
day         VAR BYTE ' day 
date        VAR BYTE ' date 
mon         VAR BYTE ' month 
yr          VAR BYTE ' year 

hr_A        var byte
min_A       var byte
on_A        var byte
off_A       var byte
test_A      var byte

ps          var byte
tm          var word

B1          VAR BYTE	'Working buffer 1 for button command
B2          VAR BYTE	'Working buffer 2 for button command
B3          VAR BYTE	'Working buffer 3 for button command

n1          var byte
n2          var byte
n3          var byte

m1          var byte
m2          var byte
m3          var byte

d1          var byte
d2          var byte
d3          var byte

c           var byte    ' Debounce loop variable


mode        var byte    '0 - часы, 1 - календарь, 2 - температура
test        var byte
cnt         var byte

Define	ADC_BITS	10	' Set number of bits in result
Define	ADC_CLOCK	3	' Set clock source (3=rc)
Define	ADC_SAMPLEUS	500	' Set sampling time in uS

adval0	    var	word		' Create adval0 to store result
adval1	    var	word		' Create adval1 to store result
rez00       VAR word
rez10       VAR word
rez01       VAR word
rez11       VAR word
rez0        VAR word
rez1        VAR word
rez000      VAR word
rez111      VAR word

fpplaces    var byte			' Used to define number of decimal places in fpdisplay,fpdisplayr
ahold       var word			' Required by fpdisplay
bhold       var word			' Required by fpdisplay
resulthold	var	word		' Used to store results of previous calculations


            TRISC = %10000001
            TRISA = %11111011
            ADCON1 = %10000101

            Pause 1000 
            LCDOut I,Clr

            mode = 0
            ps = 0
            tm = 0
            
            test_A = 0
            off_A = 0

            read 0,min_A
            read 1,hr_A
            read 2,on_A
            sound beep,[100,10]

Loop: 
            pause 30
            I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr] 
            if hr_A = hr and min_A = MINs then
                test_A = 1
            else
                test_A = 0
                off_A = 0
            endif          
            select case mon
                case 1
                    m1 = $B1 : m2 = $BD : m3 = $B3
                case 2
                    m1 = $AA : m2 = $65 : m3 = $B3
                case 3
                    m1 = $4D : m2 = $61 : m3 = $70
                case 4
                    m1 = $41 : m2 = $BE : m3 = $70
                case 5
                    m1 = $4D : m2 = $61 : m3 = $B9
                case 6
                    m1 = $A5 : m2 = $C6 : m3 = $BD
                case 7
                    m1 = $A5 : m2 = $C6 : m3 = $BB
                case 8
                    m1 = $41 : m2 = $B3 : m3 = $B4
                case 9
                    m1 = $43 : m2 = $65 : m3 = $BD
                case 16
                    m1 = $4F : m2 = $BA : m3 = $BF
                case 17
                    m1 = $48 : m2 = $6F : m3 = $C7
                case 18
                    m1 = $E0 : m2 = $65 : m3 = $BA
            end select                    

            select case day
                case 1
                    d1 = $A8 : d2 = $BD : d3 = $E3
                case 2
                    d1 = $42 : d2 = $BF : d3 = $BD
                case 3
                    d1 = $43 : d2 = $70 : d3 = $E3
                case 4
                    d1 = $AB : d2 = $BF : d3 = $B3
                case 5
                    d1 = $A8 : d2 = $BF : d3 = $BD
                case 6
                    d1 = $43 : d2 = $B2 : d3 = $BF
                case 7
                    d1 = $42 : d2 = $63 : d3 = $BA
            end select                    

            if on_A = 1 and off_A = 0 and test_A = 1 then
                sound beep,[120,20,110,20,100,20,90,20]
                Button button1,0,100,0,B1,0,Loop
                off_A = 1
                goto Loop
            endif
next_c:     SELECT CASE mode
                CASE 0
                    gosub mode0
                CASE 1
                    goto mode1
                CASE 2
                    gosub mode2
                CASE 3
                    gosub mode3
            END SELECT

kb:          
            Button button1,0,100,0,B1,0,chk_mode
            mode = mode + 1
            tm = 0
            if mode › 3 then
                mode = 0
            endif
            goto Loop            
chk_mode:
            Button button2,0,100,5,B2,0,chk_no
            if B2 = 5 then
                tm = 0
                test = 0
                goto _set
            endif
chk_no:
            ps = ps + 1
            tm = tm + 1
            if tm › 500 then
                tm = 0
                mode = 0
                goto Loop
            endif
            if ps › 50 then
                ps = 0
                goto Loop
            endif

            Pause 20
            GoTo kb

_set:        
            SELECT CASE mode
                CASE 0
                    Set[1] = hr / 16
                    Set[2] = hr // 16
                    Set[3] = MINs / 16
                    Set[4] = MINs // 16
                    Set[5] = 0
                    pos_end = 4
                    pos = 1
                    gosub modeT
                    goto setT
                CASE 2
                    Set[1] = date / 16
                    Set[2] = date // 16
                    Set[3] = mon / 16
                    Set[4] = mon // 16
                    Set[5] = yr / 16
                    Set[6] = yr // 16
                    Set[7] = day
                    pos_end = 7
                    pos = 1
                    gosub modeD
                    goto setT
                CASE 3
                    read 0,min_A
                    read 1,hr_A
                    read 2,on_A
                    Set[1] = hr_A / 16
                    Set[2] = hr_A // 16
                    Set[3] = min_A / 16
                    Set[4] = min_A // 16
                    Set[5] = 0
                    pos_end = 5
                    pos = 1
                    gosub modeA
                    goto setT
                case else
                    goto Loop
            END SELECT
            
'#################################################  ###

setT:
tmp1t:
            tm = tm + 1
            if tm › 1500 then
                tm = 0
                mode = 0
                goto Loop
            endif
            pause 25
            if pos = 1 then
                LCDOut I,Line1,I,blink
                else
                    LCDOut I,Line1,I,2
                    for cnt = 1 to pos - 1
                        LCDOut I,pr,I,blink
                    next cnt
            endif
            Button button2,0,100,5,B2,0,chk_b3t
            test = 1
            tm = 0
            pos = pos + 1
            if pos › pos_end then
                pos = 1
                goto tmp1t
            endif
chk_b3t:
            Button button3,0,100,5,B3,0,chk_b1t
            test = 1
            tm = 0
            Set[pos] = Set[pos] + 1
            if Set[1] › 9 then
                Set[1] = 0
            endif
            if Set[2] › 9 then
                Set[2] = 0
            endif
            if Set[3] › 9 then
                Set[3] = 0
            endif
            if Set[4] › 9 then
                Set[4] = 0
            endif
            if Set[5] › 9 then
                Set[5] = 0
            endif
            if Set[6] › 9 then
                Set[6] = 0
            endif
            if Set[7] › 9 then
                Set[7] = 0
            endif
            LCDOut Set[pos]+48
chk_b1t:
            if test = 0 then
                goto tmp1t
            endif
tmp2t:
            Button button1,0,100,5,B1,0,tmp1t
            tm = 0
            I2CRead DS_SDA, DS_SCL, RTC, SecReg, [sec,MINs,hr,day,date,mon,yr] 
            if mode = 0 then
                _yr = yr
                _mon = mon 
                _date = date 
                _hr = Set[1] * 16 + Set[2]
                _sec = $00
                _min = Set[3] * 16 + Set[4]
                _day = day
                I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[_sec,_min,_hr,_day,_date,_mon,_yr,cntrl] 
                goto exit
            endif 
            if mode = 2 then
                _yr = Set[5] * 16 + Set[6]
                _mon = Set[3] * 16 + Set[4]
                _date = Set[1] * 16 + Set[2]
                _hr = hr
                _sec = sec
                _min = MINs
                _day = Set[7]
                I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[_sec,_min,_hr,_day,_date,_mon,_yr,cntrl] 
                goto exit
            endif 
            if mode = 3 then
                hr_A = Set[1] * 16 + Set[2]
                min_A = Set[3] * 16 + Set[4]
                on_A = Set[5]
                write 0,min_A
                write 1,hr_A 
                write 2,on_A
                goto exit
            endif 
exit:       Pause 5 
            goto Loop
            
'#################################################  ####


'#################################################  ###

mode0:      
            LCDOut I,Clr' Clear Lcd 
            LCDOut I,Line1, HEX2 hr, ":", HEX2 MINs, ":", HEX2 sec 
            LCDOut I,Line2, " ", m1, m2, m3,", ", HEX2 date
            return
mode1:      
            rez000 = 0
            rez111 = 0
            for c = 1 to 20
                ADCIN 0, adval0		' Read channel 0 to adval0
                ADCIN 1, adval1		' Read channel 1 to adval1
                rez000 = rez000 + adval0
                rez111 = rez111 + adval1
                pause 10
            next c
            rez0 = rez000 / 20
            rez1 = rez111 / 20
            aint = 1875
            gosub itofa
            bint = 1023
            gosub itofb      
            gosub fpdiv
            bint = rez0
            gosub itofb      
            gosub fpmul
            bint = 500
            gosub itofb
            gosub fpsub
            bint = 10
            gosub itofb      
            gosub fpdiv
            Lcdout $fe, 1
            Gosub fpdisplayr
            lcdout $FE,$C0," "
            aint = 1875
            gosub itofa
            bint = 1023
            gosub itofb      
            gosub fpdiv
            bint = rez1
            gosub itofb      
            gosub fpmul
            bint = 500
            gosub itofb
            gosub fpsub
            bint = 10
            gosub itofb      
            gosub fpdiv
            Gosub fpdisplayr
            goto kb
mode2:      
            LCDOut I,Clr' Clear Lcd 
            LCDOut I,Line1, " ", d1,d2,d3," ", HEX2 date, " "
            LCDOut I,Line2, m1, m2, m3," 20", HEX2 yr
            return
mode3:      
            LCDOut I,Clr' Clear Lcd 
            read 0,min_A
            read 1,hr_A
            read 2,on_A
            LCDOut I,Line1, " ", HEX2 hr_A, ":", HEX2 min_A
            if on_A = 1 then
                LCDOut I,Line2,$A0,$79,$E3,$D0,$42,$BA,$BB 
            else
                LCDOut I,Line2,$A0,$79,$E3,$D0,$42,$C3,$BA,$BB
            endif
            return
modeT:      
            LCDOut I,Clr' Clear Lcd 
            LCDOut I,Line1,HEX2 hr,HEX2 MINs
            LCDOut I,Line2, $A9,$63,$BF,$D0,$42,$70,$65,$BC 
            return
modeD:      
            LCDOut I,Clr' Clear Lcd 
            LCDOut I,Line1, HEX2 date, hex2 mon, hex2 yr, Hex day
            LCDOut I,Line2, $A9,$63,$BF,$D0,$E0,$61,$BF,$61
            return
modeA:      
            LCDOut I,Clr' Clear Lcd 
            LCDOut I,Line1, HEX2 hr_A, hex2 min_A, hex1 on_A
            LCDOut I,Line2, $A9,$63,$BF,$D0,$A0,$79,$E3,$BA
            return

fpdisplayr: bexp = $7A
			bargb0 = $4C
			bargb1 = $CD
			If aargb0.7 Then			' Check the floating point sign bit
				Gosub fpsub				' If negative, subtract barg from aarg
			Else
				Gosub fpadd				' If positive, add barg to aarg
			Endif

fpdisplay:	bexp = aexp					' Store the FP value of aarg to the barg variables
			bargb0 = aargb0
			bargb1 = aargb1
			Gosub ftoia					' Convert aarg to integer
			ahold = aint				' Save this value for the final display
			Gosub itofa					' Convert integer back to float
			Swap aexp,bexp				' Swap the FP values of aarg and barg before subtraction
			Swap aargb0,bargb0
			Swap aargb1,bargb1
			Gosub fpsub					' Subtract the integer portion from the full number
			bint = 10					' Make bint = 10 E fpplaces
			bhold = bint				' Save the integer value of bint for zeros loop
			Gosub itofb					' Convert bint to integer prior to FP multiply
			Gosub fpmul					' Multiply the decimal portion x 10 E fpplaces
			Gosub ftoia					' Convert result to aint integer
            If (ahold.15) OR (aint.15) Then		' Check for negative integer or decimal
				Lcdout "-"				' Display minus
                else 
 			        Lcdout "+"
			Endif
			Lcdout dec abs ahold		' Display integer portion
			Lcdout "."
zeros:		
            bhold = bhold / 10								' Set bhold to be next place to right
			If (abs aint ‹ bhold) AND (bhold › 1) Then		' Check for leading zero in decimal
				Lcdout "0"									' Display leading zero
				Goto zeros									' loop to check for another zero
			Endif
			Lcdout dec abs aint,$EF,67		' Display the rest of the decimal portion
			aint = ahold				' Restore the original value of aint
			Return
        End
__________________
Опыт - это школа, в которой человек узнает, каким дураком он был раньше.
DK вне форума  
 

Закладки
Опции темы

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.

Быстрый переход

Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
FLOAT->INT anton_1000 Микроконтроллеры, АЦП, память и т.д 5 03.10.2008 16:22
Помогите разобраться со схемой. я не могу понять что не так. sofist Источники питания и свет 17 15.11.2007 15:52
FLOAT TO ASCII EVGENIY1962 Микроконтроллеры, АЦП, память и т.д 2 24.06.2007 12:02
Ошибка ISP режима (ISP Mode Error) Не могу понять в чём дело zhan99999 Микроконтроллеры, АЦП, память и т.д 4 11.01.2007 02:21
Вывод Float на ЖКИ в AVR assembler Tcom Микроконтроллеры, АЦП, память и т.д 18 07.03.2006 20:48


Часовой пояс GMT +4, время: 16:50.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd. Перевод: zCarot