本帖最后由 fxyc87 于 2021-4-28 10:54 编辑
for(int i=0;i<0x012345;i++){
__nop(); //延时约0.2秒 等待电源稳定
}
RCC->CR|=BIT(16); //1: HSE oscillator ON
while((RCC->CR&BIT(17))==0); //0: HSE oscillator not ready
//N=50-432
//M=2-63
//vco=in*(n/m) vco=100-432M
//out=vco/p P=2/4/6/8
//(8M*(200/8))/2=100M
//PLL时钟源来自外部HSE /N=6 /M=8
RCC->PLLCFGR=BIT(22)|(200<<6) |(8<<0);
//P=0=*2
RCC->CR|=BIT(24); //1: PLL ON
while((RCC->CR&BIT(25))==0); //Bit 25 PLLRDY: Main PLL (PLL) clock ready flag
//切换前FLASH时钟等待加长
FLASH->ACR=6; //100M时等待6个周期 6 WS (7 CPU cycles)
//APB1最大50M 100/2
RCC->CFGR|=4<<10; //APB1 100: AHB clock divided by 2
//主时钟切换至PLL
RCC->CFGR|=2; //Bits 1:0 SW: System clock switch / 10: PLL selected as system clock
while((RCC->CFGR&8)!=8); //等待时钟切换完成 p108
//系统定时器,相关时钟开启
SystemCoreClockUpdate();
SysTick_Config(SystemCoreClock/1000); //1ms时基
RCC->AHB1ENR|=BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4);
//GPIO时钟开启
//IO配置
{//2初始化输出点
//out-en pc15
GPIOC->ODR|=BIT(15);
GPIOC->MODER|=1<<(15*2); //01: General purpose output mode
uint16_t PinBit;
//pa输出点 位全置0,输出关闭 参见BSP.H
GPIOA->ODR=0;
GPIOA->MODER|= (1<<(0*2))| //pinF1
(1<<(1*2))| //pinF2
(1<<(4*2)); //pinCS1
//pb输出点
GPIOB->ODR=0;
GPIOB->MODER|= (1<<(0*2)) | (1<<(1*2)) |
(1<<(8*2)) | (1<<(9*2)) |
(1<<(10*2)) | (1<<(11*2)) |
(1<<(12*2)) | (1<<(13*2)) |
(1<<(14*2)) | (1<<(15*2));
//pe 输出点
GPIOE->ODR=0;
GPIOE->MODER|= (1<<(0*2)) | (1<<(1*2)) |
(1<<(2*2)) | (1<<(3*2)) |
(1<<(4*2)) | (1<<(5*2)) |
(1<<(6*2)) | (1<<(7*2)) |
(1<<(8*2)) | (1<<(9*2)) |
(1<<(10*2)) | (1<<(11*2)) |
(1<<(12*2)) | (1<<(13*2)) |
(1<<(14*2)) | (1<<(15*2));
}//pc输出
//pd输出
GPIOC->ODR&=~BIT(15); //out-en pc15
{//3.初始化输入点
//下拉输出模式 2MHz;
//pa输入点
//pb输入点
//pe 输入点
//PC输入(默认就是输入,忽略 )
//PD输入(默认就是输入,忽略 )
}//Pe输入
//4读取配置
{//5热电偶初始化 SPI1
RCC->APB2ENR|=BIT(12);
//GPIO初始化
//pa5,pa6复用 SCK MISO
GPIOA->AFR[0]|=(5<<5*4)|(5<<6*4); //af5,af5
GPIOA->MODER|=(2<<(5*2)) | (2<<(6*2)); //10: Alternate function mode
GPIOA->OSPEEDR|=(2<<(5*2)) | (2<<(6*2)); //2=GPIO_SPEED_FAST
//max6675 MAX=4.3 MHz
SPI1->CR1|=BIT(11); //Bit 11 1: 16-bit data frame format
SPI1->CR1|=BIT(9); //Bit 9 1: Software slave management enabled
SPI1->CR1|=4<<3; //5=101: fPCLK/64 Baud rate control 100/64=1.56M 100/32=3.12M
//4=clk/32
SPI1->CR1|=BIT(2); //1: Master configuration
SPI1->CR2|=BIT(2); //SSOE
//a4=cs1,pe13,14,15=cs2,3,4
GPIOA->ODR|=BIT(4);
GPIOE->ODR|=BIT(13);
GPIOE->ODR|=BIT(14);
GPIOE->ODR|=BIT(15);
SPI1->CR1|=BIT(6); //Bit 6 SPI enable
}
//配置定时器 控制最终目标温度
if(ReadCBit(20,9)){ //位9机头电机运动功能 //200 180/c
//todo:位9机头电机运动功能 换F4后删除
}
//6配置RS485串口 modbus
//modbus 实现 位写入,位读取 位映射
//rs485与屏通信
//pa9 tx af7
//pa10 rx af7
GPIOA->MODER|=(2<<(9*2))|(2<<(10*2)); //10: Alternate function mode
GPIOA->AFR[1]|=(7<<(4*2))| (7<<(4*1)); //af7
RCC->APB2ENR|=BIT(4);
//100-000-000/16/115200=54.25347222222222
//M=54 F=.25347222222222*16=4
USART1->BRR=(54<<4)|4;
USART1->CR1|=BIT(3); //Bit 3 TE: Transmitter enable
USART1->CR1|=BIT(2); //Bit 2 RE: Receiver enable
USART1->CR1|=BIT(5); //接收中断开启 ORE=1 or RXNE=1
USART1->CR1|=BIT(13); //Bit 13 UE: USART enable
// __NVIC_EnableIRQ(USART1_IRQn);
//
// NVIC->ISER[(((uint32_t)USART1_IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)USART1_IRQn) & 0x1FUL));
NVIC_EnableIRQ(USART1_IRQn);
//x9接的是PD14,用于履带机的电机脉冲触发信号,约1-30秒触发一次
EXTI->IMR|=EXTI_IMR_MR14;
EXTI->FTSR|=EXTI_FTSR_TR14; //1: Falling trigger enabled
RCC->APB2ENR|=RCC_APB2ENR_SYSCFGEN;
SYSCFG->EXTICR[3]|= 3<<8; //0011: PD[x] pin
NVIC_EnableIRQ(EXTI15_10_IRQn);
//看门狗
RCC->CSR|=RCC_CSR_LSION; //时钟开启
while((RCC->CSR & RCC_CSR_LSIRDY)==0); //1: LSI RC oscillator ready
IWDG->KR=0x5555; //启用寄存器可写
IWDG->PR=2; //0=32k/4*4096=0.5秒 1=/8=1秒 2=/16=2秒
while((IWDG->SR&IWDG_SR_PVU)==0); //更新成功
IWDG->KR=0xCCCC; //启动
DOG();
//end
所有寄存器配置都是参照手册,一点点测试。
这样出现问题时比较好查找问题。
也能学习更多知识。
|