I'd like to state disagreement with the premise that one should naturally use binary on a microcontroller like a PIC. When the bulk of an application deals with input and output in decimal (especially scaled values) then it is often easier to compute in BCD. A relatively simple 'accumulator-based' library of routines can provide simple routines to do BCD operations. For example: movlw MYVAL ; point to LSDigits of value movwf FSR movlw MYVAL_E-MYVAL ; length of value call bcd_add Bob Ammerman RAm Systems ----- Original Message ----- From: "John Brown" To: Sent: Monday, October 07, 2002 6:58 PM Subject: Re: [PIC]: BCD time-of-day addition - working solution > In my case am reading BCD from a Dallas Time and Date chip (ie BCD), and > have times and dates of particular events that I want to take places stored > in EPROM (also in BCD since they are easy to enter and read in hex and easy > to compare to the time and date I get from the Dallas chip). Therefore it > has made things pretty simple to stick with BCD so far. > > However some events need to be timed (eg water the grass for 30 minutes > once it gets dark - ie I don't know the precise starting time so I need to > calculate the end time). > > I contemplated changing everything over to binary, but think I'll give it a > go in BCD for now. > > Having considered the other replies I came up with the following working > solution (16f877), derived basically by cut, pasting and modifying Scott > Dattalo's bcd_add > (http://www.piclist.com/techref/microchip/math/add/bcdp2.htm?key=bcd&from=): > > > ;---START--- add the number of minutes in OnTime to TmpMin and increment > TmpHour if necessary > ; The resultant time is put into in ResHour and ResMin > ; NOTE: only tested for OnTime < 60 minutes > ; NOTE: if the resultant time is between midnight and 1am then > ResHour will = 24 > ; This routine an adapted version of Scott Dattalo's bcd_add from the PIC > list web site > AddMins MOVF TmpHour,W ;by default make the resultant hour the > same as TmpHour > MOVWF ResHour ;_(it will be incremented later if necessary) > COMF TmpMin, W ;add TmpMin and OnTime > ADDLW 0x9a+1 > SUBWF OnTime, W > RLF ResMin, F > BTFSS STATUS,DC ;_ if lsn of x + lsn of y < 10 (dec) > ADDLW -0x06 ;__ then remove the extra 6 added above > BTFSS ResMin, 0 ;_ Similarly for the msn > ADDLW -0x60 > RRF ResMin, F ;_restore the carry > MOVWF ResMin > MOVLW 0x60 ;see if resultant number of minutes > 59 > SUBWF ResMin,W > BTFSS STATUS,C > GOTO aend ;_ if not, then go to the end of the routine > MOVWF ResMin ;_ if it is then the result is the correct > number of minutes > COMF TmpHour, W ;__ and you need to add 1 to TmpHour (BCD) > ADDLW 0x9a+1 > SUBLW 0x01 > RLF ResHour, F > BTFSS STATUS,DC ;_ if lsn of x + lsn of y < 10 (dec) > ADDLW -0x06 ;__ then remove the extra 6 added above > BTFSS ResHour, 0 ;_ Similarly for the msn > ADDLW -0x60 > RRF ResHour, F ;_ restore the carry > MOVWF ResHour > aend RETURN > ;---End--- add the number of minutes in OnTime to TmpMin and increment > TmpHour if necessary > > John > > At 03:55 PM 06/10/2002 +0100, you wrote: > >This is not a direct solution to your problem but an attempt to rethink it. > > > >Why add in BCD? It seems unnatural on a PIC. If you are using a PIC, then > >the natural method would be binary addition. > > > >I prefer to work in binary and only convert to BCD (or ASCII) when > >necessary, which is usually for output on a device. > > > >-- > >http://www.piclist.com hint: The PICList is archived three different > >ways. See http://www.piclist.com/#archives for details. > > _______________________________________________________________________ > John Brown > PhD (Clinical Psychology) candidate > Email: john.brown@anu.edu.au > School of Psychology Phone: (02) 6125-3827 > Room 124, Building 39, Psychology Fax: (02) 6125-0499 > The Australian National University Mobile: 0429 455 504 > ACTON ACT 0200 Web: http://www.anu.edu.au/psychology/staff/BrownJ.htm > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body