;////////////////////////////////////////////////////////////
;////////////////////////////////////////////////////////////

;Dies ist kein lauffaehiges Programm! Es dient nur dazu die 
;die Initialisierung von 2xPWM Modulen und des L298-Modules
;darzustellen.

;////////////////////////////////////////////////////////////
;////////////////////////////////////////////////////////////
   TITLE "RoboCar_20230811.ASM"
   NOLIST
;*********************************************************************
; Filename: RoboCar_20230811.ASM
; Author:   Ottmar
; Date:     2023.06.15
; Version   20230811
; Projekt:  Control of Robo-Car via Bluetooth
; MCU:      PIC1F1936 (28Pin)
; Info:     Program Memory Words used:  1294
;           Program Memory Words free:  6898
;           Average duration of program cycle
;           with debugging LCD-Routines:    ca.  38ms
;           without LCD-Routines       :    ca.  2.7-4ms
;
;*********************************************************************
; Hardware  PIC16F1936
;           INTOSC fosc=8MHz
;           Program Memory Words Used:  1295
;           Program Memory Words Free:  6897
;
;           HD44780-LCD4x16 2Wire
;           HC-05 Bluetooth transmitting and receiving module
;           HC-SR04 Ultrasonic module
;           2x Infrared distance sensing module
;           L298 Dual H-Bridge Dual Motor-Driver
;           Frontlight 2xLED white 20.000mcd
;           Backlight  2xLED red    4.000mcd
;           Horn (Piezo-Buzzer)
;           1Button (MCLRE Reset)
;
; PinOut    PORTA    L298_PORT
;           RA7:6    n.c.
;           RA5(07)  Frontlight 2x LED white
;           RA4(06)  Backlight  2x LED red
;           RA3(04)  L298,IN4 Drive2 (right)
;           RA2(03)  L298,IN3 "       "
;           RA1(02)  L298,IN2 Drive1 (left)
;           RA0(01)  L298,IN1 "       "
; 
;     PORTB (LCD_PORT, AD_PORT)
;           RB7(28)  LCD_Data  ICSPDAT 
;           RB6(27)  LCD_Clock ICSPCLK
;           RB5(26)  n.c -> AN13 measure Drive-Vdd  4x 3,2V
;           RB4(25)  n.c.-> AN11 measure MCU.Vdd MCU (5V)
;           RB3:0    n.c.
;
;     PORTC (SCSI_PORT, US_PORT, PWM_PORT)
;           RC7(18)  EUSART RX_DT Input  5V
;           RC6(17)  EUSART TX_CK Output 3,3V! -R1k2-TX_HC05-R2k2-Vss
;           RC5(16)  TRIG triggers US-Burst
;           RC4(15)  ECHO receoves US-ECHO
;           RC3(14)  IRL input from IR-Sensor left (low-active)
;           RC2(13)  CCP1 PWM1 output 
;           RC1(12)  CCP2 PWM2 output 
;           RC0(11)  IRR input from IR-Sensor right (low-active)
;
;*********************************************************************
;
;--MCU DEFINITION
   LIST     P=16F1936      ;list directive to define processor
   #INCLUDE <P16F1936.INC> ;processor specific variable definitions
   ;
;--MCU CONFIGURATION
;System-clock is INTOSC
   __CONFIG _CONFIG1, _FOSC_INTOSC &  _WDTE_OFF & _CP_OFF & _CPD_OFF & _MCLRE_ON & _PWRTE_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF;
   __CONFIG _CONFIG2, _PLLEN_OFF  & _LVP_OFF &_WRT_OFF & _VCAPEN_OFF & _STVREN_OFF & _BORV_19;
;  FOSC_INTOSC I int. osc.enabled I/O function on CLKIN pin
;  PLLEN_ON/OFF     4x PLL enabled/disabled
   ;
;*********************************************************************
;  LABELS & CONSTANTS
;*********************************************************************
;--L298_PORT (PORTA)           ;Drive direction for-/backward     
   L298_PORT   EQU   PORTA
   L298_LAT    EQU   LATA
   L298_ANSEL  EQU   ANSELA
   L298_TRIS   EQU   TRISA
   EN4         EQU   RA3      ;(05) 1        0
   EN3         EQU   RA2      ;(04) 0        1
   EN2         EQU   RA1      ;(03) 1        0
   EN1         EQU   RA0      ;(02) 0        1
   ; 
   ;constants to use like this (excample):
   ;movlw	FORWARD (BACKWARD, STOPCAR,ROTATE_L/R)
   ;BANKSEL L298_LAT
   ;movwf	L298_LAT
   ;
   FORWARD     EQU   b'00000101'    ;Literal to set Cmd_Drive
   BACKWARD    EQU   b'00001010'
   STOPCAR     EQU   b'00001111'  
   ;
   ROTATE_L    EQU   b'11111001'    ;rotate left on the spot
   ROTATE_R    EQU   b'11110110'    ;rotate right on the spot
   ;
;--PWM_PORT (PORTC)
   PWM_PORT    EQU   PORTC
   PWM_LAT     EQU   LATC
   PWM_TRIS    EQU   TRISC
   CCP1        EQU   RC2      ;(13)PWM2 output
   CCP2        EQU   RC1      ;(12)PWM1 output
   ;   
;*********************************************************************
;  INITILAIZATION OF PORTS AND MODULES
;*********************************************************************   
init:
;  Calculation of the multiplier, adapting delays to fosc used.
   FOSC        EQU    d'8'       ;fosc = 8MHz
   FOSC_MULT   EQU    FOSC/d'4'  ;instruction cycles -> fosc/4
   ;
;--INTOSC                                                
	BANKSEL	OSCCON			                        ;S.75 (PLL) 
	clrf	   OSCCON                                 ;S.82
	bcf		OSCCON,SPLLEN	   ;b7=0/1 =1 4xPLL is disabled/enabled
	bsf		OSCCON,IRCF3	   ;b6:3 =1 1110 IntOsc=8MHz[*4xPLL = 32MHz]
	bsf		OSCCON,IRCF2	   ;b5   =1
	bsf		OSCCON,IRCF1	   ;b4   =1
	bcf		OSCCON,IRCF0	   ;b3   =0   
							         ;b2   =0 unimplemented
	bsf		OSCCON,SCS1		   ;b1	=1 System Clock is int.osc.
	bcf		OSCCON,SCS0		   ;b0   =0 ignored if b1=1
	;
;-----------------------PORTA-----------------------------------------
;--L298_PORT (PORTA)    ;Antriebsteuerung vor/stop/rueckwaerts
   BANKSEL  L298_PORT
   clrf     L298_PORT
   BANKSEL  L298_LAT 
   clrf     L298_LAT 
   BANKSEL  L298_ANSEL        ;bank3
   clrf     L298_ANSEL 
   BANKSEL  L298_TRIS         ;bank1
   clrf     L298_TRIS
;  bcf      L298_TRIS,EN4     ;RA3(05)    -> L298_PORT
;  bcf      L298_TRIS,EN3     ;RA2(04)    "
;  bcf      L298_TRIS,EN2     ;RA1(03)    " 
;  bcf      L298_TRIS,EN1     ;RA0(02)    "

;--PWM_PORT (PORTC)    
;  BANKSEL  PWM_TRIS         ;RC7:0 set as digital input
   bsf      PWM_TRIS,CCP1    ;RC2(13) PWM1 output disabled (high imped.)
   bsf      PWM_TRIS,CCP2    ;RC1(12) PWM2 output disabled (high imped.)
   ;
;---------------------------------------------------------------------
;  PWM-MODULE INITIANLIZATION
;  ECCP1, ECCP2   PWM1, PWM2  ENHANCED PWM MODE             DS.218
;--------------------------------------------------------------------- 
;  The ENHANCED PWM mode generates a Pulse-Width Modulation (PWM) 
;  signal on up to four different output pins with up to 10 bits of
;  resolution. The period, duty cycle, and resolution are controlled
;  by the following registers:
;   PRx registers
;   TxCON registers
;   CCPRxL registers
;   CCPxCON registers
   ;
;  1. Enable TMR2,TMR4 noPre-/Postscaler                    DS.207 
   BANKSEL  T2CON          ;bank0
   movlw    b'00000100'    ;b2=1 TMR2/4 is on
   movwf    T2CON          ;-> CCP2
   BANKSEL  T4CON          ;bank8
   movwf    T4CON          ;-> CCP1
   ;
;  2. Load CCPRxL and DCxBx bits of CCPxCON with PWM duty cycle value.
   ;CCPRxL contains the eight MSbs, DCxB<1:0 the two LSbs
   ;in PWM mode, CCPRxH is a read-only register.
   BANKSEL  CCPR1L         ;bank5
   movlw    .178           ;Value set DC to 0% ->PWM OFF
   movwf    CCPR1L         ;CCPR1L:CCP1CON,DC1B
   movwf    CCPR2L         ;CCPR2L:CCP2CON,DC2B
   ;
