Searching \ for '[PIC] PicKit 2 Fun sample game (Was: Re: [PIC] - V' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'PicKit 2 Fun sample game (Was: Re: [PIC] - V'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] PicKit 2 Fun sample game (Was: Re: [PIC] - V'
2005\10\11@091322 by Maarten Hofman

face picon face
> They are working right out of the box in the relocatable
> mode if one follows the templates from
> C:\Program Files\Microchip\MPASM Suite\Template\Object.

I discovered that yesterday, when I reprogrammed by own code. I used
both the example posted here, as well as the examples in the Microchip
documentation, and everything worked out immediately. It is fun to
have relocatable code, and for this project it was very appropriate.

Note that in the following code I have made it more difficult for
myself, because I gave the routines for delays and randoms separate
blocks. I did this because in reality these would most likely be in
separate source files. Feel free to comment on it, especially the
relocatable part (I am already aware that using FSR/INDF and loops for
the sequences are better, and that the Delay routine could be improved
greatly).

The game works as follows: first all LEDs are lit, and the progam
waits for the user to hit the button. Once pushed, a random sequence
of four LED will light. The user will then be able to choose a LED by
use of the potentiometer, and use the button to select that LED. After
this, the correct answer will blink three times. If the user's answer
was wrong, the game will start again, if the answer was right, the
next LED can be selected. If the user gets the entire sequence right,
all LED will turn off and the game ends.

; SIMON.ASM - Programmed by Maarten Hofman
; 081005: Started programming
; 101005: Modified to relocatable code

LIST R = DEC

#include <p16F690.inc>
       __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF &
_CP_OFF & _IESO_OFF & _FCMEN_OFF)

simon udata
Display res 1                ; define a variable to hold the diplay
Verify res 1                ; Store location for verification
Input res 1                        ; Store location for player input
Sequence res 4                ; 4 long sequence

delay udata
DelayCount res 2        ; Delay counter

random udata
Random res 1                ; Random values used for the game

RST CODE 0x0
       pagesel Start
   goto Start

MAIN CODE
Start
   banksel TRISA
       movlw        0xFF
       movwf        TRISA ^ 0x080                ; Make PortA all input
       clrf        TRISC ^ 0x080                ; Make PortC all output
       movlw        0x10                                ; A2D Clock Fosc/8
       movwf        ADCON1 ^ 0x080
   banksel ANSEL
       movlw        0xF7                                ; we want all Port A pins Analog
       movwf        ANSEL ^ 0x100
   banksel ADCON0
       movlw        0x01
       movwf        ADCON0                                ; configure A2D for Channel 0 (RA0), Left justified,
and turn on the A2D module
MainLoop
   call GameStart
   pagesel Delay
   call Delay
   banksel Display
   pagesel ShowSequence
; Prepare the sequence and display it. Later this can use FSR and INDF
to allow varying lengths
   call ShowSequence
   movf Display,w
   movwf Sequence
   call ShowSequence
   movf Display,w
   movwf Sequence+1
   call ShowSequence
   movf Display,w
   movwf Sequence+2
   call ShowSequence
   movf Display,w
   movwf Sequence+3
; End of showing the sequence, start of verifying sequence. Later this
can use FSR and INDF to allow varying lengths
   movf Sequence,w
   call VerifySequence
   btfsc Verify,7                        ; Check for failure
   goto MainLoop                        ; Restart
   movf Sequence+1,w
   call VerifySequence
   btfsc Verify,7                        ; Check for failure
   goto MainLoop                        ; Restart
   movf Sequence+2,w
   call VerifySequence
   btfsc Verify,7                        ; Check for failure
   goto MainLoop                        ; Restart
   movf Sequence+3,w
   call VerifySequence
   btfsc Verify,7                        ; Check for failure
   goto MainLoop                        ; Restart
   banksel PORTC
   clrf PORTC
       goto        $

VerifySequence
 movwf Verify                ; Store the value that needs to be checked against
 call ReadPotMeter        ; Read the user's input
 movf Display,w        ; Retrieve selected value
 movwf Input                ; Store for safety
 movf Verify,w                ; Load the correct value
 movwf Display                ; Move to the display
 call DisplayValue        ; Show it
 pagesel Delay
 call Delay
 banksel PORTC
 clrf PORTC
 call Delay
 pagesel DisplayValue
 call DisplayValue        ; Show it
 pagesel Delay
 call Delay
 banksel PORTC
 clrf PORTC
 call Delay
 pagesel DisplayValue
 call DisplayValue        ; Show it
 pagesel Delay
 call Delay
 banksel PORTC
 clrf PORTC
 call Delay
 banksel Input
 movf Input,w                ; Load the input value
 xorwf Verify,w        ; Check if it is the same
 btfss STATUS,Z        ; If it isn't
 bsf Verify,7                ; Set the high bit of the verification
 pagesel Start
 return

