Study Notes|Understanding the Buzzer|Control Principle|Induction Cooker LED Practical Operation|Logic Operation|STC32G Microcontroller Video Development Tutorial (Brother Chong)|Episode 8 (Part 1): Buzzer Application

1. Know the buzzer

The one on the reverse side covered with vinyl is an active buzzer.
Insert image description here
On the exposed piece of circuit board on the back is the passive buzzer.
Insert image description here
YX55675-passive buzzer module information
extraction code: nl73

the difference

1. The active buzzer has an internal oscillation source, so it will beep as long as it is powered on (one side is high, the other side is low), and the passive buzzer does not have an internal oscillation source, so if you use a DC signal, you cannot make it buzz ( Must keep giving high and low levels).
2. The price is different. The active buzzer is more expensive than the passive buzzer, because there are more shock sources in it. Active and easy to control. It is better to play music passively.
Let's start our experiment around this active buzzer.

2. Control principle

Schematic diagram:
Insert image description here
If the base of the triode is at low level, the SS8550 emits to the collector and conducts, connects to the positive pole of BEEP1, and grounds the negative pole of BEEP1.
The C35 capacitor plays a simple filtering role.
The P54 pin turns off when the high level is turned off , and the P54 pin turns on when the low level is turned on
.
The buzzer program! (Try it yourself before reading what I wrote!)

Implement buzzer control principle

Single key trigger code that needs to be memorized:

		if (KEY1 == 0)
		{
			delay_ms(10);
			if (KEY1 == 0)
			{
				while(KEY1 == 0);
				BEEP = !BEEP;
			}

		}

3. Practical application of buzzer

Physical map:
Insert image description here

demand analysis

This section chooses to control 8 LEDs (LED1~LED8) and KEY1 (switch) and KEY2 button (function selection).
Button 2, LED 8, buzzer*1
1. Press button 1, the buzzer will beep for 10ms, and the self-test will start.
LED1-8 will all light up for 200ms and then go out, indicating that it is turned on.
2. After powering on, press button 2, the buzzer will beep for 10ms, and LED1-8 will light up in turn, indicating switching between soup making, water boiling and other functions.
3. After powering on, press button 1 again, the buzzer will beep for 10ms, and all LEDs will go out, indicating shutdown.

Code writing

Step 1: Code writing and analysis

First create a test subdirectory under the project folder, and create test.c and test.h in the directory.
Add test.c to the project:
Insert image description here
Insert image description here

Fixed template for test.h

#ifndef	__TEST_H   //判断是否有test.h
#define __TEST_H


#include "COMM/stc.h"  //调用头文件
#include "COMM/usb.h"




#endif

Add the reference of test.h to test.c, add the test.c file into the project, and add the include file reference path:

Move the define file to test.h, through the previously added include, other files can access the predefined.

Then write the required functions.
The first step is to write the state variables and test function:

#include "test.h"

bit  Run_Flag = 0; //0-1开关机变量
u8   Run_Mode = 0; //0-8 模式几 0:没有模式

void Test(void)   //电磁炉的功能
{
		if (KEY1 == 0)   //开关机键
		{
			delay_ms(10);
			if (KEY1 == 0)
			{
				while(KEY1 == 0);	//按钮按下,并等待按钮松开
				if (Run_Flag = 0)  //表示还没有开机
				{
					Run_Flag = 1;   //开机变量改为1,表示已经开机
					BEEP = 0;		//打开蜂鸣
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					P40 = 0;		//打开了LED总电源
					P6 = 0X00;		//全部点亮
					delay_ms(200);   //延迟200ms
					P6 = 0XFF; 		//全部关闭
				}
			}

		}
}

The header file needs to contain the declaration of the function, such as:

void delay_ms(u16 ms);
void Test(void);

Start compiling, prompting "test\test.c(10): warning C140: 'delay_ms' undefined; assuming 'extern int delay_ms()'": The reason is that
delay_ms is not defined in test.h and needs to be moved first. If only When copying, the following error will be prompted:
Insert image description here

Tips: Suggestion: “test\test.c(14): error C16: unprintable character 0xA3 skipped

