The Essence BW16-Kit development board integrates a wealth of functions and interfaces, which can be easily used for various development applications. Among them, the function of using TIM (Timer) to control the LED can be achieved by writing corresponding codes. The following is a simple example showing how to use TIM on the Essence BW16-Kit to control the flashing of the LED.
Code:
// bool isLightOn = false; // 灯光状态,默认关闭
unsigned long previousMillis = 0; // 上一次变换状态的时间
unsigned long interval = 1000; // 每个状态之间持续的时间间隔,默认1秒
unsigned int isLightOn=0; //变量定义位置吗,有点不懂
void setup() {
pinMode(PA13, OUTPUT);
Serial.begin(115200); // 初始化串口
// digitalWrite(PA13, true);
}
void loop() {
unsigned long currentMillis = millis(); // 获取当前运行时间
int setTime=0;
// Serial.println(currentMillis);
if(Serial.available() > 0) { // 如果串口有可用数据
setTime = Serial.parseInt(); // 从串口读取设置的定时时间
interval = setTime * 500; // 将设置的时间转换为秒
Serial.print("time set!\r\n");
}
if(currentMillis - previousMillis >= interval) { // 如果超过设定的时间间隔
previousMillis = currentMillis; // 更新上次状态变换的
Serial.print("led change!\r\n");
isLightOn +=1; // 切换灯光
if(isLightOn%2==0) {
digitalWrite(PA13, HIGH);
Serial.print("led on ");
} else {
digitalWrite(PA13, LOW); // 关闭灯光
Serial.print("led off ");
}
}
}
Serial port output record:
video:
6621cd418719252ed3770e7f8ee1fbd2
The TIM (Timer) function of the Essence BW16-Kit allows developers to perform specific tasks or operations within a specified time interval. It is very useful for applications that require precise time control, such as timing to switch LED status, timing to send data, etc.
In BW16-Kit, timer tasks are implemented by configuring and using timers. Usually, select an available timer first and set its working mode (such as single trigger or periodic trigger), frequency division coefficient, initial count value and other parameters as needed. Once the timer is correctly configured and started, it will trigger an interrupt after the specified time interval, and the corresponding task can be executed in the interrupt service function.
To use the TIM function of BW16-Kit, you need to be familiar with its SDK (Software Development Kit) or related documents to understand how to configure and use the timer. SDK usually provides sample codes for the timer initialization function, configuration function, and interrupt processing function. You can refer to these codes to write your own timer tasks.
|