Hello there, I created this page to help me understanding this little chip. I am glad to know what may also be used to help other people like me!
samuel.st-Aubin (AT) openslab.com


  • Atmel Attiny13




attiny13photo.jpg


I/O


DDRB Direction pin register......................................................................................

DDRBREGr.jpg

  • 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.


PORTB register......................................................................................................
portb.jpg

  • If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.
  • If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one).



 /* -----------------------------------------------------------------------
 * Title: Led blinking reaction on pressed switch (I/O)
 * Hardware: ATtiny13
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 
 #define F_CPU 9600000UL // Define software reference clock for delay duration
 // Must be write before call delay.h
 #include <avr/io.h>
 #include <util/delay.h>
 
 #define SWT PB2 // Define ext switch pin on PB2 *1
 #define LED PB0 // Define ext led pin on PB0 *2
 
 int main(void) {
 
 DDRB &= ~(1<<SWT); // Set input direction on SWT (PB2) *1
 DDRB |= (1 << LED); // Set output direction on LED (PB0) *2
 
 for (;;) // Endless loop
 {
 if (bit_is_clear(PINB, SWT)) // Read SWT pin (if SWT pressed, do the loop one time)
 
 {
 PORTB &= ~(1 << LED); // Set 0 on LED pin (led turn on)
 _delay_ms(300); // Call delay for 300 milisec
 PORTB |= (1 << LED); // Set 1 on LED pin (led turn off)
 _delay_ms(300); // Call delay for 300 milisec
 }
 }
 
 return 0;
 }
 
*note 1
 #define SWT PB2 // Define ext switch pin on PB2
 //__DDRB &= ~(1<<SWT)__//; // Set input direction on SWT (PB2) *1

Write 0 bit in DDRB register (PB2) (shifting methode)

00000000 -------> 8 bits DDRB register
AND---------------> logic operator AND * see logical gate at the bottom of the page.
11111011 -------> mask with logical shift 3 time 0-1-2 + inverte (tilt = inverte)
=
00000000

*note2
 #define LED PB0 // Define ext led pin on PB0 *2
 DDRB |= (1 << LED); // Set output direction on LED (PB0) *2
 
write 1 bit in DDRB register (PB0) (shifting methode)

00000000 --------> 8 bits DDRB register
OR -----------------> logic operator
00000001 --------> logical inverted PB0
=
00000001

*We can also use direct metod like " DDRB = 0x00 " (but this methode erase all old DDRB register value)


Analog to Digital Converter


ADCSRA register...................................................................................................

adsra.jpg
  • 7 ADEN: Analog Digital Enable. Writing this bit to one enables the ADC.
  • 6 ADSC: ADC Start Conversion
  • 2:0 ADC: Analog digital clock prescaler Select Bits *fig1



ADMUX register....................................................................................................

admux2.jpg
  • 6 REFS0 : This bit selects the voltage reference for the ADC. (1 - internal 0 - VCC)
  • 5 ADLAR: ADC Left Adjust Result *fig.2
  • 1:0 MUX: (Multiplexing) Analog Channel Selection Bits




fig 1 Prescaler Select Bits


prescaltime.jpg

Fig 2 Left Adjust Result

adlar1.jpg

Fig 3 Analog Channel Selection Bits

mux.jpg

 /* -----------------------------------------------------------------------
 * Title: Analog read example (Analog input change led brightness)
 * Hardware: ATtiny13v
 * Software: WinAVR 20060421
 * -----------------------------------------------------------------------*/
 
 #define F_CPU 9600000 // Define software reference clock for delay duration
 
 #include <avr/io.h>
 #include <util/delay.h>
 
 #define LED PB2 // Define led ext output pin on PB2
 
 int i; // 8 bits integer
 
 
 int main(void)
 
 {
 
 DDRB |= (1 << LED); // Set output direction on LED
 ADCSRA |= (1 << ADEN)| // Analog-Digital enable bit
 (1 << ADPS1)| // set prescaler to 8 (clock / 8)
 (1 << ADPS0); // set prescaler to 8 (clock / 8)
 
 ADMUX |= (1 << ADLAR)| // AD result store in (more significant bit in ADCH)
 (1 << MUX1); // Choose AD input AD2 (BP 4)
 
 for (;;)
 {
 
 ADCSRA |= (1 << ADEN); // Analog-Digital enable bit
 ADCSRA |= (1 << ADSC); // Discarte first conversion
 
 while (ADCSRA & (1 << ADSC)); // wait until conversion is done
 
 ADCSRA |= (1 << ADSC); // start single conversion
 
 while (ADCSRA & (1 << ADSC)) // wait until conversion is done
 
 ADCSRA &= ~(1<<ADEN); // shut down the ADC
 
 //----------Show ADCH Byte in Led variable brightness indicator---------
 
 for (i = 0 ; i < ADCH ; i++) // Loop x time until i reach ADCH value
 {
 _delay_ms (10); // Loop delay
 }
 
 PORTB ^= (1 << LED); // Inverte led bit and show it
 }
 return 0;
 }
 


Interrupts


GIMSK register (General Interrupt Mask)......................................................................
Gimskp.jpg

  • The External Interrupts are triggered by the INT0 pin or any of the PCINT5..0 pin

MCUCR register (MCU Control Register).......................................................................

MCUCRr.jpg

  • Bits 1, 0 – ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0 ( 0 to 5 volts or 5 to 0 volts)



INTsenscont.jpg
Interrupt library reference (vector name)
 /* -----------------------------------------------------------------------
 * Title: External interrupt (light on when boutton pressing)
 * Hardware: ATtiny13
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 
 
 #define F_CPU 1200000 // clock 1.2MHz, internal oscillator
 
 #include <avr/io.h>
 #include <util/delay.h>
 #include <avr/interrupt.h>
 
 #define LED PB4
 
 int i;
 int Data ;
 
 
 int main(void) {
 
 
 GIMSK = _BV (INT0); // int - Enable external interrupts int0
 MCUCR = _BV (ISC01); // int - INT0 is falling edge
 sei(); // int - Global enable interrupts
 
 DDRB |= (1 << LED); // Set direction register output
 
 for (;;) // loop (endless)
 {
 PORTB |= (1 << LED); // Set 1 on LED pin (led turn off)
 }
 
 return 0;
 }
 
 ISR (INT0_vect) // Interrupt on Int0 vector
 {
 
 PORTB &= ~(1 << LED); // Set 0 on LED pin (led turn on)
 _delay_ms (1000);
 }
 



Time counter (PWM)


• Two Independent Output Compare Units
• Double Buffered Output Compare Registers
• Clear Timer on Compare Match (Auto Reload)
• Glitch Free, Phase Correct Pulse Width Modulator (PWM)
• Variable PWM Period
• Frequency Generator
• Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)

Register
  • TCNT0 - The Timer/Counter
  • OCR0A and OCR0B - Output Compare Registers
  • TIFR0 - Timer Interrupt Flag Register
  • TIMSK0 - Timer Interrupt Mask Register
  • TCCR0B Timer/Counter Control Register B

TCCR0A - Time counter control Register .............................................................

TCCR0Al.jpg

  • Bits 7:6 – COM01A:0: Compare Match Output A Mode
  • Bits 5:4 – COM0B1:0: Compare Match Output B Mode
  • Bits 3, 2 – Res: Reserved Bits
  • Bits 1:0 – WGM01:0: Waveform Generation Mode

1. Tableau Compare Output Mode, Fast PWM Mode
coutm.jpg

2. Waveform Generation Mode
wgm.jpg

TCCR0B - Time counter control Register B...........................................................

TCCR0B.jpg

  • Bit 7 – FOC0A: Force Output Compare A
  • Bit 6 – FOC0B: Force Output Compare B
  • Bit 3 – WGM02: Waveform Generation Mode
  • Bits 2:0 – CS02:0: Clock Select

WGM02 - Waveform Generation Mode (WGM2 --- > tableau 1)

CS0 Prescaler Time ( Fosc / prescaler )
1 2 0 <----- Cs01,2,3
0 0 0 No clock source (Timer/Counter stopped)
0 0 1 clkI/O/(No prescaling)
0 1 0 clkI/O/8 (From prescaler)
0 1 1 clkI/O/64 (From prescaler)
1 0 0 clkI/O/256 (From prescaler)

TIMSK0 - Timer/Counter Interrupt Mask register.................................................

TIMSK0.jpg

  • Bit 3 – OCIE0B: Timer/Counter Output Compare Match B Interrupt Enable
  • Bit 2 – OCIE0A: Timer/Counter0 Output Compare Match A Interrupt Enable
  • Bit 1 – TOIE0: Timer/Counter0 Overflow Interrupt Enable


TIFR0 - Timer/Counter 0 Interrupt Flag register....................................................

tif.jpg
  • Bit 3 – OCF0B: Output Compare Flag 0 B
  • Bit 2 – OCF0A: Output Compare Flag 0 A
  • Bit 1 – TOV0: Timer/Counter0 Overflow Flag


OCR0A-(B) Output Compare Register A (B).......................................................
  • 8 Bit register (RW)

TCNT0 Timer/Counter Register........................................................................
  • 8 Bit register (RW)

 /* -----------------------------------------------------------------------
 * Title: Change duty cycle PWM
 * Hardware: ATtiny13
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 
 #define F_CPU 9600000UL // Define software reference clock for delay duration
 #include <avr/io.h>
 #include <util/delay.h>
 #define LED PB0
 int i ;
 
 int main (void)
 {
 DDRB |= (1 << LED); // OC0A on PB0
 
 TCCR0A |= ((1 << COM0A1) | (1 << COM0A0) // COM0A1 - COM0A0 (Set OC0A on Compare Match, clear OC0A at TOP)
 | (1 << WGM01) | (1 << WGM00)); // WGM01 - WGM00 (set fast PWM)
 OCR0A = 0; // initialize Output Compare Register A to 0
 TCCR0B |= (1 << CS01); // Start timer at Fcpu / 256
 
 for (;;)
 {
 
 for (i = 0 ; i < 255 ; i++ ) // For loop (Up counter 0 - 255)
 {
 OCR0A = i; // Update Output Compare Register (PWM 0 - 255)
 _delay_ms(100);
 }
 
 for (i = 255 ; i > 1 ; i-- ) // For loop (down counter 255 - 0 )
 {
 OCR0A = i; // Update Output Compare Register (PWM 0 - 255)
 _delay_ms(100);
 }
 }
 }
 
pwmmode.jpg

cout.jpg

 /* -----------------------------------------------------------------------
 * Title: Change frequency (time counter)
 * Hardware: ATtiny13
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #define LED PB0
 
 int main (void)
 {
 DDRB |= (1 << LED);
 
 TCCR0A |= (1 << WGM01); // Configure timer 1 for CTC mode
 
 TIMSK0 |= (1 << OCIE0A); // Enable CTC interrupt
 
 sei(); // Enable global interrupts
 
 OCR0A = 16; // Set CTC compare value
 
 TCCR0B |= (1 << CS01); // Start timer at Fcpu/64
 
 for (;;)
 {
 
 }
 }
 
 ISR(TIM0_COMPA_vect)
 {
 PORTB ^= (1 << LED); // Toggle the LED
 }
 
 

Code example



Attiny13 Serial communication input

( from ardino spi homemade http://arduinocode.wikispaces.com/ )
 /* -----------------------------------------------------------------------
 * Title: Serial communication input (led blinking indication)
 * Author: Samuel St-Aubin (samuel.st-aubin@sympatico.ca)
 * Software: WinAVR 20060421
 -----------------------------------------------------------------------*/
 #include <avr/io.h>
 #include <avr/interrupt.h>
 
 #define SD_DATAIN PB0 // Data in
 #define SD_CS PB1 // Chip select
 #define SD_CLK PB2 // Clock
 #define LED PB4 // Led (indicate the value of data)
 
 int i;
 int s;
 volatile int Data;
 
 int main(){
 
 GIMSK = _BV (INT0); // int - Enable external interrupts int0
 MCUCR = _BV (ISC01); // int - INT0 is falling edge
 sei(); // int - Global enable interrupts
 
 Data = 150; // initilize data register to 150
 
 DDRB &= ~(1<<SD_DATAIN); // Set direction register output Datain
 DDRB &= ~(1<<SD_CLK); // Set direction register output clock
 DDRB |= (1 << LED); // Set direction register output
 
 for (;;) // Main loop (endless)
 
 {
 for (s = 0 ; s < 255 ; s++) // /master loop show the value
 { // /(brighness) of data input.
 
 if (Data > s)
 {
 PORTB &= ~(1 << LED); // Set 0 on LED pin (led turn on)
 }
 else
 {
 PORTB |= (1 << LED); // Set 1 on LED pin (led turn off)
 }
 }}
 return 0;
 }
 
 ISR (INT0_vect) // Interrupt on Int0 vector
 {
 
 for (i = 0 ; i < 8 ; i++) // For loop 0 to 7
 {
 while (bit_is_clear(PINB,SD_CLK)); // Rising edge detector on Serial Clk
 
 if bit_is_set(PINB,SD_DATAIN) // Bit detector on Sd_Data input
 {
 Data ^= (1<<i); // Update data register (one bit by one) 8 time
 }
 
 while (bit_is_set(PINB,SD_CLK)); // Faling edge detector on Serial Clk
 }
 
 }
ปั๊มน้ำพลังงานแสงอาทิตย์
ในระบบปั๊มน้ำพลังงานแสงอาทิตย์นั้นมีประโยชน์อันมากมายต่อชาวไร่ ชาวนาและบ่อเลี้ยง เนื่องจากระบบนี้จะช่วยทางด้านลดค่าใช้จ่ายในเรื่องการลงทุนทำเกษตรได้และปั๊มน้ำพลังงานแสงอาทิตย์นั้นก็ทำให้คุณหมดสิ้นเรื่องค่าใช้จ่ายไฟฟ้าและค่าน้ำมันได้เป็นอย่างมาก


Logic gate

 AND ( & )
 x: 10001101
 y: 01010111
 x & y: 00000101
 
 OR ( | )
 x: 10001101
 y: 01010111
 x | y: 11011111
 
 XOR ( ^ )
 x: 10001101
 y: 01010111
 x ^ y: 11011010
 
 NOT ( ! )
 x: 10001101
 y =!x: 01110010
 
 
 Val %= 255 Modulo ( recommence après 255 )
 
 Val-= 10 égal a val = val –10
 
[[#htmldiff1]]
 
 int integer = 1025;
 
 Serial.write((byte)(integer & 255));
 Serial.write(13);
 Serial.write((byte)((integer & 65280) >> 8 ));
 Serial.write(13);
 delay(10000);
 
 }
[[#htmldiff2]]