Sign In

My Program Is Compiled Successsfully But Doesnot Display And Work

sisir regmi sisi_regmi078@live.com Norway

Hardware: Atmega2560 With Avr Software: Avr With Atmel Studio7

everthing seems fine but no output as intended

Images

Program


// Project 		Door Opening System (Embedded C)
// Target 		ATMEL ATmega2560 micro-controller on adriuno board
// Program		door_opening_system.c
// Author		sisir regmi


// Function		Opens door on pressing the push button

//				Configure PortL direction for Output, bits 0..2 as output
                      //for the leds to show the opening and closing process
//				Configure PortB direction for Output, bit 5 as output
                     //for the servo motor
					 
//				Configure PortD direction for input, bit 0 as Input
                    //key to press and give the first command to open and to interrupt
// 				Uses port A for Data and Command values (8 bits, Output)
                   //
//				The LCD 'device Busy' flag also uses bit 7 of port A
//				So sometimes this bit has to be set for Input

//				Uses port C for control (register select 'RS' bit 6 Output)
//										(device enable 'ENABLE' bit 7 Output)

//				Uses port G for control (write 'WR' bit 6 Output)
//										(read 'RD' bit 7 Output)


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include "libraryfiles.h"

void initTimer1();				//initialize timer1
void initGeneral();
void initUSART0();
void initHWint();

void USART_TX_Singlebyte(unsigned char byte);
void USART_TX_Multibyte(char *byte);


char buffer [33];
volatile char _switch = 0;


int main(void)
{

		initGeneral();
		initTimer1();					// set up Timer1

		initUSART0();					// initialize usart0
		initHWint();
		unsigned int counter = 750;		//counter variable is initialize at initial position where door is closed			
		unsigned int door_mem = 750;
		
		USART_TX_Multibyte("Welcome");
		
		lcd_Clear();					// Clear the display
		lcd_SetCursor(0x04);			// 0b00000010	Set cursor position to line 1, col 2
		lcd_WriteString("welcome");
		_switch = 0;
		
    
    while (1) 
    {
		if(_switch)					// if external interrupt switch is pressed 
		{
			PORTL = 0b00000000;						// set the output value in port l off
			USART_TX_Multibyte("Door is opening");
			PORTL = 0b00000010;						// set the output value in port l bit 1 On and other bit off 
			lcd_Clear();
			lcd_SetCursor(0x04);
			lcd_WriteString("OPENING");
			for(counter = door_mem; counter<2700; counter += 10)	// loop for opening the door by servo motor
			{
				OCR1A = counter;									// assigning OCR1A with the value of counter
				_delay_ms(10);
			}
			
			USART_TX_Multibyte("Door is fully opened");
			USART_TX_Multibyte("Timeout Started");
			PORTL = 0b00000100;							// set the output value in port l bit 2 On and other bit off
			lcd_Clear();
			lcd_SetCursor(0x04);
			lcd_WriteString("OPENED");
			_delay_ms(3000);
			_switch = 0;
			USART_TX_Multibyte("Door is closing");
			PORTL = 0b00000010;							// set the output value in port l bit 1 On
			lcd_Clear();
			lcd_SetCursor(0x04);
			lcd_WriteString("CLOSING");
			_delay_ms(500);
			for(counter = 2700; counter>750; counter -= 10)
			{
				if(_switch)								// if external interrupt switch is pressed while door is closing
				{
					USART_TX_Multibyte("Door is interrupted");
					door_mem = counter;					// counter value is stored in door_mem
					break;
				}
				OCR1A = counter;
				_delay_ms(10);
			}
			if(counter <= 750)
			{
				PORTL = 0b00000001;							// set the output value in port l bit 0 On and other bit off
				USART_TX_Multibyte("Door is fully closed");
				lcd_Clear();
				lcd_SetCursor(0x04);
				lcd_WriteString("CLOSED");
				_delay_ms(1000);
			}
			
		}
		
		
    }
}


void initGeneral()
{
	DDRL = 0b00000111;			// Configure PortL direction for Output, bits 0..2 as output
	PORTL = 0b00000001;			// initialize port l with bit 0 as high
	
	DDRB= 0b00100000;			// Configure PortB direction for Output, bit 5 as output
	PORTB= 0b00000000;			// initialize port b
	
	//HW interrupts set as Inputs and attach Pullups
	DDRD = 0b11111110;			// Configure PortD direction for input, bit 0 as Input
	PORTD = 0b00000001;			//attach pullup resistor to PD0
	
	DDRA = 0xFF;			// Configure PortA direction for Output
	DDRC = 0xFF;			// Configure PortC direction for Output
	DDRG = 0xFF;			// Configure PortG direction for Output
	
	sei();					// Enable interrupts at global level set Global Interrupt Enable (I) bit
}


void initTimer1()			// OC1A = PB5
{
	TCCR1A= 0b10000010;		// output capture mode on channel A,Fast PWM mode with ICR as the top value
	TCCR1B= 0b00011001;		// Fast PWM, No prescaling
	TCCR1C= 0b00000000;		// Not relavent to set this TCCR1C register because we are using Fast PWM mode
	
	ICR1H= 0x4E;			// This will set top value to 20,000 which will give 50HZ at 1MHZ operation
	ICR1L= 0X20;
	
	OCR1A= 750;				// Pulse width ranges from 750us (micro seconds) to 2250us
	
	TIMSK1= 0b00000000;		// We don't use the interrupt because PWM sets the output
	
	TIFR1= 0b00101111;		// Clear all interrupt flags
	
}

void initUSART0()
{
	UCSR0A = 0b00000010;		//Set U2X (Double the USART Tx speed, to reduce clocking error)
	UCSR0B = 0b10011000;		// RX Complete Int Enable, RX Enable, TX Enable, 8-bit data
	UCSR0C = 0b00000111;		// Asynchronous, No Parity, 1 stop, 8-bit data, Falling XCK edge
	// UBRR0 - USART Baud Rate Register (16-bit register, comprising UBRR0H and UBRR0L)
	UBRR0H = 0;					// 9600 baud, UBRR = 12, and  U2X must be set to '1' in UCSRA
	UBRR0L = 12;
}

void USART_TX_Singlebyte(unsigned char byte)
{
	while (!(UCSR0A & (1 << UDRE0)));		// Wait for Tx Buffer to become empty( check UDRE flag)
	UDR0 = byte;							// Writing to the UDR transmit buffer causes the char to be transmitted
}

void USART_TX_Multibyte(char *byte)
{
	while(*byte != '')
	{
		USART_TX_Singlebyte(*byte);
		*byte++;
	}
	USART_TX_Singlebyte('n');
	USART_TX_Singlebyte('r');
}

void initHWint()
{
	EICRA = 0b00000010;		// INT 0 falling-edge triggered
	EICRB = 0b00000000;		// INT7 ... 4 not used
	
	EIMSK = 0b00000001;		// Enable INT0
	EIFR = 0b00000001;		// Clear INT0 interrupt flag (in case a spurious interrupt has occurred during chip startup)
}

ISR(INT0_vect)				// Hardware_INT0_Handler (Interrupt Handler for INT0)
{
	_switch = 1;
	_delay_ms(20);			//debounce
}

Post a reply to sisir regmi

Think you can help sisir regmi ? Then post your thoughts that might help sisir regmi. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.