SHARE
TWEET

Untitled

a guest Sep 11th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * NissAdiba_MCU_Project.c
  3.  *
  4.  * Created: 25-Aug-19 1:33:28 AM
  5.  * Author : Monmoy
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10.  
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. #include "LCD16x2_4bit.h"
  15. //#include "USART_RS232_H_file.h"       /* Include USART library */
  16.  
  17.  
  18. int prev_state1 = 0;
  19. int prev_state2 = 0;
  20.  
  21.  
  22. void USART_Init(unsigned long BAUDRATE)             /* USART initialize function */
  23. {
  24.     UCSRB |= (1 << RXEN) | (1 << TXEN);             /* Enable USART transmitter and receiver */
  25.     UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); /* Write USCRC for 8 bit data and 1 stop bit */
  26.     UBRRL = BAUD_PRESCALE;                          /* Load UBRRL with lower 8 bit of prescale value */
  27.     UBRRH = (BAUD_PRESCALE >> 8);                   /* Load UBRRH with upper 8 bit of prescale value */
  28. }
  29.  
  30. char USART_RxChar()                                 /* Data receiving function */
  31. {
  32.     while (!(UCSRA & (1 << RXC)));                  /* Wait until new data receive */
  33.     return(UDR);                                    /* Get and return received data */
  34. }
  35.  
  36. void USART_TxChar(char data)                        /* Data transmitting function */
  37. {
  38.     UDR = data;                                     /* Write data to be transmitting in UDR */
  39.     while (!(UCSRA & (1<<UDRE)));                   /* Wait until data transmit and buffer get empty */
  40. }
  41.  
  42. void USART_SendString(char *str)                    /* Send string of USART data function */
  43. {
  44.     int i=0;
  45.     while (str[i]!=0)
  46.     {
  47.         USART_TxChar(str[i]);                       /* Send each char of string till the NULL */
  48.         i++;
  49.     }
  50. }
  51.  
  52. void ADC_Init()
  53. {
  54.     DDRA = 0x0;     /* Make ADC port as input */
  55.     ADCSRA = 0x87;  /* Enable ADC, freq:128  */
  56. }
  57.  
  58. int ADC_Read()
  59. {
  60.     ADMUX = 0x40;                   /* VRef: AVCC, ADC Channel: 0 */
  61.     ADCSRA |= (1<<ADSC);            /* Start Conversion */
  62.     while ((ADCSRA &(1<<ADIF))==0); /* Monitor end of conversion interrupt flag */
  63.     ADCSRA |=(1<<ADIF);             /* Set the ADIF bit of ADCSRA register */
  64.     return(ADCW);                   /* return the ADCW */
  65. }
  66.  
  67. void motor_init()
  68. {
  69.     DDRC = 0x01;
  70.     PORTC = 0x00;
  71. }
  72.  
  73. void motor_control(float moisture)
  74. {
  75.     if(moisture < 25.0) {
  76.         PORTC = 0x01;
  77.         if( prev_state1 == 0)
  78.         {
  79.             //LED |= (1<<PB0);                          /* Turn ON LED */
  80.             prev_state1 = 1;
  81.             prev_state2 = 0 ;
  82.             USART_SendString("MOTOR ON");
  83.         }
  84.        
  85.     } else {
  86.         PORTC = 0x00;  
  87.          if( prev_state2 == 0 )
  88.         {
  89.             //LED &= ~(1<<PB0);                         /* Turn OFF LED */
  90.             prev_state2 = 1;
  91.             prev_state1 = 0 ;
  92.             USART_SendString("MOTOR OFF");              /* send status of LED i.e. LED OFF */
  93.         }
  94.     }
  95. }
  96.  
  97. int main(void)
  98. {
  99.     USART_Init(9600);
  100.    
  101.    
  102.     lcdinit();      /* initialize the 16x2 LCD */
  103.     lcd_clear();    /* clear the LCD */
  104.     ADC_Init();     /* initialize the ADC */
  105.    
  106.     char array[10];
  107.     int adc_value;
  108.     float moisture;
  109.    
  110.     while (1)
  111.     {
  112.         adc_value = ADC_Read(); /* Copy the ADC value */
  113.         moisture = (adc_value*100.00)/1023.00; /* Calculate moisture in % */
  114.         moisture = 100.0 - moisture;
  115.        
  116.         lcd_gotoxy(0,0);
  117.         lcd_print("Moisture: ");
  118.         dtostrf(moisture,3,2,array);
  119.         strcat(array,"%   ");   /* Concatenate unit of % */
  120.         lcd_gotoxy(0,1);        /* set column and row */
  121.         lcd_print(array);       /* Print moisture on second row */
  122.         memset(array,0,10);
  123.        
  124.         motor_control(moisture); /* Motor On-Off Control */
  125.        
  126.         _delay_ms(300);
  127.     }
  128. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top