Microcontroller › AVR › Problem in Programming——AVR——-
- This topic has 8 replies, 6 voices, and was last updated 5 years, 9 months ago by Saatwik.
-
AuthorPosts
-
June 20, 2017 at 5:53 am #4670ManojParticipantHi 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 leveli:e Full, Half, 1/4Full: when PB.3, 4, 5 are highHalf: when PB. 4, 5 high1/4: when only PB.5 highif no switch is pressed start all three relays and buzzer and show massage tank is start fillingProblems:1. the massaege on lcd is not satble it repets again and again.2. massage is not displaying in order i want3. i want order i:ePB.3 = 1PB.5 = 1PB.5 = 1TANK IS FULLPB.3 = 0PB.5 = 1PB.5 = 1TANK IS HALFPB.3 = 0PB.5 = 0PB.5 = 1TANK IS 1/4PB.3 = 0PB.5 = 0PB.5 = 0TANK IS EMply ,satrts Relays and BuzzerPlz 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 linePB.0 = RSPB.1 = R/wPB.2 = ENPB.3 = Switch 1PB.4 = Switch 2PB.5 = Switch 3PC.1 = BuzzerPC.2 = Relay 1PC.3 = Relay 2PC.4 = Relay 3 */int main(){DDRD = 0xFF; //set PORTD as out put 8 bit lcd data lineDDRB = 0b00000111; //Set PB.0,1 and 2 as OutputDDRC = 0b11111111; //PORTC as outputinit_lcd();_delay_ms(1000);lcd_cmd(0x80); //Goto Line-1,first positionlcd_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 ONlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_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 ONlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_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 ONlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_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 LCDvoid lcd_cmd(unsigned char command){PORTD = command; //Put command on the Data BusPORTB = 0b00000100; //Enable LCD for command writing_delay_ms(50);PORTB = 0b00000000; //Disable LCD again_delay_ms(50);}//Function for sending Data to LCDvoid lcd_data(unsigned char data){PORTD= data; //Put data on Data BusPORTB = 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 LCDvoid lcd_send_string(char* string){while(*string){lcd_data(*string); //Send value of pointer as data to LCDstring++; //Increment string pointer}}//Function to InitiALLISE LCDvoid init_lcd(){lcd_cmd(0x38); //Setup both lines of LCDlcd_cmd(0x0E); //Set Cursor off – Enable LCDlcd_cmd(0x01); //Clear Screenlcd_cmd(0x80); //Goto first position}June 20, 2017 at 11:44 am #14617Hari Prasaath KParticipantOn what basis your saying the LCD is not stale, whether LCD is blinking. whether you are using push button for testing?
Here you have given the conditions, based on the condition only it will display. Not able to get your question in what manner you are saying.
to display in order. give the conditions one by one and test.
June 23, 2017 at 4:57 am #14618ManojParticipantThanks Mr. Hari for Replying, i am saying that when it goes in any if case the message shows again and again, like if all three Ports are hight (PB3,4,5) it should show message on screen “TANK IS FULL”, it is showing message correctly but LCD cleares after some time and again this message starts typing on screen
And Yes currently i am using Switches for I/P and i just want to display message if any switch is pressed and starts relay and buzzer when no switch is pressed,
After Posting this Forum i hv done some R&D on my Code, and now Code is
#include
#define F_CPU 1000000UL
#include/*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 = 0xFF; //PORTC as output
PORTB = 0x00;
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 OFF
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”);
_delay_ms(1000);
}
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 OFF
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”);
_delay_ms(1000);
}
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 OFF
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”);
_delay_ms(1000);
}
else if(bit_is_clear(PINB,PB3) && bit_is_clear(PINB,PB4) && bit_is_clear(PINB,PB5)) // No Switch is Pressed
{
PORTC=PORTC & 0xFF; // All 3 Relay and Buzzer ON
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”);
_delay_ms(1000);
}
}
}//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
}June 23, 2017 at 11:12 am #14619Hari Prasaath KParticipantPlease attach the code in the aligned format.
June 24, 2017 at 5:13 am #14620ManojParticipantI am saying that when it goes in any if case the message shows again and again,
like if all three Ports are hight (PB3,4,5) it should show message on screen "TANK IS FULL",it is showing message correctly but LCD cleares after some time and again this message starts typing on screenYes currently i am using Switches for I/Pi just want to display message if any switch is pressed and starts relay and buzzer when no switch is pressedAfter Posting this Forum i hv done some R&D on my Code, and now Code is#include <avr/io.h>#define F_CPU 1000000UL#include <util/delay.h>/*PORTD = LCD Data linePB.0 = RSPB.1 = R/wPB.2 = ENPB.3 = Switch 1PB.4 = Switch 2PB.5 = Switch 3PC.1 = BuzzerPC.2 = Relay 1PC.3 = Relay 2PC.4 = Relay 3 */int main(){DDRD = 0xFF; //set PORTD as out put 8 bit lcd data lineDDRB = 0b00000111; //Set PB.0,1 and 2 as OutputDDRC = 0xFF; //PORTC as outputPORTB = 0x00;init_lcd();_delay_ms(1000);lcd_cmd(0x80); //Goto Line-1,first positionlcd_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 OFFlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_send_string("Tank is ");_delay_ms(100);lcd_cmd(0xC0); //Goto Line-2, first position_delay_ms(100);lcd_send_string("Full");_delay_ms(1000);}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 ONlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_send_string("TANK IS");_delay_ms(100);lcd_cmd(0xC0); //Goto Line-2, first position_delay_ms(100);lcd_send_string("HALF");_delay_ms(1000);}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 ONlcd_cmd(0x01); //Clear the lcd_delay_ms(100);lcd_cmd(0x80); //Goto Line-1,first positionlcd_send_string("TANK IS");_delay_ms(100);lcd_cmd(0xC0); //Goto Line-2, first position_delay_ms(100);lcd_send_string("1/4");_delay_ms(1000);}else if(bit_is_clear(PINB,PB3) && bit_is_clear(PINB,PB4) && bit_is_clear(PINB,PB5)) // No Switch is Pressed{PORTC=PORTC & 0xFF;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");_delay_ms(1000);}}}//LCD function/*
*///Function for sending commands to LCDvoid lcd_cmd(unsigned char command){PORTD = command; //Put command on the Data BusPORTB = 0b00000100; //Enable LCD for command writing_delay_ms(50);PORTB = 0b00000000; //Disable LCD again_delay_ms(50);}//Function for sending Data to LCDvoid lcd_data(unsigned char data){PORTD= data; //Put data on Data BusPORTB = 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 LCDvoid lcd_send_string(char* string){while(*string){lcd_data(*string); //Send value of pointer as data to LCDstring++; //Increment string pointer}}//Function to InitiALLISE LCDvoid init_lcd(){lcd_cmd(0x38); //Setup both lines of LCDlcd_cmd(0x0E); //Set Cursor off – Enable LCDlcd_cmd(0x01); //Clear Screenlcd_cmd(0x80); //Goto first position}June 29, 2017 at 4:39 am #14622GuruParticipantHi I am working with AT90USBKEY2 and I am new to it.. ie..AT90USB1287 chip…..Can someone please share some related programs…All I find is most of the programs related to Atmega16….Plesae share some link….ATmega324p is also fine,since it is almost similar to AT90USB1287….It will be of huge help…
July 16, 2017 at 12:23 pm #14631Ashutosh BhattParticipantfirst check whether you are getting exact 1 and 0 logic levels on your port pins?
it depends on your HW circuit? your all connections are OK?
July 28, 2017 at 5:54 pm #14638FarooghParticipantHi there is Problem in your Code
like following Finction
void lcd_send_string(char* string){while(*string){lcd_data(*string); //Send value of pointer as data to LCDstring++; //Increment string pointer}}use it like thisvoid lcd_send_string(char* string){int i=0;while(string != '