微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32 can总线通信[库函数]

stm32 can总线通信[库函数]

时间:11-22 来源:互联网 点击:

070RxMessage.Data[4],

071RxMessage.Data[5],

072RxMessage.Data[6],

073RxMessage.Data[7]

074);

075

076while(1);

077}

078

079void CAN_Configuration(void)

080{

081CAN_InitTypeDef CAN_InitStructure;

082CAN_FilterInitTypeDef CAN_FilterInitStructure;

083

084CAN_DeInit(CAN1);

085CAN_StructInit(&CAN_InitStructure);

086

087CAN_InitStructure.CAN_TTCM = DISABLE;

088CAN_InitStructure.CAN_ABOM = DISABLE;

089CAN_InitStructure.CAN_AWUM = DISABLE;

090CAN_InitStructure.CAN_NART = DISABLE;

091CAN_InitStructure.CAN_RFLM = DISABLE;

092CAN_InitStructure.CAN_TXFP = DISABLE;

093CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;

094CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

095CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq;

096CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;

097CAN_Init(CAN1,&CAN_InitStructure);

098

099CAN_FilterInitStructure.CAN_FilterNumber = 0;

100CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;

101CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;

102CAN_FilterInitStructure.CAN_FilterIdHigh = 0x00AA < 3; //匹配过滤寄存器,因为数据标志符段 还有 IDE ,RTR 和一个补零位 所以左移三位

103CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;

104CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x00FF < 3 ; //匹配屏蔽寄存器

105CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

106CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

107

108CAN_FilterInit(&CAN_FilterInitStructure);

109

110

111}

112

113

114void GPIO_Configuration(void)

115{

116GPIO_InitTypeDef GPIO_InitStructure;

117

118GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

119

120GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

121GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

122GPIO_Init(GPIOB , &GPIO_InitStructure);

123

124GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

125GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

126GPIO_Init(GPIOB , &GPIO_InitStructure);

127

128

129GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

130GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

131GPIO_Init(GPIOA , &GPIO_InitStructure);

132

133GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

134GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

135GPIO_Init(GPIOA , &GPIO_InitStructure);

136}

137

138void RCC_Configuration(void)

139{

140

141ErrorStatus HSEStartUpStatus;

142

143

144RCC_DeInit();

145

146RCC_HSEConfig(RCC_HSE_ON);

147

148HSEStartUpStatus = RCC_WaitForHSEStartUp();

149

150if(HSEStartUpStatus == SUCCESS)

151{

152

153RCC_HCLKConfig(RCC_SYSCLK_Div1);

154

155RCC_PCLK2Config(RCC_HCLK_Div1);

156

157RCC_PCLK1Config(RCC_HCLK_Div2);

158

159FLASH_SetLatency(FLASH_Latency_2);

160

161FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

162

163RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

164

165RCC_PLLCmd(ENABLE);

166

167while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

168

169RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

170

171while(RCC_GetSYSCLKSource() != 0x08);

172}

173

174RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);

175

176//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

177

178RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);

179//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG|RCC_APB1Periph_SPI2|RCC_APB1Periph_I2C1|RCC_APB1Periph_I2C2, ENABLE);

180

181}

182

183

184void USART_Configuration(void)

185{

186USART_InitTypeDef USART_InitStructure;

187USART_ClockInitTypeDef USART_ClockInitStructure;

188

189USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;

190USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;

191USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;

192USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

193USART_ClockInit(USART1 , &USART_ClockInitStructure);

194

195USART_InitStructure.USART_BaudRate = 9600;

196USART_InitStructure.USART_WordLength = USART_WordLength_8b;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top