-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
67 lines (58 loc) · 1.35 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "globals.h"
#include "uart.h"
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdbool.h>
unsigned char CS = 1; // CS pin on MAX6675
unsigned char SO = 3; // SO pin of MAX6675
unsigned char SCK = 4; // SCK pin of MAX6675
unsigned int get_temp ( void )
{
int i;
uint16_t value;
bool pinstate;
//CS low to enable device
PORTB ^= (1 << CS);
//strobe SCK to trash dummy bit
PORTB |= (1 << SCK);
PORTB ^= (1 << SCK);
//strobe SCK to read bits 14 through 3
for (i=11; i>=0; i--)
{
//SCK goes high
PORTB |= (1 << SCK);
//read value of SO into bit i stored in value
value += ((PINB & 1<<SO) > 0) << i;
//SCK goes low
PORTB ^= (1 << SCK);
}
//strobe SCK to grab thermocouple input bit
PORTB |= (1 << SCK);
PORTB ^= (1 << SCK);
//CS high to disable device
PORTB |= (1 << CS);
//use a bitshift to divide value by 4, truncating the LSBs
value >>= 2;
return value;
}
int main ( void )
{
uartInit();
messageBuf[5] = '\r';
messageBuf[6] = '\n';
//setup pinmodes - CS and SCK output, SO input
DDRB |= ( 1 << CS ) | ( 1 << SCK );
DDRB ^= ( 1 << SO );
for (;;) {
uint16_t value;
value = get_temp();
utoa(value, messageBuf, 10);
//iterate through the first ten items of messageBuf, sending values over the software UART
for (temp = 0; temp < 10; temp++) {
sendByte(messageBuf[temp]);
}
_delay_ms(500);
}
return 0;
}