> > Last night insomnia set in, and I was thinking about all the really obscure > factoids you have to keep in mind to use a PIC. These are the things that > newbies get stumped on, and end by gnashing their teeth, rending their > clothes, and pulling out their hair. That's why most PIC experts are bald. > (;-) I wondered if List Members could add enough obscure factoids to reach > 100? Most of us know these by heart, through long arduous trial and error, > and never think about them twice anymore. > Ok, I have one for you. Maybe the list could explain to me (1 year newbie) why this one didn't work. Maybe it has (or will) bitten someone else before. I was trying the "light the LEDS to = the A2D voltage" exercise I'm sure most go through. When I started the A2D conversion, the way I readit, the A2D needs a minimum aquisition time, that one needs to calculate depeding on the clock freq. and how you assign it etc. Looking at the examples, it seemed that with a 4 mhz clock, it would need at least a dozen usecs or so. Since this was just an exercise, I figured I'd give it plenty of time (2 msecs) to do the aquisition. So after starting up the conversion, I called my delay routine, and then tried to read and display the ref in binary on the LEDS. The offending lines are in the code below where the comment has a big NO!!. Once I commented out those two lines the code started "working". (i.e. I had to complement the results of the A2D to get the right LEDS to light :-) ). Took me 5 days of pain to figure this one out, since the Simulator didn't show it. Sorry for the rather long post. Any general code comments (e.g. style or efficiency, reuseability, etc.) are welcome. Steve ; * ; Filename: a2d2.asm * ; Date: Wed Jul 25 13:48:42 CDT 2001 * ; Author: -sm. * ;********************************************************************** ;#DEFINE DEBUG list p=16c716 ; list directive to define processor #include "p16c716.inc" ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _HS_OSC ;errorlevel -302 ; suppress message 302 from list file ;***** VARIABLE DEFINITIONS loop_cnt equ 0x20 ; Delay Counter (times thru delay loops) delay_1 equ loop_cnt +1 ; loop 1 delay_2 equ loop_cnt +2 ; loop 2 delay_3 equ loop_cnt +3 ; loop 3 delay_4 equ loop_cnt +4 ; loop 4 results equ loop_cnt +5 ; sent to LEDS ;********************************************************************** ORG 0x000 goto main ; go to beginning of program main bsf STATUS,RP0 ; set file register bank to 1 clrf PORTA movlw b'00000010' ; RA1 (pin 18)input, others outputs port A movwf TRISA; clrf PORTB movlw b'00000000' ; Port B all outputs movwf TRISB; bcf INTCON, GIE ; Don't bug me-all interuppts disabled movlw b'00000000' ; port A analog inputs Vref internal = Vdd movwf ADCON1 bcf STATUS,RP0 ; set file register bank to 0 movlw b'11001001' ; clock int osc, AN1 selected, a2d on movwf ADCON0 clrf ADRES clrf PORTB ; LEDS on movlw 0xfa call delay begin movlw 0xff movwf PORTB ; LEDS off movlw 0xff ; a little delay to catch leds call delay bsf ADCON0, GO ; start the A2D conversion ; BUG or anomaly next two lines ;movlw 0x02 ; give a2d some time to aquire (~2 msec) NO!!! ;call delay testbit btfss ADCON0, NOT_DONE goto testbit ; a2d complete done flag is cleared movf ADRES, w movwf results comf results, 1 ;Change 1 to 0s for lighting LEDS movf results, w movwf PORTB movlw 0xfa call delay goto begin ;********************************************************************** ; delay.asm ; with clock at 4 Mhz ; with loop_cnt = 0a, delay_X = 1 time = 165usecs ; with loop_cnt = 0a, delay_X = ff time = 30.64msecs ; with loop_cnt = 3a, delay_X = ff time = 177.7smsecs ; with loop_cnt = 4a, delay_X = ff time = 226.74msecs ; with loop_cnt = 4a, delay_X = f0 time = 213.42msecs ; with loop_cnt = 4a, delay_X = fe time = 224.97msecs ; * ;********************************************************************** delay IFDEF DEBUG movlw 0x01 ; ENDIF movwf loop_cnt delay_loop IFDEF DEBUG movlw 0x02 ; ELSE movlw 0xfd ; ENDIF movwf delay_1 movwf delay_2 movwf delay_3 movwf delay_4 dlay1 decfsz delay_1, 1 goto dlay1 dlay2 decfsz delay_2, 1 goto dlay2 dlay3 decfsz delay_3, 1 goto dlay3 dlay4 decfsz delay_4, 1 goto dlay4 decfsz loop_cnt, 1 goto delay_loop return END ; directive 'end of program' -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body