Ⅰ 如何用单片机控制舵机及程序详细
//双定时器产生PWM波,控制舵机
//定时器0负责置高电平和控制定时器1的开停,定时器1负责置低电平
#include<STC12C5A.H>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P3^7;
uint b=1500;//1.5ms 舵机0位
void Delay1ms(uint i) //1ms延时程序
{
uint j;
for(;i>0;i--)
{
for(j=0;j<125;j++)
{;}
}
}
void main()
{
b=1500;
TMOD=0X11;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
TH1=(65536-b)/256;
TL1=(65536-b)%256;
ET0=1;
EA=1;
TR0=1;
PWM=1;
P1M0=0X0F; //P1低4位推挽输出 接L298N直流电机驱动模块 带动后轮两个电机
P1M1=0X00;
while(1)
{
P1=0x0a; //低4位 1010 正转
b=1500;Delay1ms(2000);//直行
b=1000;Delay1ms(1000);//左转
b=1500;Delay1ms(2000);//直行
b=2000;Delay1ms(1000);//右转
b=1500;Delay1ms(2000);//直行
P1=0x05; //低4位 0101 反转
b=1500;Delay1ms(2000);//后退
b=1000;Delay1ms(1000);//后左转
b=1500;Delay1ms(2000);//后退
b=2000;Delay1ms(1000);//后右转
b=1500;Delay1ms(2000);//后退
}
}
void timer0()interrupt 1//定时器0定时20ms
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
PWM=1;
TR1=1;
ET1=1;
}
void timer1()interrupt 3//定时器1定时1到2ms
{
TH1=(65536-b)/256;
TL1=(65536-b)%256;
PWM=0;
TR1=0;
ET1=0;
}
Ⅱ 单片机问题:如何编写云台控制器程序
云台有控制信号标准,485协议的
照着写个驱动就行了
Ⅲ 关于云台控制
去买本单片机实战的书吧,很多的书上都有.
Ⅳ 单片机云台直线插补程序
三级片轮胎直播插补程序,到网络搜索告诉你步骤,一步一步就可以安装。
Ⅳ 怎么写单片机控制外围芯片程序
兄弟,参加电子设计大赛吧??b题?
这个最重要的是看芯片资料,芯片资料内对通信方式,协议,时序,都有的,可以利用单片机自带的通信接口,即端口第二功能。也可以自己制定时序。
这是24c02的程序。用的ATmega16;
最后祝你有个好成绩
#include <iom16v.h>
#include <macros.h>
#include "delay.h"
#include "lcd.h"
#define uchar unsigned char
#define uint unsigned int
/**************************************************************************
* 函数名称: ERROR();
* 功 能: 蜂鸣器报错
* 参 数:无
* 描 述:PA0口置低可以鸣响蜂鸣器
**************************************************************************/
void ERROR()
{
uint i;
DDRA |= (1 << PA0);
PORTA &= ~(1 << PA0);
for( i = 0 ; i < 10000 ; i ++);
PORTA |= (1 << PA0);
}
//********************************************************************8
//*函数 向24c02发送一个字节
//*名称 wtwib();
//*参数 地址,数据
//*********************************************************************/
void wtwib(uchar ad,uchar da)
{ TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA0; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWDR =da; //send the data to the bus
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
//=================================================================
//=======read a byte from 24c02
//========rtwib()
//==========need address
//================================================================
uchar rtwib(uchar ad)
{ uchar da;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x58)
ERROR();
da=TWDR;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
return da;
}
//========================================================
//====== send a page to the 24c02
//====== wtwis();
//=======need address and data and number;
//=========================================================
void wtwip(uchar ad,uchar n,uchar *da)
{ uchar i;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//发送start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA0; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
for(i=0;i<n;i++)
{ TWDR =da[i]; //send the data to the bus
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
}
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
//====================================================
//========read a string from the 24C02
//========rtwis();
//========need address,number
//=====================================================
void rtwis(uchar ad,uchar *p,uchar n)
{ uchar i;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
TWDR =ad; //adress of the data
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x28)
ERROR();
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);//send the signal of start
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x08)
ERROR();
TWDR =0xA1; //adress of e2prom
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x18)
ERROR();
for(i=0;i<n;i++)
{ TWCR = (1<<TWINT) | (1<<TWEN)|(1<<TWEA);
while (!(TWCR & (1<<TWINT)));
if ((TWSR & 0xF8) != 0x50)
ERROR();
p[i]=TWDR;
}
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send the stop signal
}
void main()
{ uchar a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},i=0;
init_lcd();
TWBR = 0x1F;
TWSR = 0x02;
for(i=0;i<16;i++)
wtwib( 0x00+i,i);
for(i=0;i<16;i++)
rtwib( 0x00+i);
for(i=0;i<16;i++)
a[i]=0;
for(i=0;i<16;i++)
send_d(a[i]+48);
}
Ⅵ 用单片机stm32控制二轴云台要用到哪些模块
看你用什么控制云台,如果用的是舵机的话,只需要用到定时器的pwm输出就行,改变pwm占空比,即可控制舵机转动。pwm频率一般在50Hz。 具体看舵机型号
Ⅶ 用单片机做一个控制系统的基本流程是什么
设计电路。
编写程序。
现场调试。
Ⅷ 单片机程序控制的实质是什么
单片机程序控制的实质是在一个规定的时间内(这叫时钟),按照自己给他设定的条件,(其中可有加、减、比较、取样)去执行自己先给它规定好的不同的执行方式。最终的表现都体现在这脚的输出高低电平上。这不只是单片机,计算机也是这样运行的。
单片机是一种集成电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU、随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计数器等功能(可能还包括显示驱动电路、脉宽调制电路、模拟多路转换器、A/D转换器等电路)集成到一块硅片上构成的一个小而完善的微型计算机系统,在工业控制领域广泛应用。