test\test.c(14): error C16: unprintable character 0xA9 skipped"
This is because the punctuation written in the Chinese input method appears in keil, for example: //lcm_w_word("Hello abc"); Full-width characters cannot appear Any characters, including spaces, and any punctuation marks and spaces can only be entered in English. If this error is prompted, just
replace the full-width characters or punctuation marks. However, you cannot see them under normal circumstances. You can only add them. // only found

Tips:“test\test.c(14): warning C137: constant in condition expression”

Instead of using = in if(), you should use ==. Using = is equivalent to assigning a constant to a variable, so the compiler prompts a warning. Next, if there is an else if, it will prompt: "warning C294: unreachable code" (there is no else if in this example, so it does not appear.)

Implement the main code

Add Test function in main.c:

    while(1) //死循环
    {
		if( DeviceState != DEVSTATE_CONFIGURED ) 	//判断USB是否连接成功,最新版usb.h中该有定义
			continue;
		if( bUsbOutReady )    //判断有没有接收到数据
		{
			usb_OUT_done(); //接收应答(固定格式)
		}
		Test();
	}

Defined in test.h:

#ifndef	__TEST_H   //判断是否有test.h
#define __TEST_H


#include "COMM/stc.h"  //调用头文件
#include "COMM/usb.h"

#define MAIN_Fosc 24000000UL

#define KEY1 P32
#define KEY2 P33

#define BEEP P54  //定义一个BEEP控制引脚

void delay_ms(u16 ms);
void Test(void);


#endif

The code to realize the custom variables and functions in the first step function in test.c:

#include "test.h"

bit  Run_Flag = 0; //0-1开关机变量
u8   Run_Mode = 0; //0-8 模式几 0:没有模式

void Test(void)   //电磁炉的功能
{
		if (KEY1 == 0)   //开关机键,第一步骤代码段
		{
			delay_ms(10);
			if (KEY1 == 0)
			{
				//while(KEY1 == 0);	//按钮按下,并等待按钮松开
				if (Run_Flag == 0)  //表示还没有开机
				{
					Run_Flag = 1;   //开机变量改为1,表示已经开机
					BEEP = 0;		//打开蜂鸣
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					P40 = 0;		//打开了LED总电源
					P2 = 0X00;		//全部点亮
					delay_ms(300);   //延迟200ms
					P2 = 0XFF; 		//全部关闭
				}
			}

		}
}

void delay_ms(u16 ms)	//unsigned int
{
	u16 i;
	do
	{
		i = MAIN_Fosc/6000;
		while(--i);
	}while(--ms);
}

After executing the code, the buzzer will beep once, and all 8 states will turn off after all lights are on. After that, there will be no response when pressing other buttons, and subsequent function codes will be added.

Step 2: Writing and analyzing shutdown code

After powering on, press button 1 again, the buzzer will beep for 10ms, all LEDs will go out, and the P40 main power indicator will go out, indicating shutdown.
The basic functional status is when key1 is pressed, and different execution codes are selected according to different boot variables.

				else
				{
					Run_Flag = 0;   //已关机
					BEEP = 0;		//打开蜂鸣(提醒)
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					P2 = 0XFF;		//状态指示全部关闭
					P40 = 1;		//关闭总电源LED
				}

Step 3: Writing and analyzing the status selection button code

After powering on, press button 2, the buzzer will beep for 10ms, and LED1-8 will light up in turn, indicating switching between soup making, water boiling and other functions.
Implementation code:

/*=======================按下按键2,执行本段代码=====================*/
		if (KEY2 == 0)   //开关机键
		{
			delay_ms(10);  //防抖
			if (KEY2 == 0)
			{
				while(KEY2 == 0);	//等待按键松开,将要执行
/*=======================还没有开机的情况下,执行本段代码=====================*/
				if (Run_Flag == 0)  //表示还没有开机
				{
					BEEP = 0;		//打开蜂鸣
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					Run_Mode++;		//每次按下,模式+1(移至下一个led)
					if (Run_Mode > 8)  //如果模式大于8,回到模式1
						Run_Mode = 1;  //Run_Mode超过8后清零,回到第一个灯

					P2 = ~(1<< (Run_Mode - 1));	//最先想到的“1<< Run_Mode”是错误写法,执行结果是8个灯中每次移动仅1个不亮。
												//灯是低电平点亮,1<< Run_Mode,即1<<1,左移以后为0000 0010,仅1个LED灭,继续左移,尾部补0,还是只有1个LED灭
												//解决方法:RunMode先减1,再左移,最后全部取反。这里用~为全取反。而!是位取反。
												//P2 = 0XFX;  P6<< 1 + 1;  也可以实现
				}

Test.c code that implements complete functionality

During execution, it is found that when the button is pressed for the first time, if it is in the power-on state and the 0-bit light does not light up, you need to add Run_Mode = 0; //The mode is cleared in the shutdown code.
The test.c code that implements the complete function is as follows:

#include "test.h"

bit  Run_Flag = 0; //0-1开关机变量
u8   Run_Mode = 0; //0-8 模式几 0:没有模式

void Test(void)   //电磁炉的功能
{
		if (KEY1 == 0)   //开关机键
		{
			delay_ms(1000);  //长按1s开机
			if (KEY1 == 0)
			{
				//while(KEY1 == 0);	//按钮按下,并等待按钮松开
/*=======================还没有开机的情况下,执行本段代码=====================*/
				if (Run_Flag == 0)  //表示还没有开机
				{
					Run_Flag = 1;   //开机变量改为1,表示已经开机
					BEEP = 0;		//打开蜂鸣
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					P40 = 0;		//打开了LED总电源
					P2 = 0X00;		//全部点亮
					delay_ms(300);   //延迟200ms
					P2 = 0XFF; 		//全部关闭
				}
/*=======================已经开机的状态下,执行关机操作=====================*/
				else
				{
					Run_Flag = 0;   //标记已关机
					BEEP = 0;		//打开蜂鸣(提醒)
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					P2 = 0XFF;		//状态指示全部关闭
					P40 = 1;		//关闭总电源LED
					Run_Mode = 0;   //模式清零
				}
			}

		}
/*=======================按下按键2,执行本段代码=====================*/
		if (KEY2 == 0)   //状态选择键被按下
		{
			delay_ms(10);  //防抖
			if (KEY2 == 0)
			{
				while(KEY2 == 0);	//等待按键松开,将要执行
				{
					BEEP = 0;		//打开蜂鸣
					delay_ms(10);   //延迟10ms
					BEEP = 1;		//关闭蜂鸣
					Run_Mode++;		//每次按下,模式+1(移至下一个led)
					if (Run_Mode > 8)  //如果模式大于8,回到模式1
						Run_Mode = 1;  //Run_Mode超过8后清零,回到第一个灯
					P2 = ~(1<< (Run_Mode - 1));	//错误写法,执行结果是8个灯中仅1个不亮
												//灯是低电平点亮,1<< Run_Mode,即1<<1,左移以后为0000 0010,仅1个LED灭,继续左移,尾部补0,还是只有1个LED灭
												//解决方法:RunMode先减1,再左移,最后全部取反。这里用~为全取反。而!是位取反。
												//P2 = 0XFX;  P6<< 1 + 1;  也可以实现
				}

			}

		}
}

void delay_ms(u16 ms)	//unsigned int
{
	u16 i;
	do
	{
		i = MAIN_Fosc/6000;
		while(--i);
	}while(--ms);
}

Tips: Logical operators in C language

1. AND operation && The
AND operation requires the use of the key character &&, which means connecting two or more expressions into one. All expressions must be TRUE before the entire expression is TRUE, otherwise it is false.
2. OR operation || The
OR operation requires the use of the key character ||, which means connecting two or more expressions into one. If any one expression is TRUE, the entire expression is TRUE; only when all expressions are false , the entire expression is false.
Three, non-operation!
Not operations require the use of key characters! , used to negate the result of a single expression.
If the result of the original expression is false, add an operator in front of the expression! The negated result is TRUE; if the original calculation result is TRUE, add an operator in front of the expression! The negated result is false.

Summarize

1. Understand how to use the buzzer
2. Try to use the buzzer

Homework

Add button 3. When pressed, it means startup. The LED of the selected function continues to flash, indicating that it is working, and the function cannot be switched while working.
Insert image description here

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/132422984