National Day holiday Daoteng five days, ready to make a mobile phone Bluetooth wireless control RGB LED controller; originally already used stm32f103c8t6 write good program, but this chip cost is higher, if more make a few words, also really expensive, and this chip used in this function, also too overqualified. So ready to use the cheap STC chip replacement, in the purchase of electronic components, deliberately found a cheap chip stc15w201s, the price of about 3 yuan a bar. I thought it would be easy to find the relevant routine on the Internet, did not think, searched, unexpectedly a related routine also did not find, tears ran ah ...
Just recently in the study of electronic technology, have bought back, anyway, try to open their own, efforts to Baidu inquiry basic information.
First introduce the basic information of stc15w201s chip, the chip's working voltage is 2.5V to 5.5V, conservative point, with 3.3V Bar, the chip with high-precision RC oscillator and can not be external crystal shock, in general applications, even the peripheral crystal shock circuit has been saved, this is worth a bit praise AH. Program storage for 1k,ram 256 bytes, also with 4K EEPROM, this on-chip EEPROM has not been tested, the next time you want to use, then test it, this feeling is good, even external memory do not have to hang, or a word, province! On-Chip has two timers, timer 0 and Timer 2, only a serial port, but can be reused, equivalent to two serial ports, but this I have not tested.
The program realizes the function as follows, the Bluetooth module connects with the stc15w201s serial port through the serial port, receives and sends the data. Output five-way PWM signal, red, green, blue and yellow five colors of the LED light color and brightness control. There is also a PWM signal control output for the control conversion of the original lamp circuit and the modified LED lamp. Always can not be the original lamp all throw away, plus go good, power after the default with the original lamp, through the controller conversion.
Program burning parameters such as, need to be aware that when burning, you need to 12 and 13 feet of the level of low to write. The real picture is a bit messy, interested in reading the circuit diagram,
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 receive status of a packet
#define LOCK 0xFF;
#define BAUD 9600//serial baud rate 115200
#define NONE_PARITY 0//No calibration
#define ODD_PARITY 1//Odd check
#define Even_parity 2//even check
#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
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 <main.h>
#include <usart.h>
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 baud rate
#elif (Paritybit = = odd_parity) | | (Paritybit = = even_parity) | | (Paritybit = = mark_parity)
SCON = 0xDA; 9-bit variable baud rate, initial check digit is 1
#elif (Paritybit = = space_parity)
SCON = 0xd2; 9-bit variable baud rate, initial check digit is 0
#endif
t2l = (65536-(Fosc/4/baud)); Setting the baud rate reload value
T2H = (65536-(Fosc/4/baud)) >>8;
AUXR = 0x14; T2 to 1T mode and start Timer 2
AUXR |= 0x01; Select the baud rate generator with timer 2 for serial port 1
ES = 1; Enable serial port 1 to interrupt
EA = 1;
}
/*----------------------------
UART Interrupt Service Program
-----------------------------*/
void Uart () Interrupt 4 using 1
{
if (RI)
{
RI = 0; Clear RI Bits
Rev_buf[buf_num] =sbuf;
Buf_num + +;
}
if (TI)
{
TI = 0; Clear Ti Bit
busy = 0; Clear Busy flag
}
}
/*----------------------------
Send serial data
----------------------------*/
void SendData (U8 dat)
{
while (busy); Wait for the previous data to be sent to completion
ACC = dat; Get check digit P (psw.0)
if (p)//Set check digit according to P
{
#if (Paritybit = = odd_parity)
TB8 = 0; Set the check bit to 0
#elif (Paritybit = = even_parity)
TB8 = 1; Set the check bit to 1
#endif
}
Else
{
#if (Paritybit = = odd_parity)
TB8 = 1; Set the check bit to 1
#elif (Paritybit = = even_parity)
TB8 = 0; Set the check bit to 0
#endif
}
busy = 1;
Sbuf = ACC; Write data to the UART data register
}
/*----------------------------
Send string
----------------------------*/
void Sendstring (char *s)
{
while (*s)//Detect string End flag
{
SendData (*s++); Send current character
}
}
Pwm. H Code
#ifndef __pwm_h__
#define __pwm_h__
#define PWM_ON 1
#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//0~255 PWM cycle, Max 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 Code
#include <main.h>
#include <pwm.h>
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 ()//initialization counter 0
{
AUXR |= (1<<7); Timer0 Set as 1T mode
Tmod &= ~ (1<<2); Timer0 Set as Timer
Tmod &= ~0x03; Timer0 set as-bits Auto Reload
TH0 = timer0_reload/256; Timer0 Load
TL0 = timer0_reload% 256;
ET0 = 1; Timer0 Interrupt Enable
PT0 = 0; High-priority
TR0 = 1; Timer0 Run
EA = 1; Open Total Interrupts
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 <reg51.h>
#define FOSC 11059200L//System frequency
SFR p1m1 = 0x91;//io Mode control register
SFR P1M0 = 0x92;//io Mode control register
SFR AUXR = 0x8e; Auxiliary Registers
SFR t2h = 0xd6; Timer 2 High 8 bit
SFR t2l = 0xd7; Timer 2 Low 8 bit
Sbit RED = p1^1; RED on P1.1 16 feet
Sbit Greed =p1^3;//greed P1.32 feet
Sbit BLUE =p1^5;//blue P1.54 feet
Sbit white = p1^2;//P1.2 1 feet
Sbit YELLOW =p1^4;//Yellow pick up P1.4 3 feet
Sbit K1 = p1^0;//P1.0 15th leg added multiple way when switch is used, also support PWM output
#endif
MAIN. C Code
#include <reg51.h>
#include <intrins.h>
#include <main.h>
#include <usart.h>
#include <pwm.h>
void delay (unsigned int x);
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;
Process data by received data
void Act_bit (void)
{
AA F9 01 Red Green Blue DD value 0~100
&& rev_buf[1] = = 0xf9
if (rev_buf[0] = = 0xaa && rev_buf[6] = = 0XDD)
{
if (rev_buf[2] = = 0x01)//Turn on the light command
{
if (rev_buf[1] = = 0xf9)//With STM32 protocol general, speed control RGB Red Green Blue
{
Red_value= Rev_buf[3];
Greed_value =rev_buf[4];
Blue_value =rev_buf[5];
}
if (rev_buf[1] = = 0xf8)//Add white, Yellow PWM control
{
White_value= Rev_buf[3];
Yellow_value = rev_buf[4];
K1_value = rev_buf[5];
}
}
Reading individual PWM values
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 protocol control separately
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)
{
P1M1 |= 0x02;
P1M0 |=0x3f; 0X3F;02 configuration io for push-pull output
Usart_int ();
Timer0_int ();
Sendstring ("Stc15f2k60s2\r\nuart Test!\r\n");
while (1)
{
if (Buf_num >= Data_len)
{
Act_bit ();
buf_num=0;
}
}
}
stc15w201s Serial bluetooth communication PWM control RGB Lantern