Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include <math.h>
- #include <stdio.h>
- #define CS 0
- int main()
- {
- //init SPI
- RCC_TypeDef * rcc = RCC;
- //enable GPIO clocks
- SET_BIT(rcc->AHB1ENR, RCC_AHB1ENR_GPIOAEN);
- SET_BIT(rcc->AHB1ENR, RCC_AHB1ENR_GPIOFEN);
- SET_BIT(rcc->APB2ENR, RCC_APB2ENR_USART1EN);
- //CS - PF0 (output), SCK - PF1 (output), SIO - PF2 (input)
- GPIO_TypeDef * gpiof = GPIOF;
- SET_BIT(gpiof->MODER, 1 << 0 | 1 << 2);
- CLEAR_BIT(gpiof->MODER, 1 << 1 | 1 << 3 | 1 << 4 | 1 << 5);
- //configure GPIOA pins to alternate mode
- GPIO_TypeDef * gpioa = GPIOA;
- SET_BIT(gpioa->MODER, 1 << 21 | 1 << 19);
- CLEAR_BIT(gpioa->MODER, 1 << 20 | 1 << 18);
- //choose correct alternate function
- SET_BIT(gpioa->AFR[1], 7 << 8 | 7 << 4);
- //CS to 1, so no transmission
- SET_BIT(gpiof->ODR, 1 << 0);
- USART_TypeDef * usart1 = USART1;
- //na TC77 od lewej: CS, Vss(GND), SCK, SI/O, Vdd(Vcc)
- int read_bit = 0;
- float result = 0;
- while (1)
- {
- //set CS to 0 to start transmission
- CLEAR_BIT(gpiof->ODR, 1 << CS);
- int buffer = 0;
- for (int i = 12; i >= 0; i--)
- {
- CLEAR_BIT(gpiof->ODR, 1 << 1);
- for (int i = 0; i < 1000; i++);
- SET_BIT(gpiof->ODR, 1 << 1);
- for (int i = 0; i < 1000; i++);
- read_bit = READ_BIT(gpiof->IDR, 1 << 2);
- //shift right to get bit value - 0 or 1
- read_bit = read_bit >> 2;
- buffer |= (read_bit << i);
- }
- //stop transmission
- SET_BIT(gpiof->ODR, 1 << 0);
- //buffer negative or not - U2 + conversion
- //TC77 documentation LSB is 0,0625 celcius degree
- result = 0;
- for (int i = 0; i < 12; i++)
- {
- if (buffer & (1 << i))
- {
- result += 0.0625 * pow(2, i);
- }
- }
- if (buffer & (1 << 12))
- {
- result -= 0.0625 * pow(2, 12);
- }
- char text[50];
- sprintf(text, "Temperature: %f \n", result);
- //configure USART1
- //USART enable
- SET_BIT(usart1->CR1, 1 << 13);
- //8 bit word length
- CLEAR_BIT(usart1->CR1, 1 << 12);
- //set baud rate to 104.1875 mantissa 104 fractional 3
- SET_BIT(usart1->BRR, 104 << 3 | 3 << 0);
- //set TE bit in CR1 to send idle frame
- SET_BIT(usart1->CR1, 1 << 3);
- for (int i = 0; i < 50; i++)
- {
- if(text[i]!='\0')
- usart1->DR = text[i];
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement