Butterfly USART

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to calibrate my butterfly and then send strings to hyper terminal. I do not really understand the calibration code. Some one please explain how to calibrate my butterfly and why my USART wont send characters.

Thanks in advance.
Here is the calibration code.

void OSCCAL_calibration()
{
    unsigned char calibrate = 0;
    int temp;
    unsigned char tempL;

    CLKPR = (1<<CLKPCE);        // set Clock Prescaler Change Enable
    // set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz
    CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
    
    TIMSK2 = 0;             //disable OCIE2A and TOIE2

    ASSR = (1<<AS2);        //select asynchronous operation of timer2 (32,768kHz)
    
    OCR2A = 200;            // set timer2 compare value 

    TIMSK0 = 0;             // delete any interrupt sources
        
    TCCR1B = (1<<CS10);     // start timer1 with no prescaling
    TCCR2A = (1<<CS20);     // start timer2 with no prescaling

    while((ASSR & 0x01) | (ASSR & 0x04));       //wait for TCN2UB and TCR2UB to be cleared

    _delay_loop_2(30000);    // wait for external crystal to stabilise
    
    while(!calibrate)
    {
        cli(); // mt __disable_interrupt();  // disable global interrupt
        
        TIFR1 = 0xFF;   // delete TIFR1 flags
        TIFR2 = 0xFF;   // delete TIFR2 flags
        
        TCNT1H = 0;     // clear timer1 counter
        TCNT1L = 0;
        TCNT2 = 0;      // clear timer2 counter
           
        // shc/mt while ( !(TIFR2 && (1<<OCF2A)) );   // wait for timer2 compareflag    
        while ( !(TIFR2 & (1<<OCF2A)) );   // wait for timer2 compareflag

        TCCR1B = 0; // stop timer1

        sei(); // __enable_interrupt();  // enable global interrupt
    
        // shc/mt if ( (TIFR1 && (1<<TOV1)) )
        if ( (TIFR1 & (1<<TOV1)) )
        {
            temp = 0xFFFF;      // if timer1 overflows, set the temp to 0xFFFF
        }
        else
        {   // read out the timer1 counter value
            tempL = TCNT1L;
            temp = TCNT1H;
            temp = (temp << 8);
            temp += tempL;
        }
    
        if (temp > 6250)
        {
            OSCCAL--;   // the internRC oscillator runs to fast, decrease the OSCCAL
        }
        else if (temp < 6120)
        {
            OSCCAL++;   // the internRC oscillator runs to slow, increase the OSCCAL
        }
        else
            calibrate = 1;   // the interRC is correct

        TCCR1B = (1<<CS10); // start timer1
    }
}

And the USART code, i do not understand the 2x speed. What is it for?

void initUSART()
{

// Set baud rate 9600
    UBRRH = (BAUD_PRESCALE >> 8);
    UBRRL = BAUD_PRESCALE;

    // Enable 2x speed
    UCSRA = (1<<U2X);

    // Enable receiver. Enable Transmitter. Inable Recieve Complete interrupt
    UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(0<<UDRIE);

    // Async. mode, 8N1
    UCSRC = (0<<UMSEL)|(0<<UPM0)|(0<<USBS)|(3<<UCSZ0)|(0<<UCPOL);
}


/*ISR

Interrupt, gets character
*/
ISR(USART0_RX_vect)
{
  
   ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived"
	makeString(ReceivedByte);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking back at your other posts (where you also like to ask a brief questions with a lot of code written by someone else), it seems to me that have already asked about calibration and had an answer and that you've already asked about USARTs and had and answer and even claimed to have it working.

If you are a serious student of AVRs then read the sticky:
General Information about Posting in the AVR Forum
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=18856
especially the link: http://www.catb.org/~esr/faqs/smart-questions.html

Smiley

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are right I should not have posted all the code and expect someone to debug it for me. Anyways i got it to work. The problem was in defining the BAUD_PRESCALE. I understand everything now except the U2X bit, my baud rate is now 19200, if I disable the U2X bit and try hyper terminal with 9600 baud rate I get random characters. In the data sheet U2X says to double the transmission speed. So why is it when it is disable I do not get 9600 baud rate?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My guess would be because it has nothing to do with the problem.

Is all your data 'random'? If so then you are almost there, and as a great guru has in his signature, 99.95 of UART problems are due to miss setting the clock. The Butterfly calibration code sets the clock to one rate and the UART Init sets it to another so sometime folks are actually using double or half the clock they think they are. The code is available and the data on setting the clock is in the data sheet. Please post your solution when you get it.

And BTW, my answer doesn't mean I take you seriously, but if you do, in fact post your fix and your questions start looking like you read the 'smart questions' link, then I will take you seriously.

Smiley