当前位置:网站首页>Design based on STM32 works: multi-functional atmosphere lamp, wireless control ws2812 of mobile app, MCU wireless upgrade program
Design based on STM32 works: multi-functional atmosphere lamp, wireless control ws2812 of mobile app, MCU wireless upgrade program
2022-06-26 08:22:00 【Turn into dust】
List of articles
2021 year 10 month 27-2022 year 1 month 1 Japan Can undertake single chip microcomputer design , Intentionally added Q2809786963
Works Bili Bili video :https://www.bilibili.com/video/BV1Yb4y1a7AQ#reply5536921990
The data link :
Bluetooth lights v1.01 The data link :
CSDN:
https://download.csdn.net/download/mbs520/25149435
Baidu SkyDrive :
https://pan.baidu.com/s/14Vout7Q2P6JBDZCCd8Gm6w
Extraction code :b7it
Bluetooth lights v1.03 The data link :
https://download.csdn.net/download/mbs520/25150035
Remember to like it when you take it away ~
If you have any questions, you can post them in the comment area or send a private letter 
One 、 Background of the work
In intelligent 2021 years , Young friends are suffering from the symptoms of laziness , So am I .
There was a sleepless night , I opened the glory of the king , I am used to playing mobile phones with the lights on , This can reduce the damage to the eyes , finally , Won several , It's already early in the morning 2 Half past six , I also began to feel sleepy , When I put down my cell phone , When you are ready to close your eyes and go to sleep , I found that the light was especially dazzling , Upset , I really don't want to press the switch that I can only touch after walking out of bed for several steps , But the design of the room is like this , Can't change . But I still struggled to press the switch in the basement , Then I fell asleep .
As an electronics major, I don't compromise , I must design a light that can be turned off without getting out of bed .
Two 、 Function design and implementation process
( One ) Functional design
1、 You can use the key to control the light on and off 、 Brightness and switching colors
2、 You can control the light with the key 6 More than display styles
4、 Design a shell , Make it more beautiful
5、 Design a mobile phone APP, Realize all the functions of the key and can adjust any color of the lamp
6、 Design a mobile phone APP Upgrade the program function of single chip microcomputer , Can save the single-chip program to a certain version of the mobile phone APP in , mobile phone APP Click upgrade , You can complete the upgrade of the single-chip program .
( Two ) Implementation process
1、 Design a key to connect with MCU , Write multi-function keys ( single click 、 double-click 、 Long press ) To control the light on and off 、 Brightness and switching colors 6 More than display styles
2、 use CAD Draw a PCB An appropriately sized shell , Give it to Taobao to customize
3、 use QT Write an android APP, Use Bluetooth module to communicate with MCU , Complete the function of control and upgrade
3、 ... and 、 Realize the basic function
( One )、 The first is to select materials
1、LED choice :WS2812
Since I want to make any color lamp , Then there is no doubt that the most common WS2812,24 Full color RGB coloured lights , Can send out 2^24=16777215 Color .
2、 SCM selection :STM32G0
In today's world MCU So rare and expensive , Of course, we should consider our own pockets ,WS2812 The drive clock of the probably needs 800KHZ, High speed requirements , First choose stm32, Took a look at the price , Chose the amiable stm32g030c8t6,6 Yuan also includes mail 
3、 Communication module selection : Bluetooth module JDY-31
To control the lights with your mobile phone , First think of using Bluetooth module , Price considerations , Choose the cheapest Bluetooth module in the whole network JDY-31, Compared with HC-05, It's smaller , The connection speed is not very fast 
With this 3 Three main materials , We can start to design the schematic diagram
( Two )、 Schematic design
1、 SCM needs 3.3V Power supply , First, a power supply is designed , First use usb Power supply 5V to ws2812, And then depressurize the voltage to 3.3V supply MCU,
2、 Design a single-chip microcomputer minimum system , And reserve a download interface , Easy to use ST-LINK Download the program 
3、 Then connect a Bluetooth and a key as the control , Reserve another LED As an indicator 
4、RGB Circuit design of lamp , Here are two groups LED, With two IO Mouth control , prevent LED Too much leads to signal distortion 
Such a schematic diagram is designed
( Two )、 The first version PCB Design
1、 The corresponding package import is given according to the schematic diagram PCB, Then carry out layout and wiring , Design a piece for Taobao customer service to print out PCB Drawing
2D:
3D:
Then give it to Taobao , Here we recommend jialichuang , cheap , The quality is also high .
This is the first board printed PCB:
( 3、 ... and )、 welding PCB plate
1、 Print out PCB after , Of course, the components should be welded to PCB On board , When the first plate is welded, it is not necessary to weld it all at once , Weld the power supply first , See if the power chip works properly , For example, the board I drew USB The motherboard package does not correspond to the schematic diagram , Cause the positive and negative poles to directly reverse , It is easy to cause component damage , After detecting that the voltage is normal , Then weld other components

