
|
INFO - FAQ - CC2-Forum - CCPro-Forum |
|
||
|
Ich habe für ein Gartenprojekt, bei dem ein Wassertank für eine Solarbewässerung über eine Pumpensteurung mit Wasser aus einem anderen Tank befüllt wird. Das erledigt die C-Control, die mit Wasserstandssensoren aus dem Grove Programm arbeitet. Ich hatte hier noch einen DHT11 Sensor rumliegen, den ich nun auch eingebunden habe. Grove nutzt für veile Sensoren einen 1-Wire Bus, der dem I2C Bus ähnelt aber dann doch anders ist. Die Geschwindigkeit in Basic reichet nicht aus, sodass ich auf Assembler ausweichen musste. Die Entwicklung erfolgte mit der L-Distribution, in der alles integriert ist. Ich hätte das gerne als Zip hinzugeügt, weiss aber nicht wie. Daher hier der Code erst asm, dann bas. Vielleicht kanne sja jemand gebrauchen. Gruss Ralf ;*********************************************************** ; ; M6805 Assembler DHT11.ASM ; Processor 4Mhz ; Read 40 bits from sensor 4 Databytes, 1 byte checksum ; Uses 5 byte of user variables starting at $A1 ; Returns data valid byte at $A5 with 0 or 1 ; $A1 returns humidity high byte ; $A2 returns humidity low byte ; $A3 returns temperature high byte ; $A4 returns temperature low byte ; $A5 0,1 ; Example: 30 0 29 5 1 = 30.0% humidity and 29.5 Grad Celsius 1 = data valid ; ;*********************************************************** ; definitions portb equ 1 ; port b register pbdir equ 5 ; port b direction register timeout equ 255 ; timeout value for counter highlow equ 249 ; value for high Low detection bitcount equ 8 ; 8 bits for every data byte DHT11_PIN equ 4 ; portpin sensor ; user variables dht11_hh equ $A1 ; uservariable high humidy dht11_hl equ $A2 ; uservariable low humidy dht11_th equ $A3 ; uservariable high temp dht11_tl equ $A4 ; uservariable low temp datavalid equ $A5 ; return value ; stack bitcounter equ $91 ; stack register bitcounter bytecounter equ $92 ; byte counter total of 40 bits (5 x 8 bits) checksum equ $93 ; temp register for chcksum calculation org $101 ; startaddress from basic syscall getHumTemp: clra ; reset all values and registers clrx ; clear index register sta datavalid ; clear return status sta dht11_hh ; clear highbyte for humidity sta dht11_hl ; clear lowbyte for humidity sta dht11_th ; clear highbyte for temperature sta dht11_tl ; clear lowbyte for temperature bset DHT11_PIN,pbdir ; port output bset DHT11_PIN, portb ; port high nop ; wait bclr DHT11_PIN, portb ; port low for 22ms lda #22 ; wait 22 * 1ms jsr delay ; call wait routine bset DHT11_PIN, portb ; pulse portpin lda #$04 ; for at least 20 us wait10us: ldx #$02 ; ca. 10 us delay loop: decx ; inner loop bne loop ; x already 0? deca ; outer loop bne wait10us ; a already 0? bclr DHT11_PIN,pbdir ; portpin input lda #timeout ; load timeout ; **** detect start sequence 20us high, 20us low, 57us high see datasheet detect_start: deca ; dec timeout beq exit ; timeout overflow?, branch to exit brset DHT11_PIN, portb, detect_start ; portpin high? jmp startBits ; jump to startbit detection exit: ; timer overflow, invalid data lda #$ff ; load errorcode 255 sta datavalid ; store error code in check rts ; return to basic startBits: ; detect 2 startbits lda #timeout ; load timeout ;ldx #$A1 ; point to first user variable detect_start_low: ; startlabel low deca ; dec timeout beq exit ; timeout overflow?, branch to exit brclr DHT11_PIN, portb, detect_start_low ; portpin low? lda #timeout ; load timeout detect_start_high: ; startlabel high deca ; dec timeout beq exit ; timeout overflow?, branch to exit brset DHT11_PIN, portb, detect_start_high ; portpin high? ; **** detect start sequence end, start databits lda #bitcount ; count of bits per databyte = 8 sta bitcounter ; bitcount, receive databits n times 20us low, 20 or 57us high ldx #$A1 ; reset index register to first user variable dataBits: ; label data bits detection lda #5 ; load bytecounter expected 5 bytes, total 40 bits sta bytecounter ; store it lda #timeout ; load timeout detect_data_low: ; label low deca ; dectimeout beq exit ; timeout overflow?, branch to exit brclr DHT11_PIN, portb, detect_data_low ; portpin low? lda #timeout ; load timeout detect_data_high: ; label high deca ; dectimeout beq exit ; timeout overflow?, branch to exit brset DHT11_PIN, portb, detect_data_high ; portpin high? clc ; clear carry for correct state of ROL instruction cmp #highlow ; test accu to 249 bhi nextBit ; higher, detected a logical 0 branch to nextbit sec ; we detected high bit, set carry for ROL (rotate through carry) nextBit: ; label for next bit detection rol ,x ; rotate left indirect trough carry, bit 0 is 0 or 1 lda #timeout ; lod timeout dec bitcounter ; dec bit counter bne detect_data_low ; end of bitcount? no, branch to databits incx ; incremnet index register to point to next user variable lda #bitcount ; initialize next 8 bis sta bitcounter ; store it lda #timeout ; load timeout for start condition dec bytecounter ; dec bytecounter bne detect_data_low ; already 5 bytes detected? if no branch to datadetection clra ; clear accu for checksum calculation decx ; point to datavalid register contains the pointer to checksum stx checksum,x ; store it indirect sta ,x ; clear datavalid register decx ; point to 4. byte add ,x ; add it to accu decx ; point to 3. byte add ,x ; add it to accu decx ; point to 2. byte add ,x ; add it to accu decx ; point to 1. byte add ,x ; add it to accu, checksum complete cmp checksum ; compare accu to expected checksum from data set beq endok ; same, branch to end ok sta datavalid ; clear return status, we have no valid data sta dht11_hh ; clear highbyte for humidity sta dht11_hl ; clear lowbyte for humidity sta dht11_th ; clear highbyte for temperature sta dht11_tl ; clear lowbyte for temperature rts ; return to basic endok: ; label end ok inc datavalid ; set datavalid to 1 means data ok rts ; return to basic delay: ldx #200 ; approx. 1ms delay inloop: decx ; dec working register, innerloop bne inloop ; not null? branch to next inner loop deca ; dec outer loop bne delay ; not nnull? branch to next outer loop rts ; return from subroutine '*********************************************************** ' ' ' C-Control/basic DHT11.BAS ' ' Lesen des Humidy Sensor DHT11 Grove ' '*********************************************************** define dht11_HH byte define dht11_HL byte define dht11_TH byte define dht11_TL byte define data_valid byte #Loop print "start" data_valid = 0 dht11_HH = 0 dht11_HL = 0 dht11_TH = 0 dht11_TL = 0 sys &H101 print "systemcall done" pause 300 print "dht11_HH:"; print dht11_HH; print "/dht11_HL:"; print dht11_HL; print "/dht11_TH:"; print dht11_TH; print "/dht11_TL:"; print dht11_TL; print "/data_valid:"; print data_valid goto Loop end syscode "dht11.s19" |
| Antwort schreiben |