//********************************************************************** // // LANC controller // //********************************************************************** // // Control a Sony DSC-V1 still camera with a 16f876A PIC // // Original code: Alberto Caballero Tante // //********************************************************************** // // This is a beta version of a Sony LANC controller. LANC output // is on bit 3 of PIC Port C. Connect the camera using a pullup // resistor. // // //********************************************************************** // // 21/10/2005: Beta version // //********************************************************************** // // Please any commets: translib@hotmail.com (subject "LANC controller"). // //********************************************************************** #include "F:\Alberto\Aplicaciones\CCS Compiler\PIC projects\test_lanc\serie01.h" int n_bits; int n_bytes; int counter; int recv_reg; int send_reg; int nEmpty; int status_byte; int send_command; int general_flags; int nB8Count; int nB8; int nA8Count; int nA8; int status_response; int nCmdCount; int special_word; int photo_flag; #define PORTA 0x05 #define PORTB 0x06 #define PORTC 0x07 #define TRISA 0x85 #define TRISB 0x86 #define TRISC 0x87 #define same 0x01 #define no_same 0x00 #define lanc 0x03 #define STATUS 0x03 #define RP0 0x05 #define RP1 0x06 #define msb 0x07 #define lsb 0x00 #define PC 0x02 #define Z_BIT 0x02 #define command_tx 0x00 #define TrisLanc 0x01 #define Tris 0x11 #define EMPTY_COUNT 0x1D #define BitStatus0 0x00 #define BitStatus1 0x02 #define BitStatus2 0x03 #define BitStatus3 0x04 #define BitStatus4 0x05 #define Bit0ECommand 0x01 #define Bit0 0x00 #define Bit1 0x01 #define Bit2 0x02 // Commands #define COMMAND_0E 0x0E #define PRE_CMD_1E 0x1E #define POWER_OFF 0x5E #define PHOTO 0x5A #define WORD1 0x43 #define WORD2 0x53 #define SEND_CMD 0x01 #define OUTPUT_PINS 0x00 #define INPUT_PINS 0xFF void main() { // Initialization // n_bits = 0; counter = 0; recv_reg = 1; send_reg = 0; nEmpty = 0; status_byte = 1; general_flags = 0; nB8 = 4; nB8Count = 0; nA8 = 3; nA8Count = 0; status_response = 1; special_word = 0x0f; nCmdCount = 10; send_command = 0; photo_flag =0; #asm bcf STATUS, RP0 bcf STATUS, RP1 clrf n_bytes sync_up: clrf counter sync_loop: btfss PORTC, lanc goto sync_up nop incfsz counter, same goto sync_loop synced: clrf n_bytes wait_strt: btfsc PORTC, lanc goto wait_strt start_bit: movlw 15 movwf counter l1: decfsz counter, same goto l1 btfsc PORTC, lanc goto error movlw 8 movwf n_bits wait_bits: movlw 15 movwf counter l2: decfsz counter, same goto l2 send_bits: btfss general_flags, command_tx goto wait_half btfss send_reg, lsb goto send_0 send_1: bcf PORTC, lanc bsf STATUS, RP0 movlw OUTPUT_PINS movwf TRISC //bcf STATUS, RP0 nop nop goto bit_sent send_0: bsf PORTC, lanc bsf STATUS, RP0 movlw INPUT_PINS movwf TRISC bcf STATUS, RP0 nop nop bit_sent: rrf send_reg, same movlw 10 goto jump_half wait_half: movlw 15 jump_half: movwf counter l3: decfsz counter, same goto l3 read_bits: rrf recv_reg, same bcf recv_reg, msb btfss PORTC, lanc bsf recv_reg, msb nop nop decfsz n_bits, same goto wait_bits wait_stop: movlw 14 movwf counter l4: decfsz counter, same goto l4 send_stop: btfss general_flags, command_tx goto wait_half_stop bsf PORTC, lanc bsf STATUS, RP0 movlw INPUT_PINS movwf TRISC bcf STATUS, RP0 bcf general_flags, command_tx movlw 11 goto jump_half_stop wait_half_stop: movlw 14 jump_half_stop: movwf counter l5: decfsz counter, same goto l5 read_stop: btfss PORTC, lanc goto error count: incf n_bytes, same decf n_bytes, w andlw 07 addwf PC, same goto check_0 goto check_1 goto check_2 goto check_3 goto check_4 goto check_5 goto check_6 goto check_7 ////////////////////////////////////////// // Byte 0 ////////////////////////////////////////// check_0: btfss send_command, 0 goto wait_strt send_command: movlw PHOTO movwf send_reg movlw SEND_CMD movwf general_flags goto wait_strt ////////////////////////////////////////// // Byte 1 ////////////////////////////////////////// check_1: btfss status_byte, BitStatus2 goto wait_strt send_43: // Send special values movf special_word, no_same movwf send_reg movlw SEND_CMD movwf general_flags goto wait_strt ////////////////////////////////////////// // Byte 2 ////////////////////////////////////////// check_2: btfsc status_byte, BitStatus0 goto status_0 btfsc status_byte, BitStatus1 goto status_1 btfsc status_byte, BitStatus3 goto status_2 goto wait_strt status_0: movlw 0 addwf recv_reg, no_same btfsc STATUS, Z_BIT incf nEmpty, same goto wait_strt status_1: movlw 0x47 subwf recv_reg, no_same btfsc STATUS, Z_BIT incf nB8Count goto wait_strt status_2: movlw 0x57 subwf recv_reg, no_same btfsc STATUS, Z_BIT incf nA8Count goto wait_strt ////////////////////////////////////////// // Byte 3 ////////////////////////////////////////// check_3: goto wait_strt check_4: goto wait_strt check_5: goto wait_strt check_6: goto wait_strt check_7: btfss status_byte, Bit0ECommand goto begin_spare // Send 0x0E command movlw COMMAND_0E movwf send_reg movlw SEND_CMD movwf general_flags // Time to check the state begin_spare: btfsc status_byte, BitStatus0 goto spare_status_0 btfsc status_byte, BitStatus1 goto spare_status_1 btfsc status_byte, BitStatus2 goto spare_status_2 btfsc status_byte, BitStatus3 goto spare_status_3 goto end_spare spare_status_0: movlw EMPTY_COUNT subwf nEmpty, no_same btfss STATUS, Z_BIT goto end_spare // Change status movlw 0x06 movwf status_byte // Send 0x0E command in next frame movlw COMMAND_0E movwf send_reg movlw SEND_CMD movwf general_flags goto end_spare spare_status_1: movf nB8, no_same subwf nB8Count, no_same btfss STATUS, Z_BIT goto end_spare // Change status movlw 0x0A movwf status_byte movlw 3 movwf nB8 clrf nB8Count //goto end_spare spare_status_2: // Send AC, AC btfsc status_response, Bit0 goto send_BC btfsc status_response, Bit1 goto send_AC1 btfsc status_response, Bit2 goto send_AC2 goto error send_BC: // Change state movlw 0x02 movwf status_response // Prepare to send movlw WORD1 movwf special_word goto end_spare send_AC1: // Change state movlw 0x04 movwf status_response // Prepare to send movlw WORD2 movwf special_word goto end_spare send_AC2: // Change state movlw 0x01 movwf status_response movlw 0x12 movwf status_byte // Prepare to send movlw WORD2 movwf special_word goto end_spare spare_status_3: // Read A8's movf nA8, no_same subwf nA8Count, no_same btfss STATUS, Z_BIT goto end_spare // Change status movlw 0x06 movwf status_byte movlw 3 movwf nA8 clrf nA8Count movlw 0xff movwf photo_flag; end_spare: btfss photo_flag, 0x00 goto sync_up //movlw 0 //movwf photo_flag; bcf status_byte, Bit0ECommand //bsf status_byte, BitStatus4 movlw 0xFF movwf send_command pre_cmd: // Send 0x1E pre-command byte movlw PRE_CMD_1E movwf send_reg movlw SEND_CMD movwf general_flags check_count: decfsz nCmdCount, same goto sync_up end_cmd: // No more command. Restore state movlw 10 movwf nCmdCount //bcf status_byte, BitStatus4 movlw 0 movwf send_command bsf status_byte, Bit0ECommand movlw COMMAND_0E movwf send_reg movlw SEND_CMD movwf general_flags movlw 0 movwf photo_flag goto sync_up luz: #endasm output_b(0xff); #asm goto fin error: #endasm while(1) { output_b(0xff); delay_ms(500); output_b(0x00); delay_ms(500); } #asm error2: #endasm while(1) { output_b(0xff); delay_ms(250); output_b(0x00); delay_ms(250); } #asm fin: #endasm }