arduino中的变频

主要内容

  1. Arduino输出模拟信号,也就是可变电压输出。
  2. 电机型号和规格,常见的电机驱动和控制电路
  3. 介绍常见的晶体管,以及晶体管电路的基本应用与设计方式

一、 调节电压变化

  1. 改变电路中的电压。
  • 缺点:体积增大,电能变成热能
  1. 省电又环保的PWM变频技术
  • 称为:脉冲宽度调制(Pulse Width Modulation)
  • 通过改变脉冲宽度,将能仿真模拟电压高低变化的效果。
  • 公式:
    = × = 模拟输出电压 = 脉冲宽度 \times 高电平值 \Rightarrow \frac {输出电压}{高电平值} = 开启时间百分比

二、 模式输出(PWM)指令和默认频率

指令

analogWrite(端口号,模拟数值) 

说明:端口号可能为3,5,6,9,10, 11
模拟数值:0~255

例如:

analogWrite(5, 168); 

上面的指令将使第5引脚输出3.3V电压,因为 168 255 × 5 3.3 \frac {168}{255}\times {5} \approx 3.3

默认频率

Arduino控制板预设采用1kHz 和 500Hz两组不同的PWM输出频率

  • 引脚5、6:976.5625Hz
  • 引脚3、11、9、10:490.196Hz
  • 若PWM频率太低,电机会抖动,LED会闪烁
  • 人耳可以感受20kHz以内的频率,为避免听到电机的抖动音,应调高PWM的输出频率

改变PWM的输出频率

Arduino的PWM输出频率是由ATmega微处理器内部的三个系统定时器(Timer0~Timer2)决定的,Timer0(5、6)、Timer1(9、10)、Timer2(3、11),通过改变定时器的设置,来调整PWM的输出频率。
Arduino的delay()、millis()、micros()等函数的基准时间,来自Timer0,若调整此定时器,将导致这些函数的延迟时间错乱。

下面的程序语句将Timer1(9、10端口输出)的PWM频率调整成31250Hz。

void setup() {
	TCCR1B = TCCR1B & 0b11111000 | 0x01;
}

关于参数的详细说明以及频率对照表

三、任务

任务1:调光器

  1. 实验说明: 利用可变电阻的输入信号变化来调整LED亮度
  2. 实验材料:
物品 数量
LED 1个
10kΩ可变电阻 1个
  • Arduino 的模拟输入(analogRead)的范围值介于0~1023之间,而模拟输出(analogWrite)介于0 ~ 255。
  1. 实验程序
byte potPin = A0;
byte ledPin = 11;
int potSpeed = 0;
byte val = 0;
void setup() {
	pinMode(ledPin OUTPUT);
}

void loop() {
	potSpeed = analogRead(potPin);
	val = map(potSpeed, 0, 1023, 0, 255);
	analogWrite(ledPin, val);
}

任务2:随机数字与烛光效果

实验说明: 随机, 通过随机调整接在数字11端口的LED亮度,以及随机持续时间来模拟烛光效果。

随机指令:

random

byte rnd = random(200); //从0~199之间挑选一个数字,存入rnd
byte rnd = random(20, 50); //从20~49之间挑选一个数字,存入rnd

然而,Arduino每次挑选的数字并不是那么随意,为了提高不重复的比率,在每次执行random()函数之前,先执行randomSeed()函数,

实验程序:

byte ledPin = 11;
void setup() {
	pinMode(ledPin, OUTPUT);
	randomSeed(analogRead(A5);  //空接端口的读取值很不稳定,适合于做randomSeed()参数。
}

void loop() {
	analogWrite(ledPin, random(135) + 120);
	delay(random(200));
}

任务3: 通过串口调整灯光亮度

实验说明: 接收用户输入的0~255数值来改变接在11引脚的LED亮度。

用户通过按键的输入值是字符串格式,而模拟输出指令所需要的参数是数字格式。有两种方法把字符或字符串转换成数字。

  • 将ASCII码减掉48。数字0的十进制ASCII码是48
    ‘2’ - ‘0’ \Rightarrow 50 - 48 \Rightarrow 2
    ‘2’ - 48 \Rightarrow 50 -48 \Rightarrow 2
  • 使用atoi()函数指令,把字符串转换成数字。

比如:Arduino收到字符串 ‘168\n’

  1. 处理第一个字:pwm = pwm * 10 + (_in - '0')
  2. 处理第二个字:pwm = pwm * 10 + (_in - '0')
  3. 处理第三个字:pwm = pwm * 10 + (_in - '0')
  4. 读取到字符’\n’,转换完毕!
    由于每次传递的数字长度不确定(如: ‘3’ 和‘168’),为了让接收端确认一串数字的结尾,在发送数据的后面加上‘\n’字符。
    实验程序:
byte ledPin = 11;
void setup() {
	Serial.begin(9600);
}

void loop() {
	int pwm = 0;
	byte _in;
	if (Serial.available()) {
		_in = Serial.read();
		while (_in != '\n') {
			if (_in >= '0' && _in <='9') {
				pwm = pwm * 10 + (_in - '0');
			}
			_in = Serial.read();
		}
		if (pwm >255) pwm = 255;
		analogWrite(ledPin, pwm);
	}
}	

活动4:使用atoi()转换字符串成数字

实验说明: 先声明一个空字符串变量,每次收到的新字符,存入字符串变量

附录

1. random()

Description

The random function generates pseudo-random numbers.

Syntax

random(max)
random(min, max)

Parameters

min - lower bound of the random value, inclusive (optional)

max - upper bound of the random value, exclusive

Returns

A random number between min and max-1 (long) .

Example Code

The code generates random numbers and displays them.

long randNumber;

void setup(){
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // print a random number from 0 to 299
  randNumber = random(300);
  Serial.println(randNumber);

  // print a random number from 10 to 19
  randNumber = random(10, 20);
  Serial.println(randNumber);

  delay(50);
}

Notes and Warnings

If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.

Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This can be accomplished by calling randomSeed() with a fixed number, before starting the random sequence.

The max parameter should be chosen according to the data type of the variable in which the value is stored. In any case, the absolute maximum is bound to the long nature of the value generated (32 bit - 2,147,483,647). Setting max to a higher value won’t generate an error during compilation, but during sketch execution the numbers generated will not be as expected.

猜你喜欢

转载自blog.csdn.net/acktomas/article/details/83756097
今日推荐