Sign In

Need Help With Writing Code For Calculator

RONAL KUMAR ronal.ronesh@gmail.com

Hardware: ATmega128 Using ET-AVR START KIT V1 Software: C Project Using AVR4.19

Hi
I need help with writing code for a simple calculator that takes 2 single digit number (0-9) as input and calculates an answer based on operational function (+, -,).

When SW1 is pushed, it selects the value from 0-9 as input A and displays on LED connected to port A
When SW2 is pushed, it selects the value from 0-9 as input B and displays on LED connected to port B
When SW3 is pushed, it selects the operational function
When SW3 is pushed, it calculates the answer and displays on LED connected to port D&E as 2 digit.

Below code is to check the wiring on AVR starter kit and does not include the calculation function.


Images

Program

#define F_CPU 16E6

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdbool.h>

void initialise();
float input (float counter_A);
void display_A(float counter_A);
void display_output_G(float counter_A);

/*
* Assumptions:
*
* - 7 segment LEDs connected to PORTA, PORTB, PORTD, & PORTE
* - In Theory, all pins should have current-limiting resistors
* - One resistor at common pin of 7 segment LED will suffice
* - F_CPU is defined to be your cpu speed
*/

// Common Anode
#define F0 0x81                  //0b10000001
#define F1 0xF3                  //0b11110011
#define F2 0x49                  //0b01001001
#define F3 0x61                  //0b01100001
#define F4 0x33                  //0b00110011
#define F5 0x25                  //0b00100101
#define F6 0x05                  //0b00000101
#define F7 0xF1                  //0b11110001
#define F8 0x01                  //0b00000001
#define F9 0x21                  //0b00100001
#define F10 0x7F                 //0b01111111   '-'

#define D 1000     // ms
#define ON 0
#define OFF 1

int main()
{
     // setting input / output PORTs
	 DDRA = 0xFF;              //output
	 DDRB = 0xFF;              //output
	 DDRD = 0xFF;              //output
	 DDRE = 0xFF;              //output
	 DDRG = 0x18;              //output  (PG4, PG3), input (PG2, PG1, PG0) - 

	 float counter_A = 0;

	 initialise();
	 while (1)
	 {
	   counter_A = input(counter_A);

	   display_A(counter_A);                    // 7 segment LED

	   display_output_G(counter_A);             // on-board Red LEDs
	   }
	   return 0;

 }

void initialise ()
{
	     PORTG |= (_BV(3) | _BV(4));           // bit values high - Both Red LEDs off


		   for (int i = 0; i < 6; i++)
		   {
		     if (i == 0)
			 {
			      PORTA = 0xFF;                // display '8.'
			      _delay_ms(200);              // time delay

			      PORTA = 0x00;                // black-out
			  }
			  else if (i == 1)
			  {
			      PORTB = 0XFF;                // dispaly '8.'
			      _delay_ms (200);             // time delay
			      PORTB = 0x00;
			   }
			   else if (i == 2)
			   {
			      PORTD = 0xFF;                // dispaly '8.'
			      _delay_ms(200);              // time delay

			      PORTD = 0x00;
			  }
			  else if (i == 3)
			  {
			      PORTE = 0XFF;                // dispaly '8.'
			      _delay_ms (200);             // time delay
			      PORTE = 0x00;
			  }
			  else if (i == 4)
			  {
			      PORTG &= ~_BV(3);            // bit value 0
				  _delay_ms(200);              // time delay
				  PORTG |= _BV(3);
			  }
			  else
			  { 
			     PORTG &= ~_BV(4);             // bit value 0
				 _delay_ms(200);               // time delay
				 PORTG |= _BV(4);
			  }
	      }
 }

 float input (float counter_A)
 {
      int push_button1 = OFF;                  // initialise bit value to 1
      int push_button2 = OFF;

      push_button1 = PING & _BV(PG0);           // Assign PG0 to pushbuton 1
      push_button2 = PING & _BV(PG1);           // Assign PG0 to pushbuton 2

      _delay_ms(3*D/10);                       // 300ms delay

      if (push_button1 == ON)                  // bit value is 0
	  {
	      if (counter_A == 3)                  // counter to max value
		      counter_A = 0;                   // change counter value to 0
		  else 
		      counter_A++;                     // increment
	   }

	   if (push_button2 == ON)                 // bit value is 0
	   {
	      if (counter_A == 0)                  // counter to max value
		      counter_A = 3;                   // change counter value to 3
		  else 
		      counter_A--;                     // decrement
	   }
	   return counter_A;
  }

  void display_A(float counter_A)
  { 
          int push_button3 = OFF;
		  push_button3 = PING & _BV(PG2);

		  if (push_button3 == ON)
		  {
		     if (counter_A == 0)
			    PORTA = F0;                    // dispaly '0'
			else if (counter_A == 1)
			    PORTA = F1;                    // dispaly '1'
			else if (counter_A == 2)
			    PORTA = F2;                    // dispaly '2'
			else
			    PORTA = F3;                    // dispaly '3'
			_delay_ms(3*D);                    // dispaly for 3 seconds
		  }
		  else
		  {
		       PORTA = F10;                    // 7-segment (-) symbol
		  }
		  PORTB = F0;                         // display '-'
		  PORTD = F1;                         // display '-'
		  PORTE = F2;                         // display '-'
	}

	void display_output_G(float counter_A)
	{
	      if (counter_A == 0)
		      PORTG |= (_BV(3) | _BV(4));	   // bit values 1 - Both Red LEDs off
	      else if (counter_A == 1)
		  {
		      PORTG |= _BV(3);                 // bit value 1 - Red LEDs off
			  PORTG &= ~_BV(4);                // bit value 0 - Red LEDs on
		  }
		  else if (counter_A == 2)
		  {
		      PORTG &= ~_BV(3);                // bit value 0 - Red LEDs on
			  PORTG |= _BV(4);                 // bit value 1 - Red LEDs off
		  }
		  else
		      PORTG &= ~(_BV(3) | _BV(4));     // bit values 0 - Both Red LEDs off
} 

Replies

R Tim Chondro
2022-05-14 04:17:03
I believe that you are trying to do a simple calculator - The program below is similar to yours and will work fine -you need to go through it slowly .... Enjoy !
// CODE / PROGRAM 

#define F_CPU 16E6

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdbool.h>

void initialise();
float LED1(float counter_A);       // first number
float LED2(float counter_B);       // second number
int operator(float counter_C);     //operator

void display_A(float counter_A);
void display_B(float counter_B);
void display_C(float counter_C);     
float calculations(float counter_A, float counter_B, float counter_C);

//Calculations
int output_left(int ans);     //left LED output       
int output_right(int ans);   //right LED output
void display_left(int dleft);
void display_right(int dright);
/*
 * Assumptions:
 *
 * 	- 7 segment LED connected to PORTA
 * 	- In theory, all pins should have current-limiting resistors
 * 	- F_CPU is defined to be your cpu clock speed
 */

// Define LED Display common Anode
#define F0 0x18             // 0
#define F1 0xDE             // 1
#define F2 0x34		// 2
#define F3 0x94		// 3
#define F4 0xD2		// 4
#define F5 0x91		// 5
#define F6 0x11		// 6
#define F7 0xDC	       // 7
#define F8 0x10		// 8
#define F9 0x90		// 9
#define F10 0xFF	       // All off
#define F11 0x00	       // All on
#define F12 0x31      	// E
#define F13 0x77     	// r

#define D 1000 		// ms
#define ON 0
#define OFF 1

int main ()
{
	// setting  input / output PORTs

	DDRA = 0xFF;			// output 0b00000000 
	DDRB = 0xFF;			// output 0b00000000
	DDRD = 0xFF;			// output 0b00000000
	DDRE = 0xFF;			// output 0b00000000
	DDRG = 0x18;			// output (PG4, PG3), input (PG2,PG1,PG0) - 0b11000 equivalent to 0,only G & DP = 11 

	float counter_A = 0;
	float counter_B = 0;
	float counter_C = 0;
	float ans = 0;
       int first_num = 0;
       int second_num = 0;
      

	initialise();
	while (1) 
	{
		counter_A = LED1(counter_A);
//		counter_B = LED2(counter_B);
//		counter_C = operator(counter_C);

		display_A(counter_A);			// 7-segment LED
/*		display_B(counter_B);                    // 7-segment LED   
		ans = calculations(counter_A, counter_B, counter_C);   //on- board red LEDs
        first_num = output_left(ans);
        second_num = output_right(ans);
        display_left(first_num);
        display_right(second_num);*/
	}
	return 0;
}


void initialise()
{
	PORTG |= (_BV(3) | _BV(4));  	// bit values high - Both Red LEDs off


	for (int j = 1; j <= 5; j++)
	{
		for (int i = 0; i < 10; i++)
		{
			if ((i%2) == 0)
			{
				PORTA = 0xFF;
				PORTB = 0xFF;
				PORTD = 0xFF;
				PORTE = 0xFF;			
				_delay_ms(200);			// time delay
			}
			else
			{
				PORTA = 0x00;
				PORTB = 0x00;
				PORTD = 0x00;
				PORTE = 0x00;			
				_delay_ms(200);			// time delay
			}
		}
	}
}


float LED1(float counter_A)
{
	int push_button1 = OFF;	       // initialise bit value to 1
	push_button1 = PING & _BV(PG0); 	// Assign PG0 to pushbutton 1
    _delay_ms(3*D/10);					// 300ms delay


	if (push_button1 == ON)           	// bit value is 0
	{
		if (counter_A == 9)				// counter at max value
			counter_A = 0;		 // change counter value to zero
		else 		
			counter_A++;				// increment
	}
        return counter_A;
}



	float LED2(float counter_B)
{
	int push_button2 = OFF;                     // initialize bit value to 2
	push_button2 = PING & _BV(PG1); 			// Assign PG1 to pushbutton 2
	_delay_ms(3*D/10);					// 300ms delay

	if (push_button2 == ON)           			// bit value is 0
	{
		if (counter_B == 9)				// counter at max value
			counter_B = 0;			// change counter value to zero
		else 		
			counter_B++;				// increment
	}
	return counter_B;
}


int operator(float counter_C)
{       
        int push_button3 = OFF;	            	// initialise bit value to 3
        push_button3 = PING & _BV(PG2); 		// Assign PG0 to pushbutton 3
        _delay_ms(3*D/10);				    	// 300ms delay

	if (push_button3 == ON)           	// bit value is 0
	{
		if (counter_C == 3)				// counter at max value
			counter_C = 0;				// change counter value to zero
		else 		
			counter_C++;				// increment
	}
        return counter_C;
}


// **********************************************
void display_A(float counter_A)
{
	if (counter_A == 0)				
		PORTA = F0;				// display '0'
	else if (counter_A == 1)
		PORTA = F1;				// display '1'
	else if (counter_A == 2)
		PORTA = F2;				// display '2'
	else if (counter_A == 3)
		PORTA = F3;				// display '3'
	else if (counter_A == 4)
		PORTA = F4;				// display '4'
	else if (counter_A == 5)
		PORTA = F5;				// display '5'
	else if (counter_A == 6)
		PORTA = F6;				// display '6'
	else if (counter_A == 7)
		PORTA = F7;				// display '7'
	else if (counter_A == 8)
		PORTA = F8;				// display '8'
	else
		PORTA = F9;				// display '9'
}


void display_B(float counter_B)
{
       int push_button2 = OFF;
	push_button2 = PING & _BV(PG1);                    	 

	if (push_button2 == ON)
	{
		if (counter_B == 0)				
			PORTD = F0;				// display '0'
		else if (counter_B == 1)
			PORTD = F1;				// display '1'
		else if (counter_B == 2)
			PORTD = F2;				// display '2'
		else if (counter_B == 3)
			PORTD = F3;				// display '3'
		else if (counter_B == 4)
			PORTD = F4;				// display '4'
		else if (counter_B == 5)
			PORTD = F5;				// display '5'
		else if (counter_B == 6)
			PORTD = F6;				// display '6'
		else if (counter_B == 7)
			PORTD = F7;				// display '7'
		else if (counter_B == 8)
			PORTD = F8;				// display '8'
		else
			PORTD = F9;				// display '9'
			
		_delay_ms(3*D);					// display for 3 seconds


	
}
}

