Programming: views : 690

Here is the code to interface 2 serial devices with AVR

There is two method by which we can interface 2 serial devices with AVR microcontroller. One is by polling method and the other is interrupt based. To create baud rate we can use delay method or a timer interrupt method. Copy and paste code to interface 2 serial devices with AVR microcontroller is given below-


Interfacing 2 serial devices using polling method

//START -set baud rate for 1200bps
unsigned char min_bit_length = 83;
void send_byte(char send_byte){
    char i;
    //start bit
    output_port4 &=~(1<<utx);
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
   
 //1 bit
    if(send_byte & 0b00000001){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//2 bit
    if(send_byte & 0b00000010){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//3 bit
    if(send_byte & 0b00000100){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//4 bit
    if(send_byte & 0b00001000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//5 bit
    if(send_byte & 0b00010000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//6 bit
    if(send_byte & 0b00100000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//7 bit
    if(send_byte & 0b01000000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//8 bit
    if(send_byte & 0b10000000){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
//stop bit
    output_port4 |= (1<<utx);
    for(i=0; i<min_bit_length; i++){_delay_us(10);}//1 bit delay
}
void send_string(char send_string[]){
    unsigned char i=0;      & amp; nbsp;               ; ;     // iterating variable
    unsigned char LENGHT = 0;// length of the word
    for(i=0;i<500; i++){
      & amp; nbsp; if(send_string[i] == NULL){
      & amp; nbsp;     break;
      & amp; nbsp; }
      & amp; nbsp; LENGHT++;
    }
    i=0;
    while(i<LENGHT){send_byte(send_s tring[i]);i++;}
}
//END
#define output_ddr DDRD
#define output_port4 PORTD
//in main()
//initialize this port
output_ddr |= (1<<utx);//as output port
output_port4 |= (1<<utx);//pull up because in idle time uart tx line is high

while(1 ){//main while
send_string("testing transmitter of software uart");// this will send out and you can see it on pc terminal
_delay_ms(1000);   
    }

Above code is made for avr microcontroller and its crystal 12Mhz. for any doubt, ask me in comment below.



Interfacing 2 serial devices using interrupt method

////////////////// ///////////////////////////////////////// /////////////////////////////
////////////////////////////// START SOFTWARE UART /////////////////////////////////////
///////////////////////////////////// ///////////////////////////////////////// //////////
unsigned char baud_delay_one=216;//this variable are only used for tune baudrate
unsigned char baud_delay_one_and_half=184;//this variable are only used for tune baudrate
char bit_count=0;
char uart_rx_byte=0;
char uart_status=0;
#define idle 0
#define transmitting 1
#define receiving 2
#define received_data_pending 3
#define start_bit 4
void init_timer0(){         //TIMER0 initialize for software uart
    TIMSK |= (1<<TOIE0);    //ENABLE 0-TIMER0 OVERFLOW INTERRUPT
    TCCR0 = (0x04);         //NORMAL MODE,INTERNAL CLK,PRESCALER=1:256
    TCNT0=baud_delay_one;        //1 bit length for 9600bps (104us) at 12Mhz crystal
    sei();                    // Interrupt Enabled GLOBALLY
}
ISR(TIMER0_OVF_vect) {
    unsigned char sreg = SREG;
    cli();                        // Disable Global interrupt
    TCNT0=baud_delay_one;                    //1 bit length for 9600bps (104us) at 12Mhz crystal
    bit_count++;
    SREG = sreg;
}
void uart_send_byte(char send_byte){
    TCNT0=baud_delay_one;
    bit_count=0;
    while(1){
        if(bit_count==0){output_port4 &=~(1<<utx);}
        if((bit_count>0) && (bit_count<9)){
            if(send_byte & (1<<(bit_count-1))){output_port4 |= (1<<utx);} else{output_port4 &=~(1<<utx);}
        }
        if(bit_count==9){output_port4 |= (1<<utx);}
        if(bit_count>9){break;}
    }
}

void uart_sent_string(char send_string[]){
    unsigned char i=0;                         // iterating variable
    unsigned char LENGHT = 0;// length of the word
    for(i=0;i<500; i++){
        if(send_string[i] == ''){
            break;
        }
        LENGHT++;
    }
    i=0;
    while(i<LENGHT){uart_send_byte(s end_string[i]);i++;}
}

#define output_ddr DDRD
#define output_port4 PORTD
#define utx 3
//in main()
//initialize this port


init_timer0();//this to create baud rate by overflow interrupt
output_ddr |= (1<<utx);//as output port
output_port4 |= (1<<utx);//pull up because in idle time uart tx line is high

while(1 ){//main while
uart_sent_string("testing transmitter of software uart");// this will send out and you can see it on pc terminal
_delay_ms(1000);   
    }
///////////////////////////////////// ///////////////////////////////////////// //////////
//////////////////////////////// END SOFTWARE UART /////////////////////////////////////
///////////////////////////////////// ///////////////////////////////////////// //////////

Above code is made for avr microcontroller and its crystal 12Mhz. for any doubt, ask me in comment below. 



Share this page with your friends
share via Whatsapp

Posted By :
Mahesh Nigam
(Scientist)
2019-09-27 17:13
See Author's other Published Topics

Happy Teacher’s Day of Dear Madam, Thank You For continually Inspiring me to do my best..
Dear Madam, Thank You For continually Inspiring me to do my best..
Happy teachers day of Thank you for teaching me how to read and write, for guiding me to distinguish between what is wrong and what is right. For allowing me to dream and soar as a kite, thank you for being my...
Thank you for teaching me how to read and write, for guiding me to distinguish between what is wrong and what is right. For allowing me to dream and soar as a kite, thank you for being my...
A good teacher is like a candle of A good teacher is like a candle – it consumes itself to light the way for others..
A good teacher is like a candle – it consumes itself to light the way for others..
The teacher who is indeed wise of The teacher who is indeed wise does not bid you to enter the house of his wisdom but rather leads you..
The teacher who is indeed wise does not bid you to enter the house of his wisdom but rather leads you..
Happy World Teachers' Day! of I am proud to let you know that you have been the great teacher in my life! Happy World Teachers' Day!...
I am proud to let you know that you have been the great teacher in my life! Happy World Teachers' Day!...
Happy teachers day of Without you, we would have been lost. Thank you teacher for guiding us, inspiring us And making us..
Without you, we would have been lost. Thank you teacher for guiding us, inspiring us And making us..
Wishing you a Teacher's Day of I was lucky to have a teacher as wonderful as you are. Wishing you a Teacher's Day that’s full of joyous...
I was lucky to have a teacher as wonderful as you are. Wishing you a Teacher's Day that’s full of joyous...
 Happy Teacher's Day! of Teacher, you have always challenged me to work hard and get good grades. I will always remember you. Happy Teacher's Day!...
Teacher, you have always challenged me to work hard and get good grades. I will always remember you. Happy Teacher's Day!...
Happy Teacher's Day! of The best teachers teach from the heart, not from the book. Thank you for being a wonderful teacher. Happy Teacher's Day!..
The best teachers teach from the heart, not from the book. Thank you for being a wonderful teacher. Happy Teacher's Day!..
Happy Teacher’s Day of Happy Teacher’s Day! It has been an honor to get to learn so many...
Happy Teacher’s Day! It has been an honor to get to learn so many...
Wishing you joy and happiness of Wishing you joy and happiness, you are an amazing teacher, and you only..
Wishing you joy and happiness, you are an amazing teacher, and you only..

Peoples

Comments...

Write Your Comment

Related to Programming

Latest topics




More Categories
latest mobile phones ireps.gov.in irctc.co.in Working Principle Viral Discussion True Love quotes True Love Today is Celebrated For Tips for better life Tips and Hacks Technology News Teachers Day Quotes Study Materials Self Motivating Quotes Science Sad Lines (Sayeri) Romantic shayari quotes Robotics Reviews Results Questions_and_Answers Programming Problem Resolved PHP Codes PCB Design Online Works New Year Quotes Shayari Status My World My Responsibilities Motivational lines Mobile Issues Microcontrollers Lucknow Local News Latest Technology Iphone Homeopathy Historical place in India Heart Touching Love Quotes Health Good Night Quotes Good Morning Quotes GeM.gov.in GST portal Funny Lines (Sayeri) Friendship Quotes Food Recipes Exam Time Table Engineering Project Ideas Electronics Electronic and electrical project making ideas Electricals Educations Desh Bhakti Shayeri Criminal Love Cricket News Corona Updates Christmas Quotes Shayari Status CBSE Updates/Circulars Breaking News Breaking Heart Shayeri Break Up Lines (Sayeri) BirthDay Wishes Best kitchen Tips Best Valentine quotes Shayeri Beauty Hacks Basic Knowledges Attitude quotes shayari Attitude quotes Attitude quotes Assembly Election 2022
Go to top

Important Links

  • Contact Us
  • About Us
  • All copyright © 2020 are reserved by Groomix - designed by Groomix

    MORE

    Programming >
    latest mobile phones >
    ireps.gov.in >
    irctc.co.in >
    Working Principle >
    Viral Discussion >
    True Love quotes >
    True Love >
    Today is Celebrated For >
    Tips for better life >
    Tips and Hacks >
    Technology News >
    Teachers Day Quotes >
    Study Materials >
    Self Motivating Quotes >
    Science >
    Sad Lines (Sayeri) >
    Romantic shayari quotes >
    Robotics >
    Reviews >
    Results >
    Questions_and_Answers >
    Problem Resolved >
    PHP Codes >
    PCB Design >
    Online Works >
    New Year Quotes Shayari Status >
    My World My Responsibilities >
    Motivational lines >
    Mobile Issues >
    Microcontrollers >
    Lucknow Local News >
    Latest Technology >
    Iphone >
    Homeopathy >
    Historical place in India >
    Heart Touching Love Quotes >
    Health >
    Good Night Quotes >
    Good Morning Quotes >
    GeM.gov.in >
    GST portal >
    Funny Lines (Sayeri) >
    Friendship Quotes >
    Food Recipes >
    Exam Time Table >
    Engineering Project Ideas >
    Electronics >
    Electronic and electrical project making ideas >
    Electricals >
    Educations >
    Desh Bhakti Shayeri >
    Criminal Love >
    Cricket News >
    Corona Updates >
    Christmas Quotes Shayari Status >
    CBSE Updates/Circulars >
    Breaking News >
    Breaking Heart Shayeri >
    Break Up Lines (Sayeri) >
    BirthDay Wishes >
    Best kitchen Tips >
    Best Valentine quotes Shayeri >
    Beauty Hacks >
    Basic Knowledges >
    Attitude quotes shayari >
    Attitude quotes >
    Attitude quotes >
    Assembly Election 2022 >

    Peoples

    Latest Comments