Microcontroller › AVR › not getting accepected answer
- This topic has 2 replies, 2 voices, and was last updated 10 years, 10 months ago by Shashank Rao.C.
-
AuthorPosts
-
June 13, 2013 at 11:29 am #2481Shashank Rao.CParticipant
hi friends,
I stucked in new problem. I programed ATmega16 with following code.
#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>
#include<string.h>#define PUMP_CONTROL PORTC // MOTOR CONTROL PIN.
void usart_init();
void usart_putch(unsigned char send);
unsigned int usart_getch();
void usart_message(unsigned char *var);
void usart_recivemsg(unsigned char *msg);
void send_message(unsigned char *message_send);unsigned char msg[50] = “”;
unsigned char on[50]=”PUMP ON”;
unsigned char off[50]=”PUMP OFF”;int main(void)
{
while(1)
{
usart_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.
//_delay_ms(3000);
usart_recivemsg(msg);
//compare_string();
//final_result=fresult;
//usart_message(“AT+CMGD=1rn”);if(strcmp(on,msg) == 0)
{PUMP_CONTROL=0X03;
send_message(“PUMP IS ON”);
//init_LCD();}
else if(strcmp(off,msg) == 0)
{
PUMP_CONTROL=0x04;
send_message(“PUMP IS OFF.”);
}else
{
send_message(“INVALID MESSAGE FORMAT”); //DEFAULT MESAGE
}}
usart_message(“AT+CMGD=1rn”); //delete the messsage}
//***********************************FUNCTIONS**********************************************//
void usart_init() // Function to initialize USART.
{
UCSRB |= (1 << RXEN) | (1 << TXEN);
// Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 <<
UCSZ1);
// Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> ; // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}void usart_putch(unsigned char send) //
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be
//written to it
UDR = send; // Send the byte
}unsigned int usart_getch() //FUNCTION TO RECIVE A CHARATER.
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data have been
//received and is ready to be read from UDR
return UDR; // return the byte
}void usart_message(unsigned char *var)
{
int h;
while(*var)
usart_putch(*var++);
for(h=0;h<=200;h++); //sends characters one by one till string ends
}void usart_recivemsg(unsigned char *msg) //Function to recive a msg.
//value of the string in pointer *msg
{
int i=0;
while(msg!=’