I am attempting to create a program for the atmega328P which will enable the aux input of a Toyota Corolla stereo. In order to do this, I'm bitbanging AVC LAN. Nearly all of my research comes from this site: https://github.com/halleysfifthinc/Toyota-AVC-LAN
In this example, Frigon has wired his Atmega8 in a peculiar way. Instead of connecting the + terminal directly to a single pin of his Atmega8, he's connected it to a pulldown resistor, resisted it to the - terminal (presumably for bus termination) and a digital pin as well as an analog pin. The - terminal has been given similar strange treatment.
In his function ReadBits(), he waits for a physical 1, then sets a timer and waits for a physical 0 to measure the length of the pulse. But when he does this, he uses the equivalent of
while ( bit_is_clear( ACSR, ACO ) );
That's completely Greek to me. Does this have something to do with something called in Internal Analog Comparator?
I attempted to create something similar using
while( digitalRead( 2 ) );
but I'm reading static as well as honest-to-goodness pulses. I assume that, anyways, because I'm getting pulses of length 2-7 ticks when many are closer to 20-40 ticks (measured using TCNT0 with no custom prescaler).
So... what exactly is he doing with ACSR and ACO? Why is his + and - terminals connected to more than one pin? Why are his diodes there?
=========================EDIT=======================
Following Edgar's advice, I've reworked my schematic and code to closely match Frigon's work. Below is my code:
#include <avr/wdt.h>
#define fwVer "v0.1"
#define fwDt __DATE__
#define DATAPIN 2
//====================================================================================COPYPASTED
#define DATAIN_PIN ACSR
#define DATAIN ACO
#define INPUT_IS_SET ( bit_is_set( DATAIN_PIN, DATAIN ) )
#define INPUT_IS_CLEAR ( bit_is_clear( DATAIN_PIN, DATAIN ) )
//===================================================================================WHAT DOES THIS MEAN
byte data[255];
void setup() {
LedOff();
Serial.begin(9600);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
digitalWrite(3,HIGH); //"leave the pins tri-stated" according to Frigon
digitalWrite(2,LOW);
pinMode(LED_BUILTIN,OUTPUT);
//two second watchdog
wdt_enable( WDTO_2S );
Serial.println("Firmware "+String(fwVer)+", compiled on "+fwDt);
LedOff();
}
void LedOff() {
digitalWrite(LED_BUILTIN, LOW);
}
void LedOn() {
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
wdt_reset();
if (INPUT_IS_CLEAR) LedOff();
else LedOn();
}
Schematic: https://i.sstatic.net/0UNzF.jpg (mistake on the schematic - I connected 5v to vcc and ground to ground) If it matters, I'm powering the head unit and the arduino from the 12v and 5v rails of an ATX power supply. They both take the same ground.
My code should theoretically only turn on my led whenever the head unit broadcasts bits, but it's just sort of erratically blipping on and off or fading gently at random. I've triple checked my wiring (breadboard if that matters). Why is this happening? Is there something about bit_is_clear(ACSR,ACO) that I don't understand?