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.

[AVR] atmega 16 potenciometar range

Status
Not open for further replies.

risbo213

Newbie level 4
Joined
Dec 7, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
Hi
I use atmega 16 uc. which is connected to two BCD displays and one potenciometar on pin A0. I try to display the number between 0 and 20 using the line equation in the code for whole range of potenciometar. But the two displays only display the number 00 on it. Please tell me where is the fault in the code.
Regards
riste
Code:
 #include <avr/io.h>
 #include <util/delay.h>
 
 void adc_init(void)
 {
     ADMUX = (1<<REFS1) | (1<<REFS0) | (1<<ADLAR);// internal 2.56v left adjasted
  
     ADCSRA = (1<<ADEN)|(0<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);// 1 000 000 HZ / 8 = 125 000 HZ
 }

 uint8_t adc_read(uint8_t ch)
 {
     // select the corresponding channel 0~7
     // ANDing with '7' will always keep the value
     // of 'ch' between 0 and 7
     ch &= 0b00000111;  
     ADMUX = (ADMUX & 0xF8)|ch;     // clears the bottom 3 bits before ORing
  
     // start single conversion
     ADCSRA |= (1<<ADSC);
  
     // wait for conversion to complete
     while(ADCSRA & (1<<ADSC));
  
    return (ADCH);
 }


 
int main(void)
{
    DDRB = 0b11111111;
	DDRD = 0b11111111;
    uint8_t adc_result0;
	unsigned char lookup[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
	long potc;
    long ten =0;
	long sto =0;
    long val = 0;
	char potc1 =0;

   
 
    // initialize adc 
    adc_init();
    
 
    
 
    while(1)
    {
        adc_result0 = adc_read(0);      // read adc value at PA0
        
 
    
		potc = adc_result0;	
		val = potc;
		// y=(((y2-y1)/(x2-x1))*(x-x1))+y1  equation of line
		potc1 = (((20)/(256))*(val)); //the value on the BCD display should be between 0 an 20
		ten = potc1%10;
		sto = potc1/10;
        
                PORTD = lookup[ten];
		PORTB = lookup[sto];	 

    }
}
 

what is the voltage you have given to potentiometer ?
 

Between + and - of potenciometar I conected 5 V. But when system working I measure with multimetar voltage between 1.7 v and 0 for whole range of potenciometar.
riste
 

Hi,

adc_result0 = adc_read(0); // read adc value at PA0



potc = adc_result0;
val = potc;
// y=(((y2-y1)/(x2-x1))*(x-x1))+y1 equation of line
potc1 = (((20)/(256))*(val)); //the value on the BCD display should be between 0 an 20
ten = potc1%10;
sto = potc1/10;

PORTD = lookup[ten];
PORTB = lookup[sto];

I wonder why you use three variables with the same value...

Your calculation make me assume you use 8 bits of ADC resolution. Is this correct?

You have 7 segment displays. I assume they are LED type common cathode. What's the LED current? Don't you overload the AVR max VCC current?

Is the supply voltage stable? Maybe the high current makes your supply voltage drop and thus resetting your AVR.

Show us your schematic.

Klaus
 

Hi,



I wonder why you use three variables with the same value...

Your calculation make me assume you use 8 bits of ADC resolution. Is this correct?

You have 7 segment displays. I assume they are LED type common cathode. What's the LED current? Don't you overload the AVR max VCC current?

Is the supply voltage stable? Maybe the high current makes your supply voltage drop and thus resetting your AVR.

Show us your schematic.

Klaus
Code:
Hi
I made mistake and use 3 variables instead of two.I use 8 bits of ADC resolution.The  7 segment displays are common cathode. The voltage drop of 1 segment of 7 segment display is 1.8 volt. 
I use 330 ohm resistors for current limiting. Which give me  10 (mA per input) * 14( LED) = 140 mA.
The maximum current for atmega 16 is 200mA and 40 mA per input. 
I use the same electrical schematic for measuring temperature with LM 35 only instead of  potenciometar  to be connected to A0 pin  is connected the LM35. The other different I use little bit different code with 10 bit resolution of ADC and 8 MHz internal oscillator. This system working and 
I measure the room temperature. I think that the problem is in the code.
In the attachment is the schematic of the connection.
Regards
Riste
 

Attachments

  • atmega16 pot (1).pdf
    988.6 KB · Views: 54

Hi,

Usa a big bulk capacitor.

Additionally use a ceramic capacitor at each VCC pin, at each ADC input and on VREF.


Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top