STC15W201S serial Bluetooth communication PWM control RGB lantern

   Five days have passed since the National Day holiday, and I am going to make a Bluetooth wireless control RGB LED controller; I have already written the program with STM32F103C8T6, but the cost of this chip is relatively high. If you do a few more, It really costs money, and the chip is used for this function, and it is too much to use. So I was going to replace it with a cheap STC chip. When buying electronic components, I deliberately found a cheap chip STC15W201S, the price is about 3 yuan. I thought it would be easy to find related routines on the Internet, but I did not expect that after searching a bit, a related routine was not found, tears rushed. . .

   just recently I was studying electronic technology, and I bought them all. Anyway, try to do it yourself, and try to find basic information on Baidu. .

Let me first introduce the basic information of the STC15W201S chip. The working voltage of the chip is 2.5V to 5.5V. To be conservative, use 3.3V; the chip has a high-precision RC oscillator and cannot be connected to a crystal oscillator. In general applications, even the external crystal oscillator circuit is saved, which is worthy of praise. The program storage is 1K, the RAM is 256 bytes, and it also has 4K EEPROM. This on-chip EEPROM has not been tested yet. When you want to use it next time, test it again. This feels good. You don’t even need to hang up the external memory. It’s still one. Word, save! There are two timers on the chip, Timer 0 and Timer 2. There is only one serial port, but it can be time-shared and multiplexed, which is equivalent to two serial ports, but I haven’t tested this yet.

The functions realized by the    program are as follows. The Bluetooth module is connected to the serial port of STC15W201S through the serial port to receive and send data. Output five PWM signals to control the color and brightness of LED lights in five colors of red, green, blue, yellow and white. There is also a PWM signal control output, which is used for the control conversion between the original lamp circuit and the modified LED lamp. You can’t throw away all the original lamp, just add it, and the original lamp will be used by default after power on, and it will be converted by the controller.

The programming parameters of    are as shown in the figure below. Note that when programming, you need to pull down the levels of pins 12 and 13 to write. The physical picture is a bit messy, if you are interested in looking at the circuit diagram,

share pictureShare pictures

  KEIL code:

USART.H content

#ifndef __USART_H__
#define __USART_H__

typedef unsigned char u8;

typedef unsigned int WORD ;

#define UNLOCK 0x00; //Used to identify the receiving status of data packets
#define LOCK 0xff;

#define BAUD 9600 //Serial port baud Rate 115200

#define NONE_PARITY 0 //No parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 / /Mark check
#define SPACE_PARITY 4 //Blank check

#define PARITYBIT NONE_PARITY //Define check digit

#define DATA_LEN 7 //Define protocol data bit Length

sfr P_SW1 = 0xA2; //Peripheral function switch register 1

#define S1_S0 0x40 //P_SW1.6
#define S1_S1 0x80 //P_SW1.7< /p>

extern bit busy;
extern unsigned int buf_num;
extern u8 rev_buf[6];

void Usart_Int(void);

void SendData (u8 dat);
void SendString(char *s) ;
void Uart();

#endif

USART.C code

#include < br>#include

unsigned int buf_num;
bit busy;
u8 rev_buf[6];

void Usart_Int(void)
{

buf_num=0;
ACC = P_SW1;
ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0
P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD)

#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable wave Special rate
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable baud rate, initial check digit 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable baud rate, initial check digit is 0
#endif

T2L = (65536-(FOSC/4/BAUD)); //Set the baud rate reload value
T2H = (65536-(FOSC/4/BAUD))>>8;
AUXR = 0x14; // T2 is 1T mode, and timer 2 is started
AUXR |= 0x01; //Select timer 2 as the baud rate generator of serial port 1
ES = 1; //Enable serial port 1 interrupt
EA = 1;
}

/*—————————-
UART interrupt Service program
—————————–*/
void Uart() interrupt 4 using 1
{

if (RI)
{
RI = 0; //Clear the RI bit
rev_buf[buf _num] =SBUF;
buf_num ++ ;

}

if (TI)
{
TI = 0; //Clear TI bit
busy = 0; //清忙标志
}

}

/*—- ————————

Send serial port data
– —————————*/
void SendData(u8 dat)
{
while (busy); //Wait for the completion of the previous data transmission
ACC = dat; //Get the check digit P (PSW.0)
if (P) //Set the check digit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set the check digit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set the check digit to 1< br>#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set the check digit to 1
#elif (PARITYBIT = = EVEN_PARITY)
TB8 = 0; //Set the parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Write data to the UART data register< br>}

/*—————————-
Send string
—————————-*/
void SendString(char *s)
{
while (*s) //Detect the end of the string
{
SendData(*s++); //Send the current character
}
}

PWM.H code< /p>

#ifndef __PWM_H__
#define __PWM_H__

#define PWM_ON 1< br>#define PWM_OFF (!PWM_ON)

#define Timer0_Rate 25000 //Interrupt frequency
#define Timer0_Reload (65536UL -(FOSC / Timer0_Rate)) //Timer 0 reload value

#define PWM_DUTY_MAX 100 // 0~255 PWM周期, 最大255

extern int Red_Value;
extern int Greed_Value;
extern int Blue_Value;
extern int White_Value ;
extern int Yellow_Value;
extern int K1_Value;

void Timer0_Int();

#endif

PWM.C 代码

#include
#include

int Red_Value;
int Greed_Value;
int Blue_Value;

int White_Value;
int Yellow_Value;

int K1_Value;

int PWM_COUNT; //PWM count

void Timer0_Int() //initialize counter 0
{
AUXR |= (1<<7); // Timer0 set as 1T mode
TMOD &= ~(1<<2); // Timer0 set as Timer
TMOD &= ~0x03 ; // Timer0 set as 16 bits Auto Reload
TH0 = Timer0_Reload / 256; //Timer0 Load
TL0 = Timer0_Reload % 256;

ET0 = 1; //Timer0 Interrupt Enable

PT0 = 0; //高优先级

TR0 = 1; //Timer0 Run

EA = 1; / /打开总中断

PWM_COUNT=0;
}

 

/***************** ***** Timer0 1ms interrupt function************************/
void timer0 (void) interrupt 1
{
PWM_COUNT++;
if (PWM_COUNT >= PWM_DUTY_MAX)
{
PWM_COUNT =0 ;
RED=PWM_ON;
GREED=PWM_ON;
BLUE=PWM_ON;
WHITE = PWM_ON;
YELLOW =PWM_ON;
K1=PWM_ON;
}

if (Red_Value <= PWM_COUNT) RED = PWM_OFF;
if (Greed_Value <= PWM_COUNT) GREED = PWM_OFF ;
if (Blue_Value <= PWM_COUNT) BLUE = PWM_OFF; if (White_Value <= PWM_COUNT) WHITE = PWM_OFF;
if (Yellow_Value <= PWM_COUNT) YELLOW = PWM_OFF;
if (K1_Value <= PWM_COUNT) K1 = PWM_OFF;

}

MAIN.H code

#ifndef __MAIN_H__
#define __MAIN_H__

#include

#define FOSC 11059200L //System frequency

sfr P1M1 = 0x91; //IO mode control register
sfr P1M0 = 0x92; //IO mode control register

sfr AUXR = 0x8e; //auxiliary register
sfr T2H = 0xd6; //timer 2 high 8 bits
sfr T2L = 0xd7; / /Timer 2 low 8 bits

sbit RED = P1^1; //RED is connected to P1.1 16 pin
sbit G REED =P1^3; //GREED connect to P1.3 2 pin
sbit BLUE =P1^5; //BLUE connect to P1.5 4 pin

sbit WHITE = P1^2; // White connect to P1.2 1 pin
sbit YELLOW =P1^4; //Yellow connect to P1.4 3 pin

sbit K1 = P1^0; // P1.0 15th pin increases more One way is used as a switch, and it also supports PWM output.

#endif

MAIN.C code

#include
#include < intrins.h>
#include
#include
#include

void delay(unsigned int x);< br>
void Act_Bit(void);

extern bit busy;

extern unsigned int buf_num;
extern u8 rev_buf[6];

extern int Red_Value;
extern int Greed_Value;
extern int Blue_Value;
extern int White_Value;
extern int Yellow_Value;
extern int K1_Value;

//Press to receive The received data is processed data
void Act_Bit(void)
{
//aa f9 01 red, green and blue dd value 0~100
//&& rev_buf[1] == 0xf9
if (rev_buf[0] == 0xaa && rev_buf[6] == 0xdd)
{
if (rev_buf[2] == 0x01) // light on instruction
{
if ( rev_buf[1] == 0xf9) //Common with STM32 protocol, speed regulation RGB  red, green and blue
{
Red_Value= rev_buf[3];
Greed_Value =rev_buf[4];
Blue_Value = rev _buf[5];
}
if (rev_buf[1] == 0xf8) //Increase white and yellow PWM control
{
White_Value = rev_buf[3];
Yellow_Value = rev_buf [4];
K1_Value = rev_buf[5];
}
}

//读取各个PWM值
if (rev_buf[2] == 0xff)
{
SendData(0xff);
SendData(Red_Value);
SendData(Greed_Value);
SendData(Blue_Value);
SendData(White_Value);
SendData(Yellow_Value);
SendData(K1_Value);
}

//For switch K1, add separate protocol control
if (rev_buf[1] == 0x00 && rev_buf[2] == 0x00 && rev_buf[3] == 0x00 && rev_buf[4] == 0x00)
{
Red_Value=0x00;
Greed_Value=0x00 ;
Blue_Value =0x00 ;
White_Value =0x00;
Yellow_Value=0x00;

if ( rev_buf[5] == 0x00)
{
K1_Value =0x00;
}
}

}

}

void main(void)< br>{

//P1M1 |= 0x02;
P1M0 |=0x3f; //0x3f; 02 Configure IO as push-pull output

Usart_Int ();
Timer0_Int();

SendString(“STC15F2K60S2
Uart Test !
“);
while(1) {

if (buf_num >= DATA_LEN)
{
Act_Bit();
buf_num=0;
}

}

}

Leave a Comment

Your email address will not be published.