From nano to Attiny

Hi
How to modify this program to make it working on attiny85?

// This code demonstrates how to generate two output signals
// with variable phase shift between them using an AVR Timer 

// The output shows up on Arduino pin 9, 10

// More AVR Timer Tricks at http://josh.com

void setup() {
  pinMode( 9 , OUTPUT );    // Arduino Pin  9 = OCR1A
  pinMode( 10 , OUTPUT );   // Arduino Pin 10 = OCR1B

  // Both outputs in toggle mode  
  TCCR1A = _BV( COM1A0 ) |_BV( COM1B0 );


  // CTC Waveform Generation Mode
  // TOP=ICR1  
  // Note clock is left off for now

  TCCR1B = _BV( WGM13) | _BV( WGM12);

  OCR1A = 0;    // First output is the base, it always toggles at 0


}

// prescaler of 1 will get us 8MHz - 488Hz
// User a higher prescaler for lower freqncies

#define PRESCALER 1
#define PRESCALER_BITS 0x01

#define CLK 16000000UL    // Default clock speed is 16MHz on Arduino Uno

// Output phase shifted wave forms on Arduino Pins 9 & 10
// freq = freqnecy in Hertz (  122 < freq <8000000 )
// shift = phase shift in degrees ( 0 <= shift < 180 )

// Do do shifts 180-360 degrees, you could invert the OCR1B by doing an extra toggle using FOC

/// Note phase shifts will be rounded down to the next neared possible value so the higher the frequency, the less phase shift resolution you get. At 8Mhz, you can only have 0 or 180 degrees because there are only 2 clock ticks per cycle.  

int setWaveforms( unsigned long freq , int shift ) {

  // This assumes prescaler = 1. For lower freqnecies, use a larger prescaler.

  unsigned long clocks_per_toggle = (CLK / freq) / 2;    // /2 becuase it takes 2 toggles to make a full wave

  ICR1 = clocks_per_toggle;

  unsigned long offset_clocks = (clocks_per_toggle * shift) / 180UL; // Do mult first to save precision

  OCR1B= offset_clocks;

  // Turn on timer now if is was not already on
  // Clock source = clkio/1 (no prescaling)
  // Note: you could use a prescaller here for lower freqnencies
  TCCR1B |= _BV( CS10 ); 

}

// Demo by cycling through some phase shifts at 50Khz  

void loop() {

  setWaveforms( 50000 , 0 );

  delay(1000); 

  setWaveforms( 50000 , 90 );

  delay(1000); 

  setWaveforms( 50000 , 180 );

  delay(1000); 


Using the PBx name in place of 9 and 10. If you want to use PWM (fading), be sure to use PB0, PB1 and PB4.

This is an example of using RGB LED (three LEDs in one device).

This web site has many ATtiny85 articles.
https://www.engineersgarage.com/?s=attiny85

1 Like

Just pins changing is not working.

error: 'TCCR1B' was not declared in this scope
   TCCR1B = _BV( WGM13) | _BV( WGM12);
   ^~~~~~
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino:23:3: note: suggested alternative: 'TCCR0B'
   TCCR1B = _BV( WGM13) | _BV( WGM12);
   ^~~~~~
   TCCR0B
In file included from c:\users\galinka\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\io.h:99:0,
                 from c:\users\galinka\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\pgmspace.h:90,
                 from C:\Users\Galinka\AppData\Local\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2\cores\tiny/Arduino.h:9,
                 from sketch\85_quadrature_2.ino.cpp:1:
85_quadrature_2:23:17: error: 'WGM13' was not declared in this scope
   TCCR1B = _BV( WGM13) | _BV( WGM12);
                 ^
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino:23:17: note: suggested alternative: 'WGM00'
85_quadrature_2:23:31: error: 'WGM12' was not declared in this scope
   TCCR1B = _BV( WGM13) | _BV( WGM12);
                               ^
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino:23:31: note: suggested alternative: 'WGM02'
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino: In function 'int setWaveforms(long unsigned int, int)':
85_quadrature_2:52:3: error: 'ICR1' was not declared in this scope
   ICR1 = clocks_per_toggle;
   ^~~~
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino:52:3: note: suggested alternative: 'OCR1C'
   ICR1 = clocks_per_toggle;
   ^~~~
   OCR1C
85_quadrature_2:61:3: error: 'TCCR1B' was not declared in this scope
   TCCR1B |= _BV( CS10 );
   ^~~~~~
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino:61:3: note: suggested alternative: 'TCCR0B'
   TCCR1B |= _BV( CS10 );
   ^~~~~~
   TCCR0B
C:\Users\Galinka\Documents\Arduino\85_quadrature_2\85_quadrature_2.ino: In function 'void loop()':
85_quadrature_2:79:14: error: expected '}' at end of input
   delay(1000);
              ^
exit status 1
'TCCR1A' was not declared in this scope

tiny and mega have different register naming

That's a lot of code for an astable multivibrator.

void setup() {
  pinMode(PB0, OUTPUT);
  pinMode(PB1, OUTPUT);
}

void loop() {
  for (int i = 0; i < 255; i++) {
    analogWrite(PB0, i);
    analogWrite(PB1, 255 - i);
    delay(200);
  }
}
1 Like

produces pulses with variable width.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.