;  3. Configure the ECCP modules to be usedOnce the SINGLE OUTPUT mode
;  is selected:
;  CCPxCON,CCPxM<3:2> = 11 b3:0 and
;  CCPxCON,PxM<1:0>   = 00 b7:6
;  The user firmware can bring out the same PWM signal 
;  to one, two, three or four output pins by setting the appropriate
;  STRx<D:A> bits of the PSTRxCON register, as shown in Table 22-8.
   ;
   BANKSEL  CCP1CON        ;bank5 CCPx CONTROL REGISTER      S.231
;  b7:6 00 Single output; PxA modulated; PxB, PxC, PxD assigned as port pins
;  b5:4=00 unused
;  b3:0 11xx = CCPxM<3:0> ECCPx Mode Select bits
   movlw    b'00001100'    ;PWM mode: PxA, PxC, PxB, PxD active-high
   movwf    CCP1CON        ;          P1A=RC2
   movwf    CCP2CON        ;          P2A=RC1 
;---------------------------  
;  The ECCP modules have the following additional PWM registers which
;  control Auto-shutdown, Auto-restart, Dead-band Delay and PWM
;  Steering modes:
;   CCPxAS autosutdown registers UNUSED!=0x00
;   PSTRxCON registers
;   PWMxCON registers
   ;
;  CCPxAS autoshutdown registers UNUSED!=0x00
   ;
;  22.4.6 PWM STEERING MODE^                                   DS.236  
   BANKSEL  PSTR1CON       ;bank5
   clrf     PSTR1CON
   bsf      PSTR1CON,STR1A ;b0=1 = P1A pin has the PWM waveform with
   clrf     PSTR2CON       ;polarity control from CCPxM<1:0>
   bsf      PSTR2CON,STR2A ;b0=P2A pin has the PWM waveform from 
   ;                       ;CCPxCON,CCPxM<1:0>
   ;  
;  The enhanced PWM module can generate the following five PWM
;  Output modes:
;   Single PWM
;   Half-Bridge PWM
;   Full-Bridge PWM, Forward Mode
;   Full-Bridge PWM, Reverse Mode
;   Single PWM with PWM Steering Mode

;  1. CCP1,2 pin output driver are disabled (vgl PWM:5_TRIS)
;  2. Load the PRx register with the PWM period value.
;  With fosc=8MHz and PRx <= d101 f_PWM =19.6kHz
;  max.PWM-Resolution <= 8bit. 
;  Therefore PRx-Value =<0x65 (d101)
   ;
   BANKSEL  PR2            ;bank0 TIMER 2 Period Register
   movlw   .101; .178       ;f_PWM=11,3kHz at fosc=8MHz (
   movwf    PR2            ;PWM-Periodregister TMR2
   BANKSEL  PR4            ;bank8
   movwf    PR4            ;PWM-Periodregister TMR4
   ;   
;  5. Select PWM-TIMER     
   BANKSEL CCPTMRS0        ;bank5
   clrf    CCPTMRS0        ;PWM TIMER SELECTION CNTRL REG    DS.231 
;  b7:6  00  unused
;  b5:4  00  unused
;  b3:2  00 = CCP2 is based off Timer 2 in PWM Mod
;  b1:0  01 = CCP1 is based off Timer 4 in PWM Mode
   movlw    b'00000001'
   movwf    CCPTMRS0 
   ;
 ; 6. Clear TMRx interrupt flag   ;                         DS.103/105
   BANKSEL  PIR1           ;bank0 Clear TMRxIF inter. flag 
   bcf      PIR1,TMR2IF    ;b1: TMR2 to PR2 Interrupt Flag bit
   bcf      PIR3,TMR4IF    ;b1  TMR4 to PR4 Interrupt Flag bit
   ;
;-------------------------------------------------------------
; PWM ENABLE
;------------------------------------------------------------
BANKSEL  PWM_TRIS          ;enable PWM
   bcf      PWM_TRIS,CCP2     ;RC1(12)
   bcf      PWM_TRIS,CCP1     ;RC2(13)
   BANKSEL  0
    ;
;--------------------------------------------------------------
; L298	STOP Motor 1 EN1=EN2,  STOP Motor 2 EN3=EN4
;--------------------------------------------------------------
   movlw   STOPCAR           ;EQU   b'00001111'
   BANKSEL L298_LAT
   movwf   L298_LAT
   ;
   GOTO main
;**************************************************************
;  MAIN PROGRAMM
;**************************************************************
main:

   GOTO main
;**************************************************************
   END