sysinit.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "sysinit.h"
  2. extern uint16_t timer;
  3. extern uint8_t MyType;
  4. void WatchDog_Init(u8 prer, u16 reld)
  5. {
  6. IWDG->KR=0x5555; //允许访问PR和RLR寄存器
  7. IWDG->PR=prer; //设置分频
  8. IWDG->RLR=reld; //设定计数器初值
  9. IWDG->KR=0xaaaa; //初次装初值
  10. IWDG->KR=0xcccc; //启动看门狗定时器
  11. }
  12. //喂狗
  13. void WatchDog_Feed(void)
  14. {
  15. IWDG->KR=0xaaaa;
  16. }
  17. void SysTick_Handler() //4ms中断
  18. {
  19. timer++;
  20. }
  21. void config_io_PuPd_DOWN(GPIO_TypeDef* GPIO_Type,uint16_t GPIO_Pin_)
  22. {
  23. GPIO_InitTypeDef GPIO_InitStructure;
  24. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_;
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  26. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  27. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  28. GPIO_Init(GPIO_Type, &GPIO_InitStructure);
  29. }
  30. void config_io_PuPd_UP(GPIO_TypeDef* GPIO_Type,uint16_t GPIO_Pin_)
  31. {
  32. GPIO_InitTypeDef GPIO_InitStructure;
  33. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_;
  34. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  36. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  37. GPIO_Init(GPIO_Type, &GPIO_InitStructure);
  38. }
  39. void config_io_NOPULL(GPIO_TypeDef* GPIO_Type,uint16_t GPIO_Pin_)
  40. {
  41. GPIO_InitTypeDef GPIO_InitStructure;
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_;
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  45. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  46. GPIO_Init(GPIO_Type, &GPIO_InitStructure);
  47. }
  48. void config_io_out(GPIO_TypeDef* GPIO_Type,uint16_t GPIO_Pin_)
  49. {
  50. GPIO_InitTypeDef GPIO_InitStructure;
  51. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_;
  52. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  53. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  54. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  55. GPIO_Init(GPIO_Type, &GPIO_InitStructure);
  56. }
  57. uint8_t temp1=0x0F,temp2=0;
  58. uint8_t get_io(void)
  59. {
  60. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  61. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  62. config_io_NOPULL(GPIOF, GPIO_Pin_0);
  63. config_io_NOPULL(GPIOF, GPIO_Pin_1);
  64. config_io_NOPULL(GPIOA, GPIO_Pin_0);
  65. config_io_NOPULL(GPIOA, GPIO_Pin_1);
  66. delay_ms_(1);
  67. temp1 = ((GPIOA->IDR&GPIO_Pin_1)==0);
  68. temp1 = (temp1<<1)|((GPIOA->IDR&GPIO_Pin_0)==0);
  69. temp1 = (temp1<<1)|((GPIOF->IDR&GPIO_Pin_1)==0);
  70. temp1 = (temp1<<1)|((GPIOF->IDR&GPIO_Pin_0)==0);
  71. delay_ms_(10);
  72. temp2 = (GPIOA->IDR&GPIO_Pin_1)==0;
  73. temp2 = (temp2<<1)|((GPIOA->IDR&GPIO_Pin_0)==0);
  74. temp2 = (temp2<<1)|((GPIOF->IDR&GPIO_Pin_1)==0);
  75. temp2 = (temp2<<1)|((GPIOF->IDR&GPIO_Pin_0)==0);
  76. if (temp1==temp2) return temp1;
  77. else return 0x0F;
  78. }
  79. void ALL_Config(void)
  80. {
  81. SysTick_Config(SystemCoreClock/1000);
  82. LED_Init();
  83. MyType = get_io(); //获取编码器当前值
  84. //MyType = 1; //获取编码器当前值
  85. USART1_Init_Config(); //配置IO口和中断配置
  86. USART1_Init(9600); //为初始化蓝牙配置
  87. WatchDog_Init(4, 1350);
  88. if (MyType==15) AutoBauRate_StartBitMethod(); //获取波特率
  89. WatchDog_Feed();
  90. BLE_init(); //初始化蓝牙配置
  91. USART1_NVIC_Config();
  92. }