ShowSequence
 pagesel getRandom
 call getRandom
 banksel Random
 movf Random,w
 andlw 3
 banksel Display
 movwf Display
 pagesel DisplayValue
 call DisplayValue
 pagesel Delay
 call Delay
 call Delay
 banksel PORTC
 clrf PORTC
 call Delay
 banksel Display
 pagesel Start
 return

GameStart
 movlw 15                        ; All LEDs on waiting for player
 banksel PORTC
 movwf PORTC                ; Turn on
 banksel Random
 incf Random,f                ; Make sure we have a nice random number
 banksel PORTA
 btfsc PORTA,3                ; Wait for user to push a button
 goto GameStart
 clrf PORTC                ; Turn LEDs off
 banksel Display
 return

ReadPotMeter
 nop                                ; Charge capacitor
 nop
 nop
 nop
 nop
 banksel ADCON0
 bsf ADCON0,GO                ; Start conversion
 btfss ADCON0,GO        ; Wait for conversion to end
 goto $-1
 swapf ADRESH,w        ; Load which LED is selected
 banksel Display
 movwf Display                ; Store the result
 rrf Display,f                ; Move one step
 rrf Display,f                ; Move another step
 movlw 3                        ; Filter out lowest two bits
 andwf Display,f        ; And store again
 call DisplayValue
 banksel PORTA
 btfsc PORTA,3
 goto ReadPotMeter
 banksel Display
 return

DisplayValue
 movlw 1
 btfsc Display,1        ; Check if we are the lower two, or the higher two
 goto DisplayHigherTwo
 btfsc Display,0        ; Check if we are the lowest
 movlw 2
 banksel PORTC
 movwf PORTC                ; Display value
 banksel Display
 return
DisplayHigherTwo
 movlw 4
 btfsc Display,0        ; Check if we are the lowest
 movlw 8
 banksel PORTC
 movwf PORTC                ; Display value
 banksel Display
 return

RANDOM CODE
getRandom
 banksel Random
 movf Random+1,w
 iorwf Random,w
 btfsc STATUS,Z        ; Check whether both are 0
 comf Random+1,f        ; If so, invert
 movlw 0x80        ; XOR HI7, HI6, HI4 and LO3
 btfsc Random+1,6
 xorwf Random+1,f
 btfsc Random+1,4
 xorwf Random+1,f
 btfsc Random,3
 xorwf Random+1,f
 rlf Random+1,w        ; Get the high bit into carry
 rlf Random,f        ; Move it to the low bit
 rlf Random+1,f        ; And shift on
 return

DELAY CODE
Delay
 movlw 255        ; Set initial delay
 banksel DelayCount
 movwf DelayCount + 1
DelayOutLoop
 movlw 255
 movwf DelayCount
DelayInLoop
 goto $+1
 goto $+1
 goto $+1
 decfsz DelayCount,f
 goto DelayInLoop
 decfsz DelayCount + 1,f
 goto DelayOutLoop
 return
       end

2005\10\11@105911 by Jan-Erik Soderholm

face picon face
Maarten Hofman wrote :

> Note that in the following code I have made it more difficult for
> myself, because I gave the routines for delays and randoms separate
> blocks.

Difficult ? In what way ?

I find it quite handy to put an extra CODE now and then, that
gives sizes for all routines calculated in the MAP file. The same
for the UDATA sections. B.t.w, I'd move the "delay udata" and
"random udata" close(r) to the "DELAY CODE" and "RANDOM CODE".
That makes it easier to split the file later and also easier
to find the variables for each routine without to much scrolling.

Why is CODE in uppercase and udata in lower case ?

> I did this because in reality these would most likely be in
> separate source files.

Doesn't matter. Multiple code blocks works just as fine
whithin a single source file.

The only (?) thing that have to be added when spitting the
file into multiple source file, are "extern" and "global" where
needed.

Regards,
Jan-Erik.



2005\10\11@112112 by Maarten Hofman

face picon face
> > Note that in the following code I have made it more difficult for
> > myself, because I gave the routines for delays and randoms separate
> > blocks.
>
> Difficult ? In what way ?

In that I had to add more banksel and pagesel commands to account for
them moving.

> I find it quite handy to put an extra CODE now and then, that
> gives sizes for all routines calculated in the MAP file. The same
> for the UDATA sections. B.t.w, I'd move the "delay udata" and
> "random udata" close(r) to the "DELAY CODE" and "RANDOM CODE".
> That makes it easier to split the file later and also easier
> to find the variables for each routine without to much scrolling.

You're absolutely right. It is some ingrained C habit to put the
variable names at the top.

> Why is CODE in uppercase and udata in lower case ?

I was following the examples... And in that particular example they
were like that, so I copied it. No specific other reason.

> The only (?) thing that have to be added when spitting the
> file into multiple source file, are "extern" and "global" where
> needed.

Yes... The delay variable could become local. The random variable
would need to remain global, although I guess it is possible to return
it using W and create a separate increment routine, which would be
neater (I actually already ran into the problem of accidentally ANDing
the random register and messing up the sequence).

Greetings,
Maarten Hofman.

More... (looser matching)
- Last day of these posts
- In 2005 , 2006 only
- Today
- New search...