找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3399|回复: 5
收起左侧

stm32f105例程 CAN500K USART_LED

[复制链接]
回帖奖励 2 黑币 回复本帖可获得 2 黑币奖励! 每人限 1 次
ID:397541 发表于 2018-9-28 09:38 | 显示全部楼层 |阅读模式
stm32f105例程

单片机源程序如下:


  1. /*
  2.         CAN1
  3.         USART1

  4.     CAN波特率、收发器 配置见CAN.h

  5.         stm32f10x.h中修改了外部晶振值8MHz
  6.        

  7. */

  8. /* Includes ------------------------------------------------------------------*/
  9. #include "stm32f10x.h"
  10. #include "stm32f10x_rcc.h"
  11. #include "stm32f10x_flash.h"
  12. #include "stm32f10x_can.h"
  13. #include "usart.h"
  14. #include "CAN.h"
  15. #include <stdio.h>

  16. void RCC_Configuration(void);
  17. void LED_Config(void);
  18. void Delay(__IO uint32_t nCount);


  19. CanTxMsg TxMsg1={0xAB,0,CAN_ID_STD,CAN_RTR_DATA,8,{0xAB,0,0,0,0,0,0,0}};
  20. CanTxMsg TxMsg2={0xCD,0,CAN_ID_STD,CAN_RTR_DATA,8,{0xCD,0,0,0,0,0,0,0}};


  21. int main(void)
  22. {
  23.                           
  24.   /* System Clocks Configuration **********************************************/
  25.   RCC_Configuration();   
  26.   LED_Config();

  27.   // 串口配置                                                                                                               
  28.   USART_Configuration();

  29.   USART_STR(USART2,"++++++++++++++++++++++++\r\n");
  30.   USART_STR(USART2,"  CAN Study Board\r\n");
  31.   USART_STR(USART2,"++++++++++++++++++++++++\r\n");       
  32.                                                                                                                   
  33.                                                                                                   
  34.   // CAN1 配置
  35.   CAN1_Config(SET_CAN_SJW,SET_CAN_BS1,SET_CAN_BS2,SET_CAN_PRES);  
  36.                                                                                                               
  37.   Delay(2000);
  38.                                                          
  39.   // CAN2 配置
  40.   CAN2_Config(SET_CAN_SJW,SET_CAN_BS1,SET_CAN_BS2,SET_CAN_PRES);                                                                                          
  41.                                    
  42.                                                   
  43.   while (1)
  44.   {     
  45.           GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  46.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  47.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  48.           GPIO_SetBits(GPIOC,GPIO_Pin_15);
  49.       Delay(5000);
  50.           Delay(5000);          
  51.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  52.           GPIO_ResetBits(GPIOC,GPIO_Pin_1);
  53.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  54.           GPIO_SetBits(GPIOC,GPIO_Pin_15);

  55.           CAN_SendData(CAN1,&TxMsg1);

  56.           Delay(5000);
  57.           Delay(5000);          
  58.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  59.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  60.           GPIO_ResetBits(GPIOC,GPIO_Pin_14);
  61.           GPIO_SetBits(GPIOC,GPIO_Pin_15);
  62.           Delay(5000);
  63.           Delay(5000);          
  64.           GPIO_SetBits(GPIOC,GPIO_Pin_0);
  65.           GPIO_SetBits(GPIOC,GPIO_Pin_1);
  66.           GPIO_SetBits(GPIOC,GPIO_Pin_14);
  67.           GPIO_ResetBits(GPIOC,GPIO_Pin_15);
  68.           Delay(5000);
  69.           Delay(5000);                                                                                       
  70.        
  71.           CAN_SendData(CAN2,&TxMsg2);                                                                                                                                     
  72.   }
  73. }


  74. void RCC_Configuration(void)
  75. {   
  76.   /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
  77.      initialize the PLL and update the SystemFrequency variable. */
  78.   SystemInit();
  79.                                                                                            
  80. }


  81. void Delay(__IO uint32_t nCount)
  82. {
  83.     uint8_t x;
  84.     for(; nCount != 0; nCount--)
  85.             for(x=0;x<100;x++);
  86. }

  87. void LED_Config(void)
  88. {
  89.   GPIO_InitTypeDef GPIO_InitStructure;

  90.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  91.   
  92.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_14|GPIO_Pin_15;                                  
  93.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  94.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  95.   GPIO_Init(GPIOC, &GPIO_InitStructure);                                         
  96. }


  97. #ifdef  USE_FULL_ASSERT

  98. /**
  99.   * @brief  Reports the name of the source file and the source line number
  100.   *   where the assert_param error has occurred.
  101.   * @param  file: pointer to the source file name
  102. ……………………

  103. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
stm32f105.rar (458.73 KB, 下载次数: 116)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:418101 发表于 2018-10-31 12:15 | 显示全部楼层
楼主你好,我用你的代码,测试105r8 CAN时,发现发送的时候还是发不出去,在tx pin看不到波形,一直是高电平,打印transmit的返回值,发现mailbox值一直从1 2 3 4 4 4 后面就一直是4了,也就是从未发出去,楼主测试过吗?
回复

使用道具 举报

ID:191583 发表于 2019-1-29 14:30 | 显示全部楼层
正在学习,谢谢提供的资料。
回复

使用道具 举报

ID:158966 发表于 2019-7-30 09:49 | 显示全部楼层
正在学习,谢谢提供的资料
回复

使用道具 举报

ID:251047 发表于 2019-7-30 15:05 | 显示全部楼层
多谢分享,哈哈哈
回复

使用道具 举报

ID:320751 发表于 2020-9-7 21:17 | 显示全部楼层
谢谢分享  学习一下 感恩付出
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表