Programming: views : 2588

Software Uart for AVR microcontroller tested on 1200 baud rate

Here is copy and paste code for Software UART. In many system design, we need more than one serial communication like GSM module and BT module. To connect these module with a single microcontroller, we need two UART port which is generally not available in AVR microcontroller. I could not found any code while Searching for Software UART code in google. So,i made software uart code for myself. Now, i am share this code so you need not to suffer from it. Following are the copy and paste code for you.


Software Uart code for AVR microcontroller tested on 1200 baud rate

//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;      // iterating variable
    unsigned char LENGHT = 0;// length of the word
    for(i=0;i<500; i++){
      if(send_string[i] == NULL){break; }
      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.



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

Airtel cheating ther customers by incomplete description of recharge pack of Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
साहस और धैर्य का प्रतिक कारगिल विजय दिवस  of साहस और धैर्य का प्रतिक कारगिल विजय दिवस
साहस और धैर्य का प्रतिक कारगिल विजय दिवस
Vaisakhi  of Vaisakhi
Vaisakhi
3 मार्च - विश्व वन्यजीव दिवस of 3 मार्च - विश्व वन्यजीव दिवस
3 मार्च - विश्व वन्यजीव दिवस
1 मार्च- विश्व प्रशंसा दिवस of 1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस of 1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च - शून्य भेदभाव दिवस of 1 march शून्य भेदभाव दिवस
1 march शून्य भेदभाव दिवस
करती रही of पूरा नही बस थोड़ा सा ही समझा होता ....
पूरा नही बस थोड़ा सा ही समझा होता ....
वैलेंटाइन of वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
National Test Agency of UGC NET December 2023 results and download its subject wise cut-off
UGC NET December 2023 results and download its subject wise cut-off
परीक्षा पे चर्चा  of परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।
परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।

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