I have 2 questions.
I have some code that other people are using and it works for them, however when I try it the attiny85 does nothing. It should be sending pulses out on pin1 but I get nothing. I'm using arduino 1.6.7. could the version have anything to do with it.
Here is the code
#define BIT_SET(a,b) ((a) |= (1<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1<<(b)))
#define BIT_CHECK(a,b) ((a) & (1<<(b)))
// CHANGE HERE THE ID OF TRANSPONDER
// possible values are 1 to 63
#define TRANSPONDER_ID 4
#define NUM_BITS 9
#define ZERO 250
#define ONE 650
unsigned int buffer[NUM_BITS];
unsigned int num_one_pulses = 0;
unsigned int get_pulse_width_for_buffer(int bit){
if(BIT_CHECK(TRANSPONDER_ID,bit)){
num_one_pulses += 1;
return ONE;
}
return ZERO;
}
unsigned int control_bit(){
if(num_one_pulses % 2 >= 1){
return ONE;
}else{
return ZERO;
}
}
void setup()
{
PORTB = 0;
DDRB = 0b00000010; // set PB1 (= OCR1A) to be an output
setFrequency(38000); // 38 kHz
buffer[0] = ZERO;
buffer[1] = ZERO;
buffer[2] = get_pulse_width_for_buffer(5);
buffer[3] = get_pulse_width_for_buffer(4);
buffer[4] = get_pulse_width_for_buffer(3);
buffer[5] = get_pulse_width_for_buffer(2);
buffer[6] = get_pulse_width_for_buffer(1);
buffer[7] = get_pulse_width_for_buffer(0);
buffer[8] = control_bit();
}
// Set the frequency that we will get on pin OCR1A but don't turn it on
void setFrequency(uint16_t freq)
{
uint32_t requiredDivisor = (F_CPU/2)/(uint32_t)freq;
uint16_t prescalerVal = 1;
uint8_t prescalerBits = 1;
while ((requiredDivisor + prescalerVal/2)/prescalerVal > 256)
{
++prescalerBits;
prescalerVal <<= 1;
}
uint8_t top = ((requiredDivisor + (prescalerVal/2))/prescalerVal) - 1;
TCCR1 = (1 << CTC1) | prescalerBits;
GTCCR = 0;
OCR1C = top;
TIMSK |=(1<<OCIE1A);
TCCR1 |= (1 << COM1A0);
}
// Turn the frequency on
void ir_pulse_on()
{
TCNT1 = 0;
TCCR1 |= (1 << COM1A0);
}
// Turn the frequency off and turn off the IR LED.
// We let the counter continue running, we just turn off the OCR1A pin.
void ir_pulse_off()
{
TCCR1 &= ~(1 << COM1A0);
}
void loop(){
for(int i = 0; i < 3; i++){
for(int b = 0; b < NUM_BITS; b++){
switch(b){
case 0:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 1:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 2:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 3:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 4:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 5:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 6:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 7:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 8:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
}
ir_pulse_off();
} // going through the buffer
delay(20 + random(0, 5));
} // 3 times
} // end of main loop
I changed the code to use timer0 instead of timer1 and it appears to work but it outputs on pin0 instead of pin 1. Can timer0 use pin 1? If so what do I change in the code below?
Thanks in advance.
#define BIT_SET(a,b) ((a) |= (1<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1<<(b)))
#define BIT_CHECK(a,b) ((a) & (1<<(b)))
// CHANGE HERE THE ID OF TRANSPONDER
// possible values are 1 to 63
#define TRANSPONDER_ID 1
#define NUM_BITS 9
#define ZERO 250
#define ONE 650
unsigned int buffer[NUM_BITS];
unsigned int num_one_pulses = 0;
unsigned int get_pulse_width_for_buffer(int bit){
if(BIT_CHECK(TRANSPONDER_ID,bit)){
num_one_pulses += 1;
return ONE;
}
return ZERO;
}
unsigned int control_bit(){
if(num_one_pulses % 2 >= 1){
return ONE;
}else{
return ZERO;
}
}
void setup()
{
PORTB = 0;
DDRB = 0b00000011; // set PB1 (= OCR1A) to be an output
//DDRB |= (1<<PB0); //Set pin PB0 as output
setFrequency(38000); // 38 kHz
buffer[0] = ZERO;
buffer[1] = ZERO;
buffer[2] = get_pulse_width_for_buffer(5);
buffer[3] = get_pulse_width_for_buffer(4);
buffer[4] = get_pulse_width_for_buffer(3);
buffer[5] = get_pulse_width_for_buffer(2);
buffer[6] = get_pulse_width_for_buffer(1);
buffer[7] = get_pulse_width_for_buffer(0);
buffer[8] = control_bit();
}
// Set the frequency that we will get on pin OCR1A but don't turn it on
void setFrequency(uint16_t freq)
{
TCNT0 = 0;// set timer value to 0
TCCR0A=0;
TCCR0B=0;//Timer/Counter0 Control Register
TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
TCCR0B |= (1 << CS00);// Prescaler table 11.6
OCR0A=105; //CTC Compare value
}
// Turn the frequency on
void ir_pulse_on()
{
TCNT0 = 0;
// TCCR0B |= (1 << CS00);
TCCR0A |=(1<<COM0A0);
}
// Turn the frequency off and turn off the IR LED.
// We let the counter continue running, we just turn off the OCR1A pin.
void ir_pulse_off()
{
//TCCR0B &= ~(1 << CS00);
TCCR0A &= ~(1<<COM0A0);
}
void loop(){
for(int i = 0; i < 3; i++){
for(int b = 0; b < NUM_BITS; b++){
switch(b){
case 0:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 1:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 2:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 3:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 4:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 5:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 6:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
case 7:
ir_pulse_off();
delayMicroseconds(buffer[b]);
break;
case 8:
ir_pulse_on();
delayMicroseconds(buffer[b]);
break;
}
ir_pulse_off();
} // going through the buffer
delay(20 + random(0, 5));
} // 3 times
} // end of main loop