当前位置:网站首页>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;
}
边栏推荐
- 圆角大杀器,使用滤镜构建圆角及波浪效果!
- Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
- ESP8266 使用 DRV8833驱动板驱动N20电机
- Multiply Floyd "suggestions collection"
- 使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
- 贪心问题01_活动安排代码分析
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- SQL注入 Less18(头部注入+报错注入)
- Several common PCB surface treatment technologies!
- Brief description of model deployment
猜你喜欢

PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying

Fillet big killer, use filter to build fillet and wave effect!

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots

The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)

相似矩阵,可对角化条件

Nowcodertop12-16 - continuous updating

Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.

全网显示 IP 归属地,是怎么实现的?

Detailed explanation of zero basis from macro to micro Bert

Reflection reflection
随机推荐
ArcMap cannot start the solution
Definition of information entropy
Stm32cubemx learning record -- installation, configuration and use
贪心问题01_活动安排问题
Multiply Floyd "suggestions collection"
动态规划问题05_导弹拦截
Several common PCB surface treatment technologies!
Filter过滤器解决request请求参数乱码的原理解析
Small and micro enterprise smart business card management applet
[recursion] 938. Range and of binary search tree
【mysql学习09】
SQL language (I)
Various controls ==pyqt5
Summary of combination problems of Li Kou brush questions (backtracking)
Detailed explanation of lvs-nat and lvs-dr modes of LVS load balancing
【mysql学习08】
教你如何通过MCU配置S2E为TCP Client的工作模式
大话DevOps监控,团队如何选择监控工具?
DICOM medical image viewing and browsing function based on cornerstone.js
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体