risbo213
Newbie level 4
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
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];
}
}