Debian Codegraph @

examples/atmega32/temp-max6675.py

raw symbols SCIP
1 # Read MAX6675 temperature Values and plot them
2 import time
3 #from kuttyPy import *
4 from matplotlib import pyplot as plt
5 import numpy as np
6
7 #Outputs, pin definitions
8 PIN_SCK = 7; PIN_MOSI = 5 ; PIN_SS = 4
9 #$0D ($2D) SPCR : SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0
10 SPE = 6 ; MSTR = 4 ; SPR0 = 0
11 PORTBvalue = 0
12 #Initialize things
13 setReg('DDRB', (1 << PIN_SCK) | (1 << PIN_MOSI) | (1 << PIN_SS)) #SCK MOSI CS/LOAD/SS
14 setReg('SPCR', (1 << SPE) | (1 << MSTR)| (1<<SPR0) ) #SPI Master Mode.
15
16 plt.ion() #Interactive plot
17 x=[] ; y = []
18 start_time = time.time() #Staring time noted
19 for a in range(5): #Get 50 readings
20 t = time.time() - start_time #Elapsed time
21
22 PORTBvalue &= ~(1<<PIN_SS) # CS Low
23 setReg('PORTB', PORTBvalue)
24
25 setReg('SPDR', 0xFF)
26 time.sleep(0.001)#Wait until transfer is complete . //while (!(SPSR & (1 << SPIF)));
27 v1 = getReg('SPDR')
28
29 setReg('SPDR', 0xFF)
30 time.sleep(0.001)#Wait until transfer is complete . //while (!(SPSR & (1 << SPIF)));
31 v2 = getReg('SPDR')
32
33 PORTBvalue |= (1<<PIN_SS) #CS High
34 setReg('PORTB', PORTBvalue)
35 temp = (v1<<8)|v2
36 if temp&0x4: #Invalid reading. thermocouple disconnected. Continue the loop from the top.
37 continue
38 temp = (temp >>3 )*0.25 #Right shift and discard first 3 bits. Divide by 4 to get Celcius equivalent
39
40
41 print(t,temp)
42 plt.scatter(t, temp , s=5) #Scatter plot (x= point number, y = temperature)
43 x.append(t); y.append(temp)
44 plt.pause(0.2) #Wait 200 mS
45 print('press any key/button to close')
46 np.savetxt('tmp.txt',np.column_stack([x,y]),fmt='%1.2f')
47 plt.waitforbuttonpress()