Hello Everyone,
i am trying to interface adc(using a potentiometre) using atmega8/atmega32 and show the analog value on four 7 segment displays.
in the begining i tried to interface it with lcd and it was perfectly showing the display.(to know i am doing the adc correctly)
mcu - atmega 32
F_CPU- 16MHz
adc initialization
[
void adc_init()
{
ADMUX |= (1<<REFS0); // 5 volt reference AVCC
ADCSRA |= (1<<ADEN) | (1<<ADPS0) | (1<<ADPS2) | (1<<ADPS1); // prescale-128
}
]
reading adc
[
uint16_t read_adc(uint8_t ch)
{
ch = ch & 0x07;
ADMUX |= ch;
ADCSRA |= (1<<ADSC);
while (! (ADCSRA & (1<<ADIF)) );
ADCSRA |= (1<<ADIF);
return(ADCW); // returning 10bit adc value
}
]
here there is more code for lcd which i am not posting as all i did is to read the adc from channel 7 and displayed it on lcd.
this is my proteus simualtion which is working perfectly showing adc value between 0-1023(as 10 bit adc)
now i tried to interface 7-segment using two buttons which can increment or decrement the value. and show it on 7-segment.(i did it to check that i can interface seven segment in general)
mcu used- atmega8
F_CPU = 16MHz
here is how i did it
[
#define Bit(x) (1<<x)
void EverythingOff()
{
PORTB &= ~(1<<PB0);
PORTB &= ~(1<<PB1);
PORTB &= ~(1<<PB2);
PORTB &= ~(1<<PB3);
}
int main(void)
{
DDRB |= Bit(PB0) | Bit(PB1) | Bit(PB2) | Bit(PB3); // bits to select the segment between 4 segments
DDRD |= Bit(PD0) | Bit(PD1) | Bit(PD2) | Bit(PD3) | Bit(PD4) | Bit(PD5) | Bit(PD6); // a,b,c,d,e,f,g of 7 segment
DDRC &= ~(1<<PC0); // button 1
DDRC &= ~(1<<PC1); // button 2
int segArray[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // array to hold the font of segment
int keypressed = 0; // variable to hold either key is pressed or not
while (1)
{
if ( !(PINC & (1<<PC0)) ) // checking button 1 to incremnt
{
EverythingOff();
_delay_ms(300);
keypressed++;
if (keypressed>9999)
{
keypressed = 0;
}
}
if ( !(PINC & (1<<PC1)) ) // checking button 2 to decrement
{
EverythingOff();
_delay_ms(300);
keypressed--;
if (keypressed<0)
{
keypressed = 9999;
}
}
// displaying on seven segment
EverythingOff(); // switching off all the bits to select segments
PORTB |= (1<<PB3); // switching on the desired one
PORTD = segArray[(keypressed%10)];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed%10)];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB0);
PORTD = segArray[(keypressed/1000)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/1000)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB1);
PORTD = segArray[(keypressed/100)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/100)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB2);
PORTD = segArray[(keypressed/10)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/10)%10];
_delay_us(0.01);
}
}
]
this code is also working perfectly and i am able to increment or decremnt the value on seven segment using two push buttons
here is the proteus image
so everything is working so far. but when i attach the adc(i.e potentiometre) with seven segment i am not getting the desired result.(this is my actual problem. all was the background details)
my code is this
mcu -atmega8
F_CPU = 16MHz
[
#include <avr/io.h>
#include <util/delay.h>
#define Bit(x) (1<<x)
void adc_init() // adc initialization
{
ADMUX |= (1<<REFS0); // 5 volt on AVCC
ADCSRA |= (1<<ADEN) | (1<<ADPS0) | (1<<ADPS2) | (1<<ADPS1); //prescale 128
}
uint16_t read_adc(uint8_t ch) // read adc
{
ch = ch & 0x07;
ADMUX |= ch;
ADCSRA |= (1<<ADSC);
while (! (ADCSRA & (1<<ADIF)) );
ADCSRA |= (1<<ADIF);
return(ADCW); // return 10 bit adc
}
void EverythingOff() // switing off all the select segments
{
PORTB &= ~(1<<PB0);
PORTB &= ~(1<<PB1);
PORTB &= ~(1<<PB2);
PORTB &= ~(1<<PB3);
}
int main(void)
{
DDRB |= Bit(PB0) | Bit(PB1) | Bit(PB2) | Bit(PB3); // for selecting segments
DDRD |= Bit(PD0) | Bit(PD1) | Bit(PD2) | Bit(PD3) | Bit(PD4) | Bit(PD5) | Bit(PD6); // a,b,c,d,e,f,g, of seven segment
DDRC &= ~(1<<PC2); // PC2 adc potentiometre connect set as input
int segArray[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // font of seven segment
adc_init(); // initializing adc
float digVal; //to store the value of adc
uint16_t keypressed;
while (1)
{
digVal = read_adc(2); // getting the adc value from channel 2
keypressed = digVal; // taking only the integer part(there is no floating part as read_adc returns integer but just to double check it . moreover who will again change all the keypressed to digval. too much lazy )
_delay_us(10);
EverythingOff(); // switching of all the select pin of segments
PORTB |= (1<<PB3); // switching on the desired one
PORTD = segArray[(keypressed%10)];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed%10)];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB0);
PORTD = segArray[(keypressed/1000)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/1000)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB1);
PORTD = segArray[(keypressed/100)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/100)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB2);
PORTD = segArray[(keypressed/10)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/10)%10];
_delay_us(0.01);
}
}
]
this is the proteus image of output
the result is sometimes 1083 too which is definetly wrong.
i know its a lengthy description. i have been trying this like 2 days and not able to get any output.
please help me with your suggestions.
thank you in advance.
i am trying to interface adc(using a potentiometre) using atmega8/atmega32 and show the analog value on four 7 segment displays.
in the begining i tried to interface it with lcd and it was perfectly showing the display.(to know i am doing the adc correctly)
mcu - atmega 32
F_CPU- 16MHz
adc initialization
[
void adc_init()
{
ADMUX |= (1<<REFS0); // 5 volt reference AVCC
ADCSRA |= (1<<ADEN) | (1<<ADPS0) | (1<<ADPS2) | (1<<ADPS1); // prescale-128
}
]
reading adc
[
uint16_t read_adc(uint8_t ch)
{
ch = ch & 0x07;
ADMUX |= ch;
ADCSRA |= (1<<ADSC);
while (! (ADCSRA & (1<<ADIF)) );
ADCSRA |= (1<<ADIF);
return(ADCW); // returning 10bit adc value
}
]
here there is more code for lcd which i am not posting as all i did is to read the adc from channel 7 and displayed it on lcd.
this is my proteus simualtion which is working perfectly showing adc value between 0-1023(as 10 bit adc)
now i tried to interface 7-segment using two buttons which can increment or decrement the value. and show it on 7-segment.(i did it to check that i can interface seven segment in general)
mcu used- atmega8
F_CPU = 16MHz
here is how i did it
[
#define Bit(x) (1<<x)
void EverythingOff()
{
PORTB &= ~(1<<PB0);
PORTB &= ~(1<<PB1);
PORTB &= ~(1<<PB2);
PORTB &= ~(1<<PB3);
}
int main(void)
{
DDRB |= Bit(PB0) | Bit(PB1) | Bit(PB2) | Bit(PB3); // bits to select the segment between 4 segments
DDRD |= Bit(PD0) | Bit(PD1) | Bit(PD2) | Bit(PD3) | Bit(PD4) | Bit(PD5) | Bit(PD6); // a,b,c,d,e,f,g of 7 segment
DDRC &= ~(1<<PC0); // button 1
DDRC &= ~(1<<PC1); // button 2
int segArray[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // array to hold the font of segment
int keypressed = 0; // variable to hold either key is pressed or not
while (1)
{
if ( !(PINC & (1<<PC0)) ) // checking button 1 to incremnt
{
EverythingOff();
_delay_ms(300);
keypressed++;
if (keypressed>9999)
{
keypressed = 0;
}
}
if ( !(PINC & (1<<PC1)) ) // checking button 2 to decrement
{
EverythingOff();
_delay_ms(300);
keypressed--;
if (keypressed<0)
{
keypressed = 9999;
}
}
// displaying on seven segment
EverythingOff(); // switching off all the bits to select segments
PORTB |= (1<<PB3); // switching on the desired one
PORTD = segArray[(keypressed%10)];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed%10)];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB0);
PORTD = segArray[(keypressed/1000)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/1000)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB1);
PORTD = segArray[(keypressed/100)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/100)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB2);
PORTD = segArray[(keypressed/10)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/10)%10];
_delay_us(0.01);
}
}
]
this code is also working perfectly and i am able to increment or decremnt the value on seven segment using two push buttons
here is the proteus image
so everything is working so far. but when i attach the adc(i.e potentiometre) with seven segment i am not getting the desired result.(this is my actual problem. all was the background details)
my code is this
mcu -atmega8
F_CPU = 16MHz
[
#include <avr/io.h>
#include <util/delay.h>
#define Bit(x) (1<<x)
void adc_init() // adc initialization
{
ADMUX |= (1<<REFS0); // 5 volt on AVCC
ADCSRA |= (1<<ADEN) | (1<<ADPS0) | (1<<ADPS2) | (1<<ADPS1); //prescale 128
}
uint16_t read_adc(uint8_t ch) // read adc
{
ch = ch & 0x07;
ADMUX |= ch;
ADCSRA |= (1<<ADSC);
while (! (ADCSRA & (1<<ADIF)) );
ADCSRA |= (1<<ADIF);
return(ADCW); // return 10 bit adc
}
void EverythingOff() // switing off all the select segments
{
PORTB &= ~(1<<PB0);
PORTB &= ~(1<<PB1);
PORTB &= ~(1<<PB2);
PORTB &= ~(1<<PB3);
}
int main(void)
{
DDRB |= Bit(PB0) | Bit(PB1) | Bit(PB2) | Bit(PB3); // for selecting segments
DDRD |= Bit(PD0) | Bit(PD1) | Bit(PD2) | Bit(PD3) | Bit(PD4) | Bit(PD5) | Bit(PD6); // a,b,c,d,e,f,g, of seven segment
DDRC &= ~(1<<PC2); // PC2 adc potentiometre connect set as input
int segArray[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // font of seven segment
adc_init(); // initializing adc
float digVal; //to store the value of adc
uint16_t keypressed;
while (1)
{
digVal = read_adc(2); // getting the adc value from channel 2
keypressed = digVal; // taking only the integer part(there is no floating part as read_adc returns integer but just to double check it . moreover who will again change all the keypressed to digval. too much lazy )
_delay_us(10);
EverythingOff(); // switching of all the select pin of segments
PORTB |= (1<<PB3); // switching on the desired one
PORTD = segArray[(keypressed%10)];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed%10)];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB0);
PORTD = segArray[(keypressed/1000)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/1000)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB1);
PORTD = segArray[(keypressed/100)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/100)%10];
_delay_us(0.01);
EverythingOff();
PORTB |= (1<<PB2);
PORTD = segArray[(keypressed/10)%10];
_delay_us(0.01);
PORTD &= ~segArray[(keypressed/10)%10];
_delay_us(0.01);
}
}
]
this is the proteus image of output
the result is sometimes 1083 too which is definetly wrong.
i know its a lengthy description. i have been trying this like 2 days and not able to get any output.
please help me with your suggestions.
thank you in advance.