SlideShare una empresa de Scribd logo
1 de 4
Descargar para leer sin conexión
Receive.c

/* Since we never know when we are going to get data, we write a
Interrupt Routine that will execute on receiving data.
   You can write different conditions depending on your convenience to
test whether data reception is complete and also
   whether it is accurate
*/


#include <avr/io.h>
#include <avr/interrupt.h>

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

void initusart(void);                                  // Set the baud
rate ( communication speed in bits per second)

int i=0;                        // Global variable acts as a counter

unsigned char str[8];     // String to be sent, declared global if to be
used in interrupts

int main (void)
{
      DDRA=0xff;        // Initialise all ports
      DDRC=0xff;
      DDRB=0xff;
      DDRD=0X00;        // Set PORTD for input, since it contains RX and
TX pins
      PORTB=0x00;       // Set all ports
      PORTC=0xff;
      PORTA=0x01;
      PORTD=0XFF;       // Pull up PORTD
      initusart();      // Initialise USART
      while(1);         // Infinite loop to keep the program running
      }



ISR(USART_RXC_vect)
{
      PORTC -= 0x01;         // Decrement PORTC by one
      str[i]=UDR;            // Load the character in the receive buffer
to string variable
      if (str[6]=='g') { PORTC=0x00;}   // Condition to test accuracy of
last variable
      i++;                   // Increment counter to read next character

}


void initusart(void)
{
   UCSRB |= (1 << RXEN);                                 // Turn on the
transmission and reception circuitry
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit
character sizes
   UBRRL = BAUD_PRESCALE;         // Load lower 8-bits of the baud rate
value into the low byte of the UBRR register

   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate
value into the high byte of the UBRR register
   UCSRB |= (1 << RXCIE);                                // Enable the
USART Recieve Complete interrupt (USART_RXC)
   sei();                         // Enable global interrupts, necessary
if you want to use ANY interrupt

}
Transmit.c

/* The execution of the code below is such: After initialising USART, I
call a send function to transmit the character.
    An interrupt is triggered on completion, the counter goes to the next
character and calls the send function. So,
    the exectution will be in the form of continuous loops and will enter
the main function only on completion of
    transmission of all characters
*/



#include <avr/io.h>
#include <avr/interrupt.h>

#define USART_BAUDRATE 9600                                // Set the
baud rate ( communication speed in bits per second)
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) //
Prescale factor,calculated automatically

void initusart(void);          // Function for USART initialisation

void send(void);               // Function to send characters

int i=0;                      // Global variable acts as a counter

unsigned char str[8]="Testing"; // String to be sent, declared global if
to be used in interrupts

int main (void)
{
   initusart();                         // Initialise USART
   send();                              // Call send function
   while(1);                            // Infinite loop to keep the
program running
   }

void send()
{ if (i<7)                              // Send the character only if i
is less than string size
      {
      UDR=str[i];                       // Loads the character into the
transmit buffer
      }

}


ISR(USART_TXC_vect)                     // Interrupt Service Routine,
fires on Transmit Complete (Tx C)
{
      i++;                              // Increments i to send next
character
UCSRA &= (0 << TXC);             // Clears the Transmit complete
flag, Do this to avoid errors
      send();                          // Call the send function again
}


void initusart(void)
{
   UCSRB |= (1 << TXEN);                                 // Turn on the
transmission and reception circuitry
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit
character sizes
   UBRRL = BAUD_PRESCALE;         // Load lower 8-bits of the baud rate
value into the low byte of the UBRR register

   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate
value into the high byte of the UBRR register
   UCSRB |= (1 << RXCIE);                                // Enable the
USART Transmit Complete interrupt (USART_TXC)
   sei();                         // Enable global interrupts, necessary
if you want to use ANY interrupt

}

Más contenido relacionado

Destacado

What Is Literary Criticism[1]2
What Is Literary Criticism[1]2What Is Literary Criticism[1]2
What Is Literary Criticism[1]2
makeefer
 
The Abc’s of Dad And Me
The Abc’s of Dad And MeThe Abc’s of Dad And Me
The Abc’s of Dad And Me
sunnymel20
 
The ABC's of Dad and Me
The ABC's of Dad and MeThe ABC's of Dad and Me
The ABC's of Dad and Me
sunnymel20
 
F1inschools Design
F1inschools DesignF1inschools Design
F1inschools Design
Kry Said
 
