SparkFun Forums 

Where electronics enthusiasts find answers.

By Manoj027
#195231
Hi i am new in AVR, very minimum knowledge of C Programming.
i just want to make a project on simple watel level controller.
there are 3 wire which detect water level
i:e Full, Half, 1/4
Full: when PB.3, 4, 5 are high
Half: when PB. 4, 5 high
1/4: when only PB.5 high
if no switch is pressed start all three relays and buzzer and show massage tank is start filling

Problems:
1. the massaege on lcd is not satble it repets again and again.
2. massage is not displaying in order i want
3. i want order i:e
PB.3 = 1
PB.5 = 1
PB.5 = 1
TANK IS FULL

PB.3 = 0
PB.5 = 1
PB.5 = 1
TANK IS HALF

PB.3 = 0
PB.5 = 0
PB.5 = 1
TANK IS 1/4

PB.3 = 0
PB.5 = 0
PB.5 = 0
TANK IS EMply ,satrts Relays and Buzzer

Plz help me in programming, give me solid programming concept, like externel intrrupt or switch caes etc.
i cant chage ports, i have a dovelopments board so all ports are fixed


#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

/*PORTD = LCD Data line
PB.0 = RS
PB.1 = R/w
PB.2 = EN
PB.3 = Switch 1
PB.4 = Switch 2
PB.5 = Switch 3
PC.1 = Buzzer
PC.2 = Relay 1
PC.3 = Relay 2
PC.4 = Relay 3 */

int main()
{
DDRD = 0xFF; //set PORTD as out put 8 bit lcd data line
DDRB = 0b00000111; //Set PB.0,1 and 2 as Output
DDRC = 0b11111111; //PORTC as output
init_lcd();
_delay_ms(1000);
lcd_cmd(0x80); //Goto Line-1,first position
lcd_send_string("Wait...");
_delay_ms(100);
lcd_cmd(0xC0); //Goto Line-2, first position
_delay_ms(100);
lcd_send_string("Booting");
while (1)
{
PORTC = 0xFF;
if ((bit_is_set(PINB,PB3))&&(bit_is_set(PINB,PB4))&&(bit_is_set(PINB,PB5))) // All 3 Switch Pressed
{
PORTC=PORTC & 0x00; // All 3 Relay and Buzzer ON
lcd_cmd(0x01); //Clear the lcd
_delay_ms(100);
lcd_cmd(0x80); //Goto Line-1,first position
lcd_send_string("Tank is ");
_delay_ms(100);
lcd_cmd(0xC0); //Goto Line-2, first position
_delay_ms(100);
lcd_send_string("Full");
}
else if ((bit_is_clear(PINB,PB3))&&(bit_is_set(PINB,PB4))&&(bit_is_set(PINB,PB5))) // 2 Switch Pressed
{
PORTC=PORTC & 0x00; // All 3 Relay and Buzzer ON
lcd_cmd(0x01); //Clear the lcd
_delay_ms(100);
lcd_cmd(0x80); //Goto Line-1,first position
lcd_send_string("TANK IS");
_delay_ms(100);
lcd_cmd(0xC0); //Goto Line-2, first position
_delay_ms(100);
lcd_send_string("HALF");
}
else if ((bit_is_clear(PINB,PB3))&&(bit_is_clear(PINB,PB4))&&(bit_is_set(PINB,PB5))) // 1 Switch Pressed
{
PORTC=PORTC & 0x00; // All 3 Relay and Buzzer ON
lcd_cmd(0x01); //Clear the lcd
_delay_ms(100);
lcd_cmd(0x80); //Goto Line-1,first position
lcd_send_string("TANK IS");
_delay_ms(100);
lcd_cmd(0xC0); //Goto Line-2, first position
_delay_ms(100);
lcd_send_string("1/4");
}
else if ((bit_is_clear(PINB,PB3))&&(bit_is_clear(PINB,PB4))&&(bit_is_clear(PINB,PB5))) // No Switch is Pressed
{
PORTC=PORTC & 0xFF;
//PORTC = 0x00;
lcd_cmd(0x01); //Clear the lcd
_delay_ms(100);
lcd_send_string("TANK EMPTY");
_delay_ms(100);
lcd_cmd(0xC0); //Goto Line-2, first position
_delay_ms(100);
lcd_send_string("PUMP ON");
}
}
}

//LCD function
/*------------------------------------------------------------------------------------------------------------*/
//Function for sending commands to LCD
void lcd_cmd(unsigned char command)
{
PORTD = command; //Put command on the Data Bus
PORTB = 0b00000100; //Enable LCD for command writing
_delay_ms(50);
PORTB = 0b00000000; //Disable LCD again
_delay_ms(50);
}
//Function for sending Data to LCD
void lcd_data(unsigned char data)
{
PORTD= data; //Put data on Data Bus
PORTB = 0b00000101; //Set R/S (Register Select) to High, and Enable to High
_delay_ms(50);
PORTB = 0b00000000; //Disable LCD again
_delay_ms(50);
}
//Function to send String to LCD
void lcd_send_string(char* string)
{
while(*string)
{
lcd_data(*string); //Send value of pointer as data to LCD
string++; //Increment string pointer
}
}
//Function to InitiALLISE LCD
void init_lcd()
{
lcd_cmd(0x38); //Setup both lines of LCD
lcd_cmd(0x0E); //Set Cursor off - Enable LCD
lcd_cmd(0x01); //Clear Screen
lcd_cmd(0x80); //Goto first position
}
less