fork download
  1. ;---------------------------------------------
  2. ; PIC16F887 Assembly Code
  3. ;
  4. ; Converted by: ChatGPT
  5. ;
  6. LIST P=16F887
  7. #INCLUDE <P16F887.INC> ; Include PIC16F887 definitions
  8. __CONFIG _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON
  9.  
  10. ;---------------------------------------------
  11. ; Define constants
  12. F_CPU EQU 16000000 ; Clock frequency (16 MHz)
  13. LM75_ADDR EQU 0x90 ; I2C Address for LM75
  14. TEMP_REG EQU 0x00 ; LM75 Temperature register
  15. WRITE_BIT EQU 0 ; Write bit
  16. READ_BIT EQU 1 ; Read bit
  17.  
  18. ;---------------------------------------------
  19. ; Reset Vector
  20. ORG 0x0000
  21. GOTO RESET
  22.  
  23. ;---------------------------------------------
  24. ; I2C Initialization
  25. I2C_INIT:
  26. MOVLW 0x36 ; Set baud rate for 100kHz @ 16MHz
  27. MOVWF SSPADD ; Load baud rate into SSPADD
  28. BCF SSPSTAT, SMP ; Disable Slew rate control for standard speed mode
  29. BSF SSPCON, SSPEN ; Enable I2C module
  30. RETLW 0
  31.  
  32. ;---------------------------------------------
  33. ; I2C Start Condition
  34. I2C_START:
  35. BSF SSPCON2, SEN ; Initiate Start condition
  36. WAIT_START:
  37. BTFSC SSPCON2, SEN ; Wait until Start condition is complete
  38. GOTO WAIT_START
  39. RETLW 0
  40.  
  41. ;---------------------------------------------
  42. ; I2C Send Slave Address + Read/Write
  43. I2C_SEND_ADDR:
  44. MOVWF SSPBUF ; Load address to SSPBUF for transmission
  45. WAIT_ADDR:
  46. BTFSS SSPSTAT, BF ; Wait until buffer is empty
  47. GOTO WAIT_ADDR
  48. RETLW 0
  49.  
  50. ;---------------------------------------------
  51. ; I2C Read Data
  52. I2C_READ_ACK:
  53. BSF SSPCON2, RCEN ; Enable receive mode
  54. WAIT_READ:
  55. BTFSS SSPSTAT, BF ; Wait for data to be received
  56. GOTO WAIT_READ
  57. MOVF SSPBUF, W ; Read data from buffer
  58. RETLW 0
  59.  
  60. ;---------------------------------------------
  61. ; I2C Stop Condition
  62. I2C_STOP:
  63. BSF SSPCON2, PEN ; Initiate Stop condition
  64. WAIT_STOP:
  65. BTFSC SSPCON2, PEN ; Wait until Stop condition is complete
  66. GOTO WAIT_STOP
  67. RETLW 0
  68.  
  69. ;---------------------------------------------
  70. ; Temperature Read Routine
  71. TEMP_READ:
  72. CALL I2C_START ; Send I2C START condition
  73. MOVLW LM75_ADDR + WRITE_BIT ; Load LM75 address with WRITE bit
  74. CALL I2C_SEND_ADDR ; Send slave address
  75. MOVLW TEMP_REG ; Point to temperature register
  76. CALL I2C_SEND_ADDR ; Send register pointer
  77. CALL I2C_START ; Restart communication (Repeated START)
  78. MOVLW LM75_ADDR + READ_BIT ; Load LM75 address with READ bit
  79. CALL I2C_SEND_ADDR ; Send slave address
  80. CALL I2C_READ_ACK ; Read temperature high byte
  81. CALL I2C_STOP ; Send I2C STOP condition
  82. RETLW 0
  83.  
  84. ;---------------------------------------------
  85. ; Main control routine for fan and heater
  86. CONTROL_LOGIC:
  87. MOVLW 0x19 ; Load 25°C into W
  88. SUBWF TEMP_VALUE, W ; Compare with temperature
  89. BTFSC STATUS, C ; If temp < 25°C, skip next line
  90. GOTO ACTIVATE_HEATER
  91.  
  92. MOVLW 0x1E ; Load 30°C into W
  93. SUBWF TEMP_VALUE, W ; Compare with temperature
  94. BTFSS STATUS, C ; If temp > 30°C, skip next line
  95. GOTO ACTIVATE_FAN
  96.  
  97. TURN_OFF_ALL:
  98. BCF PORTB, 0 ; Turn off heater
  99. BCF PORTB, 1 ; Turn off fan
  100. GOTO MAIN_LOOP
  101.  
  102. ACTIVATE_HEATER:
  103. BSF PORTB, 0 ; Turn on heater (PB0)
  104. BCF PORTB, 1 ; Turn off fan (PB1)
  105. GOTO MAIN_LOOP
  106.  
  107. ACTIVATE_FAN:
  108. BSF PORTB, 1 ; Turn on fan (PB1)
  109. BCF PORTB, 0 ; Turn off heater (PB0)
  110. GOTO MAIN_LOOP
  111.  
  112. ;---------------------------------------------
  113. ; Main Program
  114. ; Set port directions (PB0 and PB1 as output for heater/fan)
  115. MOVLW 0x03
  116. MOVWF TRISB ; Configure PB0 and PB1 as outputs
  117.  
  118. ; Initialize I2C
  119. CALL I2C_INIT
  120.  
  121. MAIN_LOOP:
  122. CALL TEMP_READ ; Read temperature from LM75
  123. CALL CONTROL_LOGIC ; Control heater/fan based on temperature
  124. GOTO MAIN_LOOP ; Loop forever
  125.  
  126.  
