;--------------------------------------------- ; 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: 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 GOTO ACTIVATE_HEATER MOVLW 0x1E ; Load 30°C into W SUBWF TEMP_VALUE, W ; Compare with temperature 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 ; 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
Standard input is empty
;--------------------------------------------- ; 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