資源簡介
STM8S輸出PWM控制模擬舵機+MPU6050陀螺儀信號解碼,用于玩具機器人控制

代碼片段和文件信息
#include?“iostm8s103f3.h“//配置TIM2通道1為PWM輸出的過程。
#define?bit0?0x01?
#define?bit1?0x02?
#define?bit2?0x04?
#define?bit3?0x08?
#define?bit4?0x10?
#define?bit5?0x20?
#define?bit6?0x40?
#define?bit7?0x80
#define?P?1
#define?D?1
int?CurPos;//current?position?75-187-300?(?0-90-180°)
unsigned?char?Re_buf[11]counter=0;//UASRT?data
unsigned?char?isRead=0;
float?a[3]w[3]angle[3]T;
void?Timer2_Init(void)?
{?
??CLK_ICKR|=0x01;?????????//開啟內部HSI?
??while(!(CLK_ICKR&0x02));//HSI準備就緒?
??CLK_SWR=0xe1;???????????//HSI為主時鐘源?
??CLK_CKDIVR=0x00;??//外設時鐘8分頻=2MHz?
??
??TIM2_PSCR=0x07;??//Timer2時鐘128分頻,計算一次8us
??TIM2_ARRH=25;?????//自動重裝的值
??TIM2_ARRL=196;??????//PWM周期為20000us
??TIM2_CCER1=bit0?|?bit4;??//?high?levelOC1
??TIM2_CCMR1=bit3|bit5|bit6;??//MODE?
??TIM2_CCR1H=0x00;????????????//占空比
??TIM2_CCR1L=0x00;?
??TIM2_CCER1?|=?0x10;???//開啟OC2信號輸出腳
??TIM2_CCMR2?=?0x60;???//PWM1模式??
??TIM2_CCMR2?|=?0x08;???//輸出比較2預裝載使能
??TIM2_CCR2H=0x00;????????????//占空比
??TIM2_CCR2L=0x00;?
??
??TIM2_IER=0x00;?????????????//更新中斷使能?
??TIM2_CR1=bit0;????????????//enable?counter?
}
void?GPIO_Init(void)
{
??PB_DDR|=0x20;
??PB_CR1|=0x20;
??PB_CR2|=0x00;
}
void?ServoPosition(int?pos)//position?40?75-187-300?2400?0?means?no?torque
{
??TIM2_CCR1H=pos/256;?//占空比
??TIM2_CCR1L=pos%256;
}
void?ServoPT(int?posint?time)//time?about?10
{
??int?kijd;
??if(CurPos>pos)
??{
????d=CurPos-pos;
????for(k=0;k ????{
????ServoPosition(--CurPos);
????for(i=0;i??????for(j=0;j<5000;j++);
????}
??}
??else?
??{
????d=pos-CurPos;
????for(k=0;k ????{
??????ServoPosition(++CurPos);
??????for(i=0;i????????for(j=0;j<5000;j++);
????}
??}
}
void?StopRotate()
{
??TIM2_CCR2H=0;?//占空比
??TIM2_CCR2L=0;
}
void?RotateClockwise()
{
??TIM2_CCR2H=0;?//占空比
??TIM2_CCR2L=50;
}
void?RotaetClockwiseTime(int?t)//rotate?clockwisely?with?given?time(no?unit)
{
??int?ijk;
??for(i=0;i ????for(j=0;j<500;j++)
????{
??????StopRotate();
??????for(k=0;k<4;k++);
??????RotateClockwise();
??????for(k=0;k<4;k++);
????}
??StopRotate();
}
void?RotateCntrClockwise()
{
??TIM2_CCR2H=600/256;?//占空比
??TIM2_CCR2L=600%256;
}
void?RotateTo0()
{
??TIM2_CCR2H=0;?//占空比
??TIM2_CCR2L=187;
}
void?Init_UART1(void)//Baud?Rate?9600
{
??????UART1_CR1=0x00;
??????UART1_CR2=0x00;
??????UART1_CR3=0x00;
??????//?設置波特率,必須注意以下幾點:
??????//?(1)?必須先寫BRR2
??????//?(2)?BRR1存放的是分頻系數的第11位到第4位,
??????//?(3)?BRR2存放的是分頻系數的第15到12位,和第3到0位
??????//?例如對于波特率位9600時,分頻系數=2000000/9600=208
??????//?對應的十六進制數為00D0,BBR1=0DBBR2=00
??????UART1_BRR2=0x00;
??????UART1_BRR1=0x0d;
??????UART1_CR2=0x2c;//允許接收,發送,開接收中斷?*asm(“rim“);開中斷,sim為關中斷
}
void?UART1_sendchar(unsigned?char?c)
{
??????while((UART1_SR?&?0x80)==0x00);
??????UART1_DR=c;
}
#pragma?vector=?UART1_R_OR_vector//0x19
__interrupt?void?UART1_R_OR_IRQHandler(void)
{
??????Re_buf[counter]=UART1_DR;//不同單片機略有差異
??????if((counter==0)&&(Re_buf[0]
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-08-06?14:29??rolbot-stm8\
?????目錄???????????0??2015-07-17?09:34??rolbot-stm8\Debug\
?????目錄???????????0??2015-08-06?14:29??rolbot-stm8\Debug\Exe\
?????文件????????5422??2015-08-06?14:29??rolbot-stm8\Debug\Exe\rolbot.hex
?????文件???????41360??2015-08-06?14:29??rolbot-stm8\Debug\Exe\rolbot.out
?????目錄???????????0??2015-08-06?14:29??rolbot-stm8\Debug\List\
?????文件???????14282??2015-08-06?14:29??rolbot-stm8\Debug\List\main.lst
?????文件???????16832??2015-08-06?14:29??rolbot-stm8\Debug\List\rolbot.map
?????目錄???????????0??2015-08-06?14:29??rolbot-stm8\Debug\Obj\
?????文件???????48970??2015-08-06?14:29??rolbot-stm8\Debug\Obj\main.o
?????文件?????????510??2015-08-06?14:29??rolbot-stm8\Debug\Obj\main.pbi
?????文件?????????141??2015-08-06?14:29??rolbot-stm8\Debug\Obj\rolbot.pbd
?????文件????????5562??2015-08-05?09:34??rolbot-stm8\code?backup.txt
?????文件????????6176??2015-08-06?14:29??rolbot-stm8\main.c
?????文件????????2306??2015-08-06?14:29??rolbot-stm8\rolbot.dep
?????文件???????10282??2015-07-18?16:03??rolbot-stm8\rolbot.ewd
?????文件???????41353??2015-07-18?16:14??rolbot-stm8\rolbot.ewp
?????文件?????????160??2015-07-17?10:25??rolbot-stm8\rolbot.eww
?????目錄???????????0??2015-07-17?11:50??rolbot-stm8\settings\
?????文件????????1954??2015-08-06?10:36??rolbot-stm8\settings\rolbot.cspy.bat
?????文件????????5273??2015-08-05?21:48??rolbot-stm8\settings\rolbot.dbgdt
?????文件?????????714??2015-08-05?21:48??rolbot-stm8\settings\rolbot.dni
?????文件????????3137??2015-08-06?14:29??rolbot-stm8\settings\rolbot.wsdt
- 上一篇:圖片從本地上傳服務端
- 下一篇:基于GTSRB數據集CNN交通標志識別
評論
共有 條評論