binaire.c 2.44 KB
#include <avr/io.h>			// for the input/output register
#include <avr/interrupt.h> 	
#include <util/delay.h>  
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define PRESCALER 1024
#define TIME_SLOT 20
#define BAUDRATE	103

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

char Donnee[24];
float temp;

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_msg2(char Donnee[]){
 
    while (( UCSR0A & (1<<UDRE0))  == 0){};
            
            for (int i = 0; i < strlen(Donnee); i++){ 
                while (( UCSR0A & (1<<UDRE0))  == 0){};
                UDR0 = Donnee[i]; 
                if (i == (strlen(Donnee) - 1)){
                    send_serial('\n');
                    send_serial('\r');
                }
            } 
}

void send_msg(char Donnee[]){
        int i=0;
      for(i=0; i<strlen(Donnee); i++){
          if (i==strlen(Donnee)-1){
              send_serial(Donnee[i]);
              send_serial('\n');
              send_serial('\r');
        }
          else{
              send_serial(Donnee[i]);
        } 
        _delay_ms(100);
        }
}


// équivalent de analogueRead()
int analogReadNew(uint8_t pin) {
  /*
  // Définition de la référence de tension
  ADMUX |= (1 << REFS0);
  // On sélectionne notre pin
  ADMUX |= pin & 0x07;
  
  // 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);
  */
  ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
  ADMUX |= (1<<REFS0)|(1<<ADLAR);
  ADMUX = (ADMUX&0xf0)|pin;
  ADCSRA |= (1<<ADEN);
  
  ADCSRA |= (1<<ADSC);
  while(bit_is_set(ADCSRA, ADSC));
  return ADCH;
}

int main()
{
   int reading;
   init_serial();
   while(1){
    reading=analogReadNew(0);
    temp = reading * 1.9607843;
    dtostrf(temp, 4, 1, Donnee);
    //send_serial('T');
    //send_serial('=');
    send_msg2(Donnee);
    _delay_ms(2000);
   }
   return 0;  
}