Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include <math.h>
  3. #include <stdio.h>
  4.  
  5. #define CS 0
  6.  
  7. int main()
  8. {
  9. //init SPI
  10. RCC_TypeDef * rcc = RCC;
  11.  
  12. //enable GPIO clocks
  13. SET_BIT(rcc->AHB1ENR, RCC_AHB1ENR_GPIOAEN);
  14. SET_BIT(rcc->AHB1ENR, RCC_AHB1ENR_GPIOFEN);
  15. SET_BIT(rcc->APB2ENR, RCC_APB2ENR_USART1EN);
  16.  
  17. //CS - PF0 (output), SCK - PF1 (output), SIO - PF2 (input)
  18. GPIO_TypeDef * gpiof = GPIOF;
  19. SET_BIT(gpiof->MODER, 1 << 0 | 1 << 2);
  20. CLEAR_BIT(gpiof->MODER, 1 << 1 | 1 << 3 | 1 << 4 | 1 << 5);
  21.  
  22. //configure GPIOA pins to alternate mode
  23. GPIO_TypeDef * gpioa = GPIOA;
  24. SET_BIT(gpioa->MODER, 1 << 21 | 1 << 19);
  25. CLEAR_BIT(gpioa->MODER, 1 << 20 | 1 << 18);
  26.  
  27. //choose correct alternate function
  28. SET_BIT(gpioa->AFR[1], 7 << 8 | 7 << 4);
  29.  
  30.  
  31. //CS to 1, so no transmission
  32. SET_BIT(gpiof->ODR, 1 << 0);
  33.  
  34. USART_TypeDef * usart1 = USART1;
  35.  
  36.  
  37. //na TC77 od lewej: CS, Vss(GND), SCK, SI/O, Vdd(Vcc)
  38.  
  39.  
  40. int read_bit = 0;
  41. float result = 0;
  42.  
  43. while (1)
  44. {
  45. //set CS to 0 to start transmission
  46. CLEAR_BIT(gpiof->ODR, 1 << CS);
  47. int buffer = 0;
  48.  
  49. for (int i = 12; i >= 0; i--)
  50. {
  51. CLEAR_BIT(gpiof->ODR, 1 << 1);
  52. for (int i = 0; i < 1000; i++);
  53. SET_BIT(gpiof->ODR, 1 << 1);
  54. for (int i = 0; i < 1000; i++);
  55.  
  56.  
  57. read_bit = READ_BIT(gpiof->IDR, 1 << 2);
  58. //shift right to get bit value - 0 or 1
  59. read_bit = read_bit >> 2;
  60. buffer |= (read_bit << i);
  61. }
  62.  
  63. //stop transmission
  64. SET_BIT(gpiof->ODR, 1 << 0);
  65.  
  66. //buffer negative or not - U2 + conversion
  67. //TC77 documentation LSB is 0,0625 celcius degree
  68. result = 0;
  69. for (int i = 0; i < 12; i++)
  70. {
  71. if (buffer & (1 << i))
  72. {
  73. result += 0.0625 * pow(2, i);
  74. }
  75. }
  76. if (buffer & (1 << 12))
  77. {
  78. result -= 0.0625 * pow(2, 12);
  79. }
  80.  
  81. char text[50];
  82. sprintf(text, "Temperature: %f \n", result);
  83.  
  84. //configure USART1
  85.  
  86. //USART enable
  87. SET_BIT(usart1->CR1, 1 << 13);
  88.  
  89. //8 bit word length
  90. CLEAR_BIT(usart1->CR1, 1 << 12);
  91.  
  92. //set baud rate to 104.1875 mantissa 104 fractional 3
  93. SET_BIT(usart1->BRR, 104 << 3 | 3 << 0);
  94.  
  95. //set TE bit in CR1 to send idle frame
  96. SET_BIT(usart1->CR1, 1 << 3);
  97.  
  98. for (int i = 0; i < 50; i++)
  99. {
  100.  
  101. if(text[i]!='\0')
  102. usart1->DR = text[i];
  103. }
  104.  
  105.  
  106.  
  107.  
  108. }
  109.  
  110.  
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement