Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * NissAdiba_MCU_Project.c
- *
- * Created: 25-Aug-19 1:33:28 AM
- * Author : Monmoy
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdlib.h>
- #include <string.h>
- #include "LCD16x2_4bit.h"
- //#include "USART_RS232_H_file.h" /* Include USART library */
- int prev_state1 = 0;
- int prev_state2 = 0;
- void USART_Init(unsigned long BAUDRATE) /* USART initialize function */
- {
- UCSRB |= (1 << RXEN) | (1 << TXEN); /* Enable USART transmitter and receiver */
- UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); /* Write USCRC for 8 bit data and 1 stop bit */
- UBRRL = BAUD_PRESCALE; /* Load UBRRL with lower 8 bit of prescale value */
- UBRRH = (BAUD_PRESCALE >> 8); /* Load UBRRH with upper 8 bit of prescale value */
- }
- char USART_RxChar() /* Data receiving function */
- {
- while (!(UCSRA & (1 << RXC))); /* Wait until new data receive */
- return(UDR); /* Get and return received data */
- }
- void USART_TxChar(char data) /* Data transmitting function */
- {
- UDR = data; /* Write data to be transmitting in UDR */
- while (!(UCSRA & (1<<UDRE))); /* Wait until data transmit and buffer get empty */
- }
- void USART_SendString(char *str) /* Send string of USART data function */
- {
- int i=0;
- while (str[i]!=0)
- {
- USART_TxChar(str[i]); /* Send each char of string till the NULL */
- i++;
- }
- }
- void ADC_Init()
- {
- DDRA = 0x0; /* Make ADC port as input */
- ADCSRA = 0x87; /* Enable ADC, freq:128 */
- }
- int ADC_Read()
- {
- ADMUX = 0x40; /* VRef: AVCC, ADC Channel: 0 */
- ADCSRA |= (1<<ADSC); /* Start Conversion */
- while ((ADCSRA &(1<<ADIF))==0); /* Monitor end of conversion interrupt flag */
- ADCSRA |=(1<<ADIF); /* Set the ADIF bit of ADCSRA register */
- return(ADCW); /* return the ADCW */
- }
- void motor_init()
- {
- DDRC = 0x01;
- PORTC = 0x00;
- }
- void motor_control(float moisture)
- {
- if(moisture < 25.0) {
- PORTC = 0x01;
- if( prev_state1 == 0)
- {
- //LED |= (1<<PB0); /* Turn ON LED */
- prev_state1 = 1;
- prev_state2 = 0 ;
- USART_SendString("MOTOR ON");
- }
- } else {
- PORTC = 0x00;
- if( prev_state2 == 0 )
- {
- //LED &= ~(1<<PB0); /* Turn OFF LED */
- prev_state2 = 1;
- prev_state1 = 0 ;
- USART_SendString("MOTOR OFF"); /* send status of LED i.e. LED OFF */
- }
- }
- }
- int main(void)
- {
- USART_Init(9600);
- lcdinit(); /* initialize the 16x2 LCD */
- lcd_clear(); /* clear the LCD */
- ADC_Init(); /* initialize the ADC */
- char array[10];
- int adc_value;
- float moisture;
- while (1)
- {
- adc_value = ADC_Read(); /* Copy the ADC value */
- moisture = (adc_value*100.00)/1023.00; /* Calculate moisture in % */
- moisture = 100.0 - moisture;
- lcd_gotoxy(0,0);
- lcd_print("Moisture: ");
- dtostrf(moisture,3,2,array);
- strcat(array,"% "); /* Concatenate unit of % */
- lcd_gotoxy(0,1); /* set column and row */
- lcd_print(array); /* Print moisture on second row */
- memset(array,0,10);
- motor_control(moisture); /* Motor On-Off Control */
- _delay_ms(300);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement