370
void main(void) { // Initialize ports TRISB = 0b00000001; // RB0 as input for DHT11 TRISC = 0b10000000; // Set RC6 as output for UART TX ADCON1 = 0x06; // Configure PORTA pins as digital I/O // Timer1 configuration - 1:2 prescaler (Timer1 clock = 1MHz at 8MHz crystal) T1CON = 0x10; TMR1H = 0; TMR1L = 0; // Initialize UART UART_TX_Init(); __delay_ms(1000); // Initial delay while(1) { Start_Signal(); // Send start signal if(Check_Response()) { // If sensor responds // Read all data bytes if(Read_Data(&RH_Byte1) || Read_Data(&RH_Byte2) || Read_Data(&T_Byte1) || Read_Data(&T_Byte2) || Read_Data(&CheckSum)) { UART_Write_Text("Sensor read timeout!\n\r"); } else { // If no timeout occurred if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)) { // Update message strings Temperature[7] = T_Byte1/10 + '0'; Temperature[8] = T_Byte1%10 + '0'; Temperature[10] = T_Byte2/10 + '0'; Humidity[7] = RH_Byte1/10 + '0'; Humidity[8] = RH_Byte1%10 + '0'; Humidity[10] = RH_Byte2/10 + '0'; // Send via UART UART_Write_Text(Temperature); UART_Write_Text("\n\r"); UART_Write_Text(Humidity); UART_Write_Text("\n\r"); } else { UART_Write_Text("Checksum Error!\n\r"); } } } else { UART_Write_Text("No response from sensor\n\r"); } TMR1ON = 0; // Disable Timer1 __delay_ms(1000); // Wait 1 second before next reading } }