51 microcontroller and buzzer realize Morse code conversion

Publisher:reaper2009Latest update time:2023-01-30 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Specific function implementation

When we press different keys in the matrix keys, the corresponding content is displayed on the LCD1602. Pressing a key multiple times at the same time can switch to different letters or numbers, and the LED light will flash. Finally, when we press confirm, the buzzer will sound with different frequencies.

device

Buzzer, AT89C51, several buttons, LCD1602, two LED lights, resistor

Simulation display diagram

Before simulation


After simulation


Knowledge introduction

Matrix button control principle

If independent buttons are used to connect to the microcontroller, each button requires an I/O port of the microcontroller. If a microcontroller system requires more buttons, using independent buttons will occupy too many I/O port resources. I/O port resources are often precious in single-chip microcomputer systems. In order to reduce I/O port pins when multiple buttons are used, matrix buttons are introduced.

Matrix key detection principle

Key detection is generally implemented by scanning. First, a certain column becomes low level, and the remaining columns are high level, and then it is detected whether a low level appears in each row. If it does not appear, it means that a certain row of keys has not been pressed. If it appears, it means that the key has been pressed and the position can be locked.

Matrix button control method

①Determinant method:

In the above picture, all the first columns are set to 0, and the remaining positions are all 1.

Set all the first rows to 0, and set all other bits to 1.

Therefore, we can conclude that the button pressed is exactly the first row and first column.

② Line flipping method:

First, set all rows to 0, then detect whether the column contains a low level, and if so, record the position of the column; then flip it, set all columns to 0, detect whether the row has a low level, and if so, record The position of the row (this article uses the line flipping method)

Main function code (C language) KEIL5 implementation

#include

#include

void delay(unsigned char ms);

void delay2(int i);

void lcd_wcmd(unsigned char cmd);

void lcd_pos(unsigned char pos);

void lcd_wdat(unsigned char dat);

void lcd_init();

void xianshi();

void KeyDown();

void la_ba();


sbit rs= P2^6;

sbit rw = P2^5;

sbit ep = P2^7;

sbit d=P3^0;

sbit d1=P3^1;

sbit lb=P3^2;

int b,c,s=0,q=0,w=0;

#define GPIO_KEY P1

unsigned char dis1[32];

unsigned char dis2[9]={',','A','D','G','J','M','P','T','W'};

unsigned char dis3[9]={'1','2','3','4','5','6','7','8','9'};

unsigned int code laba[36][5]={

1,2,3,3,3,//A

2,1,1,1,3,//B

2,1,2,1,3,//C

2,1,1,3,3,//D

1,3,3,3,3,//E

1,1,2,1,3,//F

2,2,1,3,3,//G

1,1,1,1,3,//H

1,1,3,3,3,//I

1,2,2,2,3,//J

2,1,2,3,3,//K

1,2,1,1,3,//L

2,2,3,3,3,//M

2,1,3,3,3,//N

2,2,2,3,3,//0

1,2,2,1,3,//P

2,2,1,2,3,//Q

1,2,1,3,3,//R

1,1,1,3,3,//S

2,3,3,3,3,//T

1,1,2,3,3,//U

1,1,1,2,3,//V

1,2,2,3,3,//W

2,1,1,2,3,//X

2,1,2,2,3,//Y

2,2,1,1,3,//Z

2,2,2,2,2,//0

1,2,2,2,2,//1

1,1,2,2,2,//2

1,1,1,2,2,//3

1,1,1,1,2,//4

1,1,1,1,1,//5

2,1,1,1,1,//6

2,2,1,1,1,//7

2,2,2,1,1,//8

2,2,2,2,1,//9

};

//LCD1602 code starts

bit lcd_bz()

{

bit result;

rs = 0;

rw = 1;

ep = 1;

_nop_();

_nop_();

_nop_();

_nop_();

result = (bit)(P0 & 0x80);

ep = 0;

return result;

}

void lcd_wcmd(unsigned char cmd)

{

while(lcd_bz());//Determine whether the LCD is busy

rs = 0;

rw = 0;

ep = 0;

_nop_();

_nop_();

P0 = cmd;

_nop_();

_nop_();

_nop_();

_nop_();

ep = 1;

_nop_();

_nop_();

_nop_();

_nop_();

ep = 0;

}

void lcd_pos(unsigned char pos)

{

lcd_wcmd(pos | 0x80);

}

void lcd_wdat(unsigned char dat)

{

while(lcd_bz());//Determine whether the LCD is busy

rs = 1;

rw = 0;

ep = 0;

P0 = that;

_nop_();

_nop_();

_nop_();

_nop_();

ep = 1;

_nop_();

_nop_();

_nop_();

_nop_();

ep = 0;

}

void lcd_init()

{

lcd_wcmd(0x38);

delay(1);

lcd_wcmd(0x0c);

delay(1);

lcd_wcmd(0x06);

delay(1);

lcd_wcmd(0x01);

delay(1);

}

void xianshi()

{

int i=0,j=1,n=0;

while(dis1[i] != '')

{

lcd_pos(0x00);//Set the display position

while(j!=0&&dis1[i] != '')

{

lcd_wdat(dis1[i]);//Display characters

i++;

j=i%16;

}

lcd_pos(0x40);//Set the display position

j=1;

while(j!=0&&dis1[i] != '')

{

lcd_wdat(dis1[i]);//Display characters

i++;

j=i%16;

}

j=1;

if(dis1[i] != '')

i-=16;

}

i=0;

}

