温度
#include "stm32f10x.h"
#include "MYDelay.h"
#include "MYTemper.h"
double sqrt(double a) {
double b=a/2.0;
for(int i=0;i<10;i++) b=(b+a/b)/2.0;
return b;
}
int MYTemper_quickpow(int base, int power) {
int ans=1;
while(power>0) {
if(power&1)ans*=base;
power>>=1;
base*=base;
}
return ans;
}
#define DS_PIN GPIO_Pin_15
#define DS_PORT GPIOA
#define DS_LOW GPIO_ResetBits(DS_PORT, DS_PIN)
#define DS_HIGH GPIO_SetBits(DS_PORT, DS_PIN)
void MYTemper_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=DS_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(DS_PORT, &GPIO_InitStructure);
}
void MYTemper_IOCmd(uint8_t flag) {
GPIO_InitTypeDef GPIO_InitStructure;
if(flag) {
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=DS_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
} else {
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=DS_PIN;
}
GPIO_Init(DS_PORT, &GPIO_InitStructure);
}
uint8_t MYTemper_StateUP(void) {
uint8_t Data;
MYTemper_IOCmd(1);
DS_LOW;
MYDelayUs(480);
DS_HIGH;
MYDelayUs(30);
MYTemper_IOCmd(0);
Data=GPIO_ReadInputDataBit(DS_PORT, DS_PIN);
MYDelayUs(200);
return Data;
}
void MYTemper_WriteByte(uint8_t str) {
for(int i=0;i<8;i++) {
MYTemper_IOCmd(1);
DS_LOW;
MYDelayUs(2);
if(str&0x01) DS_HIGH;
else DS_LOW;
MYDelayUs(45);
DS_HIGH;
str>>=1;
}
}
uint8_t MYTemper_ReadByte(void) {
uint8_t str=0x00;
for(int i=0;i<8;i++) {
str>>=1;
MYTemper_IOCmd(1);
DS_LOW;
MYDelayUs(2);
DS_HIGH;
MYTemper_IOCmd(0);
if(GPIO_ReadInputDataBit(DS_PORT, DS_PIN) == SET) {
str|=0x80;
}
MYDelayUs(45);
}
return str;
}
double MYTemper_GetTemp(void) {
double tans=0.0;
uint8_t TMPL=0x00;
uint8_t TMPH=0x00;
uint16_t TMP=0x0000;
MYTemper_StateUP();
MYTemper_WriteByte(0xCC);
MYTemper_WriteByte(0x44);
MYDelayUs(750);
MYTemper_StateUP();
MYTemper_WriteByte(0xCC);
MYTemper_WriteByte(0xBE);
TMPL=MYTemper_ReadByte();
TMPH=MYTemper_ReadByte();
TMP=(TMPH<<8)|TMPL;
int flag=0;
if(TMPH>7) {
flag=1;
TMP=~TMP+1;
}
int TTMPL=TMP;
int TTMPH=(TMP>>8);
for(int i=0;i<4;i++) {
tans+=((double)(TTMPL&0x01))/((double)MYTemper_quickpow(2,4-i));
TTMPL>>=1;
}
for(int i=0; i<4; i++) {
tans+=((double)(TTMPL&0x01))*((double)MYTemper_quickpow(2,i));
TTMPL>>=1;
}
for(int i=4; i<7; i++) {
tans+=((double)(TTMPH&0x01))*((double)MYTemper_quickpow(2,i));
TTMPH>>=1;
}
if(flag==1) {
tans*=-1;
}
return tans;
}
double MYTemper_GetVel(void) {
return 331.45*sqrt(1.0+MYTemper_GetTemp()/273.15);
}
#ifndef __MYTEMPER_H
#define __MYTEMPER_H
void MYTemper_Init(void);
uint8_t MYTemper_StateUP(void);
double MYTemper_GetTemp(void);
double MYTemper_GetVel(void);
#endif
超声波
#include "stm32f10x.h"
#include "MYTimer.h"
#include "MYDelay.h"
#include "MYTemper.h"
#include "MYLed.h"
#define TRIG_PORT GPIOB
#define TRIG_PIN GPIO_Pin_10
#define ECHO_PORT GPIOB
#define ECHO_PIN GPIO_Pin_11
uint32_t time=0;
uint32_t etime=0;
uint16_t Dis=0;
void MYSound_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=ECHO_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(ECHO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=TRIG_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource11);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line11;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearFlag(EXTI_Line11);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void MYSound_StartUP(void) {
GPIO_SetBits(TRIG_PORT, TRIG_PIN);
MYDelayUs(15);
GPIO_ResetBits(TRIG_PORT, TRIG_PIN);
}
uint16_t MYSound_GetDistance(void) {
uint16_t dis=0;
uint16_t tmp=0;
for(int i=0;i<3;i++) {
MYSound_StartUP();
dis=(uint16_t)((MYTemper_GetVel()/10000)*etime);
tmp+=dis;
MYDelayMs(60);
}
Dis=tmp/3;
return Dis;
}
void TIM2_IRQHandler(void) {
if(TIM_GetITStatus(TIM2, TIM_IT_Update)==SET) {
time++;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
void EXTI15_10_IRQHandler(void) {
if(EXTI_GetITStatus(EXTI_Line11)==SET) {
if(GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN)==SET) {
time=0;
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, ENABLE);
}
if(GPIO_ReadInputDataBit(ECHO_PORT, ECHO_PIN)==RESET) {
TIM_Cmd(TIM2, DISABLE);
etime=(time*10+TIM_GetCounter(TIM2))/2;
}
EXTI_ClearITPendingBit(EXTI_Line11);
}
}
#ifndef __MYSOUND_H
#define __MYSOUND_H
void MYSound_Init(void);
uint16_t MYSound_GetDistance(void);
#endif
蓝牙
#include "stm32f10x.h"
#include "MYSerial.h"
uint8_t USART1_NVIC_IRQChannelPreemptionPriority = 1;
uint8_t USART1_NVIC_IRQChannelSubPriority = 1;
uint8_t USART2_NVIC_IRQChannelPreemptionPriority = 0;
uint8_t USART2_NVIC_IRQChannelSubPriority = 1;
void MYSerial_NVIC_PriorityGroupConfig_Init(void) {
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
}
void MYSerial_USART1_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART1_NVIC_IRQChannelPreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = USART1_NVIC_IRQChannelSubPriority;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void MYSerial_USART1_SentByte(uint8_t byte) {
USART_SendData(USART1, byte);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void MYSerial_USART1_SentArray(uint8_t* Arr, uint16_t Length) {
for(int i=0; i<Length; i++) MYSerial_USART1_SentByte(Arr[i]);
}
void MYSerial_USART1_SentString(char* Str) {
for(int i=0; Str[i]!='\0'; i++) MYSerial_USART1_SentByte(Str[i]);
}
void MYSerial_USART1_SentNumber(int Num) {
char Snum[10];
int flag=0;
int i=0;
int b=0;
if(Num<0) {
Num/=-1;
flag=1;
}
while(Num!=0) {
b=Num%10;
Snum[i++]='0'+b;
if(i>9) i=0;
Num/=10;
}
if(flag==1) MYSerial_USART1_SentByte('-');
while(i>=0) MYSerial_USART1_SentByte(Snum[i--]);
}
char MYSerial_USART1_RxPacketHead = '@';
char MYSerial_USART1_RxPacketTail_A = '\r';
char MYSerial_USART1_RxPacketTail_B = '\n';
char MYSerial_USART1_RxPacketData[100];
uint8_t MYSerial_USART1_RxPacketFlag=0;
uint8_t MYSerial_USART1_GetRxPacketFlag(void) {
uint8_t tmpFlag=0;
if(MYSerial_USART1_RxPacketFlag == 1) {
tmpFlag=1;
MYSerial_USART1_RxPacketFlag=0;
}
return tmpFlag;
}
void USART1_IRQHandler(void) {
static uint8_t flag=0;
static uint8_t pData=0;
uint8_t rxData;
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) {
rxData = USART_ReceiveData(USART1);
switch(flag) {
case 0:
if(rxData == MYSerial_USART1_RxPacketHead) flag=1;
break;
case 1:
if(rxData !=MYSerial_USART1_RxPacketTail_A) MYSerial_USART1_RxPacketData[pData++] = rxData;
else flag=2;
break;
case 2:
if(rxData == MYSerial_USART1_RxPacketTail_B) {
flag=0;
MYSerial_USART1_RxPacketFlag=1;
MYSerial_USART1_RxPacketData[pData] = '\0';
pData=0;
}
break;
}
USART_ClearITPendingBit(USART1, USART_FLAG_RXNE);
}
}
void MYSerial_USART2_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART2_NVIC_IRQChannelPreemptionPriority;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = USART2_NVIC_IRQChannelSubPriority;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void MYSerial_USART2_SentByte(uint8_t byte) {
USART_SendData(USART2, byte);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
void MYSerial_USART2_SentArray(uint8_t* Arr, uint16_t Length) {
for(int i=0; i<Length; i++) MYSerial_USART2_SentByte(Arr[i]);
}
void MYSerial_USART2_SentString(char* Str) {
for(int i=0; Str[i]!='\0'; i++) MYSerial_USART2_SentByte(Str[i]);
}
void MYSerial_USART2_SentNumber(int Num) {
char Snum[10];
int flag=0;
int i=0;
int b=0;
if(Num<0) {
Num/=-1;
flag=1;
}
while(Num!=0) {
b=Num%10;
Snum[i++]='0'+b;
if(i>9) i=0;
Num/=10;
}
if(flag==1) MYSerial_USART2_SentByte('-');
while(i>=0) MYSerial_USART2_SentByte(Snum[i--]);
}
char MYSerial_USART2_RxPacketHead = '@';
char MYSerial_USART2_RxPacketTail = '%';
char MYSerial_USART2_RxPacketData[100];
uint8_t MYSerial_USART2_RxPacketFlag=0;
uint8_t MYSerial_USART2_GetRxPacketFlag(void) {
uint8_t tmpFlag=0;
if(MYSerial_USART2_RxPacketFlag == 1) {
tmpFlag=1;
MYSerial_USART2_RxPacketFlag=0;
}
return tmpFlag;
}
void USART2_IRQHandler(void) {
static uint8_t flag=0;
static uint8_t pData=0;
uint8_t rxData;
if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET) {
rxData = USART_ReceiveData(USART2);
if(flag==0) {
if(rxData == MYSerial_USART2_RxPacketHead) {
flag=1;
}
} else if(flag==1) {
if(rxData !=MYSerial_USART2_RxPacketTail) {
MYSerial_USART2_RxPacketData[pData++] = rxData;
} else {
flag=0;
MYSerial_USART2_RxPacketFlag=1;
MYSerial_USART2_RxPacketData[pData] = '\0';
pData=0;
}
}
USART_ClearITPendingBit(USART2, USART_FLAG_RXNE);
}
}
#ifndef __MYSERIAL_H
#define __MYSERIAL_H
void MYSerial_NVIC_PriorityGroupConfig_Init(void);
extern char MYSerial_USART1_RxPacketData[100];
uint8_t MYSerial_USART1_GetRxPacketFlag(void);
void MYSerial_USART1_Init(void);
void MYSerial_USART1_SentByte(uint8_t byte);
void MYSerial_USART1_SentArray(uint8_t* Arr, uint16_t Length);
void MYSerial_USART1_SentString(char* Str);
void MYSerial_USART1_SentNumber(int Num);
extern char MYSerial_USART2_RxPacketData[100];
uint8_t MYSerial_USART2_GetRxPacketFlag(void);
void MYSerial_USART2_Init(void);
void MYSerial_USART2_SentByte(uint8_t byte);
void MYSerial_USART2_SentArray(uint8_t* Arr, uint16_t Length);
void MYSerial_USART2_SentString(char* Str);
void MYSerial_USART2_SentNumber(int Num);
#endif
延时
#include "stm32f10x.h"
#include "MYDelay.h"
static u8 fac_us=0;
static u16 fac_ms=0;
void MYDelay_Init(void) {
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
fac_us=SystemCoreClock/8000000;
fac_ms=(u16)fac_us*1000;
}
void MYDelayUs(uint32_t t) {
u32 tmp;
SysTick->LOAD=t*fac_us;
SysTick->VAL=0x00;
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
do {
tmp=SysTick->CTRL;
} while((tmp&0x01)&&!(tmp&(1<<16)));
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL=0x00;
}
void MYDelayMs(uint16_t t) {
u32 tmp;
SysTick->LOAD=t*fac_ms;
SysTick->VAL=0x00;
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
do {
tmp=SysTick->CTRL;
} while((tmp&0x01)&&!(tmp&(1<<16)));
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
SysTick->VAL=0x00;
}
void MYDelayS(uint16_t t) {
for(int i=0; i<1000; i++) MYDelayMs(t);
}
#ifndef __MYDEALY_H
#define __MYDEALY_H
void MYDelay_Init(void);
void MYDelayUs(uint32_t t);
void MYDelayMs(uint16_t t);
void MYDelayS(uint16_t t);
#endif
定时器
#include "stm32f10x.h"
void MYTimer2_Internal_Init(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=10-1;
TIM_TimeBaseInitStructure.TIM_Prescaler=72-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=DISABLE;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
}
#ifndef __MYTIMER_H
#define __MYTIMER_H
void MYTimer2_Internal_Init(void);
#endif
电机
#include "stm32f10x.h"
#include "MYDelay.h"
#include "MYMotor.h"
int MYAngle_now=0;
static uint8_t step=0;
void MYMotor_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_Port, ENABLE);
GPIO_InitTypeDef GPIO_Structure;
GPIO_Structure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Structure.GPIO_Pin=A|B|C|D;
GPIO_Init(GPIO_Port, &GPIO_Structure);
MYMotor_Stop();
}
void MYMotor_Control(uint8_t flag) {
if(flag==1) {
if(step==8) step=0;
step++;
} else if(flag==0) {
if(step==0||step==1) step=9;
step--;
}
switch(step) {
case 1:
MAH;
MBL;
MCL;
MDL;
break;
case 2:
MAH;
MBH;
MCL;
MDL;
break;
case 3:
MAL;
MBH;
MCL;
MDL;
break;
case 4:
MAL;
MBH;
MCH;
MDL;
break;
case 5:
MAL;
MBL;
MCH;
MDL;
break;
case 6:
MAL;
MBL;
MCH;
MDH;
break;
case 7:
MAL;
MBL;
MCL;
MDH;
break;
case 8:
MAH;
MBL;
MCL;
MDH;
break;
}
}
void MYMotor_Speed(uint32_t speed) {
MYDelayUs(speed);
}
void MYMotor_Start(int16_t angle, uint8_t flag, uint32_t speed) {
int pulse=(int)((double)(angle/5.625)*64);
for(int i=0; i<pulse; i++) {
MYMotor_Control(flag);
MYMotor_Speed(speed);
}
}
void MYMotor_Stop(void) {
MAL;
MBL;
MCL;
MDL;
}
void MYMotor_ADceleration(uint8_t adflag, uint8_t fflag, uint32_t speed) {
if(adflag==1) {
if(speed*4>20000) MYMotor_Start(1, fflag, 20000);
else MYMotor_Start(1, fflag, speed*4);
if(speed*2>10000) MYMotor_Start(1, fflag, 10000);
else MYMotor_Start(1, fflag, speed*2);
MYMotor_Start(1, fflag, speed/2*3);
MYMotor_Start(1, fflag, speed/5*6);
MYMotor_Start(1, fflag, speed/20*21);
}
else if(adflag==0) {
MYMotor_Start(1, fflag, speed/20*21);
MYMotor_Start(1, fflag, speed/5*6);
MYMotor_Start(1, fflag, speed/2*3);
if(speed*2>10000) MYMotor_Start(1, fflag, 10000);
else MYMotor_Start(1, fflag, speed*2);
if(speed*4>20000) MYMotor_Start(1, fflag, 20000);
else MYMotor_Start(1, fflag, speed*4);
}
}
#ifndef __MYMOTOR_H
#define __MYMOTOR_H
#define GPIO_Port GPIOA
#define RCC_APB2Periph_Port RCC_APB2Periph_GPIOA
#define A GPIO_Pin_7
#define B GPIO_Pin_6
#define C GPIO_Pin_5
#define D GPIO_Pin_4
#define MAL GPIO_ResetBits(GPIO_Port, A)
#define MAH GPIO_SetBits(GPIO_Port, A)
#define MBL GPIO_ResetBits(GPIO_Port, B)
#define MBH GPIO_SetBits(GPIO_Port, B)
#define MCL GPIO_ResetBits(GPIO_Port, C)
#define MCH GPIO_SetBits(GPIO_Port, C)
#define MDL GPIO_ResetBits(GPIO_Port, D)
#define MDH GPIO_SetBits(GPIO_Port, D)
extern int MYAngle_now;
void MYMotor_Init(void);
void MYMotor_Control(uint8_t flag);
void MYMotor_Start(int16_t angle, uint8_t flag, uint32_t speed);
void MYMotor_Stop(void);
void MYMotor_ADceleration(uint8_t adflag, uint8_t fflag, uint32_t speed);
void MYMotor_Speed(uint32_t speed);
#endif
LED
#include "stm32f10x.h"
#include "MYLed.h"
void MYLed_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_Structure;
GPIO_Structure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Structure.GPIO_Pin=GPIO_Pin_13;
GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_Structure);
MYLed_OFF();
}
void MYLed_ON(void) {
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
}
void MYLed_OFF(void) {
GPIO_SetBits(GPIOC, GPIO_Pin_13);
}
#ifndef __MYLED_H
#define __MYLED_H
void MYLed_Init(void);
void MYLed_ON(void);
void MYLed_OFF(void);
#endif
有源蜂鸣器
#include "stm32f10x.h"
#include "MYBuzzer.h"
void MYBuzzer_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_Structure;
GPIO_Structure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Structure.GPIO_Pin=GPIO_Pin_8;
GPIO_Structure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_Structure);
MYBuzzer_OFF();
}
void MYBuzzer_ON(void) {
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
void MYBuzzer_OFF(void) {
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
#ifndef __MYBUZZER_H
#define __MYBUZZER_H
void MYBuzzer_Init(void);
void MYBuzzer_ON(void);
void MYBuzzer_OFF(void);
#endif
流控
#include "stm32f10x.h"
#include <string.h>
#include "MYDelay.h"
#include "MYBuzzer.h"
#include "MYSerial.h"
#include "MYMOtor.h"
#include "MYOrder.h"
#include "MYSound.h"
int SYSSTART=0;
const uint8_t minAngle=2;
uint8_t judgeINITflag=0;
uint8_t judgeBuzzerflag=1;
int Ospeed=1000;
int MAUTO_A_L=-90;
int MAUTO_A_R=90;
uint8_t S_DISTANCE=200;
int bufferAngle=5;
int motorBufferflag=0;
void motorBuffer(uint8_t adflag, uint8_t fflag, uint32_t speed) {
MYMotor_ADceleration(adflag, fflag, speed);
}
int quickpow(int base, int power) {
int ans=1;
while(power>0) {
if(power&1) ans=ans*base;
power>>=1;
base=base*base;
}
return ans;
}
inline int abs(int num) {
if(num<0) num*=-1;
return num;
}
int MYStrCmpSetting(char str[], char ans[]) {
if(str[0]==ans[0]&&str[1]==ans[1]) return 1;
else return 0;
}
int zhuanhuan(int s,int e) {
if(s==e) return 0;
int flag=1,temp;
if(s>e) {
flag=-1;
temp=s;
s=e;
e=temp;
}
int temp1=e-s,temp2=360+s-e;
return flag*(temp1>=temp2?(-1*temp2):temp1);
}
void MYOrder_Judge(char str[]) {
if(str[0]=='O') {
MYOrder_Setting("NO");
MYOrder_Order(str);
} else if(str[0]=='S') {
MYOrder_Order("NO");
MYOrder_Setting(str);
}
}
int MYF_GoZero(char str[]) {
int bridgeflag=1;
if(MYAngle_now!=0) {
int jiaozhunflag=0;
int abs_MYAngle_now=abs(MYAngle_now);
if(MYAngle_now<0) jiaozhunflag=0;
else jiaozhunflag=1;
bridgeflag=jiaozhunflag;
if(abs_MYAngle_now>(bufferAngle*2)) {
motorBufferflag=1;
if(jiaozhunflag==0) MYAngle_now+=(bufferAngle*2);
else MYAngle_now-=(bufferAngle*2);
}
if(motorBufferflag==1) {
motorBuffer(1,jiaozhunflag,Ospeed);
}
int jiaozhunbreakflag=1;
while(1) {
MYMotor_Start(1,jiaozhunflag,Ospeed);
if(jiaozhunflag==1) MYAngle_now--;
else MYAngle_now++;
if(strcmp(str, "OAUTO")!=0) {
jiaozhunbreakflag=0;
break;
}
if(motorBufferflag==1) {
if(bridgeflag==1) {
if(MYAngle_now==0) break;
} else {
if(MYAngle_now==-1*bufferAngle) {
motorBuffer(0,0,Ospeed);
break;
}
}
} else {
if(MYAngle_now==0) break;
}
}
if(jiaozhunbreakflag==0) {
if(motorBufferflag==1) {
if(jiaozhunflag==0) MYAngle_now-=bufferAngle;
else MYAngle_now+=bufferAngle;
}
}
}
return bridgeflag;
}
void MYOrder_Order(char str[]) {
if(!strcmp(str, "NO")) {
} else if(!strcmp(str, "OSTART")) {
SYSSTART=1;
MYBuzzer_ON();
MYDelayMs(500);
MYBuzzer_OFF();
} else if(!strcmp(str, "ONSTART")) {
SYSSTART=0;
MYBuzzer_ON();
MYDelayMs(200);
MYBuzzer_OFF();
} else if(!strcmp(str, "OAUTO")) {
int bridgeflag=MYF_GoZero(str);
int i=0;
int tflag=1;
int flag=1;
int syspolicebreakflag=1;
if(bridgeflag==0) motorBuffer(1,1,Ospeed);
while(1) {
MYMotor_Start(1,flag,Ospeed);
i++;
if(flag==0) {
MYAngle_now++;
if(MYAngle_now>=180) MYAngle_now=-180;
} else {
MYAngle_now--;
if(MYAngle_now<=-180) MYAngle_now=180;
}
if(tflag==1) {
if(bridgeflag==1) {
if(i==90-bufferAngle) {
motorBuffer(0,1,Ospeed);
flag=0;
tflag=0;
i=0;
}
} else {
if(i==90-bufferAngle*2) {
motorBuffer(0,1,Ospeed);
flag=0;
tflag=0;
i=0;
}
}
} else {
if(i==0 && flag==0) motorBuffer(1,0,Ospeed);
if(i==0 && flag==1) motorBuffer(1,1,Ospeed);
if(i==180-bufferAngle*2) {
if(flag==0) {
motorBuffer(0,0,Ospeed);
flag=1;
} else {
motorBuffer(0,1,Ospeed);
flag=0;
}
i=0;
}
}
if(strcmp(str, "OAUTO")!=0) {
syspolicebreakflag=0;
break;
}
}
if(syspolicebreakflag==0) {
if(tflag==1) {
MYAngle_now+=bufferAngle;
} else {
if(flag==1) {
MYAngle_now-=bufferAngle;
} else {
MYAngle_now+=bufferAngle;
}
}
}
} else if(!strcmp(str, "ONAUTO")) {
MYMotor_Stop();
} else if(!strcmp(str, "OMAUTO")) {
int bridgeflag=MYF_GoZero(str);
if(bridgeflag==0) {
motorBuffer(1,1,Ospeed);
MYAngle_now-=bufferAngle;
}
int flag=1;
while(1) {
MYMotor_Start(1,flag,Ospeed);
if(flag==1) {
MYAngle_now--;
if(MYAngle_now<=-180) MYAngle_now=180;
} else {
MYAngle_now--;
if(MYAngle_now<=-180) MYAngle_now=180;
}
if(MYAngle_now==MAUTO_A_L+bufferAngle && flag==1) {
flag=0;
motorBuffer(0,1,Ospeed);
MYAngle_now-=bufferAngle;
}
if(MYAngle_now==MAUTO_A_L && flag==1) {
flag=0;
motorBuffer(1,0,Ospeed);
MYAngle_now+=bufferAngle;
}
if(MYAngle_now==MAUTO_A_R-bufferAngle && flag==0) {
flag=1;
motorBuffer(0,0,Ospeed);
MYAngle_now+=bufferAngle;
}
if(MYAngle_now==MAUTO_A_R && flag==0) {
flag=1;
motorBuffer(1,1,Ospeed);
MYAngle_now-=bufferAngle;
}
if(strcmp(str, "OMAUTO")!=0) {
MYMotor_Stop();
break;
}
}
} else if(!strcmp(str, "ONMAUTO")) {
MYMotor_Stop();
} else if(!strcmp(str, "OBUZZER")) {
judgeBuzzerflag=1;
} else if(!strcmp(str, "ONBUZZER")) {
judgeBuzzerflag=0;
} else if(!strcmp(str, "OLEFT")) {
MYMotor_Start(minAngle,1,Ospeed);
if(judgeINITflag==0) {
MYAngle_now-=minAngle;
if(MYAngle_now<=-180) MYAngle_now=180;
}
MYMotor_Stop();
} else if(!strcmp(str, "ORIGHT")) {
MYMotor_Start(minAngle,0,Ospeed);
if(judgeINITflag==0) {
MYAngle_now+=minAngle;
if(MYAngle_now>=180) MYAngle_now=-180;
}
MYMotor_Stop();
} else if(!strcmp(str, "OLLEFT")) {
while(1) {
MYMotor_Start(1,1,Ospeed);
if(strcmp(str, "OLLEFT")!=0) break;
if(judgeINITflag==0) {
MYAngle_now-=1;
if(MYAngle_now<=-180) MYAngle_now=180;
}
}
} else if(!strcmp(str, "ORRIGHT")) {
while(1) {
MYMotor_Start(1,0,Ospeed);
if(strcmp(str, "ORRIGHT")!=0) break;
if(judgeINITflag==0) {
MYAngle_now+=1;
if(MYAngle_now>=180) MYAngle_now=-180;
}
}
} else if(!strcmp(str, "ONLLEFT")) {
MYMotor_Stop();
} else if(!strcmp(str, "ONRRIGHT")) {
MYMotor_Stop();
} else if(!strcmp(str, "OMINIT")) {
judgeINITflag=1;
MYAngle_now=0;
} else if(!strcmp(str, "ONMINIT")) {
judgeINITflag=0;
}
}
void MYOrder_Setting(char str[]) {
if(MYStrCmpSetting(str,"NO")) {
} else if(MYStrCmpSetting(str,"ST")) {
int STAngle=0;
char STAngleStr[100];
int STLength=0;
char STSettingBefore[100];
for(int i=0; str[i]!='\0'; i++) STSettingBefore[i]=str[i];
int STRoll=0;
for(int i=3; str[i]!='\0'; i++) STAngleStr[STLength++]=str[i];
for(int i=STLength, j=0; i>0; i--) STAngle+=((STAngleStr[i-1]-'0')*quickpow(10,j++));
if(str[2]=='-') STAngle*=-1;
int STChangeAngle=zhuanhuan(MYAngle_now, STAngle);
int ifSTChangeAngleZero=0;
if(STChangeAngle<0) STRoll=1;
else if(STChangeAngle>0) STRoll=0;
else if(STChangeAngle==0) {
STRoll=0;
ifSTChangeAngleZero=1;
}
STChangeAngle=abs(STChangeAngle);
int STCont=0;
if(ifSTChangeAngleZero!=1) {
while(1) {
MYMotor_Start(1,STRoll,Ospeed);
STCont++;
if(STRoll==1) {
MYAngle_now-=1;
if(MYAngle_now<=-180) MYAngle_now=180;
} else if(STRoll==0) {
MYAngle_now+=1;
if(MYAngle_now>=180) MYAngle_now=-180;
}
if(strcmp(str,STSettingBefore)!=0) {
memset(STSettingBefore,0,100);
for(int i=0; str[i]!='\0'; i++) STSettingBefore[i]=str[i];
break;
}
if(STCont==STChangeAngle) {
MYMotor_Stop();
break;
}
}
} else MYMotor_Stop();
} else if(MYStrCmpSetting(str,"SA")) {
int SALengthL=0;
int SALengthR=0;
char SAStr[100];
MAUTO_A_L=0;
MAUTO_A_R=0;
memset(SAStr,0,100);
for(int i=2;str[i]!='#';i++) {
SAStr[SALengthL++]=str[i];
}
for(int i=SALengthL, j=0;i>0;i--) MAUTO_A_L+=((SAStr[i-1]-'0')*quickpow(10,j++));
MAUTO_A_L*=-1;
memset(SAStr,0,100);
for(int i=SALengthL+3;str[i]!='\0';i++) {
SAStr[SALengthR++]=str[i];
}
for(int i=SALengthR, j=0;i>0;i--) MAUTO_A_R+=((SAStr[i-1]-'0')*quickpow(10,j++));
} else if(MYStrCmpSetting(str,"SD")) {
int SDLength=0;
char SDStr[100];
S_DISTANCE=0;
memset(SDStr,0,100);
for(int i=2;str[i]!='\0';i++) {
SDStr[SDLength++]=str[i];
}
for(int i=SDLength, j=0;i>0;i--) S_DISTANCE+=((SDStr[i-1]-'0')*quickpow(10,j++));
}
}
void MYOrder_SentStr(void) {
char str[10];
char tstr[10];
int Length=0;
int gewei=0;
int tMYAngle_now=0;
if(SYSSTART) {
if(MYAngle_now==0) {
str[0]='+';
str[1]='0';
Length=1;
} else {
if(MYAngle_now<0) {
str[0]='-';
tMYAngle_now=MYAngle_now*-1;
} else {
str[0]='+';
tMYAngle_now=MYAngle_now;
}
while(tMYAngle_now!=0) {
gewei=tMYAngle_now%10;
tstr[Length++]=gewei+'0';
tMYAngle_now/=10;
}
for(int i=Length-1, j=1;i>=0;i--) {
str[j++] = tstr[i];
}
}
str[Length+1]='#';
if(MYSound_GetDistance()<S_DISTANCE) str[Length+2]='Y';
else str[Length+2]='N';
MYSerial_USART2_SentString(str);
}
}
#ifndef __MYORDER_H
#define __MYORDER_H
void MYOrder_Judge(char str[]);
void MYOrder_Order(char str[]);
void MYOrder_Setting(char str[]);
void MYOrder_SentStr(void);
#endif
sys
#ifndef __SYS_H_
#define __SYS_H_
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+12)
#define GPIOB_ODR_Addr (GPIOB_BASE+12)
#define GPIOC_ODR_Addr (GPIOC_BASE+12)
#define GPIOD_ODR_Addr (GPIOD_BASE+12)
#define GPIOE_ODR_Addr (GPIOE_BASE+12)
#define GPIOF_ODR_Addr (GPIOF_BASE+12)
#define GPIOG_ODR_Addr (GPIOG_BASE+12)
#define GPIOA_IDR_Addr (GPIOA_BASE+8)
#define GPIOB_IDR_Addr (GPIOB_BASE+8)
#define GPIOC_IDR_Addr (GPIOC_BASE+8)
#define GPIOD_IDR_Addr (GPIOD_BASE+8)
#define GPIOE_IDR_Addr (GPIOE_BASE+8)
#define GPIOF_IDR_Addr (GPIOF_BASE+8)
#define GPIOG_IDR_Addr (GPIOG_BASE+8)
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n)
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n)
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n)
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n)
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n)
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n)
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n)
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n)
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n)
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n)
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n)
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n)
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n)
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n)
#endif
main
#include "stm32f10x.h"
#include "MYLed.h"
#include "MYDelay.h"
#include "MYMotor.h"
#include "MYBuzzer.h"
#include "MYSerial.h"
#include "MYOrder.h"
#include "OLED.h"
#include "MYSound.h"
#include "MYTimer.h"
#include "MYTemper.h"
int main(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
MYDelay_Init();
OLED_Init();
MYMotor_Init();
MYLed_Init();
MYBuzzer_Init();
MYSound_Init();
MYTemper_Init();
MYSerial_NVIC_PriorityGroupConfig_Init();
MYSerial_USART2_Init();
MYTimer2_Internal_Init();
extern uint16_t Dis;
extern int MYAngle_now;
extern uint8_t S_DISTANCE;
extern uint8_t judgeBuzzerflag;
extern int SYSSTART;
OLED_ShowString(1,1,"UMARAAS");
while(1) {
if(MYSerial_USART2_GetRxPacketFlag() == 1) {
OLED_Clear();
OLED_ShowString(1, 1, MYSerial_USART2_RxPacketData);
MYOrder_Judge(MYSerial_USART2_RxPacketData);
}
MYOrder_SentStr();
OLED_ShowString(2,1,"temper:");
OLED_ShowNum(2,8,MYTemper_GetTemp(), 2);
OLED_ShowString(2,10,"cel");
OLED_ShowString(3,1,"distance:");
OLED_ShowNum(3,10,Dis, 4);
OLED_ShowString(3,14,"cm");
OLED_ShowString(4,1,"angle:");
OLED_ShowSignedNum(4,7,MYAngle_now, 3);
OLED_ShowString(4,11,"deg");
if(judgeBuzzerflag&&SYSSTART) {
if(MYSound_GetDistance()<S_DISTANCE) MYBuzzer_ON();
else MYBuzzer_OFF();
}
}
}