#include //The global interrupt flag is maintained in the I bit of the status register (SREG).
#include //This header file includes the apropriate IO definitions for the device that has been
specified by the -mmcu= compiler command-line switch. This is done by diverting to the
appropriate file which should never be included directly. Some register names common to all
AVR devices are defined directly within , which is included in , but most of the details come
from the respective include file.
#include //The functions in this header file are wrappers around the basic busy-wait functions
from . They are meant as convenience functions where actual time values can be specified rather
than a number of cycles to wait for. The idea behind is that compile-time constant expressions
will be eliminated by compiler optimization so floating-point expressions can be used to
calculate the number of delay cycles needed based on the CPU frequency passed by the macro
F_CPU.
#include \"oi.h\"//This header file includes the apropriate IO definitions
//the #define directive allows the definition of macros within your source code. These macro
definitions allow constant values to be declared for use throughout your code. Macro definitions
are not variables and cannot be changed by your program code like variables.
#define USB 1
#define CR8 2 // toggle between usb and create on CM serial processor
//Methods used in program
void setSerial(uint8_t com);
uint8_t getSerialDestination(void);/*------------------------built in, sends back Serial Destination*/
void writeChar(char c, uint8_t com);/*------------ --taken from command modual manual. sends
data to computer via the USB cable*/
void delay(void);/* Checks the delayed period*/
void byteTx(uint8_t value);/*Transmit a byte over the serial port*/
void Init_Uart(void);/* Initialize the values */
uint8_t byteRx(void);/*------------------------------reads from the serial port*/
//This is main method for the program
void main(void)
{
uint8_t rx_data;
Init_Uart();
/* Writing the character by character using the while loop */
while(1)
{
writeChar(\'H\',USB);
writeChar(\'e\',USB);
writeChar(\'l\',USB);
writeChar(\'l\',USB);
writeChar(\'o\',USB);
writeChar(\' \',USB);
writeChar(\'W\',USB);
writeChar(\'o\',USB);
writeChar(\'r\',USB);
writeChar(\'l\',USB);
writeChar(\'d\',USB);
writeChar(\'!\',USB);
}
}
/*Initialize the values */
void Init_Uart(void)
{
UBRR0 = 59;
UCSR0B = 0x18;
UCSR0C = 0x06;
DDRB = 0X10;
PORTB = 0X10;
}
/*Used to check the delayed time */
void delay(void)
{
int i=0,j=0;
for(i=1;i<=1000;i++)
{
for(j=1;j<=1000;j++)
{
}
}
}
//****************************************************************************
****************
/*the following three functions came directly from the command module manual
they change the flow of data so that when byteTx is called it sends data
from the create to the computer through the USB cable. when it is done sending
data it returns the com port to its original state, giving commands to .
#include avrinterrupt.h The global interrupt flag is maintained.pdf
1. #include //The global interrupt flag is maintained in the I bit of the status register (SREG).
#include //This header file includes the apropriate IO definitions for the device that has been
specified by the -mmcu= compiler command-line switch. This is done by diverting to the
appropriate file which should never be included directly. Some register names common to all
AVR devices are defined directly within , which is included in , but most of the details come
from the respective include file.
#include //The functions in this header file are wrappers around the basic busy-wait functions
from . They are meant as convenience functions where actual time values can be specified rather
than a number of cycles to wait for. The idea behind is that compile-time constant expressions
will be eliminated by compiler optimization so floating-point expressions can be used to
calculate the number of delay cycles needed based on the CPU frequency passed by the macro
F_CPU.
#include "oi.h"//This header file includes the apropriate IO definitions
//the #define directive allows the definition of macros within your source code. These macro
definitions allow constant values to be declared for use throughout your code. Macro definitions
are not variables and cannot be changed by your program code like variables.
#define USB 1
#define CR8 2 // toggle between usb and create on CM serial processor
//Methods used in program
void setSerial(uint8_t com);
uint8_t getSerialDestination(void);/*------------------------built in, sends back Serial Destination*/
void writeChar(char c, uint8_t com);/*------------ --taken from command modual manual. sends
data to computer via the USB cable*/
void delay(void);/* Checks the delayed period*/
void byteTx(uint8_t value);/*Transmit a byte over the serial port*/
void Init_Uart(void);/* Initialize the values */
uint8_t byteRx(void);/*------------------------------reads from the serial port*/
//This is main method for the program
void main(void)
{
uint8_t rx_data;
Init_Uart();
/* Writing the character by character using the while loop */
while(1)
3. ****************
/*the following three functions came directly from the command module manual
they change the flow of data so that when byteTx is called it sends data
from the create to the computer through the USB cable. when it is done sending
data it returns the com port to its original state, giving commands to the create.
*/
//****************************************************************************
****************
uint8_t getSerialDestination(void)
{
if (PORTB & 0x10)
return USB;
else
return CR8;
}
void setSerial(uint8_t com)// uint8_t is unsigned char and com is its value
{//This is the condition of checking the value of unsigned char
if(com == USB)
PORTB |= 0x10;// |= is Bitwise inclusive OR and assignment operator.PORTB |= 0x10 is
same as PORTB=PORTB | 0x10
else if(com == CR8)
PORTB &= ~0x10;//&= is Bitwise AND assignment operator.PORTB &= ~0x10 meanse
PORTB = PORTB & ~0x10
}
//write char is called whenever you want to send data to the computer
void writeChar(char c, uint8_t com)
{
uint8_t originalDestination = getSerialDestination();
if (com != originalDestination)
{
setSerial(com);
delay();
}
byteTx((uint8_t)(c));
if (com != originalDestination)
4. {
setSerial(originalDestination);
delay();//Allow char to xmt
}
}
// Transmit a byte over the serial port
void byteTx(uint8_t value)
{
while(!(UCSR0A & 0x20)) ;
UDR0 = value;
}
uint8_t byteRx(void)
{
while(!(UCSR0A & 0x80)) ;
/* wait until a byte is received */
return UDR0;
}
Solution
#include //The global interrupt flag is maintained in the I bit of the status register (SREG).
#include //This header file includes the apropriate IO definitions for the device that has been
specified by the -mmcu= compiler command-line switch. This is done by diverting to the
appropriate file which should never be included directly. Some register names common to all
AVR devices are defined directly within , which is included in , but most of the details come
from the respective include file.
#include //The functions in this header file are wrappers around the basic busy-wait functions
from . They are meant as convenience functions where actual time values can be specified rather
than a number of cycles to wait for. The idea behind is that compile-time constant expressions
will be eliminated by compiler optimization so floating-point expressions can be used to
calculate the number of delay cycles needed based on the CPU frequency passed by the macro
F_CPU.
#include "oi.h"//This header file includes the apropriate IO definitions
//the #define directive allows the definition of macros within your source code. These macro
definitions allow constant values to be declared for use throughout your code. Macro definitions
are not variables and cannot be changed by your program code like variables.
5. #define USB 1
#define CR8 2 // toggle between usb and create on CM serial processor
//Methods used in program
void setSerial(uint8_t com);
uint8_t getSerialDestination(void);/*------------------------built in, sends back Serial Destination*/
void writeChar(char c, uint8_t com);/*------------ --taken from command modual manual. sends
data to computer via the USB cable*/
void delay(void);/* Checks the delayed period*/
void byteTx(uint8_t value);/*Transmit a byte over the serial port*/
void Init_Uart(void);/* Initialize the values */
uint8_t byteRx(void);/*------------------------------reads from the serial port*/
//This is main method for the program
void main(void)
{
uint8_t rx_data;
Init_Uart();
/* Writing the character by character using the while loop */
while(1)
{
writeChar('H',USB);
writeChar('e',USB);
writeChar('l',USB);
writeChar('l',USB);
writeChar('o',USB);
writeChar(' ',USB);
writeChar('W',USB);
writeChar('o',USB);
writeChar('r',USB);
writeChar('l',USB);
writeChar('d',USB);
writeChar('!',USB);
}
}
/*Initialize the values */
void Init_Uart(void)
6. {
UBRR0 = 59;
UCSR0B = 0x18;
UCSR0C = 0x06;
DDRB = 0X10;
PORTB = 0X10;
}
/*Used to check the delayed time */
void delay(void)
{
int i=0,j=0;
for(i=1;i<=1000;i++)
{
for(j=1;j<=1000;j++)
{
}
}
}
//****************************************************************************
****************
/*the following three functions came directly from the command module manual
they change the flow of data so that when byteTx is called it sends data
from the create to the computer through the USB cable. when it is done sending
data it returns the com port to its original state, giving commands to the create.
*/
//****************************************************************************
****************
uint8_t getSerialDestination(void)
{
if (PORTB & 0x10)
return USB;
else
return CR8;
}
void setSerial(uint8_t com)// uint8_t is unsigned char and com is its value
{//This is the condition of checking the value of unsigned char
7. if(com == USB)
PORTB |= 0x10;// |= is Bitwise inclusive OR and assignment operator.PORTB |= 0x10 is
same as PORTB=PORTB | 0x10
else if(com == CR8)
PORTB &= ~0x10;//&= is Bitwise AND assignment operator.PORTB &= ~0x10 meanse
PORTB = PORTB & ~0x10
}
//write char is called whenever you want to send data to the computer
void writeChar(char c, uint8_t com)
{
uint8_t originalDestination = getSerialDestination();
if (com != originalDestination)
{
setSerial(com);
delay();
}
byteTx((uint8_t)(c));
if (com != originalDestination)
{
setSerial(originalDestination);
delay();//Allow char to xmt
}
}
// Transmit a byte over the serial port
void byteTx(uint8_t value)
{
while(!(UCSR0A & 0x20)) ;
UDR0 = value;
}
uint8_t byteRx(void)
{
while(!(UCSR0A & 0x80)) ;
/* wait until a byte is received */
return UDR0;
}