Final Microcontroller Ez
Final Microcontroller Ez
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)
Serial Port = async commy àTxD of A <=> RxD of B & otherwise USART Tx Example
Ø UBBR0 = [16-bit] setup baud rate
Ø 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
Arduino Programming Ø Buzzer Example – Soil moisture sensor (Left) & Buzzer (Right)
Ø Data Type o digitalWrite(buzzerPin, MODE);
Ø 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);