Oh wow... Thank you guys for lightning fast reply and being so helpful!!!
Actually, I want to learn how to program PIC MCUs but I have so limited time... I read a lot and also get help from chatGPT (not so useful or I cant understand)...
I also used the DHT11 temp and humidity sensor instead of LM35 because I don't know how to process analog data yet. Sorry for confusion.
Anyway, I worked on it for a while and this came out:
Code:
/*
* File: test.c
*/
#include <xc.h>
#include <pic12f683.h>
// PIC12F683 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Detect (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 4000000 // Assuming 4MHz oscillator frequency
#define DHT11_PIN GP0 // Pin connected to DHT11 sensor
#define LED_GREEN GP1 // Green LED pin
#define LED_RED GP2 // Red LED pin
#define HEATING_ELEMENT GP5 // Pin connected to heating element
#define TEMP_LOWER_LIMIT 35
#define TEMP_UPPER_LIMIT 40
// Function prototypes
void dht11_start();
uint8_t dht11_read();
void readDHT11(uint8_t *temp, uint8_t *hum);
void controlHeating(uint8_t temperature);
void main(void) {
uint8_t temperature, humidity;
TRISIO = 0x01; // Set GP0 as input (Temperature Sensor Data), others as output
GPIO = 0x00; // All output pins low initially
while (1) {
readDHT11(&temperature, &humidity);
controlHeating(temperature);
// Implement LED control logic here based on temperature if needed
}
}
void dht11_start() {
TRISIO &= ~(1 << DHT11_PIN); // Set DHT11_PIN as output
GPIO &= ~(1 << DHT11_PIN); // Pull the data line low
__delay_ms(18); // Hold the line low for at least 18ms
GPIO |= (1 << DHT11_PIN); // Release the line
__delay_us(30); // Wait for DHT11 response
TRISIO |= (1 << DHT11_PIN); // Set DHT11_PIN as input to read sensor response
}
uint8_t dht11_read() {
uint8_t data = 0;
for (int i = 0; i < 8; i++) {
while (!(GPIO & (1 << DHT11_PIN))); // Wait for the data line to go high (start of data)
__delay_us(30); // Wait for 30us to ensure it's a valid bit
if (!(GPIO & (1 << DHT11_PIN))) {
// If the line is still low after waiting, it's a '0' bit
data <<= 1; // Shift left by 1 bit
} else {
// If the line is high, it's a '1' bit
data |= 0x01; // OR operation with 0x01
data <<= 1; // Shift left by 1 bit
while (GPIO & (1 << DHT11_PIN)); // Wait for the line to go low (end of data)
}
}
return data;
}
void readDHT11(uint8_t *temp, uint8_t *hum) {
dht11_start(); // Initialize communication
if (!(GPIO & (1 << DHT11_PIN))) {
__delay_us(80);
if (GPIO & (1 << DHT11_PIN)) {
__delay_us(80);
*hum = dht11_read(); // Read humidity
*temp = dht11_read(); // Read temperature
uint8_t checksum = dht11_read(); // Read checksum
if (checksum != (*hum + *temp)) {
// Checksum doesn't match data
// Handle error or retry reading
}
}
}
}
void controlHeating(uint8_t temperature) {
if (temperature < TEMP_LOWER_LIMIT) {
// Turn on heating element
GPIO |= (1 << HEATING_ELEMENT);
} else if (temperature > TEMP_UPPER_LIMIT) {
// Turn off heating element
GPIO &= ~(1 << HEATING_ELEMENT);
}
}
I compiled the code both on MPLAB IDE X and Proteus 8 Professional, with no errors. Also proteus simulation works with no errors. I know no errors do not mean it is gonna function as I wish but... After getting errors for days, it is a thing
And I am a rookie on Proteus.
For now, I didn't add LEDs both in the code and Proteus, I also attach my Proteus file.
THANK YOU FOR SPENDING TIME!
NOTE: I can't attach pdsprj file, zipping it.