Blame view

test_arduino_serial.c 1.19 KB
022e6030   pifou   Test de la com se...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <avr/io.h> // for the input/output registers
  #include <util/delay.h> // for _delay_ms
  
  // For the serial port
  #define CPU_FREQ 16000000L // Assume a CPU frequency of 16Mhz
  
  //compile -> makefile
  
  void init_serial(int speed){
  	/* Set baud rate */
  	UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;
  
  	/* Enable transmitter & receiver */
  	UCSR0B = (1<<TXEN0 | 1<<RXEN0);
  
  	/* Set 8 bits character and 1 stop bit */
  	UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
  
  	/* Set off UART baud doubler */
  	UCSR0A &= ~(1 << U2X0);
  }
  
  void send_serial(unsigned char c){
  	loop_until_bit_is_set(UCSR0A, UDRE0);
  	UDR0 = c;
  }
  
  unsigned char get_serial(void) {
  	loop_until_bit_is_set(UCSR0A, RXC0);
  	return UDR0;
  }
  
  // For I/O handling (examples for PIN 8 as output and PIN 2 as input)
  void output_init(void){
  	DDRB |= 0x01; // PIN 8 as output
  }
  
  void output_set(unsigned char value){
  	if(value == 0) PORTB &= 0xfe;
  	else PORTB |= 0x01;
  }
  
  void input_init(void){
  	DDRD &= 0xfb; // PIN 2 as input
  	PORTD |= 0x04; // Pull-up activated on PIN 2
  }
  
  unsigned char input_get(void){
  	return ((PIND & 0x04) != 0) ? 1 : 0;
  }
  
  // Dummy main
  int main(void){
  	init_serial(9600);
  	while(1){
  		send_serial('A');
  		_delay_ms(500);
  	}
  	return 0;
  }