1st try at simple program

Discussion in 'Embedded Systems and Microcontrollers' started by Man_in_UK, Jun 19, 2008.

  1. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    This is my sort of first try at a little program and would like to know if I am doing things right. I only understand the simple commands and there might be a better way of doing it, what do you think ?

    The program is to eliminate every third pulse from a sensor. The pulse enters porta and is duplicated out on portb, every third pulse should be ignored and not duplicated.

    porta,1 = pulse input
    portb,1 = pulse output

    btfss porta,1 :
    goto _on :check for pulse, if not loop
    decfsz count,f :check if its third count
    goto setout :let pulse through, set an output
    call skip :if counter is clear skip a pulse
    goto _on :

    bsf portb,1 :set pulse output

    btfsc porta,1 :
    goto waitout :loop until input goes low
    bcf portb,1 :remove output
    goto _on :

    skip :no output is sent, pulse skipped
    btfsc porta,1 :
    goto skip :wait till input is removed
    clrf count :
    movlw 0x02 :
    movlw count :reset counter with 2
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    You need to initialise the count at the beginning of your program, before you decrease it for the first time (before _on).

    In the skip function, I believe the correct code to reset the counter should be:

    movlw 0x02
    movwf count

    You dont need to clear the count variable first (clrf count).

    Don't forget to set PORTA, 1 as input and PORTB, 1 as output. And remember that in MPLAB the comment is preceded with semicolon ; and not colon : .
  3. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Cheers for that.

    I have not yet mastered Mplab. Although years ago I knew how to check code and produce a file that was suitable to program into a device but things have changed. I was even having trouble following the walk-through toutorial (I'm sure the instruction are wrong).

    I have got hold hold of a Pickit 1 flash project board and I think that will bring things along well. I do have a large complex idea of a program to tackle over the next year but I think its best to start small.

    Thanks again on pointing out those problems.
  4. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Have you anything ready to run on a 16C54 ?

    I have a PIC project board that I used to use for testing code and it would be very usefull to use as a testbed. I have a few 16C54A's left over from years back but I cant seem to get any life out of them.

    Not sure if its my use (or bad attempt at) of MPlab but all test code does nothing.
    Its probably a bad go at setting up the "Init".

    Any hex file that makes a led light will be a great help ..... and the asm to show how it was setup would be very helpfull.
  5. Man_in_UK

    Thread Starter AAC Fanatic!

    May 13, 2008
    Im having some trouble that I hope someone can help with. I can't assign the output ports correctly !

    Most of this setup routine in this asm file is taken from the first debounce lesson of the Pickit flash, setting GP3 as an input and all others as outs.

    The simple program waits for a high input on GP3 and responds by setting a low output on GP2 and vice versa (when GP3 drops low - GP2 goes high). GP1 drives an LED that is toggeled every 3 cycles to visually show that there is data being passed through.

    LED flashes to show a pulse input but GP2 has no output ?
    I found the correct output on GP5 (don't know why, I didnt want it there)so as a test I setup GP0 as a duplicate output. I CAN get my output on GP0 but it will not come out of GP2.

    This problem was measured directly on the chip pins while running on a pickit flash project board.

    (sorry for not knowing how to post this in the correct tab layout)

    list p=12F675 ; list directive to define processor
    #include <p12f675.inc> ; processor specific variable definitions

    #define Bank0 0x00
    #define Bank1 0x80
    #define input GPIO,3 ;pulse input on pin 4
    #define D0_1Tris B'11001000'
    #define ledout GPIO,1 ; led indicator on pin 6
    #define pout GPIO,2 ;output on pin5 but does not work
    #define ppout GPIO,0 ; duplicated output pin 7 works OK
    #define LEDOn flags,0
    ;General Purpose Registers (GPR's)
    cblock 0x20
    flags ; LED flags

    ORG 0x000 ; processor reset vector
    goto Init ; go to beginning of program
    ORG 0x004
    ;call 0x3FF ; retrieve factory calibration value

    BANKSEL Bank1 ; BANK1
    movwf OSCCAL ; update register with factory cal value
    movlw D0_1Tris ; set direction so LEDs D0 & D1 are outputs
    movwf TRISIO ; all others are inputs (high-z)
    clrf ANSEL ; configure A/D I/O as digital
    banksel Bank0 ; change back to PORT memory bank
    movlw CM2 | CM1 | CM0 ; configure comparator inputs as digital I/O
    movwf CMCON ;
    clrf flags ; set initial LED state as off

    btfss input
    goto main
    call unsetout


    btfsc input
    goto stilon
    call setout
    decfsz count,f
    goto main
    call toggleled

    goto main

    clrf count
    movlw 0x03
    movwf count

    btfss LEDOn
    goto TurnLedOn

    bcf LEDOn
    bcf ledout


    bsf LEDOn
    bsf ledout


    bsf pout
    bsf ppout

    bcf pout
    bcf ppout

    Last edited: Jul 3, 2008