;This code was written by Leon Maurer except where otherwise noted ;If you can figure out what's going on, you can use it as you like ;final_code.asm==================== list p=16f630 __config b'11000110000100' radix hex ;----------------------------- pc equ 0x02 STATUS equ 0x03 porta equ 0x05 ;for controll line and bit input portc equ 0x07 ;for data lines counter equ 0x20 big equ 0x21 temp equ 0x22 bin equ 0x23 tens_and_ones equ 0x24 hundreds equ 0x25 max equ 0x26 ;random numbers will be generated in the range (0,max) temp1 equ 0x27 temp2 equ 0x28 i equ 0x29 orand equ 0x2A e equ 0 rw equ 1 rs equ 2 rbit equ 3 ;RC4,RC5,RA4 are for buttons mode equ 4 ;mode button inc equ 4 ;inc button decgen equ 5 ;dec/gen button Z equ 0x02 C equ 0x00 ;----------------------------- org 0x00 start movlw 0xff movlw b'00011000' tris porta ;random bit input & mode button movlw b'00110000' tris portc ;outputs & inc/dec buttons clrf porta clrf portc call bigp ;need to wait a while for LCD to wakeup call bigp ;cut this down? call bigp ;set 4 bit mode, rs and rw are already low for writing commands movlw b'00000010' movwf portc call enable movlw b'00000110' call sendcmw ;sets DDRAM increment and not screen shift movlw b'00101100' call sendcmw ;command sets two line mode and turns the display on movlw b'00001100' ;'00001110' call sendcmw ;command turns cursor and blinking off call bigp movlw b'00000010' call sendcmw ;home cursor call bigp ;this is the end of the initialization clrf bin movlw 0x05 ;by default, the number generated will be between 0 and 5 movwf max call current ;-------------------------------------------- ;main loop circle btfsc portc,5 ;does next instruction if +/- button is not depressed goto gen btfss portc,4 ;if increment button is depressed call increment btfss porta,4 ;if decrement button is depressed call decrement goto circle gen btfss portc,4 ;does next instuction if gen button is depressed call generate goto circle ;keep at it ;-------------------------------------------- current call clear call bline movf max,w movwf bin call convert return increment call bigp btfsc portc,4 goto e1 movf max,w sublw 0xFF btfss STATUS,2 incf max,f call current e1 return decrement call bigp btfsc porta,4 goto e2 movf max,w sublw 0x01 btfss STATUS,2 decf max,f call current e2 return clear movlw b'00000001' call sendcmw call bigp return ;switch to bottom line bline movlw b'11000000' call sendcmw call bigp return ;switch to top line tline movlw b'10000000' call sendcmw call bigp return ;sendw outputs whatever is in w in two parts sendw movwf temp swapf temp,w ;puts the last nibble first andlw b'00001111' movwf portc call enable call p movf temp,w andlw b'00001111' movwf portc call enable return ;sends command sendcmw bcf porta,2 call sendw return ;sends character sendchw bsf porta,2 call sendw return ;pauses p movlw b'11111111' movwf counter loop decfsz counter,f goto loop return ;looooooonnnnnnngggggg pause bigp movlw b'11111111' movwf big bl call p decfsz big,f goto bl return ;medium pause medp movlw b'00001111' movwf big ml call p decfsz big,f goto ml return ;puts enable high then low enable bsf porta,e call p bcf porta,e return ;outputs digit to lcd PUTDIG call loaddig call sendchw return ;takes nibble containing a digit and loads the appropriate character code into w loaddig addwf pc,f retlw b'00110000' retlw b'00110001' retlw b'00110010' retlw b'00110011' retlw b'00110100' retlw b'00110101' retlw b'00110110' retlw b'00110111' retlw b'00111000' retlw b'00111001' retlw b'00100000' ;10=whitespace ;This function is borrowed from Scott Dattalo http://www.dattalo.com/ ;******************************** ;binary_to_bcd - 8-bits ; ;Input ; bin - 8-bit binary number ; A1*16+A0 ;Outputs ; hundreds - the hundreds digit of the BCD conversion ; tens_and_ones - the tens and ones digits of the BCD conversion convert CLRF hundreds SWAPF bin, W ;w = A0*16+A1 ADDWF bin, W ;w = A0+A1 ANDLW b'00001111' ;w = A0+A1 % 16 SKPNDC ;if A0+A1 > 16 ADDLW 0x16 ; w += 16 SKPNDC ;if w % 16 > 10 ADDLW 0x06 ; w += 6 ADDLW 0x06 ;w += 6 SKPDC ;if w < 10 ADDLW -0x06 ; w -= 6 BTFSC bin,4 ADDLW 0x16 - 1 + 0x6 SKPDC ADDLW -0x06 BTFSC bin, 5 ADDLW 0x30 BTFSC bin, 6 ADDLW 0x60 BTFSC bin, 7 ADDLW 0x20 ADDLW 0x60 RLF hundreds, F BTFSS hundreds, 0 ADDLW -0x60 MOVWF tens_and_ones BTFSC bin,7 INCF hundreds, F movf hundreds,w btfsc STATUS,2 movlw 0x0A call PUTDIG movf hundreds,w btfsc STATUS,2 goto ze swapf tens_and_ones,w andlw b'00001111' call PUTDIG goto ne ze swapf tens_and_ones,w andlw b'00001111' btfsc STATUS,2 movlw 0x0A call PUTDIG ne movf tens_and_ones,w andlw b'00001111' call PUTDIG return ;the generate function is based off the following c code I came up with ;int main () { ; char max, temp1, temp2, i, orand;//PORTA; ; do{ ; temp1=temp2=0; ; for(i=1; i>3; ; //PAUSE ; } while(orand==(PORTA>>3)); ; temp1=temp1|orand; ; do{ ; orand=PORTA>>3; ; //PAUSE ; } while(orand==(PORTA>>3)); ; temp2=temp2|orand; ; } ; temp1=temp1^temp2; ; } while(temp1>max); ; return 0; ;} generate call clear call current call tline do movlw b'00000001' movwf i clrf temp1 clrf temp2 for bcf STATUS,0 rlf temp1,f rlf temp2,f do1 call medp movf porta,w andlw b'00001000' movwf orand call medp movf porta,w andlw b'00001000' subwf orand,w btfsc STATUS,2 goto do1 movf orand,f btfss STATUS,2 incf temp1,f do2 call medp movf porta,w andlw b'00001000' movwf orand call medp movf porta,w andlw b'00001000' subwf orand,w btfsc STATUS,2 goto do2 movf orand,f btfss STATUS,2 incf temp2,f bcf STATUS,0 rlf i,f movf i,w subwf max,w btfsc STATUS,0 goto for movf temp1,w xorwf temp2,w movwf bin subwf max,w btfss STATUS,0 goto do call convert return end ;=============================