main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //#include <stdio.h>
  2. #include "stm32f0xx.h"
  3. #include "crutchs.h"
  4. #include "system_stm32f0xx.h"
  5. #include "stm32f0xx_rcc.h"
  6. #include "stm32f0xx_gpio.h"
  7. #include "stm32f0xx_spi.h"
  8. #include "stm32f0xx_adc.h"
  9. #include "system_stm32f0xx.c"
  10. #include "stm32f0xx_rcc.c"
  11. #include "stm32f0xx_gpio.c"
  12. #include "stm32f0xx_spi.c"
  13. #include "stm32f0xx_adc.c"
  14. //Объявляем переменные
  15. GPIO_InitTypeDef port;
  16. SPI_InitTypeDef spi;
  17. ADC_InitTypeDef adc;
  18. uint8_t sendData;
  19. uint16_t count = 0;
  20. uint16_t data;
  21. void PreInit(){
  22. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  23. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  24. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  25. port.GPIO_Mode = GPIO_Mode_OUT;
  26. port.GPIO_OType = GPIO_OType_PP;
  27. port.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
  28. port.GPIO_Speed = GPIO_Speed_50MHz;
  29. GPIO_Init(GPIOA, &port);
  30. GPIOA -> BSRR = GPIO_Pin_4;
  31. GPIOA -> BSRR = GPIO_Pin_7;
  32. while( 81){
  33. //for (count = 0; count < 1000; count++);
  34. GPIOA -> BSRR = GPIO_Pin_4;
  35. GPIOA -> BSRR = GPIO_Pin_5;
  36. //for (count = 0; count < 1000; count++);
  37. GPIOA -> BRR = GPIO_Pin_4;
  38. GPIOA -> BRR = GPIO_Pin_5;
  39. //count++;
  40. }
  41. GPIOA -> BRR = GPIO_Pin_4;
  42. GPIOA -> BRR = GPIO_Pin_7;
  43. }
  44. void initAll()
  45. {
  46. port.GPIO_Mode = GPIO_Mode_AF;
  47. port.GPIO_OType = GPIO_OType_PP;
  48. port.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  49. port.GPIO_Speed = GPIO_Speed_50MHz;
  50. GPIO_Init(GPIOA, &port);
  51. ADC_StructInit(&adc);
  52. adc.ADC_ContinuousConvMode = ENABLE;
  53. adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
  54. ADC_Init(ADC1, &adc);
  55. SPI_StructInit(&spi);
  56. spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  57. spi.SPI_Mode = SPI_Mode_Master;
  58. spi.SPI_DataSize = SPI_DataSize_8b;
  59. spi.SPI_CPOL = SPI_CPOL_Low;
  60. spi.SPI_CPHA = SPI_CPHA_2Edge;
  61. spi.SPI_NSS = SPI_NSS_Soft;
  62. //spi.SPI_NSS = SPI_NSS_Hard;
  63. spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  64. spi.SPI_FirstBit = SPI_FirstBit_MSB;
  65. spi.SPI_CRCPolynomial = 7;
  66. SPI_Init(SPI1, &spi);
  67. //GPIO_StructInit(&port);
  68. //port.GPIO_PuPd = GPIO_PuPd_DOWN;
  69. //port.GPIO_Pin = GPIO_Pin_0;
  70. //port.GPIO_Speed = GPIO_Speed_2MHz;
  71. //GPIO_Init(GPIOA, &port);
  72. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  73. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  74. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  75. }
  76. int main()
  77. {
  78. // __enable_irq();
  79. PreInit();
  80. // initAll();
  81. //Включаем АЦП
  82. // ADC_Cmd(ADC1, ENABLE);
  83. // ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  84. //И конечно же включаем SPI
  85. SPI_Cmd(SPI1, ENABLE);
  86. // GPIOA -> BRR = GPIO_Pin_4;
  87. while(1)
  88. {
  89. SPI_SendData8(SPI1, 0x40);
  90. SPI_SendData8(SPI1, 0x0);
  91. SPI_SendData8(SPI1, 0x0);
  92. SPI_SendData8(SPI1, 0x0);
  93. SPI_SendData8(SPI1, 0x0);
  94. SPI_SendData8(SPI1, 0x95);
  95. SPI_SendData8(SPI1, 0x0);
  96. SPI_SendData8(SPI1, 0x0);
  97. // GPIOA -> BSRR = GPIO_Pin_4;
  98. //for (count = 0; count < 100; count++);
  99. }
  100. }