Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing temperature Sensor and 7 segment display with Atmega8

Status
Not open for further replies.

areeckal

Member level 2
Member level 2
Joined
Jan 22, 2011
Messages
42
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,567
I am trying to display the value read from ADC in 7 segment display in Proteus.
The value is not correctly getting displayed in proteus. It is correct when simulated in avr studio.
Is there anything else to do while interfacing with 7 segment display?
Code is:
Code:
#include <avr/io.h>
#include <util/delay.h>

#define SEVEN_SEGMENT1_PORT PORTD
#define SEVEN_SEGMENT1_DDR DDRD
#define SEVEN_SEGMENT2_PORT PORTB
#define SEVEN_SEGMENT2_DDR DDRB

uint8_t digits[2];   //Holds the digits for 2 displays

void SevenSegment(uint8_t n1,uint8_t n2,uint8_t dp)
{
/*This function writes a digits given by n to the display*/
   if((n1<10)&&(n2<10))
   {
      switch (n1)
      {
         case 0:
         SEVEN_SEGMENT1_PORT=0x7E;
         break;

         case 1:
         SEVEN_SEGMENT1_PORT=0x20;
         break;

         case 2:
         SEVEN_SEGMENT1_PORT=0x5D;
         break;

         case 3:
         SEVEN_SEGMENT1_PORT=0x79;
         break;

         case 4:
         SEVEN_SEGMENT1_PORT=0x33;
         break;

         case 5:
         SEVEN_SEGMENT1_PORT=0x5B;
         break;

         case 6:
         SEVEN_SEGMENT1_PORT=0x5F;
         break;

         case 7:
         SEVEN_SEGMENT1_PORT=0x70;
         break;

         case 8:
         SEVEN_SEGMENT1_PORT=0x7F;
         break;

         case 9:
         SEVEN_SEGMENT1_PORT=0x7B;
         break;
	}
    switch (n2)
    {
		 case 0:
         SEVEN_SEGMENT2_PORT=0x7E;
         break;
         case 1:
         SEVEN_SEGMENT2_PORT=0x20;
         break;

         case 2:
         SEVEN_SEGMENT2_PORT=0x5D;
         break;

         case 3:
         SEVEN_SEGMENT2_PORT=0x79;
         break;

         case 4:
         SEVEN_SEGMENT2_PORT=0x33;
         break;

         case 5:
         SEVEN_SEGMENT2_PORT=0x5B;
         break;

         case 6:
         SEVEN_SEGMENT2_PORT=0x5F;
         break;

         case 7:
         SEVEN_SEGMENT2_PORT=0x70;
         break;

         case 8:
         SEVEN_SEGMENT2_PORT=0x7F;
         break;

         case 9:
         SEVEN_SEGMENT2_PORT=0x7B;
         break;
	}
	}
}

void Print(uint16_t num)
{
	uint8_t i=0;
   	uint8_t j;
   	if(num>999) return;
	while(num)
   	{
		digits[i]=num%10;
      	i++;
		num=num/10;
   	}
   	for(j=i;j<2;j++)
		digits[j]=0;
}
/* Main function */
int main(void)
{
	uint8_t val;
   	//Use prescale factor 64 -> ADC clock is 125kHz
   	ADCSR |= (1<<ADPS1) | (1<<ADPS2);
    
   	//AVCC with external capacitor at AREF pin   
   	ADMUX |= (1<<REFS0);
   
   	//Enable the ADC
   	ADCSR |= (1<<ADEN);  
   	
   	//Port D
   	SEVEN_SEGMENT1_DDR=0xFF;
	//Port B
	SEVEN_SEGMENT2_DDR=0xFF;
   	
	   
	while(1)
    {
		_delay_ms(1000);
		ADMUX|=0x00; 
     	//Start conversion
        ADCSR |= (1<<ADSC);   
		while(!(ADCSRA &(1<<ADIF)));
		ADCSRA|=(1<<ADIF);          
		val = ADC; 
		Print(val);
		SevenSegment(digits[0],digits[1],0);
		_delay_ms(1000);
	}
 }

Schematic and entire project is attached.
screenshot.png**broken link removed**

Pls help..
 

Attachments

  • test2_7segment.rar
    44 KB · Views: 195
Last edited:

You can't write binary numbers is C for a start (unless your using some non-standard compliler that allows it), they must be converted to and expressed in hexidecimal format 0xABCD etc

Please use the code tags when you post code, and ensure that code is properly indented and aligned if you want help on this forum...
 
I have modified the code and the schematic. Still there is problem. The modified one is attached above
 
  • Like
Reactions: jambaa

    jambaa

    Points: 2
    Helpful Answer Positive Rating
Why dont you multiplexed data line of seven segment display..??
in your void SevenSegment(uint8_t n,uint8_t dp) function ur writing data to port D only
but in ur proteus design u have connected
ur dataline to port B as well as to port D...
code is written for 3 digits display...
Better connect ur dataline to portB & digit select line to portD
switch the digit according to function argument...
 
use real sensors and hardware and not proteus...

check this site
**broken link removed** tal-temperature-sensor.html

---------- Post added at 13:02 ---------- Previous post was at 12:57 ----------

also see this ATmega8 | YourITronics - Part 2
 
It is not necessary to use real sensor..
Proteus has very good models...
It is more than sufficient to use proteus for your prototyping...
 
Just a quick off topic suggestion, when coding it would be a lot cleaner to define the outputs for your LCD into an array i.e.

declare this at global scope as static constant

Code:
static const unsigned char LCD_pattern[10] = {0x7E, 0x20, 0x5D...}

then in your code you can write

Code:
SEVEN_SEGMENT1_PORT = LCD_Pattern[n1];
SEVEN_SEGMENT2_PORT = LCD_Pattern[n2];

instead of that *** awful switch statement...

after all... cleaner code, is easier to maintain and easier to debug...
 

You can't write binary numbers is C for a start (unless your using some non-standard compliler that allows it), they must be converted to and expressed in hexidecimal format 0xABCD etc

you can definitely use binary (0b...) values with AVR-GCC or codevisionAVR and the OP code is written for the AVR-GCC.

Alex

---------- Post added at 02:02 ---------- Previous post was at 01:06 ----------

You port output for digits 1 and 2 is wrong, the correct is

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case 1:
         SEVEN_SEGMENT1_PORT=0x30; // and not 0x20
         break;
 
         case 2:
         SEVEN_SEGMENT1_PORT=0x6D; // and not 0x5D
         break;[/XCODE]
 
also in the other digit 
[syntax="c"]         case 1:
         SEVEN_SEGMENT2_PORT=0x30; //0x20
         break;
 
         case 2:
         SEVEN_SEGMENT2_PORT=0x6D; //0x5D
         break;



But the main mistake is with the val variable, you declare it as a uint8_t (1 byte) and then you assign a 2 byte value to it (ADC), if you correct it everything works fine.


Code C - [expand]
1
2
// this was wrong uint8_t val;
uint16_t val;



Alex
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top