Ⅰ 如何用單片機控制舵機及程序詳細
//雙定時器產生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轉換器等電路)集成到一塊矽片上構成的一個小而完善的微型計算機系統,在工業控制領域廣泛應用。