当前位置:网站首页>W5500 adjusts the brightness of LED light band through upper computer control
W5500 adjusts the brightness of LED light band through upper computer control
2022-07-25 11:42:00 【WIZnet】
The experiment uses W5500 The development board sends commands to the development board through the upper computer to control the brightness of the external light strip ; The main process is as follows :
1 The experiment purpose
The upper computer sends through serial port in the format of :“redbrightness,greenbrightness,bluebrightness” String to MCU.MCU Convert the number into the corresponding brightness .
2 The overall design of the experiment
The experiment is mainly divided into two parts :PWM Configuration and serial communication configuration . The difficulty of the whole experiment lies in ASCII The process of converting codes into numbers .
3 PWM The principle of production
Universal timers can be used GPIO Pin for pulse output . To make STM32 Universal timer TIMx produce PWM Output , Need to use 3 A register . Namely : Capture / Compare mode register (TIMx_CCMR1/2)、 Capture / Compare enable register (TIMx_CCER)、 Capture / Compare register (TIMx_CCR1~4).( Be careful , There's another. TIMx Of ARR Registers are used to control pwm The output frequency of ).
For capture / Compare mode register (TIMx_CCMR1/2), This register has 2 individual ,TIMx _CCMR1 and TIMx _CCMR2.TIMx_CCMR1 control CH1 and 2, and TIMx_CCMR2 control CH3 and 4. The second is capture / Compare enable register (TIMx_CCER), This register controls the switch of each input and output channel .
Finally, capture / Compare register (TIMx_CCR1~4), This register has 4 individual , Corresponding 4 Two transmission channels CH1~4.4 The functions of registers are similar , It's all used to set up pwm The duty cycle of . for example , If pulse counter is configured TIMx_CNT Count up for , Overloaded registers TIMx_ARR Be configured as N, namely TIMx_CNT The current count value of X stay TIMxCLK Under the drive of clock source, it keeps accumulating , When TIMx_CNT The numerical X Greater than N when , Reset TIMx_CNT Values for 0 Recount . And in the TIMxCNT While counting ,TIMxCNT The count of X Will compare register with TIMx_CCR Pre stored values A Compare , When the pulse counter TIMx_CNT The numerical X Less than comparison register TIMx_CCR Value A when , Output high level ( Or low level ), By contraries , When the value of the pulse counter X Greater than or equal to the value of the compare register A when , Output low level ( Or high level ). So circular , The output pulse period obtained is the overload register TIMx_ARR Stored values (N+1) Times the clock period of the trigger , Its pulse width is a comparison register TIMx_CCR Value A Times the clock period of the trigger , The output PWM The duty cycle of is A/(N+1) .
4 PWM Configuration steps
4.1 To configure GPIO
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);// Turn on multiplex clock
GPIO_InitStructure.GPIO_Pin = LED_RED| LED_BLUE | LED_GREEN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC, LED_RED | LED_BLUE | LED_GREEN);
}
4.2 Configure timer
void TIMER_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
TIM_BaseInitStructure.TIM_Period = 255;
TIM_BaseInitStructure.TIM_Prescaler = 0;
TIM_BaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
4.3 To configure PWM
void PWM_Config(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; // Choice mode 1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low // Polarity is active at high level
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM3,ENABLE);
}
4.4 Summary
PWM Pattern 1:
When counting up , once TIMx_CNTTIMx_CCR1 Time channel 1 Is the invalid level (OC1REF=0), Otherwise, it is the effective level (OC1REF=1).
PWM Pattern 2:
When counting up , once TIMx_CNTTIMx_CCR1 Time channel 1 Is the effective level , Otherwise, it is invalid level .
At the same time, the effective comments output are also related to the polarity configuration :
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
This configuration is that the high level is the effective level , vice versa .
5 UART Configuration steps
5.1 To configure UART1 And corresponding GPIO
void Usart_Config(uint32_t BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART_PC, &USART_InitStructure);
USART_ITConfig(USART_PC, USART_IT_RXNE, ENABLE); // Open serial port receive interrupt
USART_ITConfig(USART_PC, USART_IT_IDLE, ENABLE); // Open serial port receive interrupt
USART_Cmd(USART_PC, ENABLE);
}
5.2 Configuration interrupt
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
5.3 Interrupt function
void USART1_IRQHandler(void)
{
uint8_t clear = clear;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
}
else if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
clear = USART1->SR;
clear = USART1->DR; // First reading SR read DR, To clear away IDLE interrupt
RxNumber = RxCounter;
RxCounter = 0; // Counter reset
IDLE_Flag = 1; // Mark the received data of one frame
}
}
5.4 Summary
STM32 Single chip microcomputer can receive variable length byte data . because STM32 MCU with IDLE interrupt , Take advantage of this interrupt , Can receive variable length bytes of data . because STM32 Belong to ARM Single chip microcomputer , So the method of this article is also suitable for other ARM Single chip microcomputer .
IDLE After receiving a frame of data through the serial port , The interruption that happened . For example, send it to MCU once 1 Bytes , Or one time 8 Bytes , These data from one time , It's called one frame data , It can also be called a packet of data . At the end of a frame of data , It will produce IDLE interrupt . This interrupt is very useful , It can save a lot of trouble of judgment .
6 ASCII Code to number
6.1 Implementation steps :
while(RxBuffer[i] != ','){ i++; len++;}// If not for ',' The length of the add 1
for(j=i-len; j
value = RxBuffer[j]&0x0f; // take ascii Code to number
pwm_red += value * Power(len-1);
len--;
}
i++;
len = 0;
while(RxBuffer[i] != ','){ i++; len++;}
for(j=i-len; j
value = RxBuffer[j]&0x0f; // take ascii Code to number
pwm_green += value * Power(len-1);
len--;
}
i++;
len = 0;
while(RxBuffer[i] != '\0'){ i++; len++;}
for(j=i-len; j
value = RxBuffer[j]&0x0f; // take ascii Code to number
pwm_blue += value * Power(len-1);
len--;
}
RedOutput(pwm_red);
GreenOutput(pwm_green);
BlueOutput(pwm_blue);
pwm_red = 0;
pwm_green = 0;
pwm_blue = 0;
for(i=0; i<11; i++) RxBuffer[i] = NULL;// Clear array
i = 0;
len = 0;
}
}
}
6.2 10 Of n The power function
uint8_t Power(uint8_t pow)
{
uint8_t i;
uint8_t sum = 1;
for(i=0; i
return sum;
}
边栏推荐
- Convert string to number
- Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
- Nowcodertop12-16 - continuous updating
- Review recitation finishing version
- Small and micro enterprise smart business card management applet
- Introduction to shortcut keys in debug chapter
- W5500多节点连接
- MIIdock简述
- LVS load balancing lvs-nat building Web Cluster
- 布局管理==PYQT5
猜你喜欢

小微企业智能名片管理小程序

Filter过滤器解决request请求参数乱码的原理解析
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States

The B2B2C multi merchant system has rich functions and is very easy to open!!!

C# Newtonsoft. Jason advanced usage

Detailed explanation of zero basis from macro to micro Bert

教你如何通过MCU将S2E配置为UDP的工作模式

MIIdock简述

Let sports happen naturally, and fire creates a new lifestyle

Reinforcement learning (IV)
随机推荐
Reinforcement learning (IV)
Filter过滤器解决request请求参数乱码的原理解析
小微企业智能名片管理小程序
Small program of vegetable distribution in community
Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
WIZnet嵌入式以太网技术培训公开课(免费!!!)
How does the whole network display IP ownership?
同事看了我的代码惊呼:居然是这么在Unity中用单例的
JS convert pseudo array to array
Shell fourth day homework
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
第4章线性方程组
城市雕塑典型作品信息管理系统(图片分享系统SSM)
varest蓝图设置json
Definition of information entropy
SQL注入 Less18(头部注入+报错注入)
信号与槽机制==PYQT5
Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
RedisUtil