Dump The Tubes The Economic Case For Led Backlighting Revised
Dump The Tubes   The Economic Case For Led Backlighting   RevisedDump The Tubes   The Economic Case For Led Backlighting   Revised
Dump The Tubes The Economic Case For Led Backlighting Revised
andyclark
 
Chase Oaks VBX - Monday - Greatness of the Journey
Chase Oaks VBX - Monday - Greatness of the JourneyChase Oaks VBX - Monday - Greatness of the Journey
Chase Oaks VBX - Monday - Greatness of the Journey
Jason Loveless
 
VC_flier_HYD.compressed
VC_flier_HYD.compressedVC_flier_HYD.compressed
VC_flier_HYD.compressed
Suneel Sharma
 
AGUILAS 2009
AGUILAS 2009AGUILAS 2009
AGUILAS 2009
paobazzi
 
Almost vs San Juan2009
Almost vs San Juan2009Almost vs San Juan2009
Almost vs San Juan2009
paobazzi
 

Destacado (20)

What Is Literary Criticism[1]2
What Is Literary Criticism[1]2What Is Literary Criticism[1]2
What Is Literary Criticism[1]2
 
Crew, FOIA,Documents 017782- 017823
Crew, FOIA,Documents 017782- 017823Crew, FOIA,Documents 017782- 017823
Crew, FOIA,Documents 017782- 017823
 
Websense Hosted Email Security
Websense Hosted Email SecurityWebsense Hosted Email Security
Websense Hosted Email Security
 
Rest In Peace Tribute Tour 7 23 09
Rest In Peace Tribute Tour 7 23 09Rest In Peace Tribute Tour 7 23 09
Rest In Peace Tribute Tour 7 23 09
 
SADI SWSIP '09 'cause you can't always GET what you want!
SADI SWSIP '09  'cause you can't always GET what you want!SADI SWSIP '09  'cause you can't always GET what you want!
SADI SWSIP '09 'cause you can't always GET what you want!
 
PPT母版_简约_v0.1
PPT母版_简约_v0.1PPT母版_简约_v0.1
PPT母版_简约_v0.1
 
Crew, Foia, Documents 008159 - 008236
Crew, Foia, Documents 008159 - 008236Crew, Foia, Documents 008159 - 008236
Crew, Foia, Documents 008159 - 008236
 
The Abc’s of Dad And Me
The Abc’s of Dad And MeThe Abc’s of Dad And Me
The Abc’s of Dad And Me
 
The ABC's of Dad and Me
The ABC's of Dad and MeThe ABC's of Dad and Me
The ABC's of Dad and Me
 
F1inschools Design
F1inschools DesignF1inschools Design
F1inschools Design
 
The Scientific Method on the Semantic Web
The Scientific Method on the Semantic WebThe Scientific Method on the Semantic Web
The Scientific Method on the Semantic Web
 
Tutorial - Creating SADI semantic-web-services
Tutorial - Creating SADI semantic-web-servicesTutorial - Creating SADI semantic-web-services
Tutorial - Creating SADI semantic-web-services
 
Dump The Tubes The Economic Case For Led Backlighting Revised
Dump The Tubes   The Economic Case For Led Backlighting   RevisedDump The Tubes   The Economic Case For Led Backlighting   Revised
Dump The Tubes The Economic Case For Led Backlighting Revised
 
Chase Oaks VBX - Monday - Greatness of the Journey
Chase Oaks VBX - Monday - Greatness of the JourneyChase Oaks VBX - Monday - Greatness of the Journey
Chase Oaks VBX - Monday - Greatness of the Journey
 
Crew documents 020549 - 020563
Crew documents 020549 - 020563Crew documents 020549 - 020563
Crew documents 020549 - 020563
 
Alfresco企业内容管理标准方案
Alfresco企业内容管理标准方案Alfresco企业内容管理标准方案
Alfresco企业内容管理标准方案
 
VC_flier_HYD.compressed
VC_flier_HYD.compressedVC_flier_HYD.compressed
VC_flier_HYD.compressed
 
AGUILAS 2009
AGUILAS 2009AGUILAS 2009
AGUILAS 2009
 
Pysec
PysecPysec
Pysec
 
Almost vs San Juan2009
Almost vs San Juan2009Almost vs San Juan2009
Almost vs San Juan2009
 

Similar a USART

(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program
Dimz I
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 code
pradipakv
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdf
amarnathmahajansport
 
TCP IP
TCP IPTCP IP
TCP IP
hivasu
 
Gsm modem interfacing with pic microcontroller
Gsm modem interfacing with pic microcontrollerGsm modem interfacing with pic microcontroller
Gsm modem interfacing with pic microcontroller
Yonas Andualem
 

Similar a USART (20)

codings related to avr micro controller
codings related to avr micro controllercodings related to avr micro controller
codings related to avr micro controller
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
#include LPC17xx.h#include Lights.h#include traffic_fo.docx
#include LPC17xx.h#include Lights.h#include traffic_fo.docx#include LPC17xx.h#include Lights.h#include traffic_fo.docx
#include LPC17xx.h#include Lights.h#include traffic_fo.docx
 
PIC and LCD
PIC and LCDPIC and LCD
PIC and LCD
 
Uart
UartUart
Uart
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program(381877808) 102054282 5-listing-program
(381877808) 102054282 5-listing-program
 
Microcontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docxMicrocontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docx
 
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 code
 
Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
UART
UARTUART
UART
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdf
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
TCP IP
TCP IPTCP IP
TCP IP
 
Product catlog
Product catlogProduct catlog
Product catlog
 
CC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver ModuleCC2500 Wireless Trans-receiver Module
CC2500 Wireless Trans-receiver Module
 
USART.pptx
USART.pptxUSART.pptx
USART.pptx
 
Gsm modem interfacing with pic microcontroller
Gsm modem interfacing with pic microcontrollerGsm modem interfacing with pic microcontroller
Gsm modem interfacing with pic microcontroller
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

USART

  • 1. Receive.c /* Since we never know when we are going to get data, we write a Interrupt Routine that will execute on receiving data. You can write different conditions depending on your convenience to test whether data reception is complete and also whether it is accurate */ #include <avr/io.h> #include <avr/interrupt.h> #define USART_BAUDRATE 9600 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) void initusart(void); // Set the baud rate ( communication speed in bits per second) int i=0; // Global variable acts as a counter unsigned char str[8]; // String to be sent, declared global if to be used in interrupts int main (void) { DDRA=0xff; // Initialise all ports DDRC=0xff; DDRB=0xff; DDRD=0X00; // Set PORTD for input, since it contains RX and TX pins PORTB=0x00; // Set all ports PORTC=0xff; PORTA=0x01; PORTD=0XFF; // Pull up PORTD initusart(); // Initialise USART while(1); // Infinite loop to keep the program running } ISR(USART_RXC_vect) { PORTC -= 0x01; // Decrement PORTC by one str[i]=UDR; // Load the character in the receive buffer to string variable if (str[6]=='g') { PORTC=0x00;} // Condition to test accuracy of last variable i++; // Increment counter to read next character } void initusart(void)
  • 2. { UCSRB |= (1 << RXEN); // Turn on the transmission and reception circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC) sei(); // Enable global interrupts, necessary if you want to use ANY interrupt }
  • 3. Transmit.c /* The execution of the code below is such: After initialising USART, I call a send function to transmit the character. An interrupt is triggered on completion, the counter goes to the next character and calls the send function. So, the exectution will be in the form of continuous loops and will enter the main function only on completion of transmission of all characters */ #include <avr/io.h> #include <avr/interrupt.h> #define USART_BAUDRATE 9600 // Set the baud rate ( communication speed in bits per second) #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) // Prescale factor,calculated automatically void initusart(void); // Function for USART initialisation void send(void); // Function to send characters int i=0; // Global variable acts as a counter unsigned char str[8]="Testing"; // String to be sent, declared global if to be used in interrupts int main (void) { initusart(); // Initialise USART send(); // Call send function while(1); // Infinite loop to keep the program running } void send() { if (i<7) // Send the character only if i is less than string size { UDR=str[i]; // Loads the character into the transmit buffer } } ISR(USART_TXC_vect) // Interrupt Service Routine, fires on Transmit Complete (Tx C) { i++; // Increments i to send next character
  • 4. UCSRA &= (0 << TXC); // Clears the Transmit complete flag, Do this to avoid errors send(); // Call the send function again } void initusart(void) { UCSRB |= (1 << TXEN); // Turn on the transmission and reception circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UCSRB |= (1 << RXCIE); // Enable the USART Transmit Complete interrupt (USART_TXC) sei(); // Enable global interrupts, necessary if you want to use ANY interrupt }