Port control

Hello.

I seem to have successfully (to a degree) made an ATtiny85 IR remote control.
I have it sending codes I have programmed in and on my own protocol.

I currently use PB1 (pin 5), using timer 1, to generate a 38Khz signal, using a transistor and digital pin 3 (PB4) to toggle an LED off and off at 600uS intervals:

void setup() {
  // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  setup_watchdog(6); // approximately 0.5 seconds sleep

  // put your setup code here, to run once:

  DDRB = 0
         | (1 << PB1); //Set pin PB1 as output

  OCR1C = 12;    // Counter Max...clock cycles before pin is toggled.

  TCCR1 = 0
          | (1 << COM1A0) //Bits 5 and 4 of TCCR1 (COM1A0 and COM1A1) indicate what to do when comparator == counter
          | (0 << COM1A1)  // When A0 is 1 and A1 is 0, the pin is toggled on a compare.
          | (0 << CS13)
          | (1 << CS12)  // Table 12-5. Timer/Counter1 Prescale Select. Divides the Clock by an amount.
          | (0 << CS11)
          | (0 << CS10);

  pinMode(signalline, OUTPUT);
  pinMode(b1, INPUT);
  digitalWrite(b1, HIGH); // turn on pullup resistors
  pinMode(b2, INPUT);
  digitalWrite(b2, HIGH); // turn on pullup resistors
  pinMode(b3, INPUT);
  digitalWrite(b3, HIGH); // turn on pullup resistors


}

Although it seems to work, is there a way to remove the transistor?
I have been messing with registers to try and turn on and off the 38Khz signal generated by pin 6 (PB1) that is the carrier signal. I seem to be messing up and not doing something correctly…

I am assuming I need this section from the datasheet:
“Configuring the Pin
Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in “Register Description” on
page 64, the DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx I/O address, and
the PINxn bits at the PINx I/O address.
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured
as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin.
If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch
the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The
port pins are tri-stated when reset condition becomes active, even if no clocks are running.
If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If
PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).”

So am I right in thinking that as I have the pin configured as an output,

DDRB = 0
| (1 << PB1); //Set pin PB1 as output

Then according the the above “If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If
PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero).”

I should just toggle the bit in PORTxn as a 0 to stop the output of the 38Khz carrier signal on that pin and just reset the bit to 1 if I want it back on?

So

PORTB = 0
 |(0 << PB1);

and for on:

PORTB = 0
|(1<<PB1);

is my answer?

Ah nevermind. After another hour I got it ha!

I just needed to disconnect the counter from the line rather than turning the port off!

void setup() {
  DDRB = 0
         | (1 << PB1); //Set pin PB1 as output

  OCR1C = 12;    // Counter Max...clock cycles before pin is toggled.

  TCCR1 = 0
          | (1 << COM1A0) //Bits 5 and 4 of TCCR1 (COM1A0 and COM1A1) indicate what to do when comparator == counter
          | (0 << COM1A1)  // When A0 is 1 and A1 is 0, the pin is toggled on a compare.
          | (0 << CS13)
          | (1 << CS12)  // Table 12-5. Timer/Counter1 Prescale Select. Divides the Clock by an amount.
          | (0 << CS11)
          | (0 << CS10);
}

void loop() {

  TCCR1 = 0
          | (1 << COM1A0) //Bits 5 and 4 of TCCR1 (COM1A0 and COM1A1) indicate what to do when comparator == counter
          | (0 << COM1A1)  // When A0 is 1 and A1 is 0, the pin is toggled on a compare.
          | (0 << CS13)
          | (1 << CS12)  // Table 12-5. Timer/Counter1 Prescale Select. Divides the Clock by an amount.
          | (0 << CS11)
          | (0 << CS10)
          ;

          delay(500);

 TCCR1 = 0
          | (0 << COM1A0) //Bits 5 and 4 of TCCR1 (COM1A0 and COM1A1) indicate what to do when comparator == counter
          | (0 << COM1A1)  // When A0 is 1 and A1 is 0, the pin is toggled on a compare.
;
  delay(500);
}