4x4 Keypad Teil 2

In our previous Blog we have shown how to AZ-Delivery Keypad and assigns the 16 keys correctly. In our next example, we want to use the keypad creatively together with a passive buzzer Imagine. We follow the example Keypad_Piano on arduino.cc. We can neglect the pin assignment in this example because they can be mapted as desired, but we show an interesting way to switch between stored PWM frequencies using two 8-bit shift registers used as timer in the 328P.

This project is intended for advanced users because it requires deep knowledge of the 328P (timer & register)

To do this, we need:

First, we connect the following pins:

  • Keypad Pin 1 - 8 an Uno Pin 2 - 9
  • we supply the buzzer with 5V and GND from the Uno and the pin marked with "S" on pin 11

Here's what our code looks like:

 

Pin assignment for the keypad
#define c3 9
#define c2 8
#define c1 7
#define c0 6
#define r3 5
#define R2 4
#define r1 3
#define r0 2

Void Setup() {   Serial.Begin(9600);
 pinMode(c0,INPUT_PULLUP); 
 pinMode(c1,INPUT_PULLUP);
 pinMode(c2,INPUT_PULLUP);
 pinMode(c3,INPUT_PULLUP);
 pinMode(r0,Output);
 pinMode(r1,Output);
 pinMode(R2,Output);
 pinMode(r3,Output);
 pinMode(11,Output); for our Buzzer module
}

Void Loop(){
digitalWrite(r0,Low);
digitalWrite(r1,High);
digitalWrite(R2,High);
digitalWrite(r3,High);
If(digitalRead(c0)==Low){
TCCR2A = B01000011; Frequency assignment
TCCR2B = B00001110; takes place here
OCR2A=119;          using the register and the clock frequency
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c1)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=106;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c2)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=94;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c3)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=89;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}

digitalWrite(r0,High);
digitalWrite(r1,Low);
digitalWrite(R2,High);
digitalWrite(r3,High);
If(digitalRead(c0)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=79;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c1)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=71;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c2)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=63;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c3)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=59;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}

digitalWrite(r0,High);
digitalWrite(r1,High);
digitalWrite(R2,Low);
digitalWrite(r3,High);
If(digitalRead(c0)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=25;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c1)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=20;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If (digitalRead(c2)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=10;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c3)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=50;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}

digitalWrite(r0,High);
digitalWrite(r1,High);
digitalWrite(R2,High);
digitalWrite(r3,Low);
If(digitalRead(c0)==Low){

TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=50;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c1)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=50;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c2)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=50;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
else If(digitalRead(c3)==Low){
TCCR2A = B01000011;
TCCR2B = B00001110;
OCR2A=50;
Delay(100);
TCCR2B=B00001000;
TCCR2A = B000000000;
digitalWrite(11,Low);}
} Using the formula: Hz = 16MHz/prescalar/(OCRA+1)/2
PWM frequencies of up to 8MHz can be generated in this way

Have fun crafting and until the next time :-)

 

Produktvorstellungen

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN