Microcontroller › AVR › gsm based water pump controller using atmega16 › Hi NK, I have
Hi NK,
I have written a code for this purpose. Actually these code were collected and rearrange from this site only. I have done small changes.
i have not tried this. can you please check this? Please tell me any any modifications are needed or not.
#define F_CPU 12000000UL // set CPU freq.
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) – 1)
#include<avr/io.h>
#include<util/delay.h>
#define LCD_DATA PORTA // LCD DATA PORT.
#define ctrl PORTB // CONTROL PORT FOR LCD.
#define en PB2 // ENABLE SINGNAL FOR LCD.
#define rw PB1 // READ/WRITE SIGNAL FOR LCD.
#define rs PB0 // REGISTER SELECTION SIGNAL FOR LCD.
#define MOTOR_CONTROL PORTC // MOTOR CONTROL PORT.
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();
void compare_string();
unsigned int final_result;
unsigned char msg[] = “”;
unsigned char on[]=”MTR ON”;
unsigned char off[]=”MTR OFF”;
int main(void);
{
while(1)
{
uart_init(); // initialize UART port.
usart_message(“ATrn”); // Sent AT into USART.
_delay_ms(10000);
usart_message(“AT+CMGF=1rn”); //Select mesage mode in text mode.
_delay_ms(1000);
usart_message(“AT+CPMS=’SM’rn”); // Select the mesage memory.
_delay_ms(1000);
usart_message(“AT+CMGR=1rn”); // Read the message from index 1.
usart_getch(msg[]);
compare_string();
final_result=fresult;
if(final_result==0)
{
PORTC=0x01;
usart_message(“ATrn”); // Sent AT into USART.
_delay_ms(10000);
usart_message(“AT+CMGF=1rn”); //Select mesage mode in text mode.
_delay_ms(1000);
usart_message(“AT+CPMS=’SM’rn”); // Select the mesage memory.
_delay_ms(1000);
usart_message(“AT+CMGS=+918197103408rn”); // SEND the message.
usart_message(“MOTER IS ON.”);
init_LCD();
LCD_write(“MOTOR IS ON.”);
}
if(final_result==1)
{
PORTC=0x00;
usart_message(“ATrn”); // Sent AT into USART.
_delay_ms(10000);
usart_message(“AT+CMGF=1rn”); //Select mesage mode in text mode.
_delay_ms(1000);
usart_message(“AT+CPMS=’SM’rn”); // Select the mesage memory.
_delay_ms(1000);
usart_message(“AT+CMGS=+918197103408rn”); // SEND the message.
usart_message(“MOTER IS OFF.”);
init_LCD();
LCD_write(“MOTER IS OFF.”);
}
}
}
LCD_write(“MOTER IS OFF.”);
void init_LCD(void) //Function to initialize LCD.
{
LCD_cmd(0x38);
mode
_delay_ms(1);
// initialization of 16X2 LCD in 8bit
LCD_cmd(0x01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0E); // cursor ON
_delay_ms(1);
LCD_cmd(0x80);
0th position
_delay_ms(1);
return;
}
void LCD_cmd(unsigned char cmd) //Function to LCD command.
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}
void LCD_write(unsigned char data) //Function to write a letter in LCD.
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}
void LCD_write_string(unsigned char *str) //Function to write a string in LCD.
value of the string in pointer *str
{
int i=0;
while(str!=’