Thanks to all your help, the screen is finally working. In case anyone is interested, here's the working code:
Code:
#DEFINE USE_LCD_FOR_DEBUG ; comment out for non-debug use
' ***************************************************************
' Pin Connections
' ***************************************************************
' Vdd -> pin 1 -> +5V
' RC5/Rx -> pin 5 -> EUSART receive
' RC4/Tx -> pin 6 -> EUSART transmit (LCD)
' RC2 -> pin 8 -> Port motor direction signal (motor 2)
' RC1/CCP4 -> pin 9 -> Port motor PWM output (motor 2)
' RC0 -> pin 10 -> Stbd motor direction signal (motor 1)
' RA2/CCP3 -> pin 11 -> Stbd motor PWM output (motor 1)
' RA1 -> pin 12 -> trim pot input
' Vss -> pin 14 -> GND
DEFINE OSC 16 ; Set oscillator 16Mhz
' USART Settings for Tx/Rc (e.g. LCD)
' > use Mister E PIC Multi-Calc application to get register/DEFINE settings
' > as the values are dependent on the OSC and baud rate
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 130 ' 2400 Baud @ 16MHz, 0.0%
' ***************************************************************
' Device Fuses
' ***************************************************************
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
__config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
#ENDCONFIG
' ***************************************************************
' Initialization
' ***************************************************************
OSCCON = %01111000 ; 16MHz internal osc
pause 100
APFCON0.2 = 0 ; Tx on RC4 for LCD display
APFCON0.7 = 0 ; Rx on RC5
BAUDCON.4 = 1 ; Transmit inverted data to the Tx pin
' From Mister E's Multi-Calc for EUSART:
SPBRGH = 6
BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
ANSELC = 0 ; Digital only for all PortC pins
TRISC = 0 ; Make all PORTC pins output
TRISA = %00000010 ; Make all pins output except for RA1
; (trim pot input)
ANSELA = %00000010 ; Analog on PORTA.1 (AN1) only
FVRCON = 0 ; Fixed Voltage Reference is disabled
ADCON0 = %00000101 ; ADC enabled on AN1 (RA1) only; ADC conversion
; enabled
PAUSEUS 20 ; wait for the analog switch 'glitch' to die down
ADCON1 = %00110000 ; Left-justified results in 8-bits; Frc as timer
#IFDEF USE_LCD_FOR_DEBUG
' Display control codes for PC1602LRU-XWA serial LCD
' (see 'Dropbox\PBP Projects\PIC Datasheets\PC1602LRU-XWA LCD Datasheet.pdf'
' for list of control codes)
LCD_HOME CON 1 ' Cursor home
LCD_CLR CON 12 ' Clear screen
LCD_HIDE_CRSR CON 4 ' Hide cursor
LCD_NEXT_LINE CON 13 ' Move cursor to the first position of the next line
#ENDIF
MotorDuty VAR BYTE ; Actual duty cycle for motor
ADCInVal VAR BYTE ; stores ADCIN result read from trim pot
compVal VAR BYTE ; stores last-changed ADC value
MinDuty CON 100 ; Minimum speed to rotate motor for this application
MaxDuty CON 252
MaxADCVal CON 255 ; 255 for 8-bit; 1023 for 10-bit
#IFDEF USE_LCD_FOR_DEBUG
pause 1000
HSEROUT [LCD_CLR]
pause 5
HSEROUT ["LCD Init",LCD_NEXT_LINE]
pause 5
HSEROUT ["PC1602LRU-XWA"]
pause 1500
#ENDIF
gosub Do_ADC
compVal = ADCInVal ; set initial compare value
GOSUB Map_ADC_Val_to_PWM_Duty
Main:
gosub Do_ADC
pause 100
If ADCInVal <> compVal Then
compVal = ADCInVal
GOSUB Map_ADC_Val_to_PWM_Duty
endif
GOTO Main
Do_ADC:
PAUSEUS 50 ' Wait for A/D channel acquisition time
ADCON0.1 = 1 ' Start conversion
WHILE ADCON0.1 = 1 ' Wait for it to complete
WEND
ADCInVal = ADRESH
return
Map_ADC_Val_to_PWM_Duty:
' Arduino Map function to emulate:
' ===============================
' map(value, fromLow, fromHigh, toLow, toHigh)
' long map(long x, long in_min, long in_max, long out_min, long out_max)
' {
' return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
' }
MotorDuty = (compVal - 0) * (MaxDuty - MinDuty)/(MaxADCVal - 0) + MinDuty
#IFDEF USE_LCD_FOR_DEBUG
HSEROUT [LCD_CLR]
pause 5
HSEROUT ["compVal=",DEC compVal," ",LCD_NEXT_LINE]
pause 5
HSEROUT ["MotorDuty=",DEC MotorDuty," "]
#ENDIF
RETURN
Bookmarks