I tried interfacing a simple ADC through 18F4550 to Matlab to implement a certain application, but I ended up with odd results.
A 1k trim pot connected to AN0 on portA in this fashion (Vcc- 1k fixed resistor - 1 k Pot- Ground) the variable terminal of pot to AN0
here is the PIC code ( in simple assembly)
The Matlab side
the result i get is more than a byte and odd characters.
Can some one please tell me what I did wrong.
I then want to send data from Matlab to PIC and get its out put on port B which would be connected to DAC0808 to a LM358 so that i can measure the appopiate voltage, by changing it in Matlab.
A 1k trim pot connected to AN0 on portA in this fashion (Vcc- 1k fixed resistor - 1 k Pot- Ground) the variable terminal of pot to AN0
here is the PIC code ( in simple assembly)
Code:
;=========================
; setup and configuration
;=========================
;specify processor, oscillator type external =20MHz, code protection off
list p=18f4550
#include p18f4550.inc
config FOSC=HS
; config FOSCS=off
config wdt=off
config pwrt=on
;=====================================================
; PIC register equates
R1 EQU 100H
R2 EQU 101H
R3 EQU 102H
;=====================================================
org 0 ; start at address
goto main
; Space for interrupt handler, change interupt address for other processor
org 0x08
main
BCF TRISC,TX
BSF TRISC,RX
MOVLW B'00100000'
MOVWF TXSTA
MOVLW B'00010000'
MOVWF RCSTA
MOVLW B'00000000'
MOVWF BAUDCON
BSF TRISA,0
MOVLW D'2'
MOVWF SPBRG
BSF RCSTA,SPEN
MOVLW b'00000001'
MOVWF ADCON0
MOVLW b'00001110'
MOVWF ADCON1
MOVLW b'00101101'
MOVWF ADCON2
AGAIN
CLRF TXREG
CLRF ADRESH
BSF ADCON0,DONE
HERE
BTFSC ADCON0,DONE
BRA HERE
MOVFF ADRESH, TXREG
HERE2 BTFSS PIR1,TXIF
BRA HERE2
CALL DELAY
BRA AGAIN
DELAY
MOVLW D'10'
MOVWF R1
D1 MOVLW D'200'
MOVWF R2
D2 MOVLW D'200'
MOVWF R3
D3 DECF R3,F
BNZ D3
DECF R2,F
BNZ D2
DECF R1,F
BNZ D1
RETURN
;============================================================
end ; END OF PROGRAM
;============================================================
The Matlab side
Code:
a=0;
while a==0
SerPIC=serial('COM1'); %<--change this appropriately
set(SerPIC,'BaudRate', 115200, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none');
fopen(SerPIC); %--open the serial port to the PIC
display('RECEIVED = ');
xyz=fscanf(SerPIC,'%c')
display('next\n');
fclose(SerPIC) %--close the serial port when done
delete(SerPIC)
clear SerPIC
end
the result i get is more than a byte and odd characters.
Can some one please tell me what I did wrong.
I then want to send data from Matlab to PIC and get its out put on port B which would be connected to DAC0808 to a LM358 so that i can measure the appopiate voltage, by changing it in Matlab.