Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XTAL_FREQ 16000000
- #define RS RD2
- #define EN RD3
- #define D4 RD4
- #define D5 RD5
- #define D6 RD6
- #define D7 RD7
- // CONFIG1
- #pragma config FOSC = EXTRC_CLKOUT// Oscillator Selection bits (RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
- #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 Reset Selection bits (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 LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
- // CONFIG2
- #pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
- #pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
- // #pragma config statements should precede project file includes.
- // Use project enums instead of #define for ON and OFF.
- #include <xc.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include "lcd.h"
- #define DHT11_Data_Pin PORTBbits.RB0
- #define DHT11_Data_Pin_Direction TRISBbits.TRISB0
- unsigned char Check_bit,Temp_byte_1,Temp_byte_2,RH_byte_1,RH_byte_2;
- unsigned char temper,RH,Sumation;
- void dht11_init(){
- DHT11_Data_Pin_Direction= 0;
- DHT11_Data_Pin = 0;
- __delay_ms(18);
- DHT11_Data_Pin = 1;
- __delay_us(30);
- DHT11_Data_Pin_Direction = 1;
- }
- void find_response(){
- Check_bit = 0;
- __delay_us(40);
- if (DHT11_Data_Pin == 0){
- __delay_us(80);
- if (DHT11_Data_Pin == 1){
- Check_bit = 1;
- }
- __delay_us(50);}
- }
- char read_dht11(){
- char data, for_count;
- for(for_count = 0; for_count < 8; for_count++){
- while(!DHT11_Data_Pin);
- __delay_us(30);
- if(DHT11_Data_Pin == 0){
- data&= ~(1<<(7 - for_count));
- }
- else{
- data|= (1 << (7 - for_count));
- while(DHT11_Data_Pin);
- }
- }
- return data;
- }
- char Temp[] = "00.0 C";
- char Hum[] = "00.0 %";
- int a, b,c, d, e;
- void ReadHumidity(){
- dht11_init();
- find_response();
- if(Check_bit == 1){
- RH_byte_1 = read_dht11();
- RH_byte_2 = read_dht11();
- Temp_byte_1 = read_dht11();
- Temp_byte_2 = read_dht11();
- Sumation = read_dht11();
- if(Sumation == ((RH_byte_1+RH_byte_2+Temp_byte_1+Temp_byte_2) & 0XFF)){
- RH = RH_byte_1;
- Lcd_Set_Cursor(1,1);
- Lcd_Write_String("RH: ");
- e= RH * 100; //2523
- a = (e/1000) % 10; //2
- b = (e/100) %10; //5
- c = (e/10) %10; //2
- d = (e/1) %10; //3
- Hum[0] = a + '0';
- Hum[1] = b + '0';
- Hum[3] = c + '0';
- Hum[4] = d + '0';
- Lcd_Set_Cursor(1,4);
- Lcd_Write_String(Hum);
- } else {
- Lcd_Set_Cursor(1,1);
- Lcd_Write_String("Check sum error");
- }
- }
- }
- //I2C INIT
- //CONFIG I2C
- #define SDA_TRIS TRISCbits.TRISC4
- #define SCL_TRIS TRISCbits.TRISC3
- #define SDA RC4
- #define SCL RC3
- #define TSL2561_ADDR 0x93 // Change this to your sensor's I2C address if different
- void I2C_Init() {
- // Set Serial Clock Rate (SCL) frequency
- SSPADD = (_XTAL_FREQ / (100000 * 4)) - 1; // Calculate and set value for SSPCON2.CKP
- // Configure I2C Master mode
- SSPCONbits.SSPM = 0x08; // Set Master mode (I2C Master)
- SSPSTATbits.CKE = 1; // Enable Serial Clock (SCL)
- // Slew Rate Control (SRCP) can be adjusted for optimal performance if needed
- // SSPCON2bits.SRCP = 0; // Set Slew Rate Control (default)
- // Enable Serial Port (SSP)
- SCL_TRIS = 1; // Set SCL pin (RC3) as input
- SDA_TRIS = 1; // Set SDA pin (RC4) as input (precautionary for open-drain configuration)
- SSPCONbits.SSPEN = 1; // Enable Serialized Synchronous Port (SSP)
- }
- void I2C_Start() {
- // Initiate start condition
- SSPCON2bits.SEN = 1;
- // Wait for start condition to complete
- while (SSPCON2bits.SEN);
- }
- // Function to send a stop condition on the I2C bus
- void I2C_Stop() {
- // Send stop condition
- SSPCON2bits.PEN = 1;
- // Wait for stop condition to complete
- while (SSPCON2bits.PEN);
- }
- // Function to send a byte of data on the I2C bus
- void I2C_WriteByte(unsigned char data) {
- // Write data byte to SSPBUF register
- SSPBUF = data;
- // Wait for transmission to complete
- while (SSPSTATbits.BF);
- }
- // Function to read a byte of data from the I2C bus with acknowledgement
- unsigned char I2C_ReadByte(void) {
- // Initiate a read with acknowledgement
- SSPCON2bits.RCEN = 1;
- // Wait for reception to complete
- while (SSPSTATbits.BF);
- // Return the received data
- return SSPBUF;
- }
- // Function to read a byte of data from the I2C bus without acknowledgement
- void I2C_ReadByteNoAck(void) {
- // Initiate a read without acknowledgement (NACK)
- SSPCON2bits.RCEN = 1;
- SSPCON2bits.ACKDT = 0; // Set Acknowledge Data bit to 0 (NACK)
- // Wait for reception to complete
- while (SSPSTATbits.BF);
- }
- //LUMINOSITY FUNCTION
- float convert_to_lux(unsigned int adc_value) {
- // Calibration data (replace with values from datasheet or experiments)
- const float lux_per_adc = 10.0f; // Example: 10 lux per ADC unit (adjust based on datasheet)
- // Conversion
- float lux_value = adc_value * lux_per_adc;
- return lux_value;
- }
- void intToStr(int num, char *str) {
- sprintf(str, "%g", num);
- }
- void main(void) {
- TRISD = 0x00;
- ANSELH = 0x02; // Configure RB1 as analog input
- TRISB = 0x02; // Set RB1 as input
- TRISC = 0x0F; // Set RC0-RC3 as input, RC4-RC7 as output
- ANSEL = 0x00;
- Lcd_Init();
- char str[10];
- I2C_Init();
- I2C_Start(); //Start condition
- I2C_WriteByte(0x92);// Write slave address (0x93 for TSL2561) with write bit (0)
- __delay_ms(50);
- I2C_WriteByte(0xC0); // Example: Write register address (replace with desired register)
- I2C_Stop();
- I2C_Start();
- I2C_WriteByte(0x93);
- unsigned char data = I2C_ReadByte();
- I2C_ReadByteNoAck(); // Send NACK to signal end of read operation
- I2C_Stop();
- while(1) {
- ReadHumidity();
- __delay_ms(200);
- Lcd_Set_Cursor(2,1);
- Lcd_Write_String("Lum: ");
- Lcd_Write_String(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement