当前位置:网站首页>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;
}
边栏推荐
- 小微企业智能名片管理小程序
- leetcode 剑指 Offer 28. 对称的二叉树
- Introduction to shortcut keys in debug chapter
- SQL language (6)
- Multiply Floyd "suggestions collection"
- SQL injection less23 (filter comment)
- flinksql client 连接kafka select * from table没有数据报错,如何解决?
- Mlx90640 infrared thermal imager temperature measurement module development notes (V)
- 全网显示 IP 归属地,是怎么实现的?
- My colleague looked at my code and exclaimed: how can I use a singleton in unity
猜你喜欢

The most efficient note taking method in the world (change your old version of note taking method)

SQL injection less23 (filter comment)

SQL language (6)

Summary of combination problems of Li Kou brush questions (backtracking)

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
苹果美国宣布符合销售免税假期的各州产品清单细节

Smart cloud IOT platform STM32 esp8266-01s simple wireless light control

Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs

谣言检测文献阅读十一—Preventing rumor spread with deep learning

矩阵的特征值和特征向量
随机推荐
varest蓝图设置json
动态规划问题05_导弹拦截
Linked list related (design linked list and ring linked list)
Dataframe print ellipsis problem
Several common PCB surface treatment technologies!
Shell - Chapter 6 exercise
Nowcodertop12-16 - continuous updating
How to judge the performance of static code quality analysis tools? These five factors must be considered
Getting started with redis
Some errors of tensorflow calling multiple GPUs
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
SQL注入 Less18(头部注入+报错注入)
Various controls ==pyqt5
倍增Floyd「建议收藏」
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
Shell - Chapter 8 exercise
Greedy problem 01_ Activity arrangement problem
【mysql学习09】
Convert string to number
The principle analysis of filter to solve the request parameter garbled code