Device: PIC16F872 Re: couldnt get all channels to work together but it works well when I disable 3 of them and use only one. I grounded the pins that were floating already but still nothing happens. Here is the code: ;********************************************************************** ; * ; Filename: cere1.asm - Automation of Counting Fingerlings * ; Thesis' main microcontroller * ; * ; Date: Start-April 28, 2001 Latest Revision-May 20, 2001 * ; File Version: release 1.1 * ; * ; * ;********************************************************************** ; * ; Files required: p16f872.inc * ; * ; * ; * ;********************************************************************** ; * ; Notes: This application reads 4 inputs from 4 light sensors, * ; processes it, then decides whether to send a "count" * ; signal or not. * ; * ; Handshaking protocol in effect for light sensor to * ; microcontroller and microcontroller to microcontroller* ; signals. (for every sent/output signal, there will be * ; a corresponding receive/input signal) * ; * ; RA0:RA3 - sensor resets, parallel port (active low) * ; RA5 - "count" signal, parallel port * ; RB1:RB2, RB4:RB5 - sensor inputs * ; RB7 - "Received count" signal * ; 20 MHz clock; 5V supply * ; * ; Using an 20 MHz oscillator the PicMicrocontroller's * ; effective operation will be 5 MHz. * ; * ; @50 cycles/channel & 4 channels, there would be 25,000 * ; Loops / second * ; * ; Optimized values - TolON = 0x0136, compON = 0x19B1, * ; tolOFF = 0x0E8, Watcher = 0x08 * ; * ; define "_compON_ON" to enable compON operation * ; define "_test3_ON" to test channel 3 * ; define "_test4_ON" to test 1 channel (channel 4) operation* ; * ;********************************************************************** list p=16f872 ; list directive to define processor list r=DEC ; list directive to define default radix #include ; processor specific variable definitions ;***** CONSTANT DEFINITIONS HtolON EQU 0x00 ; set label to the minimum number of "gray" values, polled ; from the sensor, to count 1 fingerling LtolON EQU 0x0A HcompON EQU 0x66 ; set label to the total number of "gray" values, ; polled from the sensor while the status LcompON EQU 0xC4 ; is "on", to indicate another count tolOFF EQU 0x08 ; set label to the total number of consecutive "off" values ; polled from the sensor to indicate an "off" status Watcher EQU 0x03 ; set label to the frequency of sensor resets NewTRISA EQU 0xD0 ; set PORTA direction NewTRISB EQU 0xB6 ; set PORTB direction ;***** VARIABLE DEFINITIONS CBLOCK 0X020 iChOne:2, iChTwo:2, iChThree:2, iChFour:2 ; channel incrementors rChOne, rChTwo, rChThree, rChFour ; channel reset incrementors ChStatus ; channel status srChOne, srChTwo, srChThree, srChFour ; sensor resets w2:2 ; multipurpose 16 bit variable w3 ; multipurpose 8 bit variable ENDC ;***** MACRO DEFINITIONS inc16 Macro Reg ; increment 16 bit value incf Reg, f ; increment the Low byte btfsc STATUS, Z ; do we have zero (multiple of 256)? incf Reg + 1, f ; increment High byte (if necessary) endm equal16 Macro Reg1, Reg2 ; compare 16 bit values (equals) movf Reg2 + 1, w ; get the High byte of the result subwf Reg1 + 1, w movwf w3 ; store in a temporary register movf Reg2, w ; get the Low byte subwf Reg1, w btfss STATUS, C ; decrement High if necessary decf w3, f iorwf w3, w ; is the result == 0? endm equal Macro Reg1, Reg2 ; compare 8 bit values (x == y) movf Reg2, w subwf Reg1, w endm __CONFIG _CP_OFF & _DEBUG_OFF & _WDT_ON & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OFF ; '__CONFIG' directive is used to embed configuration data within .asm file. ; The lables following the directive are located in the respective .inc file. ; See respective data sheet for additional information on configuration word. ;********************************************************************** ORG 0x000 ; processor reset vector clrf PCLATH ; ensure page bits are cleared clrwdt ; clear the watchdog timer goto main ;subroutines Dlay16 ; 16 cycle delay call Dlay8 Dlay8 ; 8 cycle delay call Dlay4 Dlay4 ; 4 cycle delay return main ;initialize variables to zero bcf STATUS, RP0 bcf STATUS, RP1 ; select Bank 0 clrf iChOne clrf iChOne + 1 clrf iChTwo clrf iChTwo + 1 clrf iChThree clrf iChThree + 1 clrf iChFour clrf iChFour + 1 clrf srChOne clrf srChTwo clrf srChThree clrf srChFour clrf ChStatus ;initialize rCH????? movlw tolOFF movwf rChOne movwf rChTwo movwf rChThree movwf rChFour ;initialize interrupts clrf INTCON ; disable interrupts ;initialize OPTION_REG bsf STATUS, RP0 bcf STATUS, RP1 ; select Bank 1 movlw 0xFF ; set WDT prescaler to 1:128 movwf OPTION_REG ;initialize ports bcf STATUS, RP0 bcf STATUS, RP1 ; select Bank 0 clrf PORTA ; initialize PORTA by clearing output data latches movlw 0x00F movwf PORTA ; set sensor reset pins ;set port directions bsf STATUS, RP0 ; select Bank 1 movlw NewTRISA ; load PORTA direction movwf TRISA ^ 0X080 ; to TRISA, xored to ensure bank 1 movlw NewTRISB ; load PORTB direction movwf TRISB ^ 0X080 ; to TRISB, xored to ensure bank 1 bcf STATUS, RP0 ; return to bank 0 clrwdt ; clear the watchdog timer ;main loop MainLoop ifdef _test3_ON goto Test3 endif ifdef _test4_ON goto Test4 endif ;channel1 ;1 sensor reset nop movlw Watcher movwf w2 ; load w2 with Watcher equal srChOne, w2 ; is srChOne != w2? btfsc STATUS, Z goto sR1End1 ; no, goto sR1End1 goto sR1End2 ; yes, goto sR1End2 sR1End1 bcf PORTA, 0 ; clear sensor reset pin clrf srChOne ; clear srChOne sR1End2 inc16 iChOne ; increment iChOne incf srChOne, f ; increment srChOne btfsc PORTB, 1 ; is the sensor input High or Low? goto sensor1_ON ; High, goto sensor1_ON nop decfsz rChOne, f ; It's Low, decrement rChOne goto Ch1_nReset ; code != 0, goto Ch1_nReset clrf iChOne ; clear incrementor clrf iChOne + 1 movlw 0x0E andwf ChStatus, f ; clear status movlw tolOFF movwf rChOne ; load reset incrementor goto Pre_1Loop_End1 sensor1_ON movlw tolOFF movwf rChOne ; load reset incrementor Ch1_nReset movlw 0x01 ; test if status is "on" andwf ChStatus, w btfsc STATUS, Z ; execute following code if != 0 goto Ch1_OFF ; else, code == 0, status is "off" movlw HcompON ; load w2 with compON movwf w2 + 1 movlw LcompON movwf w2 equal16 iChOne, w2 ; test if iChone == compON btfss STATUS, Z ; execute following code if == 0 goto Pre_1Loop_End3 ; else, code != 0, goto Pre_1Loop_End3 ifdef _compON_ON bsf PORTA, 5 ; send "count" signal bcf PORTA, 0 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 else call Dlay4 nop endif clrf iChOne ; clear iChOne clrf iChOne + 1 goto Pre_1Loop_End2 Ch1_OFF movlw HtolON ; load w2 with tolON movwf w2 + 1 movlw LtolON movwf w2 equal16 iChOne, w2 ; test if iChOne == tolON btfss STATUS, Z ; execute following code if == 0 goto Pre_1Loop_End4 ; else, code != 0, goto Pre_1Loop_End4 movlw 0x01 iorwf ChStatus, f ; change channel status to "on" bsf PORTA, 5 ; send "count" signal bcf PORTA, 0 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 clrf iChOne ; clear iChOne clrf iChOne + 1 goto Ch1_Loop_End ;Pre_Loop_End ensures the same number of cycles for the loop regardless of conditions Pre_1Loop_End1 call Dlay8 call Dlay4 Pre_1Loop_End3 nop nop Pre_1Loop_End4 call Dlay4 nop nop Pre_1Loop_End2 nop nop goto Ch1_Loop_End Ch1_Loop_End nop nop nop bsf PORTA, 0 ; set signal to sensor and parallel port ;channel2 ;2 sensor reset nop movlw Watcher movwf w2 ; load w2 with Watcher equal srChTwo, w2 ; is srChTwo != Watcher? btfsc STATUS, Z goto sR2End1 ; no, goto sR2End1 goto sR2End2 ; yes, goto sR2End2 sR2End1 bcf PORTA, 1 ; clear sensor reset pin clrf srChTwo ; clear srChTwo sR2End2 inc16 iChTwo ; increment iChTwo incf srChTwo, f ; increment srChTwo btfsc PORTB, 2 ; is the sensor input High or Low? goto sensor2_ON ; High, goto sensor2_ON nop decfsz rChTwo, f ; It's Low, decrement rChTwo goto Ch2_nReset ; code != 0, goto Ch2_nReset clrf iChTwo ; clear incrementor clrf iChTwo + 1 movlw 0x0D andwf ChStatus, f ; clear status movlw tolOFF movwf rChTwo ; load reset incrementor goto Pre_2Loop_End1 sensor2_ON movlw tolOFF movwf rChTwo ; load reset incrementor Ch2_nReset movlw 0x02 ; test if status is "on" andwf ChStatus, w btfsc STATUS, Z ; execute following code if != 0 goto Ch2_OFF ; else, code == 0, status is "off" movlw HcompON ; load w2 with compON movwf w2 + 1 movlw LcompON movwf w2 equal16 iChTwo,w2 ; test if iChTwo == compON btfss STATUS, Z ; execute following code if == 0 goto Pre_2Loop_End3 ; else, code != 0, goto Pre_2Loop_End3 ifdef _compON_ON bsf PORTA, 5 ; send "count" signal bcf PORTA, 1 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 else call Dlay4 nop endif clrf iChTwo ; clear iChTwo clrf iChTwo + 1 goto Pre_2Loop_End2 Ch2_OFF movlw HtolON ; load w2 with tolON movwf w2 + 1 movlw LtolON movwf w2 equal16 iChTwo, w2 ; test if iChTwo == tolON btfss STATUS, Z ; execute following code if == 0 goto Pre_2Loop_End4 ; else, code != 0, goto Pre_2Loop_End4 movlw 0x02 iorwf ChStatus, f ; change channel status to "on" bsf PORTA, 5 ; send "count" signal bcf PORTA, 1 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 clrf iChTwo ; clear iChTwo clrf iChTwo + 1 goto Ch2_Loop_End ;Pre_Loop_End ensures the same number of cycles for the loop regardless of conditions Pre_2Loop_End1 call Dlay8 call Dlay4 Pre_2Loop_End3 nop nop Pre_2Loop_End4 call Dlay4 nop nop Pre_2Loop_End2 nop nop goto Ch2_Loop_End Ch2_Loop_End nop nop nop bsf PORTA, 1 ; set signal to sensor and parallel port Test3 ;channel3 ;3 sensor reset nop movlw Watcher movwf w2 ; load w2 with Watcher equal srChThree, w2 ; is srChThree != w2? btfsc STATUS, Z goto sR3End1 ; no, goto sR3End1 goto sR3End2 ; yes, goto sR3End2 sR3End1 bcf PORTA, 2 ; clear sensor reset pin clrf srChThree ; clear srChThree sR3End2 inc16 iChThree ; increment iChThree incf srChThree, f ; increment srChThree btfsc PORTB, 4 ; is the sensor input High or Low? goto sensor3_ON ; High, goto sensor3_ON nop decfsz rChThree, f ; It's Low, decrement rChThree goto Ch3_nReset ; code != 0, goto Ch3_nReset clrf iChThree ; clear incrementor clrf iChThree + 1 movlw 0x0B andwf ChStatus, f ; clear status movlw tolOFF movwf rChThree ; load reset incrementor goto Pre_3Loop_End1 sensor3_ON movlw tolOFF movwf rChThree ; load reset incrementor Ch3_nReset movlw 0x04 ; test if status is "on" andwf ChStatus, w btfsc STATUS, Z ; execute following code if != 0 goto Ch3_OFF ; else, code == 0, status is "off" movlw HcompON ; load w2 with compON movwf w2 + 1 movlw LcompON movwf w2 equal16 iChThree, w2 ; test if iChThree == compON btfss STATUS, Z ; execute following code if == 0 goto Pre_3Loop_End3 ; else, code != 0, goto Pre_3Loop_End3 ifdef _compON_ON bsf PORTA, 5 ; send "count" signal bcf PORTA, 2 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 else call Dlay4 nop endif clrf iChThree ; clear iChThree clrf iChThree + 1 goto Pre_3Loop_End2 Ch3_OFF movlw HtolON ; load w2 with tolON movwf w2 + 1 movlw LtolON movwf w2 equal16 iChThree, w2 ; test if iChThree == tolON btfss STATUS, Z ; execute following code if == 0 goto Pre_3Loop_End4 ; else, code != 0, goto Pre_3Loop_End4 movlw 0x04 iorwf ChStatus, f ; change channel status to "on" bsf PORTA, 5 ; send "count" signal bcf PORTA, 2 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 clrf iChThree ; clear iChThree clrf iChThree + 1 goto Ch3_Loop_End ;Pre_Loop_End ensures the same number of cycles for the loop regardless of conditions Pre_3Loop_End1 call Dlay8 call Dlay4 Pre_3Loop_End3 nop nop Pre_3Loop_End4 call Dlay4 nop nop Pre_3Loop_End2 nop nop goto Ch3_Loop_End Ch3_Loop_End nop nop nop bsf PORTA, 2 ; set signal to sensor and parallel port ifdef _test3_ON ifndef _test4_ON goto MainLoop endif endif Test4 ;channel4 ; 4 sensor reset nop movlw Watcher movwf w2 ; load w2 with Watcher equal srChFour, w2 ; is srChFour != w2? btfsc STATUS, Z goto sR4End1 ; no, goto sR4End1 goto sR4End2 ; yes, goto sR4End2 sR4End1 bcf PORTA, 3 ; clear sensor reset pin clrf srChFour ; clear srChFour sR4End2 inc16 iChFour ; increment iChFour incf srChFour, f ; increment srChFour btfsc PORTB, 5 ; is the sensor input High or Low? goto sensor4_ON ; High, goto sensor4_ON nop decfsz rChFour, f ; It's Low, decrement rChFour goto Ch4_nReset ; code != 0, goto Ch4_nReset clrf iChFour ; clear incrementor clrf iChFour + 1 movlw 0x07 andwf ChStatus, f ; clear status movlw tolOFF movwf rChFour ; load reset incrementor goto Pre_4Loop_End1 sensor4_ON movlw tolOFF movwf rChFour ; load reset incrementor Ch4_nReset movlw 0x08 ; test if status is "on" andwf ChStatus, w btfsc STATUS, Z ; execute following code if != 0 goto Ch4_OFF ; else, code == 0, status is "off" movlw HcompON ; load w2 with compON movwf w2 + 1 movlw LcompON movwf w2 equal16 iChFour, w2 ; test if iChFour == compON btfss STATUS, Z ; execute following code if == 0 goto Pre_4Loop_End3 ; else, code != 0, goto Pre_4Loop_End3 ifdef _compON_ON bsf PORTA, 5 ; send "count" signal bcf PORTA, 3 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 else call Dlay4 nop endif clrf iChFour ; clear iChFour clrf iChFour + 1 goto Pre_4Loop_End2 Ch4_OFF movlw HtolON ; load w2 with tolON movwf w2 + 1 movlw LtolON movwf w2 equal16 iChFour, w2 ; test if iChFour == tolON btfss STATUS, Z ; execute following code if == 0 goto Pre_4Loop_End4 ; else, code != 0, goto Pre_4Loop_End4 movlw 0x08 iorwf ChStatus, f ; change channel status to "on" bsf PORTA, 5 ; send "count" signal bcf PORTA, 3 ; send signal to sensor and parallel port btfss PORTB, 7 ; wait for "received" signal goto $-1 bcf PORTA, 5 clrf iChFour ; clear iChFour clrf iChFour + 1 goto Ch4_Loop_End ;Pre_Loop_End ensures the same number of cycles for the loop regardless of conditions Pre_4Loop_End1 call Dlay8 call Dlay4 Pre_4Loop_End3 nop nop Pre_4Loop_End4 call Dlay4 nop nop Pre_4Loop_End2 nop nop goto Ch4_Loop_End Ch4_Loop_End bsf PORTA, 3 ; set signal to sensor and parallel port clrwdt ; clear the watchdog timer goto MainLoop ; loop back and process the channels again END ; directive 'end of program' Hope this helps. Thanks! __________________________________________________ FREE voicemail, email, and fax...all in one place. Sign Up Now! http://www.onebox.com -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu