BPLightContraption
 All Classes Namespaces Files Functions Variables Macros Pages
serial.c
Go to the documentation of this file.
1 /*! \file
2  * \brief Microcontroller serial communications functions
3  * \details Prototypes in serial.h
4  * \author Benjamin Pritchard (ben@bennyp.org)
5  * \copyright 2013 Benjamin Pritchard. Released under the MIT License
6  */
7 
8 #include <avr/io.h>
9 
10 #include "bits.h"
11 #include "serial.h"
12 
13 void Serial_init(void)
14 {
15  /* Synchronous mode */
16  /*bit_set(UCSR0C, UMSEL00);*/
17 
18  /* Set baud rate */
19  UBRR0H = (uint8_t)(MYUBRR>>8);
20  UBRR0L = (uint8_t)MYUBRR;
21 
22  /* Enable receiver and transmitter */
23  bit_set(UCSR0B, RXEN0);
24  bit_set(UCSR0B, TXEN0);
25 
26  /* Set frame format: 8data, 1stop bit */
27  bit_set(UCSR0C, UCSZ00);
28  bit_set(UCSR0C, UCSZ01);
29 
30  /* enable the interrupts */
31  bit_set(UCSR0B, RXCIE0);
32 }
33 
34 
35 uint8_t Serial_receive(void)
36 {
37  /* Wait for data to be received */
38  while ( !(bit_get(UCSR0A, RXC0)) );
39 
40  /* Get and return received data from buffer */
41  return UDR0;
42 }
43 
44 
45 void Serial_flush(void)
46 {
47  /* disable interrupts */
48  bit_clear(UCSR0B, RXCIE0);
49 
50  /* disable the receiver and transmitter */
51  bit_clear(UCSR0B, RXEN0);
52  bit_clear(UCSR0B, TXEN0);
53 
54  /* wait for any transmissions to finish */
55  while(bit_get(UCSR0A, TXC0));
56 
57  /* re-enable transmitter & receiver */
58  bit_set(UCSR0B, RXEN0);
59  bit_set(UCSR0B, TXEN0);
60 
61  /* re-enable interrupts */
62  bit_set(UCSR0B, RXCIE0);
63 }
64 
65 
66 uint8_t Serial_needsreading(void)
67 {
68  return bit_get(UCSR0A, RXC0);
69 }
70 
71 
72 void Serial_send( uint8_t c )
73 {
74  /* Wait for empty transmit buffer */
75  while ( ! (bit_get(UCSR0A, UDRE0)) );
76 
77  /* Put data into buffer, sends the data */
78  UDR0 = c;
79 }
80 
81 
82 void Serial_send1( uint8_t c)
83 {
84  Serial_send( 1 );
85  Serial_send( c );
86 }
87 
88 
89 void Serial_send2( uint8_t c1 , uint8_t c2)
90 {
91  Serial_send( 2 );
92  Serial_send( c1 );
93  Serial_send( c2 );
94 }
95 
96 
97 void Serial_send3( uint8_t c1 , uint8_t c2, uint8_t c3)
98 {
99  Serial_send( 3 );
100  Serial_send( c1 );
101  Serial_send( c2 );
102  Serial_send( c3 );
103 }
104 
105 
106 void Serial_send4( uint8_t c1 , uint8_t c2, uint8_t c3,
107  uint8_t c4)
108 {
109  Serial_send( 4 );
110  Serial_send( c1 );
111  Serial_send( c2 );
112  Serial_send( c3 );
113  Serial_send( c4 );
114 }
115 
116 
117 void Serial_send5( uint8_t c1 , uint8_t c2, uint8_t c3,
118  uint8_t c4, uint8_t c5)
119 {
120  Serial_send( 5 );
121  Serial_send( c1 );
122  Serial_send( c2 );
123  Serial_send( c3 );
124  Serial_send( c4 );
125  Serial_send( c5 );
126 }
127 
128 
129 void Serial_send6( uint8_t c1 , uint8_t c2, uint8_t c3,
130  uint8_t c4, uint8_t c5, uint8_t c6)
131 {
132  Serial_send( 6 );
133  Serial_send( c1 );
134  Serial_send( c2 );
135  Serial_send( c3 );
136  Serial_send( c4 );
137  Serial_send( c5 );
138  Serial_send( c6 );
139 }
140 
141 
142 void Serial_sendarr( const uint8_t * arr, uint8_t len)
143 {
144  uint8_t i;
145  Serial_send(len);
146  for(i = 0; i < len; i++)
147  Serial_send(arr[i]);
148 }
149