Components and supplies
Pushbutton switch 12mm
General Purpose Transistor NPN
Arduino UNO
Speaker: 0.25W, 8 ohms
24C512
Resistor 2.21k ohm
Capacitor 22 µF
General Purpose Transistor PNP
Project description
Code
bell
c_cpp
1/* 2 * eeprom_bell.ino 3 * 4 * Created: 30/06/2017 23:34:47 5 * Author: moty22.co.uk 6 */ 7#include <Wire.h> 8 9 10const int audio = 6; //output D6 11const int ring = A2; //ring pushbutton 12int ringPB=1; // ring pushbutton status 13 14void setup() { 15 // put your setup code here, to run once: 16 17pinMode(ring, INPUT_PULLUP); 18 19 //PWM Timer0 20OCR0A = 64; 21TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00); //output in phase, fast PWM mode 22TCCR0B = _BV(CS00); // 16MHz/256 = 64KHz 23 24Wire.begin(); // init i2c bus 25Wire.setClock(200000); //200khz 26 27} 28 29 30 31void loop() 32{ 33 unsigned int i; 34 35 ringPB=digitalRead(ring); 36 if(ringPB==LOW) //pushbutton 37 { 38 pinMode(audio, OUTPUT); 39 //go to address 0 40 Wire.beginTransmission(0x50); 41 Wire.write((int)(0)); //MSB of eeprom address 42 Wire.write((int)(0)); // LSB 43 Wire.endTransmission(); 44 45 46 //start sequential read 47 TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //start command 48 while (!(TWCR & (1<<TWINT))); //till interrupt 49 //write address 50 TWDR = 0b10100001; //address+read 51 TWCR = (1<<TWINT)|(1<<TWEN); 52 while (!(TWCR & (1<<TWINT))); //till interrupt 53 54 //read all the bytes 55 for (i=0; i<0xfff0; i++) 56 { 57 //send ACK 58 TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA); 59 while (!(TWCR & (1<<TWINT))); 60 OCR0A = TWDR; //PWM = eeprom data 61 } 62 63 //send NACK, no-aknowledgement + stop commands ends reading 64 TWCR = (1<<TWINT)|(1<<TWEN); 65 while (!(TWCR & (1<<TWINT))); 66 OCR0A = TWDR; 67 68 //stop 69 TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN); 70 71 pinMode(audio, INPUT); //stop the audio output 72 } 73 74} 75 76 77 78
bell
c_cpp
1/* 2 * eeprom_bell.ino 3 * 4 * Created: 30/06/2017 23:34:47 5 6 * Author: moty22.co.uk 7 */ 8#include <Wire.h> 9 10 11const int audio 12 = 6; //output D6 13const int ring = A2; //ring pushbutton 14int ringPB=1; 15 // ring pushbutton status 16 17void setup() { 18 // put your setup 19 code here, to run once: 20 21pinMode(ring, INPUT_PULLUP); 22 23 //PWM Timer0 24OCR0A 25 = 64; 26TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00); //output in phase, fast 27 PWM mode 28TCCR0B = _BV(CS00); // 16MHz/256 = 64KHz 29 30Wire.begin(); // 31 init i2c bus 32Wire.setClock(200000); //200khz 33 34} 35 36 37 38void loop() 39{ 40 41 unsigned int i; 42 43 ringPB=digitalRead(ring); 44 if(ringPB==LOW) 45 //pushbutton 46 { 47 pinMode(audio, OUTPUT); 48 //go to 49 address 0 50 Wire.beginTransmission(0x50); 51 Wire.write((int)(0)); 52 //MSB of eeprom address 53 Wire.write((int)(0)); // LSB 54 Wire.endTransmission(); 55 56 57 58 //start sequential read 59 TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); 60 //start command 61 while (!(TWCR & (1<<TWINT))); //till interrupt 62 //write 63 address 64 TWDR = 0b10100001; //address+read 65 TWCR = (1<<TWINT)|(1<<TWEN); 66 67 while (!(TWCR & (1<<TWINT))); //till interrupt 68 69 //read 70 all the bytes 71 for (i=0; i<0xfff0; i++) 72 { 73 //send ACK 74 75 TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA); 76 while (!(TWCR & (1<<TWINT))); 77 78 OCR0A = TWDR; //PWM = eeprom data 79 } 80 81 //send 82 NACK, no-aknowledgement + stop commands ends reading 83 TWCR = (1<<TWINT)|(1<<TWEN); 84 85 while (!(TWCR & (1<<TWINT))); 86 OCR0A = TWDR; 87 88 //stop 89 90 TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN); 91 92 pinMode(audio, INPUT); 93 //stop the audio output 94 } 95 96} 97 98 99 100
Downloadable files
bell_eeprom
bell_eeprom
Comments
Only logged in users can leave comments
moty
5 Followers
•15 Projects
0