flat assembler
Message board for the users of flat assembler.

flat assembler > Non-x86 architectures > [x51] Deal 8051 with max6675

Author
Thread Post new topic Reply to topic
shoorick



Joined: 25 Feb 2005
Posts: 1606
Location: Ukraine
max6675 is a thermocoupler amplifier and A/D converter with SPI interface.
It is easy to read it with this procedures:
Code:
;=======================================================================
;  reads max6675 termocouple converter into B:A
;-----------------------------------------------------------------------
DATA TC_TMP 
;-----------------------------------------------------------------------
TC_SCK equ P3.1
TC_CS  equ P3.2
TC_SO  equ P3.0
TC_INT = 10
TC_FRA = 2
TC_BCD = 1 ; 0 - binary return, 1 - packed bcd return
;-----------------------------------------------------------------------
macro TC_SCK_CLOCK
    setb  TC_SCK
    clr   TC_SCK
    nop
end macro
;-----------------------------------------------------------------------
max_tc:
    setb  TC_CS
    setb  TC_SO
    clr   TC_SCK
    call  max_tc_delay
    clr   TC_CS        ; 15
    mov   TC_TMP,#1 ; chip is not responding
    jb    TC_SO,.error
    mov   TC_TMP,#TC_INT
;-----------------------------------------------------------------------
.loop:
    TC_SCK_CLOCK ; 14-5
    mov   C,TC_SO
    addc  A,ACC
if TC_BCD = 1
    da    A
end if    
    xch   A,B
    addc  A,ACC
if TC_BCD = 1
    da    A
end if    
    xch   A,B
    djnz  TC_TMP,.loop
;-----------------------------------------------------------------------
    mov   TC_TMP,#TC_FRA
.skip:
    TC_SCK_CLOCK ; 4-3 - only integer part is returning
    djnz  TC_TMP,.skip
;-----------------------------------------------------------------------
    mov   TC_TMP,#2 ; thermocouple is not connected
    TC_SCK_CLOCK ; 2
    jb    TC_SO,.error
;-----------------------------------------------------------------------
    mov   TC_TMP,#3 ; it is not max6675 
    TC_SCK_CLOCK ; 1
    jb    TC_SO,.error
;-----------------------------------------------------------------------
    TC_SCK_CLOCK ; 0
    mov   TC_TMP,#0 ; no error
    TC_SCK_CLOCK ; last bit is Z-state
    setb  TC_CS
    clr   C
    ret
;-----------------------------------------------------------------------
;   NB!: if the chip is connected, but has no power, 
;        then 0 is returned and no error is detected 
;-----------------------------------------------------------------------
.error:           ; on error sets carry and TC_TMP
    setb  TC_CS   ; if no TC connected chip returns "1023"
    setb  TC_SO
    setb  TC_SCK
    setb  C
    ret
;=======================================================================
;
;=======================================================================
max_tc_delay: ; clears A,B,TC_TMP
;-----------------------------------------------------------------------
    clr   TC_TMP
    clr   ACC
    mov   B,#4 ; 4*65536 us > 0.22 s (for 24MHz clock)
.loop:
    djnz  TC_TMP,.loop
    djnz  ACC,.loop
    djnz  B,.loop
;-----------------------------------------------------------------------
    ret
;=======================================================================
;    
;=======================================================================
spi_read:
    setb  TC_CS
    setb  TC_SO
    clr   TC_SCK
    call  max_tc_delay
    clr   TC_CS
    mov   TC_TMP,#16
.loop:
    mov   C,TC_SO
    rlc   A
    xch   A,B
    rlc   A
    xch   A,B
    TC_SCK_CLOCK 
    djnz  TC_TMP,.loop
    setb TC_CS
    setb TC_SCK
    ret
;=======================================================================    

It reads only integer part of returned value.
The TC_BCD flag changes type of generated code: if TC_BCD is one, then BCD value is returned by max_tc, thus there is no need in farther conversion to display, for thermostate purposes target temperature also may be entered in BCD and compared as usual binary numbers. In the case the program needs binary value, TC_BCD must be zero then.
In the case when max6675 is connected, but has no power, zero value may be returned - this value may be interpretated as zero degree temperature and lead to misworking of thermostate (overheat). To avoid this zero value should be treated as error.
Also, do not forget to make delay 250ms between reading to let A/D complete next conversion.

_________________
UNICODE forever!
Post 03 Jul 2018, 09:43
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.