融创嵌入式技术应用竞赛平台 智能小车底层源代码分析
2018-03-06 14:57:02 0 举报/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
/* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif /* STM32F10X_CL */
/* Reset HSEON, CSSON and PLLON bits */
RCC->CR &= (uint32_t)0xFEF6FFFF;
/* Reset HSEBYP bit */
RCC->CR &= (uint32_t)0xFFFBFFFF;
/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
RCC->CFGR &= (uint32_t)0xFF80FFFF;
#ifdef STM32F10X_CL
/* Reset PLL2ON and PLL3ON bits */
RCC->CR &= (uint32_t)0xEBFFFFFF;
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x00FF0000;
/* Reset CFGR2 register */
RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x009F0000;
/* Reset CFGR2 register */
RCC->CFGR2 = 0x00000000;
#else
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */
#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
#ifdef DATA_IN_ExtSRAM
SystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
#endif
/* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
/* Configure the Flash Latency cycles and enable prefetch buffer */
SetSysClock();
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif
// NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能
//定时器TIM5初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM5,TIM_IT_Update, DISABLE ); //使能指定的TIM5中断,允许更新中断
TIM_Cmd(TIM5, DISABLE); //使能TIMx
* @brief Returns the frequencies of different on chip clocks.
* @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
* the clocks frequencies.
* @note The result of this function could be not correct when using
* fractional value for HSE crystal.
* @retval None
*/
void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
{
uint32_t tmp = 0, pllmull = 0, pllsource = 0, presc = 0;
#ifdef STM32F10X_CL
uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, pll2mull = 0;
#endif /* STM32F10X_CL */
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
uint32_t prediv1factor = 0;
#endif
/* Get SYSCLK source -------------------------------------------------------*/
tmp = RCC->CFGR & CFGR_SWS_Mask;
switch (tmp)
{
case 0x00: /* HSI used as system clock */
RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
break;
case 0x04: /* HSE used as system clock */
RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
break;
case 0x08: /* PLL used as system clock */
/* Get PLL clock source and multiplication factor ----------------------*/
pllmull = RCC->CFGR & CFGR_PLLMull_Mask;
pllsource = RCC->CFGR & CFGR_PLLSRC_Mask;
#ifndef STM32F10X_CL
pllmull = ( pllmull >> 18) + 2;
if (pllsource == 0x00)
{/* HSI oscillator clock divided by 2 selected as PLL clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
}
else
{
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
/* HSE oscillator clock selected as PREDIV1 clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;
#else
/* HSE selected as PLL clock entry */
if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET)
{/* HSE oscillator clock divided by 2 */
RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE >> 1) * pllmull;
}
else
{
RCC_Clocks->SYSCLK_Frequency = HSE_VALUE * pllmull;
}
#endif
}
#else
pllmull = pllmull >> 18;
if (pllmull != 0x0D)
{
pllmull += 2;
}
else
{ /* PLL multiplication factor = PLL input clock * 6.5 */
pllmull = 13 / 2;
}
if (pllsource == 0x00)
{/* HSI oscillator clock divided by 2 selected as PLL clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSI_VALUE >> 1) * pllmull;
}
else
{/* PREDIV1 selected as PLL clock entry */
/* Get PREDIV1 clock source and division factor */
prediv1source = RCC->CFGR2 & CFGR2_PREDIV1SRC;
prediv1factor = (RCC->CFGR2 & CFGR2_PREDIV1) + 1;
if (prediv1source == 0)
{ /* HSE oscillator clock selected as PREDIV1 clock entry */
RCC_Clocks->SYSCLK_Frequency = (HSE_VALUE / prediv1factor) * pllmull;
}
else
{/* PLL2 clock selected as PREDIV1 clock entry */
/* Get PREDIV2 division factor and PLL2 multiplication factor */
prediv2factor = ((RCC->CFGR2 & CFGR2_PREDIV2) >> 4) + 1;
pll2mull = ((RCC->CFGR2 & CFGR2_PLL2MUL) >> 8 ) + 2;
RCC_Clocks->SYSCLK_Frequency = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull;
}
}
#endif /* STM32F10X_CL */
break;
default:
RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
break;
}
/* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/
/* Get HCLK prescaler */
tmp = RCC->CFGR & CFGR_HPRE_Set_Mask;
tmp = tmp >> 4;
presc = APBAHBPrescTable[tmp];
/* HCLK clock frequency */
RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
/* Get PCLK1 prescaler */
tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask;
tmp = tmp >> 8;
presc = APBAHBPrescTable[tmp];
/* PCLK1 clock frequency */
RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
/* Get PCLK2 prescaler */
tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask;
tmp = tmp >> 11;
presc = APBAHBPrescTable[tmp];
/* PCLK2 clock frequency */
RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
/* Get ADCCLK prescaler */
tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask;
tmp = tmp >> 14;
presc = ADCPrescTable[tmp];
/* ADCCLK clock frequency */
RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc;
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; // CAN 接收中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;//ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(USART1); //复位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口
#if EN_USART1_RX //如果使能了接收
//Usart1 NVIC 配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
#endif
USART_Cmd(USART1, ENABLE); //使能串口
USART_GetFlagStatus(USART1,USART_FLAG_TC);
//USART_ClearFlag(USART1, USART_FLAG_TC);
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2,GPIOD时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOD, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2 , ENABLE);
//USART2_TX PD.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PD5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD5
//USART2_RX PD6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD6
//USART 初始化设置
USART_DeInit(USART2); //复位串口2
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
USART_Cmd(USART2, ENABLE); //使能串口
}
else //开中断 TXD -> PA2 RXD -> PA3
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2,GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
USART_DeInit(USART2); //复位串口2
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口2
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART2, ENABLE); //使能串口
USART_GetFlagStatus(USART2,USART_FLAG_TC);
}
GPIOB->CRH&=0XFFFFFFF0;
GPIOB->CRH|=0X00000008;//PB8 设置成输入
GPIOB->ODR|=1<<8; //PB8上拉
GPIOC->CRH&=0X000FFFFF;
GPIOC->CRH|=0X33300000; //PC13/PC14/PC15推挽输出
GPIOC->ODR|=0XE000; //PC13/PC14/PC15输出高
GPIOD->CRH&=0XFFF0FFFF;
GPIOD->CRH|=0X00030000; //PD12推挽输出
GPIOD->ODR|=0X1000; //PD12推挽高
LED_L=1;
LED_R=1;
BEEP=1;
beep=0;
** 功能: 硬件端口初始化 使能端口时钟
** 参数: 无参数
** 返回值: 无
****************************************************************/
void YJ_INIT(void)
{
RCC->APB2ENR|=1<<2; //使能PORTA时钟
RCC->APB2ENR|=1<<3; //使能PORTB时钟
RCC->APB2ENR|=1<<4; //使能PORTC时钟
RCC->APB2ENR|=1<<5; //使能PORTD时钟
RCC->APB2ENR|=1<<6; //使能PORTE时钟
RCC->APB2ENR|=1<<0; //开启辅助时钟
AFIO->MAPR&=0XF8FFFFFF; //清除MAPR的[26:24]
// AFIO->MAPR|=0X04000000; //关闭JTAG
LED_Init(); //LED硬件初始化
#if 1
DJ_Init(); //电机驱动初始化
PWM_Init(); //PWM初始化
IIC_Init(); //初始化IIC
BH1750_Init(); //初始化光照
KEY_Init(); //按键初始化
// HC595_Init(); //74HC595初始化
XJ_Init(); //循迹初始化
Timer3_Init(10,71); // 1Mhz的计数频率,计数到10为10us
Timer2_Init(1000,71); // 1Mhz的计数频率,计数到500为100us
Timer4_Init(1000,71); // 1Mhz的计数频率,计数到1000为1ms
TIM6_Int_Init(2000,71); // 2ms 检测CAN总线数据
EXTIX_Init(); // 外部中断初始化
HW_Init(); //红外发射初始化
SYN7318_Init(); // 语音识别测试初始化
Electricity_Init();
#endif
}
{
RCC->APB2ENR|=1<<5; //使能PORTD时钟
GPIOD->CRH&=0Xffff0000;//清掉原来的设置,同时不影响其它位设置。
GPIOD->CRH|=0X00003333;//PD8~11推挽输出
GPIOD->ODR &=0xf0ff; //PD8~11输出高
}
RCC->APB2ENR|=1<<6;
GPIOE->CRH&=0XF00F0000;
GPIOE->CRH|=0X03303333;
//PE8/PE9/PE10/PE11/PE13/PE14推挽输出
GPIOE->ODR|=0X0000;
LEFT_NSLEEP = 1;
REFT_NSLEEP = 1;
REFT1_PWM =1;
REFT2_PWM =1;
LEFT1_PWM =0;
LEFT2_PWM =0;
#if 1
// u32 OCLK=72000000;
//此部分需手动修改IO口设置
RCC->APB2ENR|=1<<11; //TIM1时钟使能
RCC->APB2ENR|=1<<6; //使能PORTE时钟
RCC->APB2ENR|=1<<0; //开启辅助时钟
GPIOE->CRH&=0XF00F0F0F; //PE9/11/13/14输出
GPIOE->CRH|=0X0BB0B0B0; //复用功能输出
AFIO->MAPR&=0XFFFFFF3F; //清除MAPR的[7:6]
AFIO->MAPR|=1<<7; //完全重映像,TIM1_CH1->PE9 L1 TIM1_CH2->PE11 L2
AFIO->MAPR|=1<<6; //完全重映像,TIM1_CH3->PE13 R1 TIM1_CH4->PE14 R2
// TIM1->ARR=OCLK/frequency;//设定计数器自动重装值
// TIM1->PSC=psc; //预分频器不分频
TIM1->ARR=100; //设定计数器自动重装值 占空比比例0~100%
TIM1->PSC=9; //10分频 //72KHZ PWM频率输出
TIM1->CCMR1|=6<<4; //CH1 PWM1模式
TIM1->CCMR1|=1<<3; //CH1 预装载使能
TIM1->CCMR1|=6<<12; //CH2 PWM1模式
TIM1->CCMR1|=1<<11; //CH2 预装载使能
TIM1->CCMR2|=6<<4; //CH3 PWM1模式
TIM1->CCMR2|=1<<3; //CH3 预装载使能
TIM1->CCMR2|=6<<12; //CH4 PWM1模式
TIM1->CCMR2|=1<<11; //CH4 预装载使能
TIM1->CR1|=1<<7; //ARPE使能自动重装载预装载允许位
TIM1->CR1|=1<<4; //向下计数模式
TIM1->CCER|=1<<0; //OC1 输出使能 高电平有效
TIM1->CCER|=1<<4; //OC2 输出使能
TIM1->CCER|=1<<8; //OC3 输出使能
TIM1->CCER|=1<<12; //OC4 输出使能
TIM1->EGR |= 1<<0; //初始化所有的寄存器
TIM1->CR1|=1<<0; //使能定时器1
TIM1->CCR1= 50;
TIM1->CCR2= 00;
// TIM1->BDTR |=1<<15; //开启OC和OCN输出
#endif
GPIOB->CRL&=0X000FFFFF;//PB6/PB7/PB5 推挽输出
GPIOB->CRL|=0X33300000;
GPIOB->ODR|=7<<5; //PB6,PB7,PB5 输出高
#else
RCC->APB2ENR |= 1<<3;
GPIOB->CRL&=0X00FFFFFF;
GPIOB->CRL|=0X77000000;
#endif
{
Single_Write_BH1750(0x01);
ADDR = 0; //将ADDR位初始化拉低
}
{
BH1750_Start(); //起始信号
BH1750_SendByte(SlaveAddress); //发送设备地址+写信号
BH1750_SendByte(REG_Address); //内部寄存器地址,
BH1750_Stop(); //发送停止信号
}
{
SDA_OUT(); //sda线输出
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
}
{
u8 i,bit;
SDA_OUT(); //sda线输出
for (i=0; i<8; i++) //8位计数器
{
bit=dat&0x80;
if(bit) IIC_SDA=1;
else IIC_SDA=0;
dat <<= 1; //移出数据的最高位
IIC_SCL=1; //拉高时钟线
delay_us(2); //延时
IIC_SCL=0; //拉低时钟线
delay_us(2); //延时
}
BH1750_RecvACK();
}
{
SDA_OUT();//sda线输出
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
IIC_SCL=1;
delay_us(4);
IIC_SDA=1;//发送I2C总线结束信号
delay_us(4);
}
void KEY_Init(void)
{
RCC->APB2ENR|=1<<3; //使能PORTB时钟
GPIOB->CRH&=0X0000FFFF;
GPIOB->CRH|=0X88880000; //PB12~PB15设置成输入
GPIOB->ODR|=0xf000; //PB12~PB15设置上拉
}
void XJ_Init() // 循迹数据通过CAN传输
{
// RCC->APB2ENR|=1<<2; //使能PORTA时钟
//
// GPIOA->CRL&=0XFFFFFFF0;
// GPIOA->CRL|=0X00000003;//PA0 设置成输出
// GPIOA->ODR|=1<<0; //P输出为高
//
// Track_Usart2_Close();
// GPIOA->CRL&=0X00000000;
// GPIOA->CRL|=0X88888888;//PA0 设置成输出
// GPIOA->ODR|=0x00ff; //P输出为高
}
Timer2_Init(1000,71); // 1Mhz的计数频率,计数到500为100us
Timer4_Init(1000,71); // 1Mhz的计数频率,计数到1000为1ms
TIM6_Int_Init(2000,71); // 2ms 检测CAN总线数据
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC->APB2ENR|=1<<3; //使能PORTB时钟
RCC->APB2ENR|=1<<6; //使能PORTE时钟
GPIOB->CRL&=0XFFFFFFF0; // PB0 设置成输入
GPIOB->CRL|=0X00000008;
GPIOB->ODR|=1<<0;
GPIOB->CRH&=0XFFFFF00F; // PB9/PB10设置成输入
GPIOB->CRH|=0X00000880;
GPIOB->ODR|=3<<9;
GPIOE->CRL&=0XFFFFFFF0; // PE0设置成输出
GPIOE->CRL|=0X00000003;
GPIOE->ODR|=1<<0; // PE0上拉
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
//GPIOB9/10 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);
EXTI_InitStructure.EXTI_Line=EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组 2
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
#if SPPED_INT_MODE
//GPIOB10 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource10);
EXTI_InitStructure.EXTI_Line=EXTI_Line10;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组 2
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
#else
//GPIOB0 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
#endif
// Ex_NVIC_Config(GPIO_B,9,FTIR); //下降沿触发
// Ex_NVIC_Config(GPIO_B,10,FTIR); //下降沿触发
// MY_NVIC_Init(2,2,EXTI9_5_IRQHandler,2); //抢占2,子优先级2,组2
// MY_NVIC_Init(2,3,EXTI15_10_IRQHandler,2); //抢占2,子优先级3,组2
}
GPIOE->CRL&=0XFFFFFF0F;
GPIOE->CRL|=0X00000030; //PE1推挽输出
GPIOE->ODR|=0X0002; //PE1输出高
RI_TXD=1;
uart1_init( 115200 );
#else
uart2_init( 115200 ,1);
#endif
RCC->APB2ENR |= 1<<5; //PD4
GPIOD->CRL&=0XFFF0FFFF;//PD4 推挽输出
GPIOD->CRL|=0X00030000;
GPIOD->ODR|=1<<4; //PD4 输出高
Adc_Init();
Pb *= 100;
Pa = (float)(3300*11)/4096 ;
Pa = (float)((Pa *100) /PWR_DV);
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* POWER CHECK PC4 -> L PC5 ->R */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1 , ENABLE ); //使能ADC1通道时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12M,ADC最大时间不能超过14M
//PC4/5 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADC_DeInit(ADC1); //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //模数转换工作在单通道模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
ADC_ResetCalibration(ADC1); //使能复位校准
while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束
ADC_StartCalibration(ADC1); //开启AD校准
while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束
// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能
delay_us(3) ;
CSB_TX = 0;
TIM3->SR&=~(1<<0); // 清除中断标志位
status = 0; // 定时器清零
CSB_TX = 1;
delay_ms(5);
Single_Write_BH1750(0x10); // H- resolution mode
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];//合成数据,即光照数据
data=(int)temp;
return data;
Send_UpMotor(L_Spend ,R_Spend);
if(L_Spend>=0)
{
if(L_Spend>100)L_Spend=100;if(L_Spend<5)L_Spend=5; //限制速度参数
LEFT_NSLEEP =1; TIM1->CCR2= L_Spend; TIM1->CCR1= 0;
}
else
{
if(L_Spend<-100)L_Spend= -100;if(L_Spend>-5)L_Spend= -5; //限制速度参数
LEFT_NSLEEP =1; TIM1->CCR1= -L_Spend; TIM1->CCR2= 0;
}
if(R_Spend>=0)
{
if(R_Spend>100)R_Spend=100;if(R_Spend<5)R_Spend=5; //限制速度参数
REFT_NSLEEP =1; TIM1->CCR3= R_Spend; TIM1->CCR4= 0;
}
else
{
if(R_Spend<-100)R_Spend= -100;if(R_Spend>-5)R_Spend= -5; //限制速度参数
REFT_NSLEEP =1; TIM1->CCR4= -R_Spend; TIM1->CCR3= 0;
}
TIM1->BDTR |=1<<15; //开启OC和OCN输出
TIM_Cmd(TIM2, DISABLE);
// for(st=0;st<10;st++)
// {
// TIM1->CCR1=st*10;
// TIM1->CCR2=st*10;
// TIM1->CCR3=st*10;
// TIM1->CCR4=st*10;
// }
TIM1->CCR1=100;
TIM1->CCR2=100;
TIM1->CCR3=100;
TIM1->CCR4=100;
TIM1->BDTR &=~(1<<15); //开启OC和OCN输出
LEFT_NSLEEP = 1;
REFT_NSLEEP = 1;
Send_UpMotor(0 , 0); // 上传电机速度
u8 sum=0;
if(Wifi_Rx_Buf[7]==0xbb) // 判断包尾
{
//主指令与三位副指令左求和校验
//注意:在求和溢出时应该对和做256取余。
sum=(Wifi_Rx_Buf[2]+Wifi_Rx_Buf[3]+Wifi_Rx_Buf[4]+Wifi_Rx_Buf[5])%256;
if(sum == Wifi_Rx_Buf[6])
{
Rx_Flag =1;
}
else Rx_Flag =0;
}
}
Stop_Flag=0;G_Flag=0;B_Flag=0;L_Flag=0;R_Flag=0;
STOP();
break;
Stop_Flag=0;tempMP=0;
tempMP=Wifi_Rx_Buf[5];tempMP<<=8;tempMP|=Wifi_Rx_Buf[4];
Car_Spend = Wifi_Rx_Buf[3];
Control(Car_Spend,Car_Spend);
break;
Stop_Flag=0;tempMP=0;
tempMP=Wifi_Rx_Buf[5];tempMP<<=8; tempMP+=Wifi_Rx_Buf[4];
Car_Spend = Wifi_Rx_Buf[3];
Control(-Car_Spend,-Car_Spend);
break;
Car_Spend = Wifi_Rx_Buf[3];
Host_Open_UpTrack( Up_Track_Speed );
Control(-Car_Spend,Car_Spend);
break;
Car_Spend = Wifi_Rx_Buf[3];
Host_Open_UpTrack( Up_Track_Speed );
Control(Car_Spend,-Car_Spend);
break;
Track_Flag=1;MP=0;
Host_Open_UpTrack( Up_Track_Speed ); // 开启寻迹数据上传
delay_ms( 100); // 等待一会寻迹数据上传
TIM_Cmd(TIM2, ENABLE);
break;
break;
G_Tab[1]=Wifi_Rx_Buf[4];
G_Tab[2]=Wifi_Rx_Buf[5];
break;
G_Tab[4]=Wifi_Rx_Buf[4];//低位校验码
G_Tab[5]=Wifi_Rx_Buf[5];//高位校验码
break;
Track_Flag=0;MP=0;
Stop_Flag=0;G_Flag=0;B_Flag=0;L_Flag=0;R_Flag=0;
STOP();
Transmition(G_Tab,6);
break;
if(Wifi_Rx_Buf[3]) LED_L=0;
else LED_L=1;
if(Wifi_Rx_Buf[4]) LED_R=0;
else LED_R=1;
break;
else BEEP=1;
break;
break;
Stop_Flag=0;G_Flag=0;B_Flag=0;L_Flag=0;R_Flag=0;
STOP();
Transmition(H_2,4);
break;
Stop_Flag=0;G_Flag=0;B_Flag=0;L_Flag=0;R_Flag=0;
STOP();
Transmition(H_3,4);
break;
else SD_Flag=1;
break;
break;
default:Track_Flag=0;MP=0;
Stop_Flag=0;G_Flag=0;B_Flag=0;L_Flag=0;R_Flag=0;
STOP();
break;
delay_us(5);