Tuno_to_rpi.c
1.99 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <avr/io.h> // for the input/output register
#include <avr/interrupt.h>
#include <util/delay.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define PRESCALER 1024
#define TIME_SLOT 20
#define NB_TICK 113
#define BAUDRATE 103
char Donnee[20];
void init_serial(void)
{
/* ACHTUNG : we suppose UBRR value < 0xff */
/* Not true in all case */
uint8_t baudrate = BAUDRATE;
/* Set baud rate */
UBRR0H = 0;
UBRR0L = baudrate;
/* Enable transmitter *///task_led_red();
//task_send_serial('A');
UCSR0B = (1<<TXEN0);
/* Set frame format */
UCSR0C = 0x06;
}
void send_serial(unsigned char c)
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}
void send_msg(char Donnee[]){
//char msg[] = "Bonjour\r\n";
while(1){ //nécessaire pour eviter d'executer la tache une seule fois
int i=0;
for(i=0; i<strlen(Donnee); i++){
send_serial(Donnee[i]);
_delay_ms(100);
}
}
}
// L'équivalent de analogueRead() pour avr
int analogReadNew(uint8_t pin) {
// Pour les cartes Mega
#if defined(__AVR_ATmega1280__)
// Si le pin considéré est de type Ax, on utilise ça
if (pin >= 54) pin -= 54;
// Pour les Uno et Nano
#else
if (pin >= 14) pin -= 14;
#endif
// Définition de la référence de tension
ADMUX |= (1 << REFS0);
// On sélectionne notre pin
ADMUX |= pin & 0x07;
#if defined(ADCSRB) && defined(MUX5)
// Utilisation de MUX5 sur la Mega pour les pins au-dessus de A7
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif
// On lance la conversion
sbi(ADCSRA, ADSC);
// Le bit sera désactivé à la fin de la conversion
while(bit_is_set(ADCSRA, ADSC));
// Lire ADCL en premier est obligatoire, sinon l'ADC se bloque
uint8_t low = ADCL;
// Récupérer le résultat
return (ADCH << 8) | low;
}
void main()
{
init_serial();
while(1){
//send_serial('A');
Send_msg(analogReadNew(0)*0.0048828125*100);
sleep(3000);
}
}