SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
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

}

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 

Semelhante 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
 

Semelhante 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

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
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
 

Último (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
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
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 

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 }