ATtiny 85 Piano TYWu Getting Extra Pins on

  • Slides: 19
Download presentation
ATtiny 85 Piano TYWu

ATtiny 85 Piano TYWu

Getting Extra Pins on ATtiny 85 • Only Getting Extra INPUT Pins – Using

Getting Extra Pins on ATtiny 85 • Only Getting Extra INPUT Pins – Using DAC • Version 1 • Version 2 i 1~i 5 DAC

DAC (Version 1) VDD=5 V • Software Pull-up A 1 • Single ON –

DAC (Version 1) VDD=5 V • Software Pull-up A 1 • Single ON – analog. Read(A 1) • ≈ 1023 * 4/5 or • ≈ 1023 * 3/5 or • ≈ 1023 * 2/5 or • ≈ 1023 * 1/5 or • ≈ 1023 * 0 Turning On A 1: VDD*4/5

DAC (Version 1) • Multiple ON – What’s the voltage of A 1? VDD=5

DAC (Version 1) • Multiple ON – What’s the voltage of A 1? VDD=5 V

DAC (Version 1) • button = (analog. Read(A 1)*5+512)/1024; – Return a number between

DAC (Version 1) • button = (analog. Read(A 1)*5+512)/1024; – Return a number between 0 and 5. – Return 5 when a ? a VDD=5 V

DAC (Version 1) • Disadvantage – Draw current even when no button is pressed;

DAC (Version 1) • Disadvantage – Draw current even when no button is pressed; VDD I

DAC (Version 2) • Hardware Pull-up – No current (I = 0) when no

DAC (Version 2) • Hardware Pull-up – No current (I = 0) when no button is pressed VDD

DAC (Version 2) • button = (analog. Read(A 1)*5+512)/1024; VDD=5 V (0*1023*5+512)/1024 = 0

DAC (Version 2) • button = (analog. Read(A 1)*5+512)/1024; VDD=5 V (0*1023*5+512)/1024 = 0 (1. 5/(6. 8+1. 5)*1023*5+512)/1024 = 1 (24. 5/31. 3*1023*5+512)/1024 = 4

DAC (Version 2) • Tinkercad 4. 5 V

DAC (Version 2) • Tinkercad 4. 5 V

Keypad • Schematic R 1, R 2, R 3, R 4, C 1, C

Keypad • Schematic R 1, R 2, R 3, R 4, C 1, C 2, C 3, C 4

Piano • Tinkercad

Piano • Tinkercad

Piano • Schematic

Piano • Schematic

Piano • Sketch #define speaker. Pin 3 #define column. Pin A 2 int row.

Piano • Sketch #define speaker. Pin 3 #define column. Pin A 2 int row. Pin[] = {0, 1, 2}; //row 1, row 2, row 3 int tempo = 50; void setup() { pin. Mode(speaker. Pin, OUTPUT); for(int i=0; i<3; i++) pin. Mode(row. Pin[i], OUTPUT); for(int i=0; i<3; i++) digital. Write(row. Pin[i], LOW); }

Piano • Sketch int key=0; void loop() { int note=0; if((note=scan. Pad())!=0 && note!=9)

Piano • Sketch int key=0; void loop() { int note=0; if((note=scan. Pad())!=0 && note!=9) play. Note(note, tempo); }

Piano • Sketch int scan. Pad() { int pad. Value=0; int column. Value=0; int

Piano • Sketch int scan. Pad() { int pad. Value=0; int column. Value=0; int temp=0; int i; for(i=0; i<3; i++) { digital. Write(row. Pin[i], HIGH); delay. Microseconds(50); for(int j=0; j<2; j++) { //scan 20 ms temp=0; if((temp=analog. Read(column. Pin))!=0) { column. Value=temp; break; } }

Piano • Sketch digital. Write(row. Pin[i], LOW); if(column. Value!=0) break; } //End of Loop

Piano • Sketch digital. Write(row. Pin[i], LOW); if(column. Value!=0) break; } //End of Loop if(column. Value!=0) { if(column. Value>750) pad. Value=i*3+3; //3, 6, 9 else if(column. Value<=750 && column. Value>630) pad. Value=i*3+2; //2, 5, 8 else if(column. Value>500)pad. Value=i*3+1; //1, 4, 7 } return(pad. Value); }

Piano • Sketch void play. Note(char note, int duration) { int tones[] = {

Piano • Sketch void play. Note(char note, int duration) { int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; play. Tone(tones[note-1], duration); }

Piano • Sketch void play. Tone(int tone, int duration) { for (long i =

Piano • Sketch void play. Tone(int tone, int duration) { for (long i = 0; i < duration * 1000 L; i += tone * 2) { digital. Write(speaker. Pin, HIGH); delay. Microseconds(tone); digital. Write(speaker. Pin, LOW); delay. Microseconds(tone); } return; }

Reference • http: //www. technoblogy. com/show? LSE • http: //091 labs. com/dokuwiki/lib/exe/detail. p hp?

Reference • http: //www. technoblogy. com/show? LSE • http: //091 labs. com/dokuwiki/lib/exe/detail. p hp? id=security_system_project&media=key pad-schematic. gif • https: //circuitdigest. com/microcontrollerprojects/keypad-interfacing-with-8051 microcontroller