SV: [Icc-avr] ATMega644p -> ADC

Johannes Assenbaum jassenbaum at htp-tel.de
Tue Sep 30 15:40:33 PDT 2008


Hi Rainer,

I'd never use printf() within an isr on AVRs. It lasts to many clock cycles.

Best regards,
Johannes


> Hi John,

> many thanks for your response!

> This code works like expected:

> //ICC-AVR application builder : 29.09.2008 22:44:51
> // Target : M644p
> // Crystal: 20.000Mhz

> #include <iccioavr.h>
> #include <macros.h>
> #include <stdio.h>

> #pragma interrupt_handler timer0_comp_isr:iv_TIM0_COMPA
> #pragma interrupt_handler adc_isr:iv_ADC

> extern int _textmode;

> unsigned int adc_data[9];


> void port_init(void)
> {
> PORTA = 0x00;
> DDRA  = 0x00;
> PORTB = 0x00;
> DDRB  = 0x00;
> PORTC = 0x00;
> DDRC  = 0x00;
> PORTD = 0x00;
> DDRD  = 0x00;
> }


> //TIMER0 initialize - prescale:256
> // WGM: CTC
> // desired value: 1mSec
> // actual value:  1,011mSec (-1,1%)
> void timer0_init(void)
> {
> TIMSK0=0x02;
> TIFR0=0x00;
> TCCR0A=0x02;
> TCCR0B=0x04;
> TCNT0=0xB2;
> OCR0A=0x4E;
> OCR0B=0x4E;
> GTCCR=0x00;
> }


> //*****************************************************************************
> int putchar(char c) {
> //*****************************************************************************
>   if (_textmode && c == '\n')
>     putchar('\r');
>   while ( !(UCSR1A & (1<<UDRE1)) )
>     ;
>   UDR1 = c;
>   return c;
> }


> void timer0_comp_isr(void)
> {
>   //  Timer/Counter0 Compare Match A
> }


> //UART1 initialize
> // desired baud rate:115200
> // actual baud rate:113636 (1,4%)
> // char size: 8 bit
> // parity: Disabled
> void uart1_init(void)
> {
> UDR1=0x00;
> UCSR1A=0x00;
> UCSR1B=0x08;
> UCSR1C=0x06;
> UBRR1H=0x00;
> UBRR1L=0x0A;
> }


> //ADC initialize
> // Conversion time: 83uS
> void adc_init(void)
> {
> ADMUX  = 0x00;
> ADCSRA = 0b10101111;
> ADCSRB = 0b00000011;
> ADCH   = 0x00;
> ADCL   = 0x00;
> DIDR0  = 0b11111111;
> }


> //*****************************************************************************
> void adc_isr(void) {
> //*****************************************************************************
>   static unsigned char input_index;

>   printf("%d\n\r", adc_data[7]);    //for debug only

>   adc_data[input_index] = ADC;

>   if(++input_index == 8)
>     input_index = 0;

>   ADMUX = (0b10000000 + input_index);
> }


> //call this routine to initialize all peripherals
> void init_devices(void)
> {
>  //stop errant interrupts until set up
>  CLI(); //disable all interrupts
>  port_init();
>  timer0_init();
>  uart1_init();
>  adc_init();

> TIMSK0 = 0x02;
> TIMSK2 = 0x00;
> MCUCR  = 0x00 ;
> PCMSK0 = 0x00;
> PCMSK1 = 0x00;
> PCMSK2 = 0x00;
> PCMSK3 = 0x00;
> EIMSK  = 0x00 ;
> EICRA  = 0x00 ;
> PCICR  = 0x00 ;
> TIMSK1 = 0x00;
> MCUCR  = 0x00 ;
> PRR0   = 0x00   ;
>  SEI(); //re-enable interrupts
>  //all peripherals are now initialized
> }


> //
> void main(void)
> {
>  init_devices();
>  //insert your functional code here...
> }

> If I insert these few lines in a present and to enhance source the
> adc-isr doesn't trigger anymore.

> In my project I use the 'uartintr.c' and 'uart.h' buffer from the iccavr
> examples-directory. I've removed the "while" statements within the
> uart-isrs using the query 'DataInReceiveBuffer' instead. Disabling the
> two uart-isrs (RXC & udre) the adc works like with the little example above!

> Have you any idea solving this problem?

> Many thanks and best Regards,
> Rainer



> John Baraclough schrieb:
>> Hi Rainer,
>>
>> A better method of ensuring that you have the correct header file is
>> to put the line:
>>
>> #include <iccioavr.h>
>>
>> instead and then the compiler does the work for you.
>>
>> Firstly try the ADC in free-running mode to check that the ISR is
>> serviced correctly. Then try starting the conversion by setting the
>> ADSC bit in the Timer0 compare ISR. That will help to narrow down the
>> problem area. Don't forget that the first conversion takes 25 clocks
>> rather than the 13 clocks of subsequent conversions.
>>
>> Let us know what you find.
>>
>> All the best for now,
>> John
>>


>>
>> Rainer Dehnert wrote:
>>> Hi Steven,
>>>
>>> I included <iom644pv.h>. Should be the right one...
>>>
>>> Thanks
>>> Rainer
>>>
>>>
>>
>> _______________________________________________
>> Icc-avr mailing list
>> Icc-avr at imagecraft.com
>> http://dragonsgate.net/mailman/listinfo/icc-avr
>>
>>

> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr



More information about the Icc-avr mailing list