Advertisement
Guest User

Untitled

a guest
Apr 24th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1.  
  2. #define F_CPU 9216000UL
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5.  
  6. #define BAUDRATE 230400
  7. #define BAUD_PRESCALLER 8
  8.  
  9. //Declaration of our functions
  10.  
  11. void USART_init(void);
  12. unsigned char USART_receive(void);
  13. void USART_send( unsigned char data);
  14. void USART_putstring(char* StringPtr);
  15.  
  16. char String[]="Hello world!!"; //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
  17.  
  18. int main(void){
  19. USART_init(); //Call the USART initialization code
  20.  
  21. while(1){ //Infinite loop
  22. USART_putstring(String); //Pass the string to the USART_putstring function and sends it over the serial
  23. _delay_ms(5000); //Delay for 5 seconds so it will re-send the string every 5 seconds
  24. }
  25.  
  26. return 0;
  27. }
  28.  
  29. void USART_init(void){
  30.  
  31. UCSR0A = (1<<U2X0);
  32. UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
  33. UBRR0L = (uint8_t)(BAUD_PRESCALLER);
  34. UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  35. UCSR0C = (3<<UCSZ00);
  36. }
  37.  
  38. unsigned char USART_receive(void){
  39.  
  40. while(!(UCSR0A & (1<<RXC0)));
  41. return UDR0;
  42.  
  43. }
  44.  
  45. void USART_send( unsigned char data){
  46.  
  47. while(!(UCSR0A & (1<<UDRE0)));
  48. UDR0 = data;
  49.  
  50. }
  51.  
  52. void USART_putstring(char* StringPtr){
  53.  
  54. while(*StringPtr != 0x00){
  55. USART_send(*StringPtr);
  56. StringPtr++;}
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement