reorganizing project to more general synchronizer in preparation for export/delivery to RVK museum.
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
/* IR Remote control
|
||||
* capture a infrared burst from a remote control and generate code template for reproducing that burst
|
||||
* measure the infrared carrier frequency / capture ir pulse train (max 200 pulses )
|
||||
* connect the IR receiver Module (IR_reveiver_schematic.jpg)
|
||||
* to pin 4,5,6,7
|
||||
*
|
||||
*
|
||||
* KHM 2010 / Martin Nawrath
|
||||
* Kunsthochschule fuer Medien Koeln
|
||||
* Academy of Media Arts Cologne
|
||||
*/
|
||||
|
||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||||
|
||||
//! Macro that clears all Timer/Counter1 interrupt flags.
|
||||
#define CLEAR_ALL_TIMER1_INT_FLAGS (TIFR1 = TIFR1)
|
||||
|
||||
|
||||
int pinLed = 13; // LED connected to digital pin 13
|
||||
int pinGND = 4; // Ground for IR reveiver Module
|
||||
int pinVCC = 5; // +5V for IR reveiver Module
|
||||
int pinAIN0= 6; // Analog Comparator In 0 / IR reveiver Module
|
||||
int pinAIN1= 7; // Analog Comparator In 0 / IR reveiver Module
|
||||
int pinTest = 8;
|
||||
unsigned int khz;
|
||||
|
||||
byte bb;
|
||||
byte bba;
|
||||
int cnt;
|
||||
const int maxlen = 300;
|
||||
int codelen;
|
||||
unsigned int timecode[maxlen+5];
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(pinLed, OUTPUT); // sets the digital pin as output
|
||||
pinMode(pinTest, OUTPUT);
|
||||
pinMode(pinGND, OUTPUT);
|
||||
pinMode(pinVCC, OUTPUT);
|
||||
pinMode(pinAIN0, INPUT);
|
||||
pinMode(pinAIN1, INPUT);
|
||||
|
||||
|
||||
digitalWrite(pinVCC,1);
|
||||
digitalWrite(pinGND,0);
|
||||
|
||||
|
||||
Serial.begin(115200); // connect to the serial port
|
||||
|
||||
// hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
|
||||
TCCR1A=0; // reset timer/counter1 control register A
|
||||
TCCR1B=0; // reset timer/counter1 control register A
|
||||
TCNT1=0; // counter value = 0
|
||||
// set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5)
|
||||
// normal mode, wgm10 .. wgm13 = 0
|
||||
cbi (TCCR1B ,CS10); // no clock
|
||||
cbi (TCCR1B ,CS11);
|
||||
cbi (TCCR1B ,CS12);
|
||||
|
||||
|
||||
// timer2 setup / is used for frequency measurement gatetime generation
|
||||
// timer 2 presaler set to 256 / timer 2 clock = 16Mhz / 256 = 62500 Hz
|
||||
cbi (TCCR2B ,CS20);
|
||||
sbi (TCCR2B ,CS21);
|
||||
sbi (TCCR2B ,CS22);
|
||||
|
||||
//set timer2 to CTC Mode
|
||||
cbi (TCCR2A ,WGM20);
|
||||
sbi (TCCR2A ,WGM21);
|
||||
cbi (TCCR2B ,WGM22);
|
||||
OCR2A = 124; // CTC at top of OCR2A / timer2 interrupt when coun value reaches OCR2A value
|
||||
|
||||
// interrupt control
|
||||
|
||||
// sbi (TIMSK2,OCIE2A); // enable Timer2 Interrupt
|
||||
cbi(ADCSRB,ACME);
|
||||
cbi(ACSR,ACD);
|
||||
cbi(ACSR,ACBG);
|
||||
|
||||
Serial.println(" Infrared code detector");
|
||||
while ((ACSR & 32) == 0) {
|
||||
}
|
||||
khz=f_measure();
|
||||
|
||||
Serial.print(khz);
|
||||
Serial.print( " KHz");
|
||||
Serial.println( "");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
|
||||
if (ACSR & 32) { // if IR Signal on pin 5 present
|
||||
|
||||
TCCR1B=0; //timer1 off
|
||||
sbi(TIFR1,TOV1); // clear Timer/Counter 1 overflow flag
|
||||
TCNT1=0; // clear timer1
|
||||
TCCR1B=3; //timer1 prescaler =64;
|
||||
bba=0;
|
||||
|
||||
cnt=1;
|
||||
while ((TIFR1 & 1)==0 ) { // wait for T1 period length 250ms TIFR1 Flag
|
||||
|
||||
bb=0;
|
||||
bb = ACSR & 32; // sample IR Strobe Signal for HIGH/LOW
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
delayMicroseconds(5);
|
||||
bb =bb | ACSR & 32;
|
||||
|
||||
if (bb != bba) { // if IR strobe level changed
|
||||
PORTB = PORTB ^ 32; // LED blink
|
||||
timecode[cnt]=TCNT1; // Store Timecode in Table
|
||||
codelen=TCNT1;
|
||||
cnt++; // Count IR Strobes
|
||||
if (cnt > maxlen) cnt =maxlen-1; // cnt max
|
||||
delayMicroseconds(200);
|
||||
}
|
||||
bba=bb;
|
||||
}
|
||||
timecode[cnt]=0;
|
||||
print_code_template();
|
||||
for (int ii=1;ii < maxlen;ii++) timecode[ii]=0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//******************************************************************
|
||||
// print a code template according to a pressed button
|
||||
void print_code_template(){
|
||||
unsigned int ix,ia,ia1,ia2;
|
||||
char st1[20];
|
||||
unsigned long int w1,w2;
|
||||
Serial.println( "//****************** CODE TEMPLATE FOR IR REMOTE ****************************");
|
||||
|
||||
|
||||
Serial.print( "// IR codelength = ");
|
||||
Serial.print( codelen*4/1000);
|
||||
Serial.print( " ms");
|
||||
Serial.println( "");
|
||||
|
||||
Serial.print( "ICR1 = 16000 /") ;
|
||||
Serial.print(khz);
|
||||
Serial.println( "; // IR carrier frequency") ;
|
||||
for (ix=1;ix < maxlen;ix+=2) {
|
||||
ia=timecode[ix];
|
||||
ia1=timecode[ix+1];
|
||||
ia2=timecode[ix+2];
|
||||
|
||||
if (ia !=0) {
|
||||
Serial.print( "sbi(DDRB,1); delayMicroseconds(");
|
||||
w1=ia1-ia;
|
||||
w1=w1*4;
|
||||
|
||||
sprintf(st1, "%4d );", w1);
|
||||
Serial.print( st1);
|
||||
timecode[ix]=0;
|
||||
|
||||
}
|
||||
if (ia2 !=0) {
|
||||
w2=ia2-ia1;
|
||||
w2=w2*4;
|
||||
Serial.print( " cbi(DDRB,1); delayMicroseconds(" );
|
||||
sprintf(st1, "%4d );", w2);
|
||||
Serial.print( st1);
|
||||
|
||||
timecode[ix]=0;
|
||||
timecode[ix+1]=0;
|
||||
Serial.print( " // ");
|
||||
sprintf(st1, "ix:%3d", ix);
|
||||
Serial.print( st1);
|
||||
sprintf(st1, " ton:%4d", w1);
|
||||
Serial.print( st1);
|
||||
sprintf(st1, " toff:%4d", w2);
|
||||
Serial.print( st1);
|
||||
Serial.println("");
|
||||
}
|
||||
}
|
||||
Serial.print( " cbi(DDRB,1);" );
|
||||
Serial.println("");
|
||||
Serial.println("");
|
||||
Serial.println("");
|
||||
|
||||
}
|
||||
//******************************************************************
|
||||
// measure infrared carrier frequency at pin 6,7 / analog comparator
|
||||
unsigned int f_measure() {
|
||||
|
||||
TCCR1B=0; //timer1 off
|
||||
sbi(TIFR1,TOV1); // clear Timer/Counter 1 overflow flag
|
||||
TCNT1=0; // clear timer1
|
||||
TCCR1B=3; //timer1 prescaler =64;
|
||||
int cnt=0;
|
||||
byte bb,bba;
|
||||
|
||||
while (TCNT1 < 250) {
|
||||
bb=ACSR;
|
||||
if (bb != bba) {
|
||||
cnt++;
|
||||
// PORTB ^= 32;
|
||||
}
|
||||
bba=bb;
|
||||
}
|
||||
cnt=cnt/2;
|
||||
return (cnt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user