PDA

View Full Version : PIC18F4620 , what's wrong with my A/D converter code



Porcusoru
- 1st July 2009, 23:31
Hello everyone, I'm new to this forum.
My project is due in 1 week and I can't figure out why my code doesn't work. I'm doing an analog to digital conversion from the port AN0 and I must send it via the USART. The rate of transfer must be 19200. When I program it in the microcontroller it doesn't send anything through the serial port. PLS HELP !!! Thank you 

list p=18f4620 ; type
include <P18f4620.INC>
org 20h

Initialisation
movlw B'11101111'; RC6 and 7 must be set to 1(it's in the datasheet)
movwf TRISC

movlw 0x02; 0x02 =19200 bps (calculated for 4mhz clock, it’s in the datasheet)
movwf SPBRG ; register for setting the transfer rate

movlw B'00100100' ;
movwf TXSTA; activate asynch transmition
movlw B'10000000'
movwf RCSTA ; activate serial port for receiving (i dont know if this is necessary


movlw B'00000001'
movwf ADCON0 ; AN0 selected, idle state, A/D enabled

movlw B'00000000' ; VDD and VSS set as references
movwf ADCON1 ;

movlw B'00001000' ; small endian(only interested in ADRESH), aquisition time 2AD, aquisition clock FOSC/2 (This might be the problem)
movwf ADCON2

Main
call conversion
goto Main

conversion
bsf ADCON0,GO; starts the conversoin
Wait
btfsc ADCON0,GO; waits for the conversion to end
goto Wait

movf ADRESH,W ; moves the 8 most significant bits in W
movwf TXREG ; W to TXREG, serial transmition register

Transmisiune
btfss TXSTA,TRMT; it TRMT is 1 the transmision is done
goto Transmisiune
return

end

ScaleRobotics
- 2nd July 2009, 17:40
Hello Porcusoru,

It looks like your code is suffering from a general lack of PicBasic commands. I personally, would throw a lot of the picbasic commands in, and take most of the assembly out. For instance, debug, serout or serout2 should solve the serial problem, and ADCIN pretty much would solve the rest. But your professor may have other ideas.

Porcusoru
- 2nd July 2009, 20:35
U were kind of right. I addes this lines and now the b*tch works just fine :D

CONFIG WDT=OFF; disable watchdog timer
CONFIG MCLRE=ON;
CONFIG DEBUG=ON;
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG OSC = INTIO67; (INTO67 was NOT in the 400 page datasheet, i found it somewhere else)