本文为原创文章,转载需要注明转载出处
前言:嵌入式开发是相比其他IT行业是比较难的行业,它不仅需要你去掌握软件的经验,还要去学习一些硬件的知识,
它是软件和硬件的一个粘合剂,起到一个至关重要的地位
下面我们接着讲第三弹
【STM32】【C】【嵌入式】分享一下我的项目经验--基于stm32桌面主从机械臂设计(一弹)
【STM32】【C】【嵌入式】分享一下我的项目经验--基于stm32桌面主从机械臂设计(二弹)
【STM32】【C】【嵌入式】分享一下我的项目经验--基于stm32桌面主从机械臂设计(三弹)
下面是从机械臂的代码
下面是/*motor.h*/
#ifndef __MOTOR_H__
#define __MOTOR_H__
#include "sys.h"
#define FALSE 0
#define TRUE 1
#define MS1_left PAout(0)
#define MS2_left PAout(1)
#define MS3_left PAout(2)
#define DIR_left PAout(3)
#define STEP_left PAout(4)
#define ENABLE1_left PAout(5)
#define MS1_right PBout(0)
#define MS2_right PAout(7)
#define MS3_right PAout(6)
#define DIR_right PBout(1)
#define STEP_right PBout(10)
#define ENABLE2_right PBout(11)
#define MS1 PBout(3)
#define MS2 PBout(5) //PA15ÓÐÎÊÌâ PB4ÓÐÎÊÌâ
#define MS3 PAout(8)
#define DIR PBout(15)
#define STEP PBout(14)
#define ENABLE0 PBout(13)
#define Full_step {MS1_left = 0;MS2_left = 0;MS3_left = 0;MS1_right = 0;MS2_right = 0;MS3_right = 0;}
#define Half_step {MS1_left = 1;MS2_left = 0;MS3_left = 0;MS1_right = 1;MS2_right = 0;MS3_right = 0;}
#define Quarter_step {MS1_left = 0;MS2_left = 1;MS3_left = 0;MS1_right = 0;MS2_right = 1;MS3_right = 0;}
#define Eighth_step {MS1_left = 1;MS2_left = 1;MS3_left = 0;MS1_right = 1;MS2_right = 1;MS3_right = 0;}
#define Sixteenth_step {MS1_left = 1;MS2_left = 1;MS3_left = 1;MS1_right = 1;MS2_right = 1;MS3_right = 1;}
#define Full_step0 {MS1 = 0;MS2 = 0; MS3 = 0;}
#define Half_step0 {MS1 = 1;MS2 = 0; MS3 = 0;}
#define Quarter_step0 {MS1 = 0;MS2 = 1; MS3 = 0;}
#define Eighth_step0 {MS1 = 1;MS2 = 1; MS3 = 0;}
#define Sixteenth_step0 {MS1 = 1;MS2 = 1; MS3 = 1;}
#define Full_360 5000
#define Half_180 2500
#define Quarter_90 1250
#define Eighth_45 625
#define Nine_9 125
#define Eighteen_18 250
#define Twenty_seven_27 375
#define Thirty_six_36 500
#define One_1 14
#define Four_5 70
void MOTOR_Step_Control(int dir,u16 period,u32 steps);
void MOTOR_Init(void);
void MOTOR_Step_Init(void);
void MOTOR_Step_Enable(void);
void MOTOR_Step_Micr(u16);
void MOTOR_Step2_Enable(void);
void MOTOR_Step2_Micr(u16);
void MOTOR_Step2_Control(int dir,u16 period,u32 steps);
void MOTOR_Step3_Enable(void);
void MOTOR_Step3_Micr(u16);
void MOTOR_Step3_Control(int dir,u16 period,u32 steps);
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
下面是/*motor.c*/
#include "motor.h"
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
void MOTOR_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // PA,PD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO ???? 50MHz
//MS1->L MS2->L MS3->L Full step //2 Phase
//MS3
GPIO_Init(GPIOA, &GPIO_InitStructure); //??? GPIOA.2
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA.1 ???
//MS2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
//MS1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
//DIR
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
//STEP
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
//MS1->L MS2->L MS3->L Full step //2 Phase
//ENABLE
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure); //??? GPIOA.2
GPIO_ResetBits(GPIOB,GPIO_Pin_11); //PA.1 ???
//STEP
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
//DIR
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
//MS1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
//MS2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
//MS3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
//MS1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_3);
//MS2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
//MS3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
//DIR
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
//STEP
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
//ENABLE
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
}
void MOTOR_Step_Control(int dir,u16 period,u32 steps){
u32 i;
if(dir == 1){
for(i = 0;i < steps;i++){
DIR_left = 1;
STEP_left = 0;
//delay_ms(800);
delay_ms(1);
STEP_left = 1;
//delay_ms(800);
delay_us(period);
}
}
if(dir == 0){
for(i = 0;i < steps;i++){
DIR_left = 0;
STEP_left = 0;
//delay_ms(800);
delay_ms(1);
STEP_left = 1;
//delay_ms(800);
delay_us(period);
}
}
/*
for(i = 0;i < steps * 2;i++){
DIR = !dir;
STEP = 1;
delay_us(4);
STEP = 0;
delay_us(period);
}
*/
//i = 0;
}
void MOTOR_Step_Init(void){
#if 0
RCC->APB2ENR |= 1 << 3; //ʹÄÜʱÖÓ
GPIOA->CRL &= 0xFF000000;
GPIOA->CRL |= 0x00333333;
#endif
#if 0
//RCC->APB2ENR |= 1 << 3;
GPIOB->CRL &= 0x00000FFF;
GPIOB->CRH &= 0xFFFFFFF0;
GPIOB->CRL |= 0x33333000;
GPIOB->CRH |= 0x00000003;
//MS1 = 0;
//MS2 = 0;
//MS3 = 0;
#endif
}
void MOTOR_Step_Enable(void){
ENABLE1_left = 0;
ENABLE2_right = 0;
}
void MOTOR_Step_Micr(u16 x){
switch(x){
case 1:Full_step; break;
case 2:Half_step; break;
case 4:Quarter_step; break;
case 8: Eighth_step; break;
case 16: Sixteenth_step; break;
defalut: break;
}
}
void MOTOR_Step2_Control(int dir,u16 period,u32 steps){
u32 i;
if(dir == 1){
for(i = 0;i < steps;i++){
DIR_right = 1;
STEP_right = 0;
//delay_ms(800);
delay_ms(1);
STEP_right = 1;
//delay_ms(800);
delay_us(period);
}
}
if(dir == 0){
for(i = 0;i < steps;i++){
DIR_right = 0;
STEP_right = 0;
//delay_ms(800);
delay_ms(1);
STEP_right = 1;
//delay_ms(800);
delay_us(period);
}
}
}
void MOTOR_Step2_Enable(void){
ENABLE0 = 0;
}
void MOTOR_Step2_Micr(u16 x){
switch(x){
case 1:Full_step0; break;
case 2:Half_step0; break;
case 4:Quarter_step0; break;
case 8: Eighth_step0; break;
case 16: Sixteenth_step0; break;
defalut: break;
}
}
void MOTOR_Step3_Control(int dir,u16 period,u32 steps){
u32 i;
if(dir == 1){
for(i = 0;i < steps;i++){
DIR = 1;
STEP = 0;
//delay_ms(800);
delay_ms(1);
STEP = 1;
//delay_ms(800);
delay_us(period);
}
}
if(dir == 0){
for(i = 0;i < steps;i++){
DIR = 0;
STEP = 0;
//delay_ms(800);
delay_ms(1);
STEP = 1;
//delay_ms(800);
delay_us(period);
}
}
}
void MOTOR_Step3_Enable(void){
ENABLE0 = 0;
}
void MOTOR_Step3_Micr(u16 x){
switch(x){
case 1:Full_step0; break;
case 2:Half_step0; break;
case 4:Quarter_step0; break;
case 8: Eighth_step0; break;
case 16: Sixteenth_step0; break;
defalut: break;
}
}
下面是/*blue.h*/
#ifndef __BLUE_H__
#define __BLUE_H __
#include "sys.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "usart.h"
#include <stdio.h>
#include <string.h>
void BLUE_Init(void);
//void RCC_Configuration(void);
//void GPIO_Configuration(void);
//void USART_Configuration(void);
//void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);
//void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str);
//int Putchar(int c);
void USART1_IRQHandler(void);
extern void data_IRQHandller1(void);
#endif
下面是/*blue.c*/
#include "blue.h"
//#include "usart.h"
#include "delay.h"
void BLUE_Init(void){
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);//?
GPIO_InitStrue.GPIO_Mode= GPIO_Mode_IN_FLOATING; //GPIO_Mode_IPU;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
USART_InitStrue.USART_BaudRate=9600;
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStrue.USART_Parity=USART_Parity_No;
USART_InitStrue.USART_StopBits=USART_StopBits_1;
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStrue);//?
USART_Cmd(USART1,ENABLE);
#if 1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStrue);
#endif
}
#if 0
void data_IRQHandller1(void){
u8 a = 0;
u8 Res;
int len;
BUF4[1] = '\0';
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
Res= USART_ReceiveData(USART1);
printf("%c",Res);
BUF2[u++] = (Res & (uint16_t)0x01FF);
//USART1->DR = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[u-1] ;
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
if(BUF2[0] == 0x61 && BUF2[1] == 0x3d){
printf("ok");
}
//USART1->DR = (0x2a & (uint16_t)0x01FF);
//while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
}
}
#endif
#if 0
void USART1_IRQHandler(void)
{ u8 a = 0;
u8 Res;
//BUF4[1] = '\0';
//printf("%s\r\n","Im fun");
data_IRQHandller1();
//printf("%s\r\n","Im fun");
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)
{
Res= USART_ReceiveData(USART1);
//printf("%c",(Res & (uint16_t)0x01FF));
//USART1->DR = (Res & (uint16_t)0x01FF);
BUF2[a] = (Res & (uint16_t)0x01FF);
//strcat(str,BUF2);
USART_SendData(USART1,BUF2[a]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
a++;
//BUF4[0] = 0;
BUF2[a] = '\0';
USART_SendData(USART1,BUF2[5]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
//printf("str=%s,strlen(str)=%d",str,strlen(str));
#endif
//USART_SendData(USART1,Res);
//while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
//printf("jieshoudaole");
#if 0
if((USART_RX_STA & 0x8000)== 0){
if(USART_RX_STA&0x4000){
if(Res!=0x0a)
USART_RX_STA=0;
else {
USART_RX_STA|=0x8000;
//printf("jieshoudaole");
//USART_SendData(USART1,Res);
}
//USART_SendData(USART1,Res);
}
else{
if(Res==0x0d)
USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res;
if((USART_RX_BUF[USART_RX_STA&0X3FFF] == 0x6f)){
BUF2[0] = 0x6f;
#if 0
while(BUF[a] != 0){
USART_SendData(USART1,BUF[a]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
a++;
}
#endif
}
if((USART_RX_BUF[(USART_RX_STA)&0X3FFF] == 0x6b )){
BUF2[1] = 0x6b;
}
if(USART_RX_BUF[(USART_RX_STA)&0X3FFF] != 0){
BUF4[0] = (char)USART_RX_BUF[(USART_RX_STA)&0X3FFF];
strcat(str,BUF4);
USART_SendData(USART1,BUF4[0]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
printf("\r\n");
BUF4[0] = 0;
printf("str=%s,strlen(str)=%d\r\n",str,strlen(str));
}
if(BUF2[0] == 0x6f && BUF2[1] == 0x6b){
while(BUF[a] != 0){
USART_SendData(USART1,BUF[a]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
a++;
BUF2[0] = 0;
BUF2[1] = 0;
}
if(strlen(str) >= 5){
printf("oknihao");
printf(str);
sscanf(str,"%s:%d,%s:%d %s:%d,%s:%d %s:%d,%s:%d",s1,a,s2,n,s3,a2,s4,n2,s5,a3,s6,n3);
printf(s1);
}
//USART_RX_STA++;
}
//USART_SendData(USART1,USART_RX_BUF[USART_RX_STA&0X3FFF] ); //send ok
USART_RX_STA++;
a++;
if(USART_RX_STA>(USART_REC_LEN-1))
USART_RX_STA=0;
}
//USART_SendData(USART1,Res); //send ok
}
}
//USART_SendData(USART1,Res);
}
#endif
}
#endif
int Putchar(int c)
{
if (c == '\n'){putchar('\r');}
USART_SendData(USART1,c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return c;
}
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}
}
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)
{ //str--;
while (0 != *str)
{
UART_PutChar(USARTx, *str++);
//str++;
}
}
下面是/*main.c*/
#include "stm32f10x.h"
#include "motor.h"
#include "key.h"
#include "sys.h"
#include "delay.h"
#include <stdio.h>
//#include "usart.h"
#include "blue.h"
#include <string.h>
int USART1_RX_STA=0;
#define USART1_MAX_RECV_LEN 7
char BUF2[8] ;
char BUF3[5] = {0x0d,0x59,0x65,0x73};
#define USART_REC_LEN 200
u8 USART_RX_BUF[USART_REC_LEN];
char BUF[5] = {0x59,0x65,0x73};
char BUF4[2];
char *str = BUF2;
u16 USART_RX_STA=0;
int u =0;
int a,a2,a3;
int n,n2,n3;
char *s1 = NULL,*s2 = NULL,*s3 = NULL;
char *s4 = NULL,*s5 = NULL,*s6 = NULL;
int IRQ_RX = 0;//·½Ïò±ê־λ
int IRQ_RX_a = 0,IRQ_RX_b = 0,IRQ_RX_c = 0; //Èý¸ö·½Ïò±ê־λ
char step_a = 0,step_b = 0,step_c = 0;
char step_a1 = 0x30,step_a2 = 0x30,step_b1 = 0x30, step_b2 = 0x30,step_c1 = 0x30,step_c2 = 0x30;
int fputc(int ch,FILE *p) //
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
int chartoint10(char a){
int w;
switch(a){
// 0-9
case 0x30: w = 0;break;
case 0x31: w = 1;break;
case 0x32: w = 2;break;
case 0x33: w = 3;break;
case 0x34: w = 4;break;
case 0x35: w = 5;break;
case 0x36: w = 6;break;
case 0x37: w = 7;break;
case 0x38: w = 8;break;
case 0x39: w = 9;break;
}
return w;
}
int chartoint_two(char a,char b){
int w,v;//Ê®½øÖÆ
int sum;
switch(a){
// 0-9
case 0x30: w = 0;break;
case 0x31: w = 1;break;
case 0x32: w = 2;break;
case 0x33: w = 3;break;
case 0x34: w = 4;break;
case 0x35: w = 5;break;
case 0x36: w = 6;break;
case 0x37: w = 7;break;
case 0x38: w = 8;break;
case 0x39: w = 9;break;
}
w = w * 10;
switch(b){
// 0-9
case 0x30: v = 0;break;
case 0x31: v = 1;break;
case 0x32: v = 2;break;
case 0x33: v = 3;break;
case 0x34: v = 4;break;
case 0x35: v = 5;break;
case 0x36: v = 6;break;
case 0x37: v = 7;break;
case 0x38: v = 8;break;
case 0x39: v = 9;break;
}
sum = w + v;
printf("%d\r\n",sum);
return sum ;
}
void USART1_IRQHandler(void)
{
u8 a = 0;
u8 Res;
int len;
//BUF4[1] = '\0';
//&& BUF2[7] ==0x0D && BUF2[8] == 0x0A
#if 1
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
// if(USART1_RX_STA< USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
if( BUF2[0] == 0x59 && (BUF2[1] == 0x61 || BUF2[1] == 0x62 || BUF2[1] == 0x30 )&& (BUF2[2] >= 0x30 && BUF2[2] <= 0x39) \
&& (BUF2[3] >= 0x30 && BUF2[3] <= 0x39) && (BUF2[4] == 0x61 || BUF2[4] == 0x62 || BUF2[4] == 0x30) && (BUF2[5] >= 0x30 && BUF2[5] <= 0x39) \
&& (BUF2[6] >= 0x30 && BUF2[6] <= 0x39) && (BUF2[7] == 0x61 || BUF2[7] == 0x62 || BUF2[7] == 0x30)&& (BUF2[8] >= 0x30 && BUF2[8] <= 0x39) \
&& (BUF2[9] >= 0x30 && BUF2[9] <= 0x39) && BUF2[10] == 0x0d && BUF2[11] == 0x0a){
//printf("ok\r\n");
step_a1 = BUF2[2];
step_a2 = BUF2[3];
step_b1 = BUF2[5];
step_b2 = BUF2[6];
step_c1 = BUF2[8];
step_c2 = BUF2[9];
if(BUF2[1] == 0x61){
IRQ_RX_a = 1;
}
else if( BUF2[1] == 0x62 ){
IRQ_RX_a = 2;
}
else if(BUF2[1] == 0x30){
IRQ_RX_a = 3;
}
if(BUF2[4] == 0x61){
IRQ_RX_b = 1;
}
else if(BUF2[4] == 0x62){
IRQ_RX_b = 2;
}
else if(BUF2[4] == 0x30){
IRQ_RX_b = 3;
}
if(BUF2[7] == 0x61){
IRQ_RX_c =1;
}
else if(BUF2[7] == 0x62){
IRQ_RX_c = 2;
}
else if(BUF2[7] == 0x30){
IRQ_RX_c = 3;
}
#if 0
if(IRQ_RX_a == 0 && IRQ_RX_b == 0 && IRQ_RX_c == 0){
USART1_RX_STA = 0;
printf("test2\r\n");
}
#endif
//printf("test1\r\n");
//printf("......");
//memset(BUF2,0,sizeof(BUF2));
}
// }
// else{
//USART1_RX_STA|=1<<15;
// USART1_RX_STA = 0;
// }
}
#endif
//test6 OK
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
// if(USART1_RX_STA< USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
if( BUF2[0] == 0x59 && (BUF2[1] == 0x61 || BUF2[1] == 0x62 )&& (BUF2[2] >= 0x30 && BUF2[2] <= 0x39) \
&& (BUF2[3] == 0x61 || BUF2[3] == 0x62 ) && (BUF2[4] >= 0x30 && BUF2[4] <= 0x39) \
&& (BUF2[5] == 0x61 || BUF2[5] == 0x62 )&& (BUF2[6] >= 0x30 && BUF2[6] <= 0x39) \
){
step_a = BUF2[2];
step_b = BUF2[4];
step_c = BUF2[6];
if(BUF2[1] == 0x61){
IRQ_RX_a = 1;
}
else if( BUF2[1] == 0x62 ){
IRQ_RX_a = 2;
}
if(BUF2[3] == 0x61){
IRQ_RX_b = 1;
}
else if(BUF2[3] == 0x62){
IRQ_RX_b = 2;
}
if(BUF2[5] == 0x61){
IRQ_RX_c =1;
}
else if(BUF2[5] == 0x62){
IRQ_RX_c = 2;
}
//memset(BUF2,0,sizeof(BUF2));
}
// }
// else{
//USART1_RX_STA|=1<<15;
// USART1_RX_STA = 0;
// }
}
#endif
#if 0
//test4
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
if(USART1_RX_STA< USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
//USART1->DR = (Res & (uint16_t)0x01FF);
// 0x2c , | 0x61 a | 0x62 b | 0x63 c | 0x31 1 | 0x39 9 | 0x3d = | 0x6d m } 0x6e n | 0x6f o |
if(BUF2[0] == 0x61 && BUF2[1] == 0x3D && BUF2[USART1_RX_STA - 2] ==0x0D && BUF2[USART1_RX_STA - 1] == 0x0A){
step_a = BUF2[6];
step_b = BUF2[14];
step_c = BUF2[22];
if(BUF2[2] == 0x31){
IRQ_RX_a = 1;
}
else if( BUF2[2] == 0x32 ){
IRQ_RX_a = 2;
}
if(BUF2[10] == 0x31){
IRQ_RX_b = 1;
}
else if(BUF2[10] == 0x32){
IRQ_RX_b = 2;
}
if(BUF2[18] == 0x31){
IRQ_RX_c =1;
}
else if(BUF2[18] == 0x32){
IRQ_RX_c = 2;
}
//memset(BUF2,0,sizeof(BUF2));
}
}
else{
//USART1_RX_STA|=1<<15;
USART1_RX_STA = 0;
USART_ClearFlag(USART1, USART_FLAG_TC);
}
}
#endif
//test3
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
if(USART1_RX_STA< USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
//USART1->DR = (Res & (uint16_t)0x01FF);
// 0x2c , | 0x61 a | 0x62 b | 0x63 c | 0x31 1 | 0x39 9 | 0x3d = | 0x6d m } 0x6e n | 0x6f o |
if(BUF2[0] == 0x61 && BUF2[1] == 0x3D && (BUF2[2] == 0x31 || BUF2[2] == 0x32 ) \
&& BUF2[3] ==0x2C && BUF2[4] == 0x6D \
&& BUF2[5] == 0x3D && (BUF2[6] >= 0x30 && BUF2[6] <= 0x39) && BUF2[7] == 0x2C \
&& BUF2[8] == 0x62 && BUF2[9] == 0x3D && (BUF2[10] == 0x31 || BUF2[10] == 0x32) && BUF2[11] == 0x2C \
&& BUF2[12] == 0x6E && BUF2[13] == 0x3D && (BUF2[14] >= 0x30 && BUF2[14] <= 0x39) \
&& BUF2[15] == 0x2C && BUF2[16] == 0x63 && BUF2[17] == 0x3D && (BUF2[18] == 0x31 || BUF2[18] == 0x32) \
&& BUF2[19] == 0x2C && BUF2[20] == 0x6F && BUF2[21] == 0x3D && (BUF2[22] >= 0x30 && BUF2[22] <= 0x39) \
&& BUF2[23] ==0x0D && BUF2[24] == 0x0A){
step_a = BUF2[6];
step_b = BUF2[14];
step_c = BUF2[22];
if(BUF2[2] == 0x31){
IRQ_RX_a = 1;
}
else if( BUF2[2] == 0x32 ){
IRQ_RX_a = 2;
}
if(BUF2[10] == 0x31){
IRQ_RX_b = 1;
}
else if(BUF2[10] == 0x32){
IRQ_RX_b = 2;
}
if(BUF2[18] == 0x31){
IRQ_RX_c =1;
}
else if(BUF2[18] == 0x32){
IRQ_RX_c = 2;
}
//memset(BUF2,0,sizeof(BUF2));
}
}
else{
//USART1_RX_STA|=1<<15;
USART1_RX_STA = 0;
}
}
#endif
//test2 word OK ------------------------------------
//#define USART1_MAX_RECV_LEN 24
//char BUF2[23];
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
if(USART1_RX_STA<USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
//USART1->DR = (Res & (uint16_t)0x01FF);
if(BUF2[0] == 0x61 && BUF2[1] == 0x3d && (BUF2[2] == 0x31 || BUF2[2] == 0x32 \
&& (BUF2[3] >= 0x30 || BUF2[3] <= 0x39) && BUF2[4] == 0x2c && BUF2[5] == 0x6d \
&& BUF2[6] == 0x3d && (BUF2[7] >= 0x30 || BUF2[7] <= 0x39) && BUF2[8] == 0x2c \
&& BUF2[9] == 0x62 && BUF2[10] == 0x3d && (BUF2[11] >= 0x30 || BUF2[11] <= 0x39) && BUF2[12] == 0x2c \
&& BUF2[13] == 0x6e && BUF2[14] == 0x3d && (BUF2[15] >= 0x30 || BUF2[15] <= 0x39) \
&& BUF2[16] == 0x2c && BUF2[17] == 0x63 && BUF2[18] == 0x3d && (BUF2[19] >= 0x30 || BUF2[19] <= 0x39)\
&& BUF2[20] == 0x2c && BUF2[21] == 0x6f && BUF2[22] == 0x3d && (BUF2[23] >= 0x30 || BUF2[23] <= 0x39) )){
//printf("ok");
//MOTOR_Step3_Micr(1);
//MOTOR_Step3_Control(BUF2[2],1600,BUF2[3] );
//MOTOR_Step3_Control(1,1600,125 );
//MOTOR_Step3_Enable();
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
if(BUF2[2] == 0x31){
IRQ_RX = 1;
}
//memset(BUF2,0,u);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//MOTOR_Step3_Control(0,1600,125);
//if(u > 4){
//memset(BUF2,0,sizeof(BUF2));
//u = 0;
// }
if( BUF2[2] == 0x32 ){
//MOTOR_Step3_Micr(1);
//USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
IRQ_RX = 2;
//MOTOR_Step3_Control(2,1600,125 );
//MOTOR_Step3_Enable();
}
}
//USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
else{
//USART1_RX_STA|=1<<15;
USART1_RX_STA = 0;
}
}
#endif
#if 0
if((BUF2[2] == 0x31 || BUF2[2] == 0x32) && (BUF2[3] >= 0x30 && BUF2[3] <= 0x39)){
//MOTOR_Step3_Micr(1);
//MOTOR_Step3_Control(BUF2[2],1600,BUF2[3] );
memset(BUF2,0,u);
//MOTOR_Step3_Control(0,1600,chartoint10(BUF2[3]) * One_1);
//MOTOR_Step3_Control(1,1600,625);
//MOTOR_Step3_Enable();
//BUF2[3] =0;
//BUF2[2] = 0;
//BUF2[0] = 0;
//BUF2[1] = 0;
//u = 0;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
#endif
//test1 word OK ------------------------------------
//#define USART1_MAX_RECV_LEN 4
//char BUF2[5];
#if 0
if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET){
Res= USART_ReceiveData(USART1);
//printf("%c",Res);
if(USART1_RX_STA<USART1_MAX_RECV_LEN){
BUF2[USART1_RX_STA++] = (Res & (uint16_t)0x01FF);
USART1->DR = (uint16_t)BUF2[USART1_RX_STA-1];
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
if(BUF2[0] == 0x61 && BUF2[1] == 0x3d && (BUF2[2] == 0x31 || BUF2[2] == 0x32 )){
if(BUF2[2] == 0x31){
IRQ_RX = 1;
}
if( BUF2[2] == 0x32 ){
IRQ_RX = 2;
}
}
}
else{
USART1_RX_STA = 0;
}
}
#endif
//USART1->DR = (0x2a & (uint16_t)0x01FF);
//while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
}
int main(void){
//int ret=0;
// u16 led0pwmval=0;
//u8 dir=1;
int i = 0;
u8 Res;
u8 temp;
MOTOR_Step_Init();
MOTOR_Init();
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
BLUE_Init();
//uart_init(115200);
//DIR = TRUE;
//½ÓÏß×óµç»ú
//printf("\r\nhello");
#if 0
for(i = 0;i < 4;i++){
USART_SendData(USART1,BUF3[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
//BUF2[0] = 0;
//BUF2[1] = 0;
}
#endif
/*
for(i = 0;i < 1 ;i++){
MOTOR_Step_Micr(1);
MOTOR_Step3_Micr(1);
//delay_ms(1000);
MOTOR_Step_Control(1,1600,625);
MOTOR_Step3_Control(1,1600,1250);
MOTOR_Step3_Control(0,1600,1250);
MOTOR_Step3_Enable();
delay_ms(1000);
MOTOR_Step_Control(0,1600,625);
MOTOR_Step_Control(1,1600,625);
MOTOR_Step_Enable();
}
*/
while(1){
//printf("**************************\n");
//MOTOR_Step_Control(FALSE,3600,400);
//MOTOR_Step_Control(0,1600,200);
//delay_ms(1000);
//delay_ms(1000);
//test4
#if 1
if(IRQ_RX_a == 1){
MOTOR_Step_Micr(1);
//MOTOR_Step_Control(1,1600,150);
MOTOR_Step_Control(0,1600,chartoint_two(step_a1,step_a2) * One_1);
MOTOR_Step_Enable();
IRQ_RX_a = 0;
step_a1 = 0x30;
step_a2 = 0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_a == 2){
MOTOR_Step_Micr(1);
//MOTOR_Step_Control(0,1600,150);
MOTOR_Step_Control(1,1600,chartoint_two(step_a1,step_a2) * One_1 );
MOTOR_Step_Enable();
IRQ_RX_a= 0;
step_a1 = 0x30;
step_a2 = 0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_a == 3){
USART1_RX_STA = 0;
}
if(IRQ_RX_b == 1){
MOTOR_Step2_Micr(1);
//MOTOR_Step2_Control(1,1600,150);
MOTOR_Step2_Control(0,1600,chartoint_two(step_b1,step_b2) * One_1);
MOTOR_Step2_Enable();
IRQ_RX_b = 0;
step_b1 = 0x30;
step_b2 = 0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_b == 2){
MOTOR_Step2_Micr(1);
// MOTOR_Step2_Control(0,1600,150);
MOTOR_Step2_Control(1,1600,chartoint_two(step_b1,step_b2) * One_1 );
MOTOR_Step2_Enable();
IRQ_RX_b= 0;
step_b1 = 0x30;
step_b2 = 0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_b == 3){
USART1_RX_STA = 0;
}
if(IRQ_RX_c == 1){
MOTOR_Step3_Micr(1);
//MOTOR_Step3_Control(1,1600,150);
MOTOR_Step3_Control(0,1600,chartoint_two(step_c1,step_c2) * One_1);
MOTOR_Step3_Enable();
IRQ_RX_c = 0;
step_c1 =0x30;
step_c2 =0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_c == 2){
MOTOR_Step3_Micr(1);
// MOTOR_Step3_Control(0,1600,150);
MOTOR_Step3_Control(1,1600,chartoint_two(step_c1,step_c2) * One_1 );
MOTOR_Step3_Enable();
IRQ_RX_c= 0;
step_c1 = 0x30;
step_c2 = 0x30;
USART1_RX_STA = 0;
}
else if(IRQ_RX_c == 3){
USART1_RX_STA = 0;
}
#endif
//test3 ok
#if 0
if(IRQ_RX_a == 1){
MOTOR_Step_Micr(1);
//MOTOR_Step_Control(1,1600,150);
MOTOR_Step_Control(1,1600,chartoint10(step_a) * One_1);
MOTOR_Step_Enable();
IRQ_RX_a = 0;
step_a = 0;
USART1_RX_STA = 0;
}
else if(IRQ_RX_a == 2){
MOTOR_Step_Micr(1);
//MOTOR_Step_Control(0,1600,150);
MOTOR_Step_Control(0,1600,chartoint10(step_a) * One_1 );
MOTOR_Step_Enable();
IRQ_RX_a= 0;
step_a = 0;
USART1_RX_STA = 0;
}
if(IRQ_RX_b == 1){
MOTOR_Step2_Micr(1);
//MOTOR_Step2_Control(1,1600,150);
MOTOR_Step2_Control(1,1600,chartoint10(step_b) * One_1);
MOTOR_Step2_Enable();
IRQ_RX_b = 0;
step_b = 0;
USART1_RX_STA = 0;
}
else if(IRQ_RX_b == 2){
MOTOR_Step2_Micr(1);
// MOTOR_Step2_Control(0,1600,150);
MOTOR_Step2_Control(0,1600,chartoint10(step_b) * One_1 );
MOTOR_Step2_Enable();
IRQ_RX_b= 0;
step_b = 0;
USART1_RX_STA = 0;
}
if(IRQ_RX_c == 1){
MOTOR_Step3_Micr(1);
//MOTOR_Step3_Control(1,1600,150);
MOTOR_Step3_Control(1,1600,chartoint10(step_c) * One_1);
MOTOR_Step3_Enable();
IRQ_RX_c = 0;
step_c = 0;
USART1_RX_STA = 0;
}
else if(IRQ_RX_c == 2){
MOTOR_Step3_Micr(1);
// MOTOR_Step3_Control(0,1600,150);
MOTOR_Step3_Control(0,1600,chartoint10(step_c) * One_1 );
MOTOR_Step3_Enable();
IRQ_RX_c= 0;
step_c = 0;
USART1_RX_STA = 0;
}
#endif
//test1 and 2 word OK ------------------------------------------------
#if 0
if(IRQ_RX == 1){
MOTOR_Step3_Micr(1);
MOTOR_Step3_Control(1,1600,150);
//MOTOR_Step3_Control(1,1600,chartoint10(BUF2[3]) * One_1);
MOTOR_Step3_Enable();
IRQ_RX = 0;
}
else if(IRQ_RX == 2){
MOTOR_Step3_Micr(1);
MOTOR_Step3_Control(0,1600,150);
//MOTOR_Step3_Control(0,1600,chartoint10(BUF2[3]) * One_1 );
MOTOR_Step3_Enable();
IRQ_RX = 0;
}
#endif
}
while(1);
}
完结~,谢谢