Kar Yee
Newbie level 1
I'm currently studying Diploma in Electronic & Telecommunication Engineering. My project title is 12V DC Geared Motor Speed Control using Microcontroller PIC16F877A. In this project, a 12V DC Geared Motor with Encoder, bought from Cytron Technologies is being used. It has 2 channel, which are Channel A and Channel B. In this case, i need some help in writing assembly code for the encoder.
I need some help in my final year project. My project works this way:
Visual Basic 6.0 is being used to enter the desired speed by user and calculate the pulse. Then, send the desired speed to the microcontroller and the encoder will calculate the speed. It will feedback the speed to the microcontroller and the microcontroller will adjust the speed according to the speed entered by the user.
Thank you!
Here are the attachment:
I need some help in my final year project. My project works this way:
Visual Basic 6.0 is being used to enter the desired speed by user and calculate the pulse. Then, send the desired speed to the microcontroller and the encoder will calculate the speed. It will feedback the speed to the microcontroller and the microcontroller will adjust the speed according to the speed entered by the user.
Thank you!
Here are the attachment:
Code:
List p=16f877a;
ERRORLEVEL-302
ERRORLEVEL-305
#include <p16f877a.inc>;
__CONFIG 0x3D72; HS MODE, WDT OFF, BOREN
;================MACRO CHANGE BANK========================
BANK0 MACRO ; Change to BANK0
BCF STATUS,RP0;
BCF STATUS,RP1;
ENDM
BANK1 MACRO ; Change to BANK1
BSF STATUS,RP0;
BCF STATUS,RP1;
ENDM
;===========================================================================
; Set up initialize value and cblock for the variable needed.
BAUD_RATE EQU 0X81; Set the baud rate 9600 for 20MHz for USART Communication
CBLOCK 0X20;
D1; For Delay
D2; For Delay
D3; For Delay
X1; For Counter
X2; For Gain
REFERENCE ; For Reference Speed
CHANGE ; For error correction
ENDC
;-------------------------------------------------------------------------------------------------------
ORG 0X00
GOTO MAIN;
ORG 0X04;
GOTO INT; Interrupt process
;=======================MAIN PROGRAM=======================
MAIN BANK0
CALL INIT; Initialization
STANDBY CALL SERIALSCAN; Get data for reference speed
MOVF REFERENCE,W;
SUBLW 0X00;
BTFSC STATUS,Z; Reference speed =0?
GOTO STANDBY; Yes. Get data again
MOVLW D'30'; Set Timer1 as 30*13.107ms = 0.39321 where X2 as gain
MOVWF X2;
;==================RA0- AS SPEED COUNTER=====================
LOOP BTFSS PORTA,5; Channel A of encoder is chosen
GOTO $-1;
CALL CHECKNOISE;
BTFSC PORTA,5;
GOTO $-1;
BTFSS PIR1,TMR1IF;
GOTO LOOP;
; Check Noise function
CHECKNOISE CALL DELAY1;If bandwidth is less than 1ms, then it is noise
BTFSC PORTA,5;
INCF X1,1; Not noise, increase counter
RETURN
;============================================================
;###################### INITIALIZATION##############################
INIT BANK0;
CLRF PORTA;
CLRF PORTB;
CLRF PORTC;
CLRF PORTD;
CLRF X1;
CLRF X2;
CLRF REFERENCE;
;-----------------Initialize PORT------------------------------
BANK1
MOVLW 0X06;
MOVWF ADCON1; PortA as digital input
MOVLW 0XFF; PortA as input
MOVWF TRISA;
MOVLW 0X00; Set PORTC as output
MOVWF TRISC;
BANK0
BSF PORTC,4; RC4, RC5 (pin 23&24)connect to Input 1&2 (pin 2&7) of L293D
BCF PORTC,5;
;---------Initialize PWM-------------------------------
BANK1
MOVLW 0XFF; PWM Setup: Period kHz(19.152kHz)
MOVWF PR2;
BCF TRISC,1;
BCF TRISC,2;
BANK0
CLRF TMR2;
MOVLW 0X00; Duty Cycle = 0%
MOVWF CCPR1L;
MOVLW 0X04; ON TMR2, Prescale = 1
MOVWF T2CON;
MOVLW 0X0C;
MOVWF CCP1CON; PWM Mode
;--------Set TIMER as TIMER MODE--------------------------
BANK0
MOVLW 0X00;
MOVWF TMR1H; Timer = 0000-FFFFH =65535*1/(20M/4) = 13.107ms
MOVLW 0X00;
MOVWF TMR1L;
MOVLW B'00000001'; Pre = 1:1, TMR1 = Int, TMR1 = ON
MOVWF T1CON;
BANK1
MOVLW B'00000001'; TMR1IE = 1
MOVWF PIE1;
BANK0
MOVLW B'11000000'; GIE = 1, PEIE = 1
MOVWF INTCON
;---------Set up Serial Port-------------------------------
SERIAL_SETUP: BANK1;
MOVLW 0XC0; RC6&RC7 ->Input, Others output
IORWF TRISC,F; Keep in file register
MOVLW BAUD_RATE;
MOVWF SPBRG;
MOVLW 0X24;
MOVWF TXSTA; Enable transmission & high baud rate
BANK0
MOVLW 0X90;
MOVWF RCSTA; Enable serial port & continuous reception
RETURN
;####################END OF INITIALIZATION#########################
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^SUBROUTINE^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;-------------CORRECTION OF OVERRUN ERROR----------------
OVERRUN_ERROR BCF RCSTA,CREN; Disable continuous reception
BSF RCSTA,CREN; Enable continuous reception
RETURN
;------------CORRECTION OF FRAMMING ERROR----------------
FERR_ERROR MOVF RCREG,W; Discard Framming error
RETURN
;------------SEND THE DETECT SPEED TO PC-----------------
SERIAL_TRANSMIT: BTFSS PIR1,TXIF; Check if data TXREG is transfer to TSR -> TXREG is empty
GOTO $-1;
MOVWF TXREG
RETURN
;--------------GET DATA FOR REFERENCE SPEED--------------
SERIALSCAN CALL OVERRUN_ERROR; Correction of Overrun error
CALL FERR_ERROR; Correction of Framming error
BTFSS PIR1,RCIF; Check if data receive
GOTO $-1; Wait until new data
MOVF RCREG,W; Get received data to W
MOVWF REFERENCE;
RETURN
;---------------SHORT DELAY------------------------------
DELAY1 MOVLW D'5'; Pause for about 1ms
MOVWF D3;
MOVLW D'9';
MOVWF D2;
MOVLW D'36';
MOVWF D1;
DECFSZ D1;
GOTO $-1
DECFSZ D2;
GOTO $-5;
DECFSZ D3;
GOTO $-9;
RETURN
DELAY2 MOVLW D'255'; Pause for about 1ms
MOVWF D3;
MOVLW D'255';
MOVWF D2;
MOVLW D'36';
MOVWF D1;
DECFSZ D1;
GOTO $-1;
DECFSZ D2;
GOTO $-5;
DECFSZ D3;
GOTO $-9;
RETURN
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;**********************INTERRUPT PROCESS**************************
INT CLRF PIR1; Clear interruption flag
DECFSZ X2,F; Haven't reach 0.799527s
GOTO CONTINUE; Continue counter
GETDATA MOVF X1,W; Get value of counter, detect speed
SUBWF REFERENCE,W; Ref speed - Detect speed
MOVWF CHANGE; CHANGE = error correction
BTFSC STATUS,C; Reference < Detect?
GOTO GREATER; No. Jump to > or = check
LESS MOVF CHANGE,W;
ADDWF CCPR2L,F; Add duty cycle with error correction
GOTO SHOWSPEED;
GREATER BTFSC STATUS,Z; Reference = Detect?
GOTO SHOWSPEED; Yes, no need correction
MOVF CHANGE,W; Detect speed > ref speed, decrease duty cycle with error correction
SUBLW 0XFF; Convert the negative value to positive value
SUBWF CCPR2L,F;
SHOWSPEED MOVF X1,W;
CALL SERIAL_TRANSMIT;
CALL OVERRUN_ERROR; Correction of Overrun Error
CALL FERR_ERROR; Correction of Framming Error
CLRF X1; Reset Counter
MOVLW D'30'; Reset Timer
MOVWF X2;
CALL SERIALSCAN; Get data for reference speed
MOVF REFERENCE,W;
SUBLW 0X00;
BTFSS STATUS,Z; Reference speed = 0?
GOTO CONTINUE; No, continue the process
STOPMOTOR MOVLW 0X00; Yes. Stop motor
MOVWF CCPR2L;
CALL DELAY2;
CONTINUE RETFIE;
;*******************END OF INTERRUPT PROCESS*********************
END
;---------------------------------------END OF PROGRAM-------------------------------------