avr单片机定时器控制串口
It is also a very simple program, the Simple DC motor can be rotated clockwise when the input is given to the positive terminal and vice versa for Anti-clockwise rotation.
这也是一个非常简单的程序, 当将输入提供给正极端子时,可将Simple DC电动机顺时针旋转,反之亦然 。
程序顺时针和逆时针旋转直流电动机 (Program to Rotate DC motor clockwise and anti clockwise )
#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>
int main(void)
{
DDRA = 0x81;
while(1)
{
PORTA = 0x01;
_delay_ms(1000);
PORTA = 0x00;
_delay_ms(500);
PORTA = 0x80;
_delay_ms(1000);
PORTA = 0x00;
_delay_ms(500);
}
}
Explanation
说明
When you will connect the motor ATmega16 with the DC motor the terminals required would be PA0 and PA7.
当您将电动机ATmega16与直流电动机连接时,所需的端子将是PA0和PA7。
PA7 PA6 PA5 PA4 PA4 PA3 PA2 PA1 PA0
1 0 0 0 0 0 0 0 1
The above representation can be represented in Hexadecimal as 0x81, the first four term i.e. 1000 represents 8 in binary and the last four term 0001 will represent 1 in binary. Therefore the input pins are defined as DDRA = 0x81.
上面的表示可以十六进制表示为0x81 ,前四个项(即1000表示二进制形式的8),后四个项0001将以二进制表示1。 因此,输入引脚定义为DDRA = 0x81 。
Inside the while loop firstly we have given input to the PA0 i.e. (PORTA = 0x01) the motor will rotate in the clockwise direction.
首先在while循环内部,我们给PA0提供了输入,即( PORTA = 0x01 ),电动机将沿顺时针方向旋转。
Then for 500 milliseconds we have stopped our motor (PORTA = 0x00) & then again we have given our input to PA7 terminal which would move the Motor in anti-clockwise direction.
然后在500毫秒内我们停止了电动机( PORTA = 0x00 ),然后再次将输入提供给PA7端子,该输入将使电动机沿逆时针方向移动。
Again it is stopped and further the same cycle repeats.
再次停止,并重复相同的循环。
Simulation
模拟
Explanation
说明
Devices required:
所需设备:
- ATMEGA16
- Simple DC motor
Arrange the setup as shown in the figure.
如图所示安排设置。
Double click on the motor and click on edit properties and then change the voltage our motor to 3v or any small value, decreasing the voltage of our Motor will increase its speed of rotation.
双击电动机,然后单击编辑属性,然后将电动机的电压更改为3v或任何较小的值,降低电动机的电压将提高其旋转速度。
Put the HEX file in ATmega16 by double clicking it and selecting the path.
双击将HEX文件放入ATmega16,然后选择路径。
Click on the play Button and your Simulation will run as we wanted.
单击播放按钮,您的模拟将按照我们的要求运行。
avr单片机定时器控制串口