void display_C(float counter_C)
{
	if (counter_C == 0)				
	{
        PORTG |= (_BV(3) | _BV(4));		// bit values 1 - Both Red LEDs off
	}
	else if (counter_C == 1)
	{
		PORTG |= _BV(3);				// bit value 1 - Red LED off
		PORTG &= ~_BV(4);				// bit value 0 - Red LED on
	}
	else if (counter_C == 2)
	{
		PORTG &= ~_BV(3);				// bit value 0 - Red LED on
		PORTG |= _BV(4);				// bit value 1 - Red LED off
	}
	else
		PORTG &= ~(_BV(3) | _BV(4)); 		// bit value 0 - Both Red LEDs on
}


float calculations(float counter_A, float counter_B, float counter_C)
{
	float ans = 0;

	if (counter_C == 0)    		             //addition	
	{
	    ans = counter_A + counter_B;	
		PORTG |= (_BV(3) | _BV(4));			// bit values 1 - Both Red LEDs off
    }
	else if (counter_C == 1)                //subtraction
	{   
	    ans = counter_A - counter_B;
		PORTG |= _BV(3);				    // bit value 1 - Red LED off
		PORTG &= ~_BV(4);				    // bit value 0 - Red LED on
	}
	else if (counter_C == 2)                //multiplication
	{   
	    ans = counter_A * counter_B;
		PORTG &= ~_BV(3);				    // bit value 0 - Red LED on
		PORTG |= _BV(4);				    // bit value 1 - Red LED off
	}
	else					               //division
	{
	    ans = counter_A / counter_B;
		PORTG &= ~(_BV(3) | _BV(4));		// bit value 0 - Both Red LEDs on
    }

    return ans; 
}

int output_left(int ans)
{
float identifier = 0;
float decimal;
int rem = 0;
int left = 0;
if (ans > 9)
{
identifier = ans / 10;
rem = ans % 10;
decimal = rem * 0.1;
left = identifier - decimal;
}
else
{
left = ans;
}
return left;

}

int output_right(int ans)
{
float identifier = 0;
float decimal;
int rem = 0;
int right = 0;
if (ans > 9)
{
identifier = ans / 10;
rem = ans % 10;
decimal = rem * 0.1;
right = rem;
}
else
{
right=ans;
}
return right;
}

void display_left(int dleft)
{
	
              if (dleft == 0)				
			PORTB = F0;				// display '0'
		else if (dleft == 1)
			PORTB = F1;				// display '1'
		else if (dleft == 2)
			PORTB = F2;				// display '2'
		else if (dleft == 3)
			PORTB = F3;				// display '3'
		else if (dleft == 4)
			PORTB = F4;				// display '4'
		else if (dleft == 5)
			PORTB = F5;				// display '5'
		else if (dleft == 6)
			PORTB = F6;				// display '6'
		else if (dleft == 7)
			PORTB = F7;				// display '7'
		else if (dleft == 8)
			PORTB = F8;				// display '8'
		else
			PORTB = F9;				// display '9'
			
		_delay_ms(10*D);					// display for 10 seconds
}

void display_right(int dright)
{
              if (dright == 0)				
			PORTE = F0;				// display '0'
		else if (dright == 1)
			PORTE = F1;				// display '1'
		else if (dright == 2)
			PORTE = F2;				// display '2'
		else if (dright == 3)
			PORTE = F3;				// display '3'
		else if (dright == 4)
			PORTE = F4;				// display '4'
		else if (dright == 5)
			PORTE = F5;				// display '5'
		else if (dright == 6)
			PORTE = F6;				// display '6'
		else if (dright == 7)
			PORTE = F7;				// display '7'
		else if (dright == 8)
			PORTE = F8;				// display '8'
		else
			PORTE = F9;				// display '9'
			
		_delay_ms(10*D);					// display for 10 seconds
}

Post a reply to RONAL KUMAR

Think you can help RONAL KUMAR ? Then post your thoughts that might help RONAL KUMAR. 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.