Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
TG!
PostPosted: May 07, 2006 - 10:32 PM
Newbie


Joined: May 07, 2006
Posts: 3
Location: Germany

hey guys,

I really need someone's help. I'm trying for some days now but it still doesn't work. I'm trying to read to signals (on pin 19 and 20) with the ATtiny26. On both pins I want to read the voltage (0...5 V), so that I get a value between 0 and 255. ..and so on... at last the value should be sent to the hardware-PWM-output of the controller.
It's quite strage.. I tried this with one signal and it worked without any problem. But if I try to read a second signal it doesn't work anymore. >__<
Please, I really need help!! I have to finish the work till tomorrow! >__<

Here's my code. It might explain it better. Sorry for my poor English. ><

Code:
#include <tiny26.h>
#include <delay.h>

#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
unsigned char value_1;
unsigned char value_2;
#define ADC_VREF_TYPE 0x20
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
register static unsigned char input_index=0;
// Read the 8 most significant bits
// of the AD conversion result
adc_data[input_index]=ADCH;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
   input_index=0;
ADMUX=(FIRST_ADC_INPUT|ADC_VREF_TYPE)+input_index;
// Start the AD conversion
ADCSR|=0x40;
}

unsigned char temp;

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=0 State2=T State1=0 State0=T
PORTB=0x00;
DDRB=0x0A;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500,000 kHz
// Mode: PWMA & B top=OCR1C
// OC1A output: Inverted, /OC1A disconnected
// OC1B output: Inverted, /OC1B disconnected
PLLCSR=0x00;
TCCR1A=0xF3;
TCCR1B=0x04;
TCNT1=0x00;
OCR1A=0xFF;
OCR1B=0xFF;
OCR1C=0xFF;

// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PA3, PA6, PA7 and PB4-7: Off
// Interrupt on any change on pins PB0-3: Off
GIMSK=0x00;
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;

// ADC initialization
// ADC Clock frequency: 125,000 kHz
// ADC Voltage Reference: AREF
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=FIRST_ADC_INPUT|ADC_VREF_TYPE;
ADCSR=0xCD;

// Global enable interrupts
#asm("sei")

while (1)
      {     
      value_1=adc_data[0];
      value_2=adc_data[1];
      if (value_1 == 0) value_1=1;
      if (value_2 == 0) value_2=1;
      temp=value_1*(1-(value_2/255));
      OCR1B=temp;
      };
}


Thank you very much for your help!!! ^__^
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
brberie
PostPosted: May 07, 2006 - 11:22 PM
Resident


Joined: Sep 12, 2003
Posts: 529
Location: XX century

This is the AVR GCC forum. CVAVR does not belong to it!
Moderators, could you please move this topic.
 
 View user's profile Send private message  
Reply with quote Back to top
TG!
PostPosted: May 07, 2006 - 11:27 PM
Newbie


Joined: May 07, 2006
Posts: 3
Location: Germany

I'm sorry, I'm a noob.. ><'
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
mckenney
PostPosted: May 07, 2006 - 11:37 PM
Raving lunatic


Joined: Mar 27, 2002
Posts: 2066
Location: Selkirk, NY, USA

Code:
temp=value_1*(1-(value_2/255));

"value_2/255" will always be 0 if value_2 < 255, so temp will
always be =value_1 or =0. I suspect you want something like:
Code:
temp=value_1-((unsigned)value_1 * value_2)/255;

Also, you're not interlocking with the ADC completion, but I
suppose if the signals don't change rapidly maybe this isn't
a big deal (?).
 
 View user's profile Send private message  
Reply with quote Back to top
TG!
PostPosted: May 08, 2006 - 12:18 AM
Newbie


Joined: May 07, 2006
Posts: 3
Location: Germany

Thank you very much!!!! It works fine!!! Thanks a lot!! You're great!! ^___^
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits