diff --git a/AVCLan_mini/AVCLanDrv.cpp b/AVCLan_mini/AVCLanDrv.cpp index 112a06a..d1fc9d9 100644 --- a/AVCLan_mini/AVCLanDrv.cpp +++ b/AVCLan_mini/AVCLanDrv.cpp @@ -7,45 +7,25 @@ void AVCLanDrv::begin () { // AVCLan TX+/TX- read line INPUT cbi(DATAIN_DDR, DATAIN); -#ifdef AVCLAN_ST485 - sbi(DATAIN_PORT, DATAIN); -#else cbi(DATAIN_PORT, DATAIN); -#endif - // AVCLan TX+/TX- write line OUTPUT -#ifdef AVCLAN_RESISTOR - cbi(DATAOUT_DDR, DATAOUT); - cbi(DATAOUT_PORT, DATAOUT); - cbi(ADCSRB, ACME); // Analog Comparator Multiplexer Enable - NO - cbi(ACSR, ACIS1); // Analog Comparator Interrupt Mode Select - cbi(ACSR, ACIS0); // Comparator Interrupt on Output Toggle - cbi(ACSR, ACD); // Analog Comparator Disbale - NO -#else -#ifdef AVCLAN_ST485 - sbi(DATAOUT_DDR, DATAOUT); - sbi(OUTEN_DDR, OUTEN); - AVC_OUT_DIS; - OUTPUT_SET_0; -#else - //avclan driver on PCA82C250 & LM239N + //avclan driver on PCA82C250 sbi(DATAOUT_DDR, DATAOUT); AVC_OUT_DIS; OUTPUT_SET_0; -#endif -#endif + // timer2 setup, prescaler factor - 8 #if defined(__AVR_ATmega8__) // ASSR=0x00; TCCR2 = 0x02; - // TCNT0=0x00; + // TCNT2=0x00; // OCR2=0x00; #else // ATMega168 // ASSR=0x00; // TCCR2A=0x00; - TCCR0B = 0x02; - // TCNT0=0x00; + TCCR2B = 0x02; + // TCNT2=0x00; // OCR2A=0x00; // OCR2B=0x00; #endif @@ -75,22 +55,19 @@ word AVCLanDrv::readBits (byte nbBits) } // Reset timer to measure bit length. - TCNT0 = 0; + TCNT2 = 0; // Wait until falling edge. while (INPUT_IS_SET); // Compare half way between a '1' (20 us) and a '0' (32 us ): 32 - (32 - 20) /2 = 26 us - if (TCNT0 < AVC_BIT_0_HOLD_ON_MIN_LENGTH) { + if (TCNT2 < AVC_BIT_0_HOLD_ON_MIN_LENGTH) { // Set new bit. data |= 0x0001; // Adjust parity. _parityBit = !_parityBit; } } - while (INPUT_IS_CLEAR && TCNT0 < AVC_NORMAL_BIT_LENGTH); + while (INPUT_IS_CLEAR && TCNT2 < AVC_NORMAL_BIT_LENGTH); - // char buff[10] = {0}; - // sprintf(buff, "%x", data ); - // // bSDLog.logs( buff ); return data; } @@ -106,18 +83,18 @@ byte AVCLanDrv::_readMessage () // Start bit. while (INPUT_IS_CLEAR); - TCCR0B = 0x03; // prescaler 32 - TCNT0 = 0; + TCCR2B = 0x03; // prescaler 32 + TCNT2 = 0; // Wait until falling edge. while (INPUT_IS_SET) { - t = TCNT0; + t = TCNT2; if (t > 0xFF) { - TCCR0B = 0x02; // prescaler 8 + TCCR2B = 0x02; // prescaler 8 SREG = oldSREG; return 1; } } - TCCR0B = 0x02; // prescaler 8 + TCCR2B = 0x02; // prescaler 8 if (t < AVC_START_BIT_HOLD_ON_MIN_LENGTH) { //if (t < 0x16){ @@ -231,17 +208,17 @@ byte AVCLanDrv::readMessage () ////-------------------------------------------------------------------------------- //{ // // Reset timer to measure bit length. -// TCCR0B = 0x03; // prescaler 32 -// TCNT0 = 0; +// TCCR2B = 0x03; // prescaler 32 +// TCNT2 = 0; // OUTPUT_SET_1; // // // Pulse level high duration. -// while ( TCNT0 < AVC_START_BIT_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_START_BIT_HOLD_ON_LENGTH ); // OUTPUT_SET_0; // // // Pulse level low duration until ~185 us. -// while ( TCNT0 < AVC_START_BIT_LENGTH ); -// TCCR0B = 0x02; // prescaler 8 +// while ( TCNT2 < AVC_START_BIT_LENGTH ); +// TCCR2B = 0x02; // prescaler 8 // //} @@ -251,17 +228,17 @@ void AVCLanDrv::send1BitWord (bool data) //-------------------------------------------------------------------------------- { // Reset timer to measure bit length. - TCNT0 = 0; + TCNT2 = 0; OUTPUT_SET_1; if (data) { - while (TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH); + while (TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH); } else { - while (TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH); + while (TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH); } OUTPUT_SET_0; - while (TCNT0 < AVC_NORMAL_BIT_LENGTH); + while (TCNT2 < AVC_NORMAL_BIT_LENGTH); } //// Send a 4 bit word to the AVCLan @@ -274,20 +251,20 @@ void AVCLanDrv::send1BitWord (bool data) // // Most significant bit out first. // for ( char nbBits = 0; nbBits < 4; nbBits++ ) { // // Reset timer to measure bit length. -// TCNT0 = 2; +// TCNT2 = 2; // OUTPUT_SET_1; // // if (data & 0x8) { // // Adjust parity. // _parityBit = ! _parityBit; -// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH ); // } else { -// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH ); // } // // OUTPUT_SET_0; // // Hold output low until end of bit. -// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); +// while ( TCNT2 < AVC_NORMAL_BIT_LENGTH ); // // // Fetch next bit. // data <<= 1; @@ -304,20 +281,20 @@ void AVCLanDrv::send1BitWord (bool data) // // Most significant bit out first. // for ( char nbBits = 0; nbBits < 8; nbBits++ ) { // // Reset timer to measure bit length. -// TCNT0 = 2; +// TCNT2 = 2; // OUTPUT_SET_1; // // if (data & 0x80) { // // Adjust parity. // _parityBit = ! _parityBit; -// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH ); // } else { -// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH ); // } // // OUTPUT_SET_0; // // Hold output low until end of bit. -// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); +// while ( TCNT2 < AVC_NORMAL_BIT_LENGTH ); // // // Fetch next bit. // data <<= 1; @@ -334,20 +311,20 @@ void AVCLanDrv::send1BitWord (bool data) // // Most significant bit out first. // for ( char nbBits = 0; nbBits < 12; nbBits++ ) { // // Reset timer to measure bit length. -// TCNT0 = 2; +// TCNT2 = 2; // OUTPUT_SET_1; // // if (data & 0x0800) { // // Adjust parity. // _parityBit = ! _parityBit; -// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH ); // } else { -// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); +// while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH ); // } // // OUTPUT_SET_0; // // Hold output low until end of bit. -// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); +// while ( TCNT2 < AVC_NORMAL_BIT_LENGTH ); // // // Fetch next bit. // data <<= 1; @@ -361,11 +338,11 @@ bool AVCLanDrv::isAvcBusFree (void) //-------------------------------------------------------------------------------- { // Reset timer. - TCNT0 = 0; + TCNT2 = 0; while (INPUT_IS_CLEAR) { // We assume the bus is free if anything happens for the length of 1 bit. - if (TCNT0 > AVC_NORMAL_BIT_LENGTH) { + if (TCNT2 > AVC_NORMAL_BIT_LENGTH) { return true; } } @@ -384,21 +361,21 @@ bool AVCLanDrv::isAvcBusFree (void) // // taken over the bus maintaining the pulse until the equivalent of a bit '0' (32 us) is formed. // // // Reset timer to measure bit length. -// TCNT0 = 0; +// TCNT2 = 0; // OUTPUT_SET_1; // // // Generate bit '0'. -// while (TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH); +// while (TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH); // OUTPUT_SET_0; // // AVC_OUT_DIS; // -// while (TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH + AVC_1U_LENGTH); +// while (TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH + AVC_1U_LENGTH); // // Measure final resulting bit. // while ( INPUT_IS_SET ); // // // Sample half-way through bit '0' (26 us) to detect whether the target is acknowledging. -// if (TCNT0 > AVC_BIT_0_HOLD_ON_MIN_LENGTH) { +// if (TCNT2 > AVC_BIT_0_HOLD_ON_MIN_LENGTH) { // // Slave is acknowledging (ack = 0). Wait until end of ack bit. // while (INPUT_IS_SET ); // AVC_OUT_EN; diff --git a/AVCLan_mini/AVCLan_mini.ino b/AVCLan_mini/AVCLan_mini.ino index 235a417..9527619 100644 --- a/AVCLan_mini/AVCLan_mini.ino +++ b/AVCLan_mini/AVCLan_mini.ino @@ -7,21 +7,19 @@ //-------------------------------------------------------------------------------- #define LED_ON sbi(LED_PORT, LED_OUT); -#define LED_OFF cbi(LED_PORT, LED_OUT); +#define LED_OFF cbi(LED_PORT, LED_OUT); -#define HONDA_DIS_ON sbi(LED_PORT, COMMUT_OUT); -#define HONDA_DIS_OFF cbi(LED_PORT, COMMUT_OUT); +#define HONDA_DIS_ON sbi(COMMUT_PORT, COMMUT_OUT); +#define HONDA_DIS_OFF cbi(COMMUT_PORT, COMMUT_OUT); static int MAX_ERROR_COUNT = 30; byte errorID; int error_count; +char BUFFF[15]; //-------------------------------------------------------------------------------- void setup() //-------------------------------------------------------------------------------- { - sbi(LED_DDR, COMMUT_OUT); - cbi(LED_PORT, COMMUT_OUT); - avclan.begin(); avclanHonda.begin(); errorID = 0; @@ -29,6 +27,7 @@ void setup() avclanBT.begin(); avclanBT.println("Start HONDA avclan."); + LED_ON; } //-------------------------------------------------------------------------------- @@ -56,6 +55,10 @@ void loop() avclanHonda.getActionID(); if ( avclan.actionID != ACT_NONE ) { + sprintf( BUFFF, "Action: %d", avclan.actionID ); + avclanBT.println( BUFFF ); + + LED_ON; avclanHonda.processAction( (AvcActionID)avclan.actionID ); } } else { @@ -63,11 +66,6 @@ void loop() else error_count = 1; errorID = res; - - if ( error_count > MAX_ERROR_COUNT ) { - error_count = 0; - avclanHonda.setHondaDis(true); - } } } @@ -82,11 +80,14 @@ void loop() } } - if ( !error_count && errorID ) { - char BUFFF[15]; - sprintf(BUFFF, "Error: %d", errorID); + if ( error_count > MAX_ERROR_COUNT ) { + error_count = 0; + avclanHonda.setHondaDis(true); + + LED_OFF; + + sprintf(BUFFF, "Error: %d", errorID ); avclanBT.println( BUFFF ); - delay(2000); } } diff --git a/AVCLan_mini/config.h b/AVCLan_mini/config.h index c97d6a1..0993282 100644 --- a/AVCLan_mini/config.h +++ b/AVCLan_mini/config.h @@ -11,31 +11,38 @@ #define MAXSENDATTEMP 3 //********** arduino & driver based on PCA82C250 ********** +// http://pighixxx.com/nanopdf.pdf #ifdef AVCLAN_PCA82C250 #define AVCLANDRIVER_TYPE "Arduino - PCA82C250" -// define out pin (pin D6 arduino) -#define DATAOUT_DDR DDRD -#define DATAOUT_PORT PORTD -#define DATAOUT_PIN PIND -#define DATAOUT 6 +// define out pin (pin 8 arduino) for AVCLAN_PCA82C250 +#define DATAOUT_DDR DDRB +#define DATAOUT_PORT PORTB +#define DATAOUT_PIN PINB +#define DATAOUT 0 -// define in pin (pin D5 arduino) -#define DATAIN_DDR DDRD -#define DATAIN_PORT PORTD -#define DATAIN_PIN PIND -#define DATAIN 5 +// define in pin (pin 9 arduino) for AVCLAN_PCA82C250 +#define DATAIN_DDR DDRB +#define DATAIN_PORT PORTB +#define DATAIN_PIN PINB +#define DATAIN 1 + +// connected to digital pin A0 +#define COMMUT_DDR DDRC +#define COMMUT_PORT PORTC +#define COMMUT_PIN PINC +#define COMMUT_OUT 0 // LED connected to digital pin 13 -#define LED_DDR DDRC -#define LED_PORT PORTC -#define LED_PIN PINC -#define COMMUT_OUT 0 +#define LED_DDR DDRB +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED_OUT 5 // AZFM board activate -#define AZFM_INIT -#define AZFM_ON -#define AZFM_OFF +//#define AZFM_INIT +//#define AZFM_ON +//#define AZFM_OFF #endif diff --git a/sketch/sketchbook_20100805.rar b/sketch/sketchbook_20100805.rar new file mode 100644 index 0000000..6296135 Binary files /dev/null and b/sketch/sketchbook_20100805.rar differ