Advertisement
Cosy_13

PIC Project Degree

Apr 17th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | Source Code | 0 0
  1. #define _XTAL_FREQ 16000000
  2.  
  3. #define RS RD2
  4. #define EN RD3
  5. #define D4 RD4
  6. #define D5 RD5
  7. #define D6 RD6
  8. #define D7 RD7
  9.  
  10.  
  11.  
  12. // CONFIG1
  13. #pragma config FOSC = EXTRC_CLKOUT// Oscillator Selection bits (RC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, RC on RA7/OSC1/CLKIN)
  14. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  15. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  16. #pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
  17. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  18. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  19. #pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
  20. #pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
  21. #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
  22. #pragma config LVP = ON         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
  23.  
  24. // CONFIG2
  25. #pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  26. #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  27.  
  28. // #pragma config statements should precede project file includes.
  29. // Use project enums instead of #define for ON and OFF.
  30.  
  31. #include <xc.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "lcd.h"
  36.  
  37. #define DHT11_Data_Pin   PORTBbits.RB0
  38. #define DHT11_Data_Pin_Direction  TRISBbits.TRISB0
  39.  
  40. unsigned char Check_bit,Temp_byte_1,Temp_byte_2,RH_byte_1,RH_byte_2;
  41. unsigned char temper,RH,Sumation;
  42.  
  43. void dht11_init(){
  44.  DHT11_Data_Pin_Direction= 0;
  45.  DHT11_Data_Pin = 0;
  46.  __delay_ms(18);
  47.  DHT11_Data_Pin = 1;
  48.  __delay_us(30);
  49.  DHT11_Data_Pin_Direction = 1;
  50.  }
  51.  
  52.  void find_response(){
  53.  Check_bit = 0;
  54.  __delay_us(40);
  55.  if (DHT11_Data_Pin == 0){
  56.  __delay_us(80);
  57.  if (DHT11_Data_Pin == 1){
  58.     Check_bit = 1;
  59.  }    
  60.  __delay_us(50);}
  61.  }
  62.  
  63.  
  64.  char read_dht11(){
  65.  char data, for_count;
  66.  for(for_count = 0; for_count < 8; for_count++){
  67.      while(!DHT11_Data_Pin);
  68.     __delay_us(30);
  69.     if(DHT11_Data_Pin == 0){
  70.         data&= ~(1<<(7 - for_count));
  71.     }
  72.     else{
  73.         data|= (1 << (7 - for_count));
  74.         while(DHT11_Data_Pin);
  75.     }
  76.     }
  77.  return data;
  78.  }
  79.  
  80. char Temp[] = "00.0  C";
  81. char Hum[] = "00.0  %";
  82. int a, b,c, d, e;
  83.  
  84. void ReadHumidity(){
  85.       dht11_init();
  86.       find_response();
  87.    
  88.       if(Check_bit == 1){
  89.         RH_byte_1 = read_dht11();
  90.         RH_byte_2 = read_dht11();
  91.         Temp_byte_1 = read_dht11();
  92.         Temp_byte_2 = read_dht11();
  93.         Sumation = read_dht11();
  94.         if(Sumation == ((RH_byte_1+RH_byte_2+Temp_byte_1+Temp_byte_2) & 0XFF)){
  95.             RH = RH_byte_1;                        
  96.        
  97.             Lcd_Set_Cursor(1,1);            
  98.             Lcd_Write_String("RH: ");
  99.            
  100.             e= RH * 100; //2523
  101.             a = (e/1000) % 10; //2
  102.             b = (e/100) %10;  //5
  103.             c = (e/10) %10;  //2
  104.             d = (e/1) %10; //3
  105.        
  106.             Hum[0] = a + '0';
  107.             Hum[1] = b + '0';
  108.             Hum[3] = c + '0';
  109.             Hum[4] = d + '0';
  110.             Lcd_Set_Cursor(1,4);
  111.             Lcd_Write_String(Hum);
  112.  
  113.        } else {
  114.             Lcd_Set_Cursor(1,1);
  115.             Lcd_Write_String("Check sum error");
  116.       }
  117.     }
  118. }
  119.  
  120. //I2C INIT
  121. //CONFIG I2C
  122. #define SDA_TRIS   TRISCbits.TRISC4
  123. #define SCL_TRIS   TRISCbits.TRISC3
  124. #define SDA    RC4
  125. #define SCL    RC3
  126. #define TSL2561_ADDR 0x93 // Change this to your sensor's I2C address if different
  127. void I2C_Init() {
  128.     // Set Serial Clock Rate (SCL) frequency
  129.     SSPADD = (_XTAL_FREQ / (100000 * 4)) - 1; // Calculate and set value for SSPCON2.CKP
  130.  
  131.     // Configure I2C Master mode
  132.     SSPCONbits.SSPM = 0x08; // Set Master mode (I2C Master)
  133.     SSPSTATbits.CKE = 1;     // Enable Serial Clock (SCL)
  134.  
  135.     // Slew Rate Control (SRCP) can be adjusted for optimal performance if needed
  136.     // SSPCON2bits.SRCP = 0;  // Set Slew Rate Control (default)
  137.  
  138.     // Enable Serial Port (SSP)
  139.     SCL_TRIS = 1; // Set SCL pin (RC3) as input
  140.     SDA_TRIS = 1; // Set SDA pin (RC4) as input (precautionary for open-drain configuration)
  141.     SSPCONbits.SSPEN = 1;   // Enable Serialized Synchronous Port (SSP)
  142. }
  143. void I2C_Start() {
  144.     // Initiate start condition
  145.     SSPCON2bits.SEN = 1;
  146.  
  147.     // Wait for start condition to complete
  148.     while (SSPCON2bits.SEN);
  149. }
  150.  
  151. // Function to send a stop condition on the I2C bus
  152. void I2C_Stop() {
  153.     // Send stop condition
  154.     SSPCON2bits.PEN = 1;
  155.  
  156.     // Wait for stop condition to complete
  157.     while (SSPCON2bits.PEN);
  158. }
  159. // Function to send a byte of data on the I2C bus
  160. void I2C_WriteByte(unsigned char data) {
  161.     // Write data byte to SSPBUF register
  162.     SSPBUF = data;
  163.  
  164.     // Wait for transmission to complete
  165.     while (SSPSTATbits.BF);
  166. }
  167.  
  168. // Function to read a byte of data from the I2C bus with acknowledgement
  169. unsigned char I2C_ReadByte(void) {
  170.     // Initiate a read with acknowledgement
  171.     SSPCON2bits.RCEN = 1;
  172.  
  173.     // Wait for reception to complete
  174.     while (SSPSTATbits.BF);
  175.  
  176.     // Return the received data
  177.     return SSPBUF;
  178. }
  179. // Function to read a byte of data from the I2C bus without acknowledgement
  180. void I2C_ReadByteNoAck(void) {
  181.     // Initiate a read without acknowledgement (NACK)
  182.     SSPCON2bits.RCEN = 1;
  183.     SSPCON2bits.ACKDT = 0; // Set Acknowledge Data bit to 0 (NACK)
  184.  
  185.     // Wait for reception to complete
  186.     while (SSPSTATbits.BF);
  187. }
  188. //LUMINOSITY FUNCTION
  189.  
  190. float convert_to_lux(unsigned int adc_value) {
  191.     // Calibration data (replace with values from datasheet or experiments)
  192.     const float lux_per_adc = 10.0f; // Example: 10 lux per ADC unit (adjust based on datasheet)
  193.  
  194.     // Conversion
  195.     float lux_value = adc_value * lux_per_adc;
  196.  
  197.     return lux_value;
  198. }
  199.  
  200. void intToStr(int num, char *str) {
  201.     sprintf(str, "%g", num);
  202. }
  203. void main(void) {
  204.  
  205.     TRISD = 0x00;
  206.     ANSELH = 0x02; // Configure RB1 as analog input
  207.     TRISB = 0x02; // Set RB1 as input
  208.     TRISC = 0x0F; // Set RC0-RC3 as input, RC4-RC7 as output
  209.     ANSEL = 0x00;
  210.     Lcd_Init();
  211.     char str[10];
  212.  
  213.     I2C_Init();
  214.     I2C_Start();      //Start condition
  215.     I2C_WriteByte(0x92);// Write slave address (0x93 for TSL2561) with write bit (0)
  216.     __delay_ms(50);
  217.     I2C_WriteByte(0xC0); // Example: Write register address (replace with desired register)
  218.     I2C_Stop();
  219.     I2C_Start();   
  220.     I2C_WriteByte(0x93);
  221.     unsigned char data = I2C_ReadByte();
  222.     I2C_ReadByteNoAck();    // Send NACK to signal end of read operation
  223.     I2C_Stop();
  224.    
  225.     while(1) {
  226.         ReadHumidity();
  227.         __delay_ms(200);
  228.    
  229.    
  230.    
  231.         Lcd_Set_Cursor(2,1);            
  232.         Lcd_Write_String("Lum: ");
  233.         Lcd_Write_String(str);
  234.  
  235.      
  236.      
  237.   }
  238. }
  239.  
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement