1. cs6110

    Thread Starter New Member

    Mar 4, 2009
    Hey Everyone,
    I was hoping that someone could help me with the following program. I haven't been in school in 20 years and the economy has sent me back.And I find myself behind all the young kids.
    here is what the program i supposed to do...
    drive a moter with on off speed control.
    there will be four pushbuttons start stop increase and decrease speed
    there will be a red light to indicate power to the circuit
    the moter will have four speed...stop 1/4 1/2 3/4 full
    a row of red leds will indicate speed of motor
    at stop all lights out
    at full all lights will light
    leds will change with motor speed
    a green light will come on when start button is pushed

    Thanks for all the help
  2. cs6110

    Thread Starter New Member

    Mar 4, 2009
    Actually, when I was typing this out I was reading from a sheet. so it is no a moter its actually a 5 volt fan. But here is what I did so far, I got the fan to work and the lights come on. BUT, the sequence is flawed, it will work the way it should only once and then it stops. Here is the program any ideas?

    MC6110 Motor Control 2
    ;Name: herb connors
    ;Date: 4 March 2009
    include "p16f84a.inc"
    PILOT equ 0x0C ;hold the value to determine the speed of the pilot light
    SPEED equ 0x0D ;hold the value representing the speed of the motor
    TON equ 0x0E ;hold the value of the ON Pulse
    TOFF equ 0x0F ;hold the value of the OFF Pulse

    org 0x00 ;organize chip
    goto Init ;goto Initialize
    org 0x04 ;organize Option Register
    goto ISR
    ; Initialize
    banksel TRISA ;change bank
    movlw 0xF0 ;move literal #FE into w
    movwf TRISA ;set up TRIS A
    movlw 0xf2 ;
    movwf TRISB ;set up TRISB
    movlw 0xD4 ;move Number
    movwf OPTION_REG ;
    movlw 0xAC ;move Number
    movwf INTCON ;
    banksel PORTA ;change bank
    clrf TMR0 ;clear Timer Zero
    movlw 0x30 ;arbitrary to slow down pilot light
    movwf PILOT ;put in address 0x0C
    bcf PORTB,2

    btfss PORTB,4 ;check start PB, skip next if not depressed
    goto Start
    btfss PORTB,1 ;test stop pb, skip if not depressed
    goto Stop

    PWM ;sets up Duty Cycle
    btfss PORTB,2 ;check for green LED ON, skip next if on
    goto Main ;if off go back and check switches
    movfw SPEED ;move setpoint to W Reg
    movwf TON ;move setpoint into TON to turn on fan
    sublw 0xFF ;subtract TON from the total to get TOFF
    movwf TOFF
    ;turn on transistor
    bsf PORTB,3 ;turn on the fan
    nop ;kill a cycle
    decfsz TON ;Timing for Duty Cycle on
    goto $-2 ;go back 2 instructions

    ;turn off transistor
    bcf PORTB,3 ;turn off the fan
    decfsz TOFF ;drop TOFF, repeat until zero, include nop
    goto $-2 ;TON+TOFF=255

    goto Main ;go back to start of program
    ;; Interrupt Service Routine

    btfsc INTCON,T0IF ;test for TMR0 Interrupt
    goto Pilot
    btfsc PORTB,4
    goto Speed
    goto ENDISR ;quit ISR

    decfsz PILOT ;decrement PILOT, after the 0x30th TMR0
    goto ENDISR ;interrupt, this section will toggle
    movlw 0x01
    xorwf PORTB ;pilot LED
    movlw 0x30
    movwf PILOT ;in between, it kicks right out of the ISR
    goto ENDISR
    btfss PORTB,6
    goto DecSpeed ;check the slower button
    btfss PORTB,7
    goto IncSpeed ;check the faster button
    goto ENDISR

    bcf STATUS,C ;set the carry bit, it will clear if result<0
    rrf PORTA
    movlw 0x3F ;set the amount to change
    subwf SPEED ;subtract 0x3F from current speed setpoint
    movfw SPEED
    btfsc STATUS,C ;check to see if answer is<0
    goto ENDISR ;kick out
    movlw 0x01 ;if answer <0, move 0x01 into setpoint
    movwf SPEED
    goto ENDISR

    bsf STATUS,C ;set the C bit high
    rlf PORTA
    movlw 0x3F ;set the amount to change
    addwf SPEED ;increase the current setpoint by 0x3F
    movfw SPEED
    btfss STATUS,C ;check to see if answer>FF
    goto ENDISR ;kick out
    movlw 0xFE ;if answer>FF, move FE into setpoint
    movwf SPEED
    goto ENDISR

    bsf PORTB,2 ;turn on green LED
    movlw 0x7F ;start the fan at half speed
    movwf SPEED
    movlw 0x03
    movwf PORTA ;turn on two RED LEDS
    bcf PORTB,2 ;turn off green LED
    clrf PORTA
    bcf INTCON,RBIF ;clear flags
    bcf INTCON,T0IF
    retfie ;return from interrupt