1
0
mirror of https://github.com/Oleg-Stepanenko-owo/IEBUS synced 2025-06-28 18:26:24 +00:00

back timer2, add main sketch

This commit is contained in:
OlegStepanenko_noute 2016-03-30 20:26:05 +03:00
parent 45a82442bd
commit b87cc0fcaf
4 changed files with 80 additions and 95 deletions

View File

@ -7,45 +7,25 @@ void AVCLanDrv::begin ()
{ {
// AVCLan TX+/TX- read line INPUT // AVCLan TX+/TX- read line INPUT
cbi(DATAIN_DDR, DATAIN); cbi(DATAIN_DDR, DATAIN);
#ifdef AVCLAN_ST485
sbi(DATAIN_PORT, DATAIN);
#else
cbi(DATAIN_PORT, DATAIN); cbi(DATAIN_PORT, DATAIN);
#endif
// AVCLan TX+/TX- write line OUTPUT //avclan driver on PCA82C250
#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
sbi(DATAOUT_DDR, DATAOUT); sbi(DATAOUT_DDR, DATAOUT);
AVC_OUT_DIS; AVC_OUT_DIS;
OUTPUT_SET_0; OUTPUT_SET_0;
#endif
#endif
// timer2 setup, prescaler factor - 8 // timer2 setup, prescaler factor - 8
#if defined(__AVR_ATmega8__) #if defined(__AVR_ATmega8__)
// ASSR=0x00; // ASSR=0x00;
TCCR2 = 0x02; TCCR2 = 0x02;
// TCNT0=0x00; // TCNT2=0x00;
// OCR2=0x00; // OCR2=0x00;
#else // ATMega168 #else // ATMega168
// ASSR=0x00; // ASSR=0x00;
// TCCR2A=0x00; // TCCR2A=0x00;
TCCR0B = 0x02; TCCR2B = 0x02;
// TCNT0=0x00; // TCNT2=0x00;
// OCR2A=0x00; // OCR2A=0x00;
// OCR2B=0x00; // OCR2B=0x00;
#endif #endif
@ -75,22 +55,19 @@ word AVCLanDrv::readBits (byte nbBits)
} }
// Reset timer to measure bit length. // Reset timer to measure bit length.
TCNT0 = 0; TCNT2 = 0;
// Wait until falling edge. // Wait until falling edge.
while (INPUT_IS_SET); while (INPUT_IS_SET);
// Compare half way between a '1' (20 us) and a '0' (32 us ): 32 - (32 - 20) /2 = 26 us // 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. // Set new bit.
data |= 0x0001; data |= 0x0001;
// Adjust parity. // Adjust parity.
_parityBit = !_parityBit; _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; return data;
} }
@ -106,18 +83,18 @@ byte AVCLanDrv::_readMessage ()
// Start bit. // Start bit.
while (INPUT_IS_CLEAR); while (INPUT_IS_CLEAR);
TCCR0B = 0x03; // prescaler 32 TCCR2B = 0x03; // prescaler 32
TCNT0 = 0; TCNT2 = 0;
// Wait until falling edge. // Wait until falling edge.
while (INPUT_IS_SET) { while (INPUT_IS_SET) {
t = TCNT0; t = TCNT2;
if (t > 0xFF) { if (t > 0xFF) {
TCCR0B = 0x02; // prescaler 8 TCCR2B = 0x02; // prescaler 8
SREG = oldSREG; SREG = oldSREG;
return 1; return 1;
} }
} }
TCCR0B = 0x02; // prescaler 8 TCCR2B = 0x02; // prescaler 8
if (t < AVC_START_BIT_HOLD_ON_MIN_LENGTH) { if (t < AVC_START_BIT_HOLD_ON_MIN_LENGTH) {
//if (t < 0x16){ //if (t < 0x16){
@ -231,17 +208,17 @@ byte AVCLanDrv::readMessage ()
////-------------------------------------------------------------------------------- ////--------------------------------------------------------------------------------
//{ //{
// // Reset timer to measure bit length. // // Reset timer to measure bit length.
// TCCR0B = 0x03; // prescaler 32 // TCCR2B = 0x03; // prescaler 32
// TCNT0 = 0; // TCNT2 = 0;
// OUTPUT_SET_1; // OUTPUT_SET_1;
// //
// // Pulse level high duration. // // Pulse level high duration.
// while ( TCNT0 < AVC_START_BIT_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_START_BIT_HOLD_ON_LENGTH );
// OUTPUT_SET_0; // OUTPUT_SET_0;
// //
// // Pulse level low duration until ~185 us. // // Pulse level low duration until ~185 us.
// while ( TCNT0 < AVC_START_BIT_LENGTH ); // while ( TCNT2 < AVC_START_BIT_LENGTH );
// TCCR0B = 0x02; // prescaler 8 // TCCR2B = 0x02; // prescaler 8
// //
//} //}
@ -251,17 +228,17 @@ void AVCLanDrv::send1BitWord (bool data)
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
{ {
// Reset timer to measure bit length. // Reset timer to measure bit length.
TCNT0 = 0; TCNT2 = 0;
OUTPUT_SET_1; OUTPUT_SET_1;
if (data) { if (data) {
while (TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH); while (TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH);
} else { } else {
while (TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH); while (TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH);
} }
OUTPUT_SET_0; OUTPUT_SET_0;
while (TCNT0 < AVC_NORMAL_BIT_LENGTH); while (TCNT2 < AVC_NORMAL_BIT_LENGTH);
} }
//// Send a 4 bit word to the AVCLan //// Send a 4 bit word to the AVCLan
@ -274,20 +251,20 @@ void AVCLanDrv::send1BitWord (bool data)
// // Most significant bit out first. // // Most significant bit out first.
// for ( char nbBits = 0; nbBits < 4; nbBits++ ) { // for ( char nbBits = 0; nbBits < 4; nbBits++ ) {
// // Reset timer to measure bit length. // // Reset timer to measure bit length.
// TCNT0 = 2; // TCNT2 = 2;
// OUTPUT_SET_1; // OUTPUT_SET_1;
// //
// if (data & 0x8) { // if (data & 0x8) {
// // Adjust parity. // // Adjust parity.
// _parityBit = ! _parityBit; // _parityBit = ! _parityBit;
// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH );
// } else { // } else {
// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH );
// } // }
// //
// OUTPUT_SET_0; // OUTPUT_SET_0;
// // Hold output low until end of bit. // // Hold output low until end of bit.
// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); // while ( TCNT2 < AVC_NORMAL_BIT_LENGTH );
// //
// // Fetch next bit. // // Fetch next bit.
// data <<= 1; // data <<= 1;
@ -304,20 +281,20 @@ void AVCLanDrv::send1BitWord (bool data)
// // Most significant bit out first. // // Most significant bit out first.
// for ( char nbBits = 0; nbBits < 8; nbBits++ ) { // for ( char nbBits = 0; nbBits < 8; nbBits++ ) {
// // Reset timer to measure bit length. // // Reset timer to measure bit length.
// TCNT0 = 2; // TCNT2 = 2;
// OUTPUT_SET_1; // OUTPUT_SET_1;
// //
// if (data & 0x80) { // if (data & 0x80) {
// // Adjust parity. // // Adjust parity.
// _parityBit = ! _parityBit; // _parityBit = ! _parityBit;
// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH );
// } else { // } else {
// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH );
// } // }
// //
// OUTPUT_SET_0; // OUTPUT_SET_0;
// // Hold output low until end of bit. // // Hold output low until end of bit.
// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); // while ( TCNT2 < AVC_NORMAL_BIT_LENGTH );
// //
// // Fetch next bit. // // Fetch next bit.
// data <<= 1; // data <<= 1;
@ -334,20 +311,20 @@ void AVCLanDrv::send1BitWord (bool data)
// // Most significant bit out first. // // Most significant bit out first.
// for ( char nbBits = 0; nbBits < 12; nbBits++ ) { // for ( char nbBits = 0; nbBits < 12; nbBits++ ) {
// // Reset timer to measure bit length. // // Reset timer to measure bit length.
// TCNT0 = 2; // TCNT2 = 2;
// OUTPUT_SET_1; // OUTPUT_SET_1;
// //
// if (data & 0x0800) { // if (data & 0x0800) {
// // Adjust parity. // // Adjust parity.
// _parityBit = ! _parityBit; // _parityBit = ! _parityBit;
// while ( TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH );
// } else { // } else {
// while ( TCNT0 < AVC_BIT_0_HOLD_ON_LENGTH ); // while ( TCNT2 < AVC_BIT_0_HOLD_ON_LENGTH );
// } // }
// //
// OUTPUT_SET_0; // OUTPUT_SET_0;
// // Hold output low until end of bit. // // Hold output low until end of bit.
// while ( TCNT0 < AVC_NORMAL_BIT_LENGTH ); // while ( TCNT2 < AVC_NORMAL_BIT_LENGTH );
// //
// // Fetch next bit. // // Fetch next bit.
// data <<= 1; // data <<= 1;
@ -361,11 +338,11 @@ bool AVCLanDrv::isAvcBusFree (void)
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
{ {
// Reset timer. // Reset timer.
TCNT0 = 0; TCNT2 = 0;
while (INPUT_IS_CLEAR) { while (INPUT_IS_CLEAR) {
// We assume the bus is free if anything happens for the length of 1 bit. // 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; 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. // // taken over the bus maintaining the pulse until the equivalent of a bit '0' (32 us) is formed.
// //
// // Reset timer to measure bit length. // // Reset timer to measure bit length.
// TCNT0 = 0; // TCNT2 = 0;
// OUTPUT_SET_1; // OUTPUT_SET_1;
// //
// // Generate bit '0'. // // Generate bit '0'.
// while (TCNT0 < AVC_BIT_1_HOLD_ON_LENGTH); // while (TCNT2 < AVC_BIT_1_HOLD_ON_LENGTH);
// OUTPUT_SET_0; // OUTPUT_SET_0;
// //
// AVC_OUT_DIS; // 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. // // Measure final resulting bit.
// while ( INPUT_IS_SET ); // while ( INPUT_IS_SET );
// //
// // Sample half-way through bit '0' (26 us) to detect whether the target is acknowledging. // // 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. // // Slave is acknowledging (ack = 0). Wait until end of ack bit.
// while (INPUT_IS_SET ); // while (INPUT_IS_SET );
// AVC_OUT_EN; // AVC_OUT_EN;

View File

@ -7,21 +7,19 @@
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
#define LED_ON sbi(LED_PORT, LED_OUT); #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_ON sbi(COMMUT_PORT, COMMUT_OUT);
#define HONDA_DIS_OFF cbi(LED_PORT, COMMUT_OUT); #define HONDA_DIS_OFF cbi(COMMUT_PORT, COMMUT_OUT);
static int MAX_ERROR_COUNT = 30; static int MAX_ERROR_COUNT = 30;
byte errorID; byte errorID;
int error_count; int error_count;
char BUFFF[15];
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
void setup() void setup()
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
{ {
sbi(LED_DDR, COMMUT_OUT);
cbi(LED_PORT, COMMUT_OUT);
avclan.begin(); avclan.begin();
avclanHonda.begin(); avclanHonda.begin();
errorID = 0; errorID = 0;
@ -29,6 +27,7 @@ void setup()
avclanBT.begin(); avclanBT.begin();
avclanBT.println("Start HONDA avclan."); avclanBT.println("Start HONDA avclan.");
LED_ON;
} }
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
@ -56,6 +55,10 @@ void loop()
avclanHonda.getActionID(); avclanHonda.getActionID();
if ( avclan.actionID != ACT_NONE ) { if ( avclan.actionID != ACT_NONE ) {
sprintf( BUFFF, "Action: %d", avclan.actionID );
avclanBT.println( BUFFF );
LED_ON;
avclanHonda.processAction( (AvcActionID)avclan.actionID ); avclanHonda.processAction( (AvcActionID)avclan.actionID );
} }
} else { } else {
@ -63,11 +66,6 @@ void loop()
else error_count = 1; else error_count = 1;
errorID = res; errorID = res;
if ( error_count > MAX_ERROR_COUNT ) {
error_count = 0;
avclanHonda.setHondaDis(true);
}
} }
} }
@ -82,11 +80,14 @@ void loop()
} }
} }
if ( !error_count && errorID ) { if ( error_count > MAX_ERROR_COUNT ) {
char BUFFF[15]; error_count = 0;
sprintf(BUFFF, "Error: %d", errorID); avclanHonda.setHondaDis(true);
LED_OFF;
sprintf(BUFFF, "Error: %d", errorID );
avclanBT.println( BUFFF ); avclanBT.println( BUFFF );
delay(2000);
} }
} }

View File

@ -11,31 +11,38 @@
#define MAXSENDATTEMP 3 #define MAXSENDATTEMP 3
//********** arduino & driver based on PCA82C250 ********** //********** arduino & driver based on PCA82C250 **********
// http://pighixxx.com/nanopdf.pdf
#ifdef AVCLAN_PCA82C250 #ifdef AVCLAN_PCA82C250
#define AVCLANDRIVER_TYPE "Arduino - PCA82C250" #define AVCLANDRIVER_TYPE "Arduino - PCA82C250"
// define out pin (pin D6 arduino) // define out pin (pin 8 arduino) for AVCLAN_PCA82C250
#define DATAOUT_DDR DDRD #define DATAOUT_DDR DDRB
#define DATAOUT_PORT PORTD #define DATAOUT_PORT PORTB
#define DATAOUT_PIN PIND #define DATAOUT_PIN PINB
#define DATAOUT 6 #define DATAOUT 0
// define in pin (pin D5 arduino) // define in pin (pin 9 arduino) for AVCLAN_PCA82C250
#define DATAIN_DDR DDRD #define DATAIN_DDR DDRB
#define DATAIN_PORT PORTD #define DATAIN_PORT PORTB
#define DATAIN_PIN PIND #define DATAIN_PIN PINB
#define DATAIN 5 #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 // LED connected to digital pin 13
#define LED_DDR DDRC #define LED_DDR DDRB
#define LED_PORT PORTC #define LED_PORT PORTB
#define LED_PIN PINC #define LED_PIN PINB
#define COMMUT_OUT 0 #define LED_OUT 5
// AZFM board activate // AZFM board activate
#define AZFM_INIT //#define AZFM_INIT
#define AZFM_ON //#define AZFM_ON
#define AZFM_OFF //#define AZFM_OFF
#endif #endif

Binary file not shown.