//LCD1602 code ends

void delay(unsigned char ms)

{

unsigned char i;

while(ms--)

{

for(i = 0; i< 250; i++)

{

_nop_();

_nop_();

_nop_();

_nop_();

}

}

}

void delay2(int i)

{

while(i--);

}

//Press the button

void KeyDown()

{

int KeyValue=0;

GPIO_KEY=0x0f;

if(GPIO_KEY!=0x0f)

{

delay(5); //Debounce

if(GPIO_KEY!=0x0f)

{

GPIO_KEY=0X0F;

switch(GPIO_KEY)

{

case(0X07): KeyValue=0;break;

case(0X0b): KeyValue=1;break;

case(0X0d): KeyValue=2;break;

case(0X0e): KeyValue=12;break;

}

}

GPIO_KEY=0XF0;

switch(GPIO_KEY)

{

case(0X70): KeyValue=KeyValue;break;

case(0Xb0): KeyValue=KeyValue+3;break;

case(0Xd0): KeyValue=KeyValue+6;break;

case(0Xe0): KeyValue=KeyValue+9;break;

}

while(GPIO_KEY!=0xf0);

if(KeyValue!=c&&dis1[w]!='')

{b=0;w++;}

if(KeyValue<=8&&s==0)

{

if(c==12||c==15)w--;

dis1[w]=dis2[KeyValue]+b;

d=0;delay(25);d=1;delay(25);

}

else if(KeyValue<=8&&s==1)

{

if(c==12||c==15)w--;

dis1[in]=dis3[KeyValue];

d=0;delay(25);d=1;delay(25);

}

else if(KeyValue==12)

{

//When the key is 12, delete one digit (backspace)

if(w>0)

{

In--;

dis1[w]=' ';

}

else

{

dis1[w]=' ';

}

d1=0;delay(25);d1=1;delay(25);

}

else if(KeyValue==15)

{

//When 15 is pressed, add a space

while(w!=0)

{

In--;

dis1[w]=' ';

}

}

else if(KeyValue==9)

{

//display number 7

q++;

s=q%2;

if(c==12||c==15)w-=2;

}

else if(KeyValue==10)

{

//display number 8

if(s==0)

dis1[w]=' ';

else

dis1[w]='0';

}

else if(KeyValue==11)

{

//display number 9

dis1[w]=' ';

}

else if(KeyValue==18)

{

dis1[w]=' ';

In--;

}

else if(KeyValue==21)

{

la_ba();

}

b++;

if(b==3)b=0;

c=KeyValue;

}

}

//buzzer

void la_ba()

{

int i,j,t;

for(i=0;dis1[i]!='';i++)

{

if(dis1[i]>=65&&dis1[i]<=90)

{

d1=0;delay(25);d1=1;delay(25);

for(j=0;laba[dis1[i]-65][j]!=3;j++)

{

if(laba[dis1[i]-65][j]==1)

{

t=100;

while(t--)

{

lb=~lb;

delay2(70);

}

delay(50);

}

if(laba[dis1[i]-65][j]==2)

{

t=300;

while(t--)

{

lb=~lb;

delay2(70);

}

delay(50);

}

if(laba[dis1[i]-65][j+1]==3)

{

delay(100);

}

}

}

if(dis1[i]>=48&&dis1[i]<=57)

{

d=0;delay(25);d=1;delay(25);

for(j=0;j<5;j++)

{

if(laba[dis1[i]-22][j]==1)

{

t=100;

while(t--)

{

lb=~lb;

delay2(70);

}

delay(50);

}

if(laba[dis1[i]-22][j]==2)

{

t=300;

while(t--)

{

lb=~lb;

delay2(70);

}

delay(50);

}

if(j==4)

{

delay(100);

}

}

}

}

}

void main()

{

lcd_init();//Initialize LCD

delay(10);

while(1)

{

KeyDown();

xianshi();

}

}


Reference address:51 microcontroller and buzzer realize Morse code conversion

Previous article:Alternative entry methods and programming ideas for 51 microcontrollers
Next article:Implementing "reversing radar" anti-collision system based on HC-SR04 ultrasonic module

Recommended ReadingLatest update time:2024-11-16 10:24

51 single chip microcomputer uses an independent button to control the buzzer
After the key is pressed for the first time, the buzzer sounds; After pressing the key for the second time, the buzzer stops. #include reg52.h sbit beep=P1^5; sbit key=P3^1; unsigned int mode; //define a mode   void delay(unsigned int i) { while(i--); }   void voice() { if(mode) {   beep=~beep; delay(10
[Microcontroller]
8051 MCU (STC89C52) buzzer sound
The statement "sbit beep = P1 ^ 5" is used to specify the buzzer drive port: when beep = 0, the buzzer does not sound; when beep = 1, the buzzer sounds. In addition, different tones can be obtained by changing the time of the driver port level flip, and different volumes can be obtained by changing the ratio of high a
[Microcontroller]
6.Buzzer
buzzer: The source refers to whether it contains an oscillation circuit. Passive buzzer: has a green circuit board; cannot be directly controlled by high or low level, but can only be triggered by pulses of a certain frequency, and has no oscillator circuit inside. The development board uses a passive one. Active
[Microcontroller]
6.Buzzer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号