( Four ) Write single-chip program
SCM program contains a lot of knowledge
1、 Lightweight multitasking systems
2、 Bluetooth data custom control protocol 、 Bluetooth wireless upgrade MCU
3、 Multiple control mode keys + bluetooth
4、 Multifunction buttons , single click 、 double-click 、 Long press
5、ws2812 Series control
6、 Breathing lamp algorithm
7、 Color gradient algorithm
…
/**************************************** * The name of the function : DIS_TASK() * Input parameters : nothing * Output parameters : nothing * work can : Show tasks * *****************************************/
void DIS_TASK(void)
{
static u8 r=0,g=0,b=0,a=0,dir=0;
static int i,cnt=0;
static int color_rgb;
SCHTaskBegin(); // It is necessary to start the fixed format
while (1)
{
if(SysState.Dis_flag == 1)// The display can be updated
{
/*********************** static state *******************************/
if(SysState.Dismode == DisMode_Static)// static state
{
SysState.Dis_flag = 0;
RGB_Refresh(SysState.StaticRgb,LED_NUM);// Show
RGB2_Refresh(SysState.StaticRgb,LED_NUM);// Show
}
/*********************** breathing *******************************/
else if(SysState.Dismode == DisMode_Breathe)// breathing
{
SysState.Dedlay_Time=20;
if(dir==0)
{
a += (1+a*10/0xff);
if(a > 0xf0)dir = 1;
}else if(dir)
{
a -= (1+a*10/0xff);
if(a <= 4)dir = 0;
}
r = ((SysState.StaticRgb>>16)%0x100)*a/0xff;
g = ((SysState.StaticRgb>>8)%0x100)*a/0xff;
b = ((SysState.StaticRgb>>0)%0x100)*a/0xff;
color_rgb = (r<<16) + (g<<8) + b;
printf("%d %d %d %d\r\n",r,g,b,a);
RGB_Refresh(color_rgb,LED_NUM);// Show
RGB2_Refresh(color_rgb,LED_NUM);// Show
SCHCurTaskDly(SysState.Dedlay_Time);
}
/*********************** flashing *******************************/
else if(SysState.Dismode ==DisMode_Twinkle)// flashing
{
SysState.Dedlay_Time=200;//*SysState.Dedlay_Ratio/0x0f;;
RGB_Refresh(SysState.StaticRgb,LED_NUM);
RGB2_Refresh(SysState.StaticRgb,LED_NUM);
SCHCurTaskDly(SysState.Dedlay_Time);
RGB_Refresh(0,LED_NUM);
RGB2_Refresh(0,LED_NUM);
SCHCurTaskDly(SysState.Dedlay_Time);
}
/*********************** The gradient *******************************/
else if(SysState.Dismode ==DisMode_GraChange)// The gradient
{
extern u8 GraChange_flag;
SysState.Dedlay_Time=100;//*SysState.Dedlay_Ratio/0x0f;
RgbAlg(&SysState.StaticRgb,&GraChange_flag);// Gradient algorithm
RGB_Refresh(SysState.StaticRgb,LED_NUM);// Show
RGB2_Refresh(SysState.StaticRgb,LED_NUM);// Show
SCHCurTaskDly(SysState.Dedlay_Time);
}
/*********************** Disco dancing *******************************/
else if(SysState.Dismode == DisMode_DiscoDance)// Disco dancing
{
SysState.Dedlay_Time=20;//*SysState.Dedlay_Ratio/0x0f;
RGB_Refresh(Static_DisColor[cnt],LED_NUM);
RGB2_Refresh(Static_DisColor[cnt],LED_NUM);
SCHCurTaskDly(SysState.Dedlay_Time);
RGB_Refresh(0,LED_NUM);
RGB2_Refresh(0,LED_NUM);
SCHCurTaskDly(SysState.Dedlay_Time*50);
}
/*********************** Running water *******************************/
else if(SysState.Dismode == DisMode_RunWater)// Running water
{
static int i=0,flag=0;
SysState.Dedlay_Time=100;
i++;
if(i == LED_NUM)
{
i=0;flag=!flag;
}
if(flag){
// Set the color
RGB_Refresh(SysState.StaticRgb,i+1);
RGB2_Refresh(SysState.StaticRgb,i+1);
SysState.Dedlay_Time=50;//*SysState.Dedlay_Ratio/0x0f;
SCHCurTaskDly(SysState.Dedlay_Time);
}
else{
// destroy
RGB_Refresh(0,i+1);
RGB2_Refresh(0,i+1);
SysState.Dedlay_Time=50;//*SysState.Dedlay_Ratio/0x0f;
SCHCurTaskDly(SysState.Dedlay_Time);
}
}
/*********************** user *******************************/
else if(SysState.Dismode ==DisMode_User1) // user
{
SysState.Dedlay_Time=1000;
RGB_Refresh(SysState.StaticRgb,1);
RGB2_Refresh(0,1);
SCHCurTaskDly(SysState.Dedlay_Time);
RGB2_Refresh(SysState.StaticRgb,1);
RGB_Refresh(0,1);
SCHCurTaskDly(SysState.Dedlay_Time);
}
}
SCHCurTaskDly(10);
}
SCHTaskEnd(); // It is necessary to end the fixed format
}
( 5、 ... and ) Download program validation
Test after downloading the program ws2812 Whether it works properly
Four 、 Housing design
( One )CAD Drawing design
I feel it will be very ugly without a shell , Add a shell , Make the world beautiful 

The size of the hole in the middle is wrong :5 ride 7 Change 7 ride 7

( Two ) Frosted acrylic board
Taobao Search frosted acrylic board customization , send out CAD Give the drawing to Shifu , I can make it for you
This is the finished acrylic board , Is in accordance with the PCB The board size is customized 
5、 ... and 、 The redesign PCB
Rearrange the layout and design 3 Version finished board :PCBV1.3
This is the welded appearance after proofing :
6、 ... and 、QT Android APP Design
( One ) Interface design

( Two )QT Programming
Show part of the code :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
this->Start_Init();
this->File_Init();
this->BuleTooth_Init();
this->Label_Init();
this->PushButton_Init();
this->ColorSlider_Init();
this->setFocus();
}
MainWindow::~MainWindow()
{
bin_save(FileInfo);
delete ui;
}
// Starting code
void MainWindow::Start_Init()
{
// Set the background image
this->setStyleSheet("QMainWindow{border-image: url(:/pic/btmenuv2.jpg);}");
// Get screen size
QScreen *screen = QApplication::screens().at(0);
src_w = screen->size().width();
src_h = screen->size().height();
if(src_w <= 0 || src_h <= 0)
{
src_h = 2267;src_w = 1080;
this->setGeometry(0,0,src_w,src_h);//1080 2267
qDebug() << "src get err ======== "<< src_w <<src_h << endl;
}
else
{
this->setGeometry(0,0,src_w,src_h);//1080 2267
qDebug() << "src get ok ======== " << src_w <<src_h << endl;
}
}
// Bluetooth initialization
void MainWindow::BuleTooth_Init(void)
{
// Bluetooth connection initialization code
timer_conflag = new QTimer;
ptimer = new QTimer;
//QBluetoothDeviceDiscoveryAgent This refers to scanning the surrounding Bluetooth devices !
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
//QBluetoothLocalDevice It refers to configuring and obtaining the Bluetooth status information of the device !
localDevice = new QBluetoothLocalDevice();
//QBluetoothSocket Refers to linking Bluetooth devices , Read and write information !
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
// Multi window initialization
btcwindow = new BTMainWindow(this);
btcwindow->hide();
aboutwindow = new AboutMainWindow(this);
aboutwindow->hide();
connect(socket,
SIGNAL(readyRead()),
this,
SLOT(readBluetoothDataEvent())
);
connect(socket,
SIGNAL(connected()),
this,
SLOT(bluetoothConnectedEvent())
);
connect(socket,
SIGNAL(disconnected()),
this,
SLOT(bluetoothDisconnectedEvent())
);
localDevice->powerOn();// Turn on Bluetooth
discoveryAgent->start();// Start scanning
}
// Color bar initialization
void MainWindow::ColorSlider_Init(void)
{
QColor color;
color.setRgb(0x00,0x00,0x00);
colorslider_R = new ColorSlider(this);
colorslider_G = new ColorSlider(this);
colorslider_B = new ColorSlider(this);
colorslider_A = new ColorSlider(this);
colorslider_R->init(ColorSlider::RGB,ColorSlider::RED,color,0x00,0xFF);qDebug() << color << endl;
colorslider_G->init(ColorSlider::RGB,ColorSlider::GREEN,color,0x00,0xff);qDebug() << color << endl;
colorslider_B->init(ColorSlider::RGB,ColorSlider::BLUE,color,0x00,0xff);qDebug() << color << endl;
colorslider_A->init(ColorSlider::RGB,ColorSlider::ALPHA,color,0x00,0xff);qDebug() << color << endl;
colorslider_R->setGeometry(100,200,880,60);
colorslider_G->setGeometry(100,400,880,60);
colorslider_B->setGeometry(100,600,880,60);
colorslider_A->setGeometry(100,800,880,60);
}
// Button initialization
void MainWindow::PushButton_Init(void)
{
// Refresh timer
static QColor last_Color;
time1= new QTimer(this);
time1->start(1000);
connect(time1,&QTimer::timeout,[=](){
time1->start(100);
if(Connect_Flag == 1)// Connection indication
{
Connect_Flag = 0;
btcwindow->hide();
this->show();
QMessageBox::information(this,tr(" Tips "),tr(" Bluetooth connection successful !"));
QByteArray arrayData; // Send an empty instruction
QString s = QString("NONE\r\n");
qDebug() << s << endl;
arrayData = s.toUtf8();
socket->write(arrayData);
s.clear();
arrayData.clear();
}
if(last_Color != Color_sum)// Send instructions
{
update();// to update
unsigned int color_d = ((Color_sum.alpha()/16)<<24)+(Color_sum.red()<<16) + (Color_sum.green()<<8)
+ (Color_sum.blue()<<0) ;
QByteArray arrayData;
QString s = QString("COLOR:%1\r\n").arg(color_d);
qDebug() << s << endl;
arrayData = s.toUtf8();
socket->write(arrayData);
s.clear();
arrayData.clear();
}
last_Color = Color_sum;
});
// Color patch button
// QPushButton *phbutton[10];
for(int i=0; i<10; i++)
{
int r,g,b;
r = FileInfo->color_tab[i]>>16;
g = (FileInfo->color_tab[i]>>8)%256;
b = FileInfo->color_tab[i]%256;
phbutton[i] = new QPushButton(this);
if(i<5)phbutton[i]->setGeometry(90+i*200*src_w/1080,1600*src_h/2267,100*src_w/1080,100*src_h/2267);
else if(i>=5)phbutton[i]->setGeometry(90+(i-5)*200*src_w/1080,1800*src_h/2267,100*src_w/1080,100*src_h/2267);
QString s = QString("background-color: rgb(%1, %2, %3);").arg(r).arg(g).arg(b);
phbutton[i]->setStyleSheet(s);
phbutton[i]->setWindowFlags(Qt::WindowStaysOnTopHint);
connect( phbutton[i],&myPushButton::clicked,[=](){
int r,g,b;
if(SaveColorFlag != 0)
{
r = Color_sum.red();
g = Color_sum.green();
b = Color_sum.blue();
SaveColorFlag = 0;
update();
FileInfo->color_tab[i] = (r<<16)+(g<<8)+b;
QString s = QString("background-color: rgb(%1, %2, %3);").arg(r).arg(g).arg(b);
phbutton[i]->setStyleSheet(s);
}
else
{
r = FileInfo->color_tab[i]>>16;
g = (FileInfo->color_tab[i]>>8)%256;
b = FileInfo->color_tab[i]%256;
Color_sum.setRgb(r,g,b,Color_sum.alpha());
ColorSlider_paint_Flag = 2;
}
});
}
( 3、 ... and )APP Functional design
1、 Main interface function
| Button | Functional specifications |
| Connect button | Enter the Bluetooth connection interface / Double click the connected device |
| About the buttons | Enter the software introduction / upgrade MCU Interface |
| Red slider | Adjust the red color |
| Green slider | Adjust the green color |
| Blue slider | Adjust the blue color |
| Gray sliding bar | Adjust the overall brightness |
| Color preview ball | Preview the current display color |
| Color quick display | Click to display the button color |
| display mode | Display mode switch |
| The bottom message shows | Display the received information / Welcome message |
| The connection information is displayed | Displays the current connection status |
First click the color preview ball , The save selection box appears ,
Then click the color selection button , You can save the currently adjusted color 
2、 Bluetooth connection interface
| Button | Functional specifications |
| Device connection selection | Double click the connected device |
| Return button | Back to the main interface |
1、 If the connection is successful, it will automatically return to the main interface
2、 Automatic connection function : open APP No action required , Automatically find Bluetooth lights and connect devices automatically , No manual connection required 
3、 About the interface
| Button | Functional specifications |
| Author information shows | read-only |
| Return button | Back to the main interface |
| Display version | Bluetooth color light response signal for example 1.2 Version flashing red 1 Time , Green light twice |
| upgrade MCU | Connect Bluetooth , Click upgrade , It can upgrade the software of single chip microcomputer to the current version , Do not repeatedly upgrade the software version , The upgrade may fail |
| Upgrade progress bar | Show upgrade progress After the MCU fails to upgrade, you need to enter the upgrade mode to upgrade again , Otherwise, it will not work normally . |

4、 Upgrade code prompt
| Display type | According to the content |
| Upgrade failed | The Bluetooth color lamp displays red |
| Upgrade success | The Bluetooth color lamp displays green |
| Upgrade process | Bluetooth lights show light blue progress |

边栏推荐
- Read excel table and render with FileReader object
- Oracle database self study notes
- 批量修改文件名
- Solve the problem that pychar's terminal cannot enter the venv environment
- Oracle 19C local listener configuration error - no listener
- Chapter II (summary)
- STM32 encountered problems using encoder module (library function version)
- Pic 10B parsing
- Delete dictionary from list
- Late 2021 plan
猜你喜欢

What if the service in Nacos cannot be deleted?

WiFi-802.11 2.4G频段 5G频段 信道频率分配表

Discrete device ~ diode triode

Example of offset voltage of operational amplifier

JMeter performance testing - Basic Concepts

Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor

Idea auto Guide

Uploading pictures with FileReader object

Use a switch to control the lighting and extinguishing of LEP lamp

2020-10-29
随机推荐
Livevideostackcon | evolution of streaming media distribution for online education business
Go language shallow copy and deep copy
Undefined symbols for architecture i386 is related to third-party compiled static libraries
SOC wireless charging scheme
(4) Independent key
Undefined symbols for architecture i386与第三方编译的静态库有关
Use of jupyter notebook
Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda
JWT in go
Chapter VI (pointer)
Double linked list -- tail interpolation construction (C language)
Win11 open folder Caton solution summary
[postgraduate entrance examination] group planning exercises: memory
Common uniapp configurations
Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
golang json unsupported value: NaN 处理
Why are you impetuous
How to Use Instruments in Xcode
Rewrite string() method in go language
MySQL practice: 3 Table operation