0% found this document useful (0 votes)
2 views2 pages

Final Microcontroller Ez

The document provides an overview of AVR Timer-Counter functionality, including registers for timer operations, interrupt handling, and analog-to-digital conversion. It details programming examples in C and Arduino for controlling timers, external interrupts, and ADC readings. Additionally, it covers digital and analog pin interfaces, including examples for using sensors and actuators.

Uploaded by

6522780624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Final Microcontroller Ez

The document provides an overview of AVR Timer-Counter functionality, including registers for timer operations, interrupt handling, and analog-to-digital conversion. It details programming examples in C and Arduino for controlling timers, external interrupts, and ADC readings. Additionally, it covers digital and analog pin interfaces, including examples for using sensors and actuators.

Uploaded by

6522780624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

AVR Timer-Counter KRITTIN THIRASAK 6422780088

1) TCNT0 register = [8-bits] start clock number (0x00 - 0xFF) à set TOV0 flag C programming C - Interrupt Example
when overflow Ø Set n-th bit of PORTB à PORTB |= (1<<n) Ø External INT1 (falling-edge) = SW1 – LED1
2) TCCR0A & TCCR0B register = set mode & clock Ø Clear n-th bit of PORTB à PORTB &= ~(1<<n) Ø Pin Change PD5 = SW2 – LED2
Ø Toggle n-th bit of PORTB à PORTB ^= (1<<n) Ø Timer0 (normal/pre-scaling 1024) = LED3 every 1 sec
Vector name of interrupt in C (every 5000 us à 200 Timer0)

Ø Timer operation
o Normal mode à WGM02:00 = 000 [set TCCR0A = 0x00]
o CTC mode à WGM02:00 = 010
Ø Input source
o Stop clock à CS02:00= 0x00 [set TCCR0B = 0x00]

3) OCR0A & OCR0B = [8-bits] set upper bound for CTC mode
4) TIFR0 = flag for choose which OCR & TOV0 flag (equal 1 when overflow)

Arduino
Time Delay Calculation
Ø Normal mode (no pre-scaling) à B=255 / P=1
Ø Normal mode (with pre-scaling) à B=255 / P=pre-scaling
Ø CTC mode à B=upper bound / P=pre-scaling

AVR Interrupt
1) Timer0 Interrupt
Ø TIMSK0 register = enable interrupt & compare with TIFR0

C - Timer Example

2) External Interrupt
Ø EIMSK = enable interrupt à INT0 = PD2 (0x02) / INT1 = PD3 (0x04)

Ø EICRA = set condition à INT0 = ISC01:00 / INT1 = ISC11:10

Serial Port = async commy àTxD of A <=> RxD of B & otherwise USART Tx Example
Ø UBBR0 = [16-bit] setup baud rate

Ø UDR0 = transmitted/received data

3) Pin Change Interrupt Ø UCSR0A


Ø PCICR = enable interrupt for specific port o RXC0 à RX complete = 1 / no data = 0 USART Rx Example
o UDRE0 à ready to TX = 1 / TX buffer not empty = 0

Ø UCSR0B
o RXEN0 à RX enable, 1 if RX
o TXEN0 à TX enable, 1 if TX

Ø UCSR0C
Ø PCMSK = specific PIN o UCSZ02:00 = char size
(000=5 / 001=6 / 010=7 / 011=8 / 111=9)
ADC AVR Analog to Digital Ø ADLAR = digital value result à ADC = ADCH:ADCL ADC Example
Ø ADMUX = flag Ø Read potentiometer into 10-bit and 10 LEDs
o Last 8 bit on PIND (PD0 – PD7)
o First 2 bit on PINB (PB0 – PB1)
o REFS1:0 à reference voltage [step size = Vref/1024] Ø Using VCC (5 V) / ADC0 pin / right-justified

Ø ADCSRA = status & control register

o ADLAR à specify which bit to store result o ADEN à 1 if ADC enable


o MUX3:0 à analog channel selection o ADSC à ADC start conversion, set to 1 for each conversion
o ADIF à set to 1 when conversion complete & data updated
o ADPS2:0 à prescaler select bit

Arduino Programming Ø Buzzer Example – Soil moisture sensor (Left) & Buzzer (Right)
Ø Data Type o digitalWrite(buzzerPin, MODE);

Ø Soil Moisture Sensor


o analogRead(pin);

Ø Serial Monitor
o Serial.begin(9600); à initialize with baud rate = 9600 bps Ø Keypad
o Serial.print(“msg”); Serial.println(x); à print
text/variable
Ø Time Delay
o delay(1000); à delay = 1000 ms
o delayMicroseconds(1000); à delay = 1000 us
Digital Interface/Pin
Ø Setting Pin à pinMode(pin, MODE);
o pin = indicate between pin 0 – 13
o MODE = INPUT or OUTPUT
Ø Write Output à digitalWrite(pin, value); Example – Keypad Setup
o pin = indicate between pin 0 – 13
o value = LOW (0V) or HIGH (5V)
Ø Read Output à x=digitalRead(pin); Extra: ADC calculation (5V input)
o x = store value between 0 and 1
o pin = indicate between pin 0 – 13
Analog Interface/Pin
Ø PWM pin à analogWrite(pin, value);
o pin = pin name which is 3, 5, 6, 9, 10, 11 to indicate PWM pin
o value = between 0 (0V) – 255 (5V)
Ø Analog pin à x=analogRead(pin);
o x = store value between 0 – 1023 [map from 0V – 5V]
o pin = analog pin between A0 – A5
Sensor & Actuator
Ø Photoresistor or Light-Dependent Resistor (LDR)
o analogRead(pin);

Example - Basic Digital Arduino


Example – Keypad Usage

KRITTIN THIRASAK 6422780088

You might also like