0
\$\begingroup\$

I have found code of serial communication for attiny84.https://electronut.in/serial-communications-with-the-attiny84/ If I compile this code in Arduino Ide then this code is running.I see hello world text.But If I compile this code in Atmel Studio,I don't see error.But I saw —g'ÿ´Ù¶¶;~ instead of hello world.this text very absurd.What is the problem?

#define  F_CPU 8000000UL
#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>



char pinTX =1;
volatile unsigned char currByte = 0;
volatile int sendingData = 0;

// initialize
void init_serial()
{
    // set TX pin as output
    DDRB |= (1 << pinTX);

    // set up timer1:
    cli();
    // 16 bit timer
    // Divide by 1
    TCCR1B |= (1<<CS10);
    // Count cycles - (1/9600)*8000000
    OCR1A = 833;
    // Put Timer/Counter1 in CTC mode
    TCCR1B |= 1<<WGM12;
    // set interrupt flag
    TIMSK1 |= 1<<OCIE1A;
    sei();
}

// send a byte
void send_byte(char data)
{
    // set flag
    sendingData = 1;
    // set current data byte
    currByte = data;
    // wait till done
    while(sendingData);
}

// which bit was sent last? (0-10)
// -1 implies none sent
volatile char bitNum = -1;

// 16-bit timer CTC handler - Interrupt Service Routine
ISR(TIM1_COMPA_vect)
{
    // serial data sent as:
    //
    // 9600 8 N 1
    //
    // start(low)-0-1-2-3-4-5-6-7-stop(high)-idle(high)-idle(high)
    // 12 bits sent per packet

    // idle => high
    if(!sendingData) {
        PORTB |= (1 << pinTX);
    }
    else {

        if(bitNum < 0) {
            // start bit - low
            PORTB &= ~(1 << pinTX);
            // set bit number
            bitNum = 0;
        }
        else if(bitNum >= 8) {
            // stop bit - high
            PORTB |= (1 << pinTX);
            // increment
            bitNum++;

            // send 2 idle - high - bits
            // not necessary strictly speaking
            if(bitNum == 10) {
                // done
                sendingData = 0;
                // unset bit number
                bitNum = -1;
            }
        }
        else {
            // data bits:

            // bit num is in [0, 7]

            // extract relevant bit from data
            char dataBit = currByte & (1 << bitNum);
            if(dataBit) {
                PORTB |= (1 << pinTX);
            }
            else {
                PORTB &= ~(1 << pinTX);
            }

            // update bit number
            bitNum++;
        }
    }
}


// write null terminated string
void send_str(const char* str)
{
    int len = strlen(str);
    int i;
    for (i = 0; i < len; i++) {
        send_byte(str[i]);
    }
}


int main (void)
{
    init_serial();

    while (1) {

        // print count


        send_str("hello world");

        _delay_ms(200);
    }

    return 1;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This might be a baud rate mismatch. Are the fuses set the same way in both attempts? Can you look at the output with a scope or logic analyzer or couple it into a PC soundcard through a small capacitor? Or even send something like 0x55 (see electronics.stackexchange.com/questions/425348/…) on a pin connected to a piezo disc and listen if the tone is the same? \$\endgroup\$ – Chris Stratton Mar 4 at 18:15
  • \$\begingroup\$ You might also see if there are relevant examples in the ASF shipped with Atmel studio. \$\endgroup\$ – Chris Stratton Mar 4 at 18:16
  • \$\begingroup\$ Are you running on the bare parts or an arduino board? Is there an on-board crystal oscillator or are you using the internal calibrate one? Arduino boards (at least, the most common ones) ran at 16MHz. You define the frequency as 8MHz in that first line, but raw AVRs default to the internal 8MHz divided by 8 for a 1 MHz clock rate. Weird characters almost always point to a clock rate/baud mismatch. \$\endgroup\$ – Kurt E. Clothier May 6 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.