/****************************************** ******************************************* USART.c "Using 8-N-1 serial port parameter setting or configuration in asynchronous mode." Created: 19-03-2013 12:19:26 Author: Hitesh ******************************************* ******************************************* itoa() added on 22 June 2013 ******************************************* ******************************************/ #include
#define F_CPU 16000000UL #include #define BAUDRATE 9600 // The baud rate we want to use #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1) //Declaration of functions void USART_init (void); unsigned char USART_receive (void); o void USART_send (unsigned char data); duino void USART_putstring (char *Stringptr);
//function to initialize //function to receive one char in arduin //function to send one character from ar //function to send a whole string
char String[]="Data coming from serial communication"; /*String[] is in fact an array but when we put the text between the " " symbols the compiler threats it as a String and automatically puts the null termination character in the end of the text*/ int main(void) { USART_init(); //Call the USART initialization code int count = 0; unsigned char buffer[7]; //buffer for itoa() while(1) { count++; itoa(count, itoa(count, buffer, buffer, 10); 10); //integer to string string conversing conversing USART_putstring("\r\n "); // Send CR LF (Carriage Return & Line Feed to ter minal) USART_putstring(buffer); // Send whats in buffer USART_putstring(" "); // Sends space USART_putstring(String); USART_putstring(String); /*Pass the string to the USART_putstring USART_putstring func func tion and sends it over the serial*/
_delay_ms(1000); _delay_ms(1000); _delay_ms(1000); _delay_ms(1000); _delay_ms(1000); /*Delay for 5 seconds so it will re-send the string every 5 seconds. We can also put some more operations
here :) like turning a sequence of leds on and then off e.t.c. _delay_ms(5000); note:why would the delay of 5 second work? - _delay_ms() can provide max 1 sec delay, it'll work with decreased resolution In this mode _delay_ms() will work with a resol ution of 1/10 ms, providing delays up to 6.5535 seconds (independent from CPU frequency).*/ } return 0; } //Definitions of initially declared functions void USART_init(void) { UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8); UBRR0L = (uint8_t)(BAUD_PRESCALLER); UCSR0B = (1<
is loaded into that register it will be sent by the USART.*/ void USART_putstring(char *StringPtr) { while(*StringPtr != 0x00) /*Here we check if there is still more cha rs to send, this is done checking the actual char an d see if it is different from the null char*/ { USART_send(*StringPtr); //Using the simple send function we send o ne char at a time StringPtr++; //We increment the pointer so we can read the next char } }