Success #stdin #stdout 0.02s 25952KB
stdin
Standard input is empty
stdout
;---------------------------------------------
; PIC16F887 Assembly Code
;
; Converted by: ChatGPT
;
    LIST    P=16F887
    #INCLUDE <P16F887.INC>            ; Include PIC16F887 definitions
    __CONFIG _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON

;---------------------------------------------
; Define constants
F_CPU        EQU     16000000         ; Clock frequency (16 MHz)
LM75_ADDR    EQU     0x90             ; I2C Address for LM75
TEMP_REG     EQU     0x00             ; LM75 Temperature register
WRITE_BIT    EQU     0                 ; Write bit
READ_BIT     EQU     1                 ; Read bit

;---------------------------------------------
; Reset Vector
    ORG     0x0000
    GOTO    RESET

;---------------------------------------------
; I2C Initialization
I2C_INIT:
    MOVLW   0x36                     ; Set baud rate for 100kHz @ 16MHz
    MOVWF   SSPADD                   ; Load baud rate into SSPADD
    BCF     SSPSTAT, SMP             ; Disable Slew rate control for standard speed mode
    BSF     SSPCON, SSPEN            ; Enable I2C module
    RETLW   0

;---------------------------------------------
; I2C Start Condition
I2C_START:
    BSF     SSPCON2, SEN             ; Initiate Start condition
WAIT_START:
    BTFSC   SSPCON2, SEN             ; Wait until Start condition is complete
    GOTO    WAIT_START
    RETLW   0

;---------------------------------------------
; I2C Send Slave Address + Read/Write
I2C_SEND_ADDR:
    MOVWF   SSPBUF                   ; Load address to SSPBUF for transmission
WAIT_ADDR:
    BTFSS   SSPSTAT, BF              ; Wait until buffer is empty
    GOTO    WAIT_ADDR
    RETLW   0

;---------------------------------------------
; I2C Read Data
I2C_READ_ACK:
    BSF     SSPCON2, RCEN            ; Enable receive mode
WAIT_READ:
    BTFSS   SSPSTAT, BF              ; Wait for data to be received
    GOTO    WAIT_READ
    MOVF    SSPBUF, W                ; Read data from buffer
    RETLW   0

;---------------------------------------------
; I2C Stop Condition
I2C_STOP:
    BSF     SSPCON2, PEN             ; Initiate Stop condition
WAIT_STOP:
    BTFSC   SSPCON2, PEN             ; Wait until Stop condition is complete
    GOTO    WAIT_STOP
    RETLW   0

;---------------------------------------------
; Temperature Read Routine
TEMP_READ:
    CALL    I2C_START                ; Send I2C START condition
    MOVLW   LM75_ADDR + WRITE_BIT    ; Load LM75 address with WRITE bit
    CALL    I2C_SEND_ADDR            ; Send slave address
    MOVLW   TEMP_REG                 ; Point to temperature register
    CALL    I2C_SEND_ADDR            ; Send register pointer
    CALL    I2C_START                ; Restart communication (Repeated START)
    MOVLW   LM75_ADDR + READ_BIT     ; Load LM75 address with READ bit
    CALL    I2C_SEND_ADDR            ; Send slave address
    CALL    I2C_READ_ACK             ; Read temperature high byte
    CALL    I2C_STOP                 ; Send I2C STOP condition
    RETLW   0

;---------------------------------------------
; Main control routine for fan and heater
CONTROL_LOGIC:
    MOVLW   0x19                     ; Load 25°C into W
    SUBWF   TEMP_VALUE, W            ; Compare with temperature
    BTFSC   STATUS, C                ; If temp < 25°C, skip next line
    GOTO    ACTIVATE_HEATER

    MOVLW   0x1E                     ; Load 30°C into W
    SUBWF   TEMP_VALUE, W            ; Compare with temperature
    BTFSS   STATUS, C                ; If temp > 30°C, skip next line
    GOTO    ACTIVATE_FAN

TURN_OFF_ALL:
    BCF     PORTB, 0                 ; Turn off heater
    BCF     PORTB, 1                 ; Turn off fan
    GOTO    MAIN_LOOP

ACTIVATE_HEATER:
    BSF     PORTB, 0                 ; Turn on heater (PB0)
    BCF     PORTB, 1                 ; Turn off fan (PB1)
    GOTO    MAIN_LOOP

ACTIVATE_FAN:
    BSF     PORTB, 1                 ; Turn on fan (PB1)
    BCF     PORTB, 0                 ; Turn off heater (PB0)
    GOTO    MAIN_LOOP

;---------------------------------------------
; Main Program
RESET:
    ; Set port directions (PB0 and PB1 as output for heater/fan)
    MOVLW   0x03
    MOVWF   TRISB                    ; Configure PB0 and PB1 as outputs

    ; Initialize I2C
    CALL    I2C_INIT

MAIN_LOOP:
    CALL    TEMP_READ                ; Read temperature from LM75
    CALL    CONTROL_LOGIC            ; Control heater/fan based on temperature
    GOTO    MAIN_LOOP                ; Loop forever