Skip to content
Snippets Groups Projects
Commit bdafec94 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Increased the communication speed to 1Mbps and solved some communication...

Increased the communication speed to 1Mbps and solved some communication problesm due to interrupt priorities.
parent c7d2b4e0
No related branches found
No related tags found
No related merge requests found
......@@ -9,5 +9,6 @@ void adc_start(void);
void adc_stop(void);
void adc_set_num_samples(uint8_t num);
void adc_set_sample_period(uint8_t time_ms);
void adc_loop(void);
#endif
......@@ -30,34 +30,21 @@ void adc_start_conv(void)
ADCSRA|=(1<<ADSC);
}
/* interrupt handling functions */
ISR(ADC_vect)
uint8_t adc_is_conversion_done(void)
{
uint8_t i;
uint16_t data;
data=(ADCL | (ADCH<<8));
ram_data[ADC_CHANNEL0_L+adc_current_ch*2]=data%256;
ram_data[ADC_CHANNEL0_H+adc_current_ch*2]=data/256;
adc_ch_data[adc_current_ch][adc_current_sample]=data;
/* compute the average for the current channel */
data=0;
for(i=0;i<adc_num_samples;i++)
data+=adc_ch_data[adc_current_ch][i];
data=data/adc_num_samples;
ram_data[ADC_AVG_CHANNEL0_L+adc_current_ch*2]=data%256;
ram_data[ADC_AVG_CHANNEL0_H+adc_current_ch*2]=data/256;
if(adc_current_ch==NUM_CH-1)// last channel
adc_current_sample=(adc_current_sample+1)%adc_num_samples;
adc_current_ch=(adc_current_ch+1)%NUM_CH;
if(ADCSRA&(1<<ADIF))
return 0x01;
else
return 0x00;
}
ISR(TIMER2_COMP_vect)
uint8_t adc_is_period_done(void)
{
// start a new conversion
adc_set_channel(adc_current_ch);
adc_start_conv();
}
if(TIFR&(1<<OCF2))
return 0x01;
else
return 0x00;
}
/* public functions */
void adc_init(void)
......@@ -67,7 +54,7 @@ void adc_init(void)
PORTF = 0x00;
ADMUX = (1<<REFS0);// use AVCC as reference, Right justified, channel 0
ADCSRA = (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // free running mode disabled, interrupts enabled, prescaler to 128
ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // free running mode disabled, interrupts enabled, prescaler to 128
// clear any pending interrupt
ADCSRA|=(1<<ADIF);
......@@ -83,6 +70,7 @@ void adc_init(void)
// configure the timer 2 to perform periodic conversions (1 ms period)
TCCR2=(1<<WGM21);// CTC mode, no output, prescaler to 0 (no clock source)
TCNT2=0x00;// start from 0
TIMSK=0x00;// interrupts not enabled
}
void adc_start(void)
......@@ -91,7 +79,10 @@ void adc_start(void)
ADCSRA|=(1<<ADEN);
// enable the timer 2 interrupts
TCNT2=0x00;// start from 0
TIMSK|=(1<<OCIE2);// the timer ISR will start the ADC conversion
TIMSK=0x00;// interrupts not enabled
// clear interrupts
TIFR|=(1<<OCF2);
TIFR|=(1<<TOV2);
// configure the timer prescaler and count value
adc_set_sample_period(adc_sample_period_ms);
ram_data[ADC_CONTROL]|=0x02;
......@@ -99,8 +90,6 @@ void adc_start(void)
void adc_stop(void)
{
// disable the timer 2
TIMSK&=~(1<<OCIE2);
TCCR2&=0xF8;// set prescaler to 0 (no clock source)
// disable the ADC
ADCSRA&=~(1<<ADEN);
......@@ -140,3 +129,47 @@ void adc_set_sample_period(uint8_t time_ms)
}
}
}
void adc_loop(void)
{
uint8_t i;
uint16_t data;
static int count=1000;
if(adc_is_period_done())
{
TIFR|=(1<<OCF2);// clear interrupt flag
TIFR|=(1<<TOV2);
TCNT2=0x00;// start from 0
// start a new conversion
adc_set_channel(adc_current_ch);
adc_start_conv();
}
else
{
if(adc_is_conversion_done())
{
count--;
if(count==0)
{
count=1000;
toggle_led();
}
ADCSRA|=(1<<ADIF);// clear interrupt flag
data=(ADCL | (ADCH<<8));
ram_data[ADC_CHANNEL0_L+adc_current_ch*2]=data%256;
ram_data[ADC_CHANNEL0_H+adc_current_ch*2]=data/256;
adc_ch_data[adc_current_ch][adc_current_sample]=data;
// compute the average for the current channel
data=0;
for(i=0;i<adc_num_samples;i++)
data+=adc_ch_data[adc_current_ch][i];
data=data/adc_num_samples;
ram_data[ADC_AVG_CHANNEL0_L+adc_current_ch*2]=data%256;
ram_data[ADC_AVG_CHANNEL0_H+adc_current_ch*2]=data/256;
if(adc_current_ch==NUM_CH-1)// last channel
adc_current_sample=(adc_current_sample+1)%adc_num_samples;
adc_current_ch=(adc_current_ch+1)%NUM_CH;
}
}
}
......@@ -16,12 +16,13 @@ int16_t main(void)
dyn_slave_diff_init(ram_data[Baud_Rate],ram_data[ID]);
init_ports();
adc_init();
pwm_init();
dac_init();
// pwm_init();
// dac_init();
sei();
while (1)
{
adc_loop();
dyn_slave_se_loop();
dyn_slave_diff_loop();
}
......
......@@ -5,7 +5,7 @@
uint8_t ram_data[RAM_SIZE];
// Dynamixel EEPROM variables
uint8_t EEMEM eeprom_data[17]={0x1C,0x00,0x00,0x01,0x22,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02};
uint8_t EEMEM eeprom_data[17]={0x1C,0x00,0x00,0x01,0x01,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02};
void ram_init(void)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment