#include "main.h"
#include <stdio.h>
extern u8 borad;
extern u8 TimFlag;
extern u8 speed; //µç»úËÙ¶È
extern int16_t zero_start_up;//ʹÄÜ
extern int16_t zero_stop; //²»Ê¹ÄÜ
extern int16_t one_start;//µÚÒ»¸ö¼Ä´æÆ÷дÊý¾Ý
extern int16_t two_start;//µÚ¶þ¸ö´æÆ÷дÊý¾Ý
extern uint16_t data1;
extern u32 num1;
extern u32 num2;
extern u32 num3;
void init_soft_spiA(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);
//ÊäÈë PA7 SPIÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Êä³ö PA5ʱÖÓ PA6 SPIÊä³ö PA0 DRV8711¸´Î»Òý½Å¸´Î»Òý½Å PA1 DRV8711ʹÄÜÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_1 | GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB0Ƭѡ PB1 DIR·½Ïò¿ØÖÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//PE7 SETP ²½Öè
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//PE8ÊäÈë Ä£Äâ·´µç¶¯ÊÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOE, &GPIO_InitStructure);
// SPIA_DIR = 0;//¿ØÖƵç»úÐýת·½Ïò
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //ʱÖÓ
GPIO_ResetBits(GPIOB,GPIO_Pin_0); //Ƭѡ
}
void init_soft_spiB(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE,ENABLE);
//PE4Ƭѡ PE2 DIR·½Ïò¿ØÖÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//PE3 SETP ²½Öè
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
//PC13ÊäÈë Ä£Äâ·´µç¶¯ÊÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// SPIB_DIR = 0;//¿ØÖƵç»úÐýת·½Ïò
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //ʱÖÓ
GPIO_ResetBits(GPIOE,GPIO_Pin_4); //Ƭѡ
}
void init_soft_spiC(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOD,ENABLE);
//PD3Ƭѡ PD1 DIR·½Ïò¿ØÖÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//PD2 SETP ²½Öè
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//PC13ÊäÈë Ä£Äâ·´µç¶¯ÊÆ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// SPIC_DIR = 0;//¿ØÖƵç»úÐýת·½Ïò
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //ʱÖÓ
GPIO_ResetBits(GPIOE,GPIO_Pin_4); //Ƭѡ
}
uint16_t soft_spiA(uint16_t val)
{
uint16_t valx=0;
int x,i;
uint16_t SerialBuffer[2];
SerialBuffer[1]=val & (0x00ff);
SerialBuffer[0]=val & (0xff00);
SerialBuffer[0] = SerialBuffer[0] >> 8;
GPIO_SetBits(GPIOB,GPIO_Pin_0);//Ƭѡ
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//ʱÖÓ
delay_us(1000);
for (x = 0; x < 2; x++)
{
for (i = 7; i >= 0; i--)
{
delay_us(1000);
if (SerialBuffer[x] & (1<<i))
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);// 1
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);// 0
}
delay_us(1000);
valx = valx << 1;
if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) )
{
valx ++;
}
GPIO_SetBits(GPIOA,GPIO_Pin_5); //CLKÀ¸ß
delay_us(1000);
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //CLKÀµÍ
delay_us(1000);
}
}
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
delay_us(1000);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
return valx;
}
uint16_t soft_spiB(uint16_t val)
{
uint16_t valx=0;
int x,i;
uint16_t SerialBuffer[2];
SerialBuffer[1]=val & (0x00ff);
SerialBuffer[0]=val & (0xff00);
SerialBuffer[0] = SerialBuffer[0] >> 8;
GPIO_SetBits(GPIOE,GPIO_Pin_4);//Ƭѡ
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//ʱÖÓ
delay_us(2000);
for (x = 0; x < 2; x++)
{
for (i = 7; i >= 0; i--)
{
delay_us(2000);
if (SerialBuffer[x] & (1<<i))
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);// 1
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);// 0
}
delay_us(2000);
valx = valx << 1;
if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) )
{
valx ++;
}
GPIO_SetBits(GPIOA,GPIO_Pin_5); //CLKÀ¸ß
delay_us(2000);
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //CLKÀµÍ
delay_us(2000);
}
}
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
delay_us(2000);
GPIO_ResetBits(GPIOE,GPIO_Pin_4);
return valx;
}
uint16_t soft_spiC(uint16_t val)
{
uint16_t valx=0;
int x,i;
uint16_t SerialBuffer[2];
SerialBuffer[1]=val & (0x00ff);
SerialBuffer[0]=val & (0xff00);
SerialBuffer[0] = SerialBuffer[0] >> 8;
GPIO_SetBits(GPIOD,GPIO_Pin_3);//Ƭѡ
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//ʱÖÓ
delay_us(1000);
for (x = 0; x < 2; x++)
{
for (i = 7; i >= 0; i--)
{
delay_us(1000);
if (SerialBuffer[x] & (1<<i))
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);// 1
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);// 0
}
delay_us(1000);
valx = valx << 1;
if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) )
{
valx ++;
}
GPIO_SetBits(GPIOA,GPIO_Pin_5); //CLKÀ¸ß
delay_us(1000);
GPIO_ResetBits(GPIOA,GPIO_Pin_5); //CLKÀµÍ
delay_us(1000);
}
}
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
delay_us(1000);
GPIO_ResetBits(GPIOD,GPIO_Pin_3);
return valx;
}
/*DRV8711³õʼ»¯*/
void Spi_DRV8711_Init(void)
{
GPIO_SetBits(GPIOE,GPIO_Pin_7);//STEP
GPIO_SetBits(GPIOE,GPIO_Pin_3);//STEP
GPIO_SetBits(GPIOD,GPIO_Pin_2);//STEP
GPIO_SetBits(GPIOA,GPIO_Pin_0);//Ó²¼þ¸´Î»ËùÓмĴæÆ÷
GPIO_SetBits(GPIOA,GPIO_Pin_1);//SLEEPN ʹÄÜÆ÷¼þ ´¦ÓÚ»½ÐÑģʽ ²»½øÈë˯Ãß
delay_ms(5000); //ÑÓʱ5000MS
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//Ó²¼þ¸´Î»ËùÓмĴæÆ÷
}
void ARM_Init(void)//»úе±Û³õʼ»¯
{
// speed = 50;//ËÙ¶È
while(1)
{
// printf("TimFlag==%d\r\n",TimFlag);
switch(TimFlag)
{
case 1:
SPIA_DIR =1;//·½Ïò
borad=1;
soft_spiB(zero_stop);//Í£Ö¹µÚ¶þ¸öµç»ú
soft_spiC(zero_stop);//Í£Ö¹µÚÈý¸öµç»ú
soft_spiA(one_start);//µÚÒ»¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiA(two_start);//µÚ¶þ¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiA(zero_start_up);//ʹÄܵÚÒ»¸öµç»ú
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
TIM_Cmd(TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷ ¿ªÊ¼²úÉúÂö³å
while(SW1)
{
}
TIM_Cmd(TIM3,DISABLE);//²»¹¤×÷¶¨Ê±Æ÷
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE ); //²»Ê¹ÄÜÖ¸¶¨µÄTIM3ÖжÏ,²»ÔÊÐí¸üÐÂÖÐ¶Ï Í£Ö¹¶¨Ê±Æ÷
printf("µç»úÒ»¹¤×÷Íê³É\r\n");
TimFlag=2;
LED2=0;
break;
case 2:
SPIB_DIR =0;//·½Ïò
borad=2;
soft_spiA(zero_stop);//Í£Ö¹µÚÒ»¸öµç»ú
soft_spiC(zero_stop);//Í£Ö¹µÚÈý¸öµç»ú
soft_spiB(one_start);//µÚÒ»¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiB(two_start);//µÚ¶þ¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiB(zero_start_up);//ʹÄܵڶþ¸öµç»ú
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
TIM_Cmd(TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷ ¿ªÊ¼²úÉúÂö³å
while(SW2)
{
}
TIM_Cmd(TIM3,DISABLE);//²»¹¤×÷¶¨Ê±Æ÷
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE ); //²»Ê¹ÄÜÖ¸¶¨µÄTIM3ÖжÏ,²»ÔÊÐí¸üÐÂÖÐ¶Ï Í£Ö¹¶¨Ê±Æ÷
printf("µç»ú¶þ¹¤×÷Íê³É\r\n");
TimFlag=3;
LED3=0;
break;
case 3:
SPIC_DIR =1;//·½Ïò
borad=3;
soft_spiA(zero_stop);//Í£Ö¹µÚÒ»¸öµç»ú
soft_spiB(zero_stop);//Í£Ö¹µÚ¶þ¸öµç»ú
soft_spiC(one_start);//µÚÒ»¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiC(two_start);//µÚ¶þ¸ö¼Ä´æÆ÷дÊý¾Ý
soft_spiC(zero_start_up);//ʹÄܵÚÈý¸öµç»ú
num1 =0;
num2 =0;
num3 =0;
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM3ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
TIM_Cmd(TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷ ¿ªÊ¼²úÉúÂö³å
while(SW3)
{
}
TIM_Cmd(TIM3,DISABLE);//²»¹¤×÷¶¨Ê±Æ÷
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE ); //²»Ê¹ÄÜÖ¸¶¨µÄTIM3ÖжÏ,²»ÔÊÐí¸üÐÂÖÐ¶Ï Í£Ö¹¶¨Ê±Æ÷
printf("µç»úÈý¹¤×÷Íê³É\r\n");
TimFlag=4;
LED4=0;
break;
case 4:
soft_spiA(zero_stop);//Í£Ö¹µÚÒ»¸öµç»ú
soft_spiB(zero_stop);//Í£Ö¹µÚ¶þ¸öµç»ú
soft_spiC(zero_stop);//Í£Ö¹µÚÈý¸öµç»ú
printf("num1==%d\r\n",num1);
printf("num2==%d\r\n",num2);
printf("num3==%d\r\n",num3);
TimFlag=1;
num1 =0;
num2 =0;
num3 =0;
TIM_Cmd(TIM3,DISABLE);//²»¹¤×÷¶¨Ê±Æ÷
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE );//ʧÄܶ¨Ê±Æ÷
return;//½áÊøº¯Êý
}
}
}