NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Total C virgin Newbie

January 07, 2010
by RICHRICH36
RICHRICH36's Avatar

I will admit, I am a TOTAL NEWBIE. I am trying to get in the world of microcontrollers and the first thing I want to do is emulate credit pulses for some game machines that use digital coin acceptors. The following code was provided to me but I have absolutely no idea how to turn it in to something useful for the atmega. Anyone out there that wasnt to help me?

;***********

;equates section

tmr0 equ 1 ;means "tmr0" is file 1 status equ 3 ;means "status" is file 3 porta equ 5 ;means "porta" is file 5 portb equ 6 ;means "portb" is file 6 trisa equ 85h ;means "trisa" (the "porta" I/O selection) is file 85h trisb equ 86h ;means "trisa" (the "portb" I/O selection) is file 85h option_r equ 81h ;means the "option" register is file 81h zerobit equ 2 ;means "zerobit" is bit 2 count equ 0ch ;means "count" is file 0ch

;***********

list p=16f84 org 0 goto start

;***********

;configuration bits __config h'3ff0' ;selects LP oscillator, WDT off, PUT on (Power On Timer), code protection ;disabled

;***********

;subroutine section

;Coil Sense delay

sense1 clrf tmr0 ;start timer0 loopa movf tmr0,w ;read tmr0 into working register sublw .5 ;time - 5 btfss status,zerobit ;check time - working register = 0 goto loopa ;time is not 0, so do it again retlw 0 ;time is 5, return to the last point in program

;Travel between sensors delay

travel clrf tmr0
loopb movf tmr0,w sublw .3 btfss status,zerobit goto loopb retlw 0

;Led Sense delay

sense2 clrf tmr0
loopc movf tmr0,w
sublw .4
btfss status,zerobit
goto loopc
retlw 0

;***********

;configuration section

start bsf status,5 ;turns to bank 1 movlw b'00011111' ;5 bits of "porta" are inputs movwf trisa

    movlw   b'00000000'
    movwf   trisb               ;"portb" is all output

    movlw   b'00000111'     ;to set the prescaler at /256
    movwf   option_r            ;timer is now 1/32 of a second using the 32.768 Khz cristal

    bcf status,5            ;return to bank0
    clrf    porta               ;clears "porta"
    clrf    portb               ;clears "portb"

;***********

;Beginning of program:

;Button x1: add one credit each time pressed

x1 btfsc porta,0 ;check if switch is pressed goto x10 ;switch wasn't pressed, check again!

        bsf portb,0         ;switch was pressed, send the first pulse
        call    sense1          ;wait
        bcf portb,0         ;clear the first pulse
        call    travel              ;wait

        bsf portb,1         ;start sending the second pulse
        call    sense2          ;wait
        bcf portb,1         ;clear the second pulse
        call    travel              ;wait

;Button x10: add 10 credits each time pressed

x10 btfsc porta,1 ;check if switch is pressed goto x1 ;switch wasn't pressed, check again!

        movlw   .10         ;load 10 into W
        movwf   count       ;load 10 into "count"

repeat bsf portb,0 ;switch was pressed, send the first pulse call sense1 ;wait bcf portb,0 ;clear the first pulse call travel ;wait

        bsf portb,1         ;start sending the second pulse
        call    sense2          ;wait
        bcf portb,1         ;clear the second pulse
        call    travel              ;wait

        decfsz  count       ;count - 1, skip if cero
        goto    repeat

        goto    x1

END

January 07, 2010
by Rick_S
Rick_S's Avatar

It's hard for me to tell as I'm not totally familiar with it, but it looks like assembly to me not C.

Rick

January 08, 2010
by justin
justin's Avatar

I got the same impression, Rick_S. RICHRICH36, I did a couple of quick Google searches, and I'm pretty sure that's PIC assembly. In other words, it's low-level instructions for a very different type of microcontroller. I'm sure that program can be translated into something that will work on the ATmega, but it looks like a task best suited for someone with some experience programming PICs. :-)

Justin

January 08, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

It does appear to be PIC assembly for a part number 16F84.

I think that your best bet is to only look at the comments, and then write AVR C code that matches the description. BCF appears to be a "bit clear", and BSF is a "bit set" -- similar to the digital I/O bit manipulations we discuss extensively in The NerdKits Guide.

Mike

Post a Reply

Please log in to post a reply.

Did you know that essentially all power supplies' voltages drop when current is drawn from them? Learn more...