Tải bản đầy đủ
CODE Lập trình điều khiển

CODE Lập trình điều khiển

Tải bản đầy đủ

char dem=0;
void GPIO_config(void);
void EXT_Timer6(void);
void EXT_Timer7(void);
void delay_ms(u32 nTime);
void delay(u32 nTime);
void PWM_Config(void);
void run_DC0(long int toc_do,char chieu);
void run_DC1(long int toc_do,char chieu);
void run_DC2(long int toc_do,char chieu);
void run_DC3(long int toc_do,char chieu);
/* Private macro -------------------------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main(void)
{
PeriphClk_config();
GPIO_config();
Timer3_config();
SPI_config();
EXT_Timer6();
PWM_Config();
GPIO_SetBits(GPIOB,GPIO_Pin_13);//Tat he thong ong khi khoi dong
GPIO_SetBits(GPIOB,GPIO_Pin_12);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
GPIO_SetBits(GPIOB,GPIO_Pin_15);
GPIO_SetBits(GPIOD,GPIO_Pin_8);
71

GPIO_SetBits(GPIOD,GPIO_Pin_9);
while (1)
{
Test_PS2();
}
}
//PA5 - SPI1_SCLK clk
//PA4 - SPI1_NSS
//PA6 - SPI1_MISO data
//PA7 - SPI1_MOSI command
//PA3 - CS (GPIO) att
void PS2_send(unsigned char commands[], unsigned char data[], int length)
{
int i = 0;
GPIO_ResetBits(GPIOA, GPIO_Pin_3); //set CS pin low to select PS2
delay_us(50); //15us delay before sending commands
for(i = 0; i < length; i = i + 1)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET){}
delay_us(70);
SPI_I2S_SendData(SPI1, commands[i]);
delay_us(70);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET){}
delay_us(70);
data[i] = SPI_I2S_ReceiveData(SPI1);
delay_us(70); //15us delay before sending next command
}
GPIO_SetBits(GPIOA, GPIO_Pin_3); //set CS pin high to deselect PS2
Select = Data[3]&0x01;
L = (Data[3]&0x02)/2;
R = (Data[3]&0x04)/4;
Start = (Data[3]&0x08)/8;
72

Up = (Data[3]&0x10)/16;
Right = (Data[3]&0x20)/32;
Down = (Data[3]&0x40)/64;
Left = (Data[3]&0x80)/128;
L2 = (Data[4]&0x01);
R2 = (Data[4]&0x02)/2;
L1 = (Data[4]&0x04)/4;
R1 = (Data[4]&0x08)/8;
Tamgiac = (Data[4]&0x10)/16;
Tron = (Data[4]&0x20)/32;
X = (Data[4]&0x40)/64;
Vuong = (Data[4]&0x80)/128;
}
void delay_us(int delay_time)
{
//can delay to a max of 60000us or 60ms
int time = TIM3->CNT; //get initial time
while ((TIM3->CNT - time) < delay_time);
}
void PeriphClk_config(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOC|
RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOB, ENABLE);
//APB1 clock runs at SystemCoreClock/APB1_Prescaler = 64/2 = 32MHz
//Max speed of APB1 clock is 42Mhz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//APB2 clock runs at SystemCoreClock/8 = 8MHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
void Timer3_config(void)
{
73

//Tim3Clk = 2* PCLK1
//PCLK1 = (HCLK)/(APB1 Prescaler) = SystemCoreClock/2
//Get the APB1 Prescaler from system_stm32f4xx.c
//Tim3Clk = SystemCoreCLock
//CONFIGURE TIMER3 CHANNEL 3 as timer only
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock) / 1000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 60000; //final frequency = clock frequency/
(prescalervalue * tim_period)
//count from 0-60000 at 1Mhz
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE); //enable timer TIM3
}
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOA|
RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOB, ENABLE);
//DIR for DC motor
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3|
GPIO_Pin_4 |GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14| GPIO_Pin_13 ;
74

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//=========================Xilanh=============================//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14|
GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11
;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//=======================Read
Sensor===================================//
//Configure pins associated with SPI for PSII
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure); //configure CS pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
75

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //sclk
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //MOSI
GPIO_SetBits(GPIOA, GPIO_Pin_3); //set CS high to deselect PS2
}
void SPI_config(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_I2S_DeInit(SPI1);
// if you want to incease lengh of wire you must incease Prescaler in SPI
SPI_InitStructure.SPI_BaudRatePrescaler =
SPI_BaudRatePrescaler_128; //8MHz/64 = 125000kHz
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //sample on second edge (falling)
of clock
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //clock high when idle
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //send 8 bits at a time
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //full duplex
communication
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //least significant bit first
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void Test_PS2(void)
{
PS2_send(PS2_InitialPoll, Data,5);
76

Data[3] = ~Data[3];
//

if(Select==0)

//

{}

//

else if(L==0)

//
//

{}
else if(R==0)

//
//

{}
else if(Start==0)

//
//

{}
else
if(Up==0)
{
run_DC0(0,lui);
run_DC1(0,lui);
run_DC2(0,tien);
run_DC3(0,tien);
}
else if(Right==0)
{
run_DC0(0,lui);
run_DC1(0,tien);
run_DC2(0,tien);
run_DC3(0,lui);
}
else if(Down==0)
{
run_DC0(0,tien);
run_DC1(0,tien);
run_DC2(0,lui);
run_DC3(0,lui);
}
else if(Left==0)
77

{
run_DC0(0,tien);//
run_DC1(0,lui);//
run_DC2(0,lui);
run_DC3(0,tien);
}
else
if((Up==0)&&(Right==0))
{ run_DC0(0,lui);
run_DC1(8128,lui);
run_DC2(0,tien);
run_DC3(8128,lui);
}
else if((Up==0)&&(Left==0))
{
run_DC0(8128,lui);
run_DC1(0,lui);
run_DC2(8128,lui);
run_DC3(0,tien);
}
else if((Down==0)&&(Right==0))
{
run_DC0(8128,lui);
run_DC1(0,tien);
run_DC2(8128,lui);
run_DC3(0,lui);
}
else if((Down==0)&&(Left==0))
{
run_DC0(0,tien);
run_DC1(8128,lui);
run_DC2(0,lui);
78

run_DC3(8128,lui);
}
else if((Left!=0)&&(Down!=0)&&(Up!=0)&&(Right!
=0)&&(L1!=0)&&(R1!=0))
{
run_DC0(8128,lui);
run_DC1(8128,lui);
run_DC2(8128,lui);
run_DC3(8128,lui);
}
Data[4] = ~Data[4];
if(L2==0)
{
}
else if(R2==0)
{
delay_ms(300);
if(R2==0)
{
dem++;
if(dem==1)
GPIO_ResetBits(GPIOD,GPIO_Pin_8);
else if(dem==2)
{GPIO_SetBits(GPIOD,GPIO_Pin_8);dem=0;}
}
}
else
if(L1==0)
{
run_DC0(7700,tien);
run_DC1(7700,tien);
79

run_DC2(7700,tien);
run_DC3(7700,tien);
}
else if(R1==0)
{
run_DC0(7700,lui);
run_DC1(7700,lui);
run_DC2(7700,lui);
run_DC3(7700,lui);
}
else if(Tamgiac==0)
{
GPIO_ResetBits(GPIOD,GPIO_Pin_8);
delay_ms(267);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
delay_ms(630);
GPIO_SetBits(GPIOB,GPIO_Pin_15);
GPIO_SetBits(GPIOD,GPIO_Pin_8);
}
else if(Tron==0)
{
delay_ms(300);
if(Tron==0)
{
dem++;
if(dem==1)
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
else if(dem==2)
{GPIO_SetBits(GPIOB,GPIO_Pin_13);dem=0;}
}
}
else if(X==0)
80

{
GPIO_ResetBits(GPIOB,GPIO_Pin_13);//Tha
cau bong
delay_ms(410);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);//Dap
cau bong
delay_ms(930);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
GPIO_SetBits(GPIOB,GPIO_Pin_13);
}
else if(Vuong==0)
{
run_DC0(8128,lui);
run_DC1(8128,lui);
run_DC2(8128,lui);
run_DC3(8128,lui);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
delay_ms(630);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
else if((Left!=0)&&(Down!=0)&&(Up!=0)&&(Right!=0)&&(L1!=0)&&(R1!=0))
{
run_DC0(8128,lui);
run_DC1(8128,lui);
run_DC2(8128,lui);
run_DC3(8128,lui);
}
delay_us(300); //0.3ms delay
}
/*void test_analog(unsigned char commands[], unsigned char data[], int length)
{
81