0% found this document useful (0 votes)
32 views2 pages

#Define #Include #Include #Include #Include #Include #Define #Define Void Void Void Void Unsigned Int Int Void

This code initializes an AVR microcontroller for analog to digital conversion (ADC) and serial communication (USART) to measure carbon monoxide (CO) levels. It initializes the ADC to read values from pin PA0, initializes USART for 9600 baud serial transmission, and takes continuous ADC readings to calculate and transmit the CO level in parts per million (ppm) based on the sensor readings.

Uploaded by

comyleejones
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

#Define #Include #Include #Include #Include #Include #Define #Define Void Void Void Void Unsigned Int Int Void

This code initializes an AVR microcontroller for analog to digital conversion (ADC) and serial communication (USART) to measure carbon monoxide (CO) levels. It initializes the ADC to read values from pin PA0, initializes USART for 9600 baud serial transmission, and takes continuous ADC readings to calculate and transmit the CO level in parts per million (ppm) based on the sensor readings.

Uploaded by

comyleejones
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* * AVRGCC1.c * * Created: 6/27/2013 12:06:52 AM * Author: CoMyLee JoNeS */ #define F_CPU 8000000 #include <avr/io.h> #include <avr/interrupt.

h> #include <math.h> #include <stdlib.h> #include <inttypes.h> #define USART_BAUDRATE 9600 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) void adc_init(void); void ADC_read(); uint8_t UM,RS_RL,send; void usart_init(); unsigned int usart_sendch(); int main(void) { DDRA=0x01; DDRC=0x02; DDRD=0x02; while(1) { RS_RL=(255-UM)/UM; // raportul din care putem calcula valoarea CO send=("valoarea CO= ",((1000-100) * (RS_RL-0.3))/(0.1-0.3)," ppm"); // etalonare senzor pentru a obtine valoarea CO } } void adc_init() // initializare ADC { ADMUX=(1<<ADLAR); // selectare tensiune ARef, canal ADC0, aliniere stanga cu precizie de 8biti ADCSRA=(1<<ADEN) | (1<<ADPS2) | (1<<ADPS1); //activare ADC, factor de divizare 64 pentru frecventa } void ADC_read() { ADCSRA|=(1<<ADSC); // incepe conversie while(!(ADCSRA & (1<<ADIF))); // asteapta sfarsitul conversiei ADCSRA|=(1<<ADIF); // sfarsit conversie UM=ADCH; // se memoreaza valoarea conversiei return(ADCH); }

void usart_init() { UCSRB=(1<<TXEN); // pornire transmisie modul USART [(1<<TXCIE) ??? este nevoie de intrerupere] UCSRC=(1<<URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // selectare marime caracter 8 biti UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register } unsigned int usart_sendch() { while ((UCSRA & (1 << TXC)) == 0); // Asteapta pana cand transmisia este completa return(UDR); // return the byte } void usart_putch(unsigned char send) { while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready.. // for more data to be written to it UDR = send; // Send the byte }

You might also like