SHARE
TWEET

Untitled

a guest Apr 5th, 2017 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * main.c
  3.  *
  4.  * Created: 2/23/2017 12:14:15 PM
  5.  * Author : usr510
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <stdio.h>
  10. #include "i2cmaster.h"
  11.  
  12. #define hih9131w  0x4E  //write mode
  13. #define hih9131r  0x4F  //read mode
  14.  
  15. void waterSense(void);
  16. void THSense(void);
  17.  
  18. unsigned int adc_result;
  19. unsigned int adc_H;
  20. unsigned int a;
  21. double adc_v;
  22. unsigned char humL;
  23. unsigned char humH;
  24. unsigned char tempL;
  25. unsigned char tempH;
  26.  
  27.  
  28. int main(void)
  29. {
  30.     //--Water Sensor Register Setup--
  31.     PRR = (0<<0);   //disable Power Reduction for ADC
  32.     ADCSRA = (1<<7) | (1<<6) | (1<<2) | (1<<1) | (1<<0);        //set ADC Enable and prescaler to 32 and start conversion
  33.     ADMUXA = 0x00;  //ADC input channel set to ADC0 (PA0)
  34.     ADMUXB = (0<<7) | (0<<6) | (0<<5);  //Vref = Vcc
  35.     DDRB = (1<<0); //PB0 to output
  36.     DDRA = (0<<3);
  37.     PORTA = (0<<3);
  38.     //-------------------------------
  39.    
  40.         i2c_init();                             // initialize I2C library
  41.         i2c_start_wait(hih9131w);               // set device address and write mode for MR
  42.         i2c_stop();                             // set stop condition = release bus
  43.         while(1) {
  44.             THSense();
  45.         }
  46. }
  47.  
  48. void waterSense(void) {
  49.     if(ADCSRA & 0x10) {
  50.         adc_result = ADCL;      //get ADCL value
  51.         adc_H = ADCH;           //get ADCH value
  52.         adc_result += adc_H;    //add them together
  53.         adc_v = ((double)adc_result*5.0)/1024.0;    //get voltage value
  54.         printf("%f", adc_v);
  55.         if(adc_v > 0.05)
  56.             PORTB = (1<<0); //output logic 1 in PORTB2
  57.         else
  58.             PORTB = (0<<0);
  59.         ADCSRA |= (1<<6) | (1<<4);  //clr ADIF and start conversion again
  60.     }
  61. }
  62.  
  63. void THSense(void) {
  64.     i2c_start_wait(hih9131r);       // set device address and read mode
  65.     humH = i2c_readAck();                    // read one byte from EEPROM
  66.     humL = i2c_readAck();
  67.     tempH = i2c_readAck();
  68.     tempL = i2c_readNak();
  69.     i2c_stop();
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top