-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
827 lines (585 loc) · 13 KB
/
main.asm
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
;CKOPT 1, CKSEL3:1 111, CKSEL0 1, SUT1:0 01
.def var = r16
.def var2 = r17
.def delayReg1 = r18
.def delayReg2 = r19
.def delayReg3 = r20
.def var3 = r21
.def char = r22
.def sensorDataB1 = r23
.def sensorDataB2 = r24
.def sensorDataCRC = r25
longString: .db "Ein langer, langer Test String.", '\n'
; The Positions in SRam where our Data will be stored
.equ sensorTempB1 = 0x0060
.equ sensorTempB2 = 0x0061
.equ sensorTempCRC = 0x0062
.equ sensorHumB1 = 0x0063
.equ sensorHumB2 = 0x0064
.equ sensorHumCRC = 0x0065
.equ G_LED = 4
.equ R_LED = 5
.equ SCK = 6
.equ DATA = 7
.equ F_CPU = 4000000 ; Systemtakt in Hz
.equ BAUD = 9600 ; Baudrate
; Berechnungen
.equ UBRR_VAL = ((F_CPU+BAUD*8)/(BAUD*16)-1) ; clever runden
.equ BAUD_REAL = (F_CPU/(16*(UBRR_VAL+1))) ; Reale Baudrate
.equ BAUD_ERROR = ((BAUD_REAL*1000)/BAUD-1000) ; Fehler in Promille
.if ((BAUD_ERROR>10) || (BAUD_ERROR<-10)) ; max. +/-10 Promille Fehler
.error "Systematischer Fehler der Baudrate grösser 1 Prozent und damit zu hoch!"
.endif
init:
ldi var, low(RAMEND) ; init the stack pointer for rcall
out spl, var
ldi var, high(RAMEND)
out sph, var
; Green LED 4
; Red LED 5
; Sensor SCK 6
; Sensor Data 7
ldi var, 0b11110000
out DDRD, var ;Configure Port D4,5,6,7 as output
;ldi var, 0x00
;out DDRC, var ;Configure Ports C as input
ldi var, 0b10000000
out PORTD, var ;Enable Pull Up resistor on Port D7
ldi var, 0b10010000
;ldi var, 0x00
;ldi var, 0b00000000
out PORTD, var ; Turn LED on Port D4 on and pull Data high
; Baudrate einstellen
ldi var, HIGH(UBRR_VAL)
out UBRRH, var
ldi var, LOW(UBRR_VAL)
out UBRRL, var
; Frame-Format: 8 Bit
ldi var, (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0)
out UCSRC, var
sbi UCSRB,TXEN ; TX aktivieren
;rcall btTest
rcall delay1sec ; wait for the sensor to initialize
mainloop:
rcall delay2sec
rcall measureTemp
rcall measureHumidity
;rcall btSendBits
rcall btSendBytes
rjmp mainloop
ldi zl, low(longString << 1) ; point the Z Pointer to our constant String in Program Memory
ldi zh, high(longString << 1)
rcall btTest
ldi var3, 5
again:
rcall blink
dec var3
brne again
rjmp held
measureTemp:
rcall initSensor
rcall sendAddr
rcall sendCmdTemp
ret
measureHumidity:
rcall initSensor
rcall sendAddr
rcall sendCmdHum
ret
initSensor:
; Sending the init sequence
; Set Data to output
sbi DDRD, DATA
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 1
sbi PORTD, SCK
; Set Data to 0
cbi PORTD, DATA
; Set Clock to 0
cbi PORTD, SCK
; Set Clock to 1
sbi PORTD, SCK
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 0
cbi PORTD, SCK
ret
sendAddr:
; Sending 000
; Set Data to 0
cbi PORTD, DATA
ldi var, 3
clockAddr:
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
dec var
brne clockAddr
ret
sendCmdTemp:
; Cmd is 00011
; Sending 000
ldi var, 3
clockTemp1:
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
dec var
brne clockTemp1
; Sending 1
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Set Data to 0
cbi PORTD, DATA
; Sending 1, keep Data high
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Set Data to input
cbi DDRD, DATA
; Wait 3 cycles until reading the Data line
nop
nop
nop
; If Data is set, skip reading temp
sbic PIND, DATA
ret
; #### Reading Temperature ####
; #### Send Ack Clock Cycle to acknoledge Sensor response ####
; Turn Red Led on to indicate that we are communicating
sbi PORTD, R_LED
; Send ACK
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; #### Wait for the Sensor to Send Data ####
rcall delay1ms
waitForTemp:
; Wait until Data pin is 0
sbic PIND, DATA
rjmp waitForTemp
; #### Read Data into our registers ####
ldi sensorDataB1, 0
ldi sensorDataB2, 0
ldi sensorDataCRC, 0
; #### Read first Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadTemp1:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataB1, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadTemp1
waitForTemp2:
; Wait until Data pin is 1
sbis PIND, DATA
rjmp waitForTemp2
rcall sendDataAck
; #### Read second Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadTemp2:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataB2, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadTemp2
waitForTemp3:
; Wait until Data pin is 1
sbis PIND, DATA
rjmp waitForTemp3
rcall sendDataAck
; #### Read CRC Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadTemp3:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataCRC, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadTemp3
; #### Reset Outputs to prepare for next transmission ####
; Set Data to output
sbi DDRD, DATA
; Set Data to 1
sbi PORTD, DATA
; Send ACK
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Store the Recieved Data into the SRam Locations defined at the Top
sts sensorTempB1, sensorDataB1
sts sensorTempB2, sensorDataB2
sts sensorTempCRC, sensorDataCRC
; Turn Red Led back of
cbi PORTD, R_LED
ret
sendCmdHum:
; Cmd is 00101
; Sending 00
ldi var, 2
clockHum1:
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
dec var
brne clockHum1
; Sending 1
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Set Data to 0
cbi PORTD, DATA
; Sending 0
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Sending 1, keep Data high
; Set Data to 1
sbi PORTD, DATA
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Set Data to input
cbi DDRD, DATA
; Wait 3 cycles until reading the Data line
nop
nop
nop
; If Data is set, skip reading humidity
sbic PIND, DATA
ret
; #### Reading Humidity ####
; #### Send Ack Clock Cycle to acknoledge Sensor response ####
; Turn Red Led on to indicate that we are communicating
sbi PORTD, R_LED
; Send ACK
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; #### Wait for the Sensor to Send Data ####
rcall delay1ms
waitForHum:
; Wait until Data pin is 0
sbic PIND, DATA
rjmp waitForHum
; #### Read Data into our registers ####
ldi sensorDataB1, 0
ldi sensorDataB2, 0
ldi sensorDataCRC, 0
; #### Read first Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadHum1:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataB1, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadHum1
waitForHum2:
; Wait until Data pin is 1
sbis PIND, DATA
rjmp waitForHum2
rcall sendDataAck
; #### Read second Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadHum2:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataB2, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadHum2
waitForHum3:
; Wait until Data pin is 1
sbis PIND, DATA
rjmp waitForHum3
rcall sendDataAck
; #### Read CRC Byte ####
; Listen for 8 bits
ldi var, 8
ldi var2, 0b10000000
clockReadHum3:
; Set Clock to 1
sbi PORTD, SCK
sbic PIND, DATA
or sensorDataCRC, var2
; Set Clock to 0
cbi PORTD, SCK
lsr var2
dec var
brne clockReadHum3
; #### Reset Outputs to prepare for next transmission ####
; Set Data to output
sbi DDRD, DATA
; Set Data to 1
sbi PORTD, DATA
; Send ACK
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Store the Recieved Data into the SRam Locations defined at the Top
sts sensorHumB1, sensorDataB1
sts sensorHumB2, sensorDataB2
sts sensorHumCRC, sensorDataCRC
; Turn Red Led back of
cbi PORTD, R_LED
ret
sendDataAck:
; Set Data to output
sbi DDRD, DATA
; Set Data to 0
cbi PORTD, DATA
; Send ACK
; Set Clock to 1
sbi PORTD, SCK
; Set Clock to 0
cbi PORTD, SCK
; Set Data to input
cbi DDRD, DATA
; Enable Data Pull Up Resistor again
sbi PORTD, DATA
ret
; Send the received bits as a String on 1 and 0 via bluetooth
btSendBits:
; #### Send the First Byte ####
; Loop for 8 bits
ldi var, 8
lds var2, sensorTempB1
btSendBitsLoop1:
ldi char, '1'
; If the left most Bit is set, we skip setting char to '0'
sbrs var2, 7
ldi char, '0'
rcall serout
; Shift The Sensor data to the left
lsl var2
dec var
brne btSendBitsLoop1
ldi char, ' '
rcall serout
; #### Send the Second Byte ####
; Loop for 8 bits
ldi var, 8
lds var2, sensorTempB2
btSendBitsLoop2:
ldi char, '1'
; If the left most Bit is set, we skip setting char to '0'
sbrs var2, 7
ldi char, '0'
rcall serout
; Shift The Sensor data to the left
lsl var2
dec var
brne btSendBitsLoop2
ldi char, ' '
rcall serout
; #### Send the CRC Byte ####
; Loop for 8 bits
ldi var, 8
lds var2, sensorTempCRC
btSendBitsLoop3:
ldi char, '1'
; If the left most Bit is set, we skip setting char to '0'
sbrs var2, 7
ldi char, '0'
rcall serout
; Shift The Sensor data to the left
lsl var2
dec var
brne btSendBitsLoop3
ldi char, ' '
rcall serout
ldi char, '\n'
rcall serout
ret
; Send the Bytes directly as chars
; Load each Byte directly from SRam
btSendBytes:
; send two sync bytes first, so the client knows where to start decoding
ldi char, 123
rcall serout
ldi char, 127
rcall serout
lds char, sensorTempB1
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorTempB1
andi char, 0b00001111
rcall serout
lds char, sensorTempB2
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorTempB2
andi char, 0b00001111
rcall serout
lds char, sensorHumB1
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorHumB1
andi char, 0b00001111
rcall serout
lds char, sensorHumB2
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorHumB2
andi char, 0b00001111
rcall serout
lds char, sensorTempCRC
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorTempCRC
andi char, 0b00001111
rcall serout
lds char, sensorHumCRC
lsr char
lsr char
lsr char
lsr char
rcall serout
lds char, sensorHumCRC
andi char, 0b00001111
rcall serout
ldi char, '\n'
rcall serout
; Send a Test String via BT
btTest:
lpm char, Z ; Load next Character from Program Memory where Z points to
or char, char ; If loaded 0, set the Zero Status register to 1
breq btTestEnd ; If Zero Status register is set, branch to end
rcall serout
adiw zh:zl, 1 ; Increment the Z Pointer by 1
rjmp btTest
btTestEnd:
ret
; Put the char into Serial Port
serout:
sbis UCSRA,UDRE ; Wait until usart is ready for the next byte
rjmp serout
out UDR, char ; Put one character to the usart output
ret
; Blink the LED
blink:
;enable led
sbi PORTD, 5
;delay for a little while
rcall delay1
;disable led
cbi PORTD, 5
;delay for a long while
rcall delay3
ret
; Wait until the user releases the Button
held:
sbis PIND, 7 ; skip next if Input 7 is active
rjmp held
ldi delayReg1, 0x01
ldi delayReg2, 0x30
ldi delayReg3, 0xFF
rcall exeDelay
rjmp mainloop
; 1ms Delay
delay1ms:
ldi delayReg1, 1
ldi delayReg2, 6
ldi delayReg3, 49
rcall exeDelay
ret
; A Shorter Delay
delay1:
ldi delayReg1, 1
ldi delayReg2, 0
ldi delayReg3, 0
rcall exeDelay
ret
; A little longer Delay
delay3:
ldi delayReg1, 3
ldi delayReg2, 0
ldi delayReg3, 0
rcall exeDelay
ret
; Delay for 1 second
delay1sec:
ldi delayReg1, 21
ldi delayReg2, 75
ldi delayReg3, 191
rcall exeDelay
ret
; Delay for 3 seconds
delay2sec:
ldi delayReg1, 41
ldi delayReg2, 150
ldi delayReg3, 128
rcall exeDelay
ret
; Execute the Delay
exeDelay:
dec delayReg3
brne exeDelay
dec delayReg2
brne exeDelay
dec delayReg1
brne exeDelay
ret