fsoender
Member level 2
- Joined
- Aug 7, 2012
- Messages
- 45
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,553
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define LONGOUT 7
char buffer[LONGOUT];
void adc_init(void);
unsigned int adc_read(void);
void adc_conversion(uint16_t);
// ADC configuration
void ADC_init(void) {
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
uint16_t ADC_get_reading(void) {
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
return ADC;
}
uint16_t ADC_read(void) {
uint8_t i;
uint16_t retval = 0;
ADC_get_reading(); // dummy read - just discarded
for (i=0; i<8; i++) {
retval += ADC_get_reading();
}
return retval / 8;
}
void init_UART(void)
{
UCSRB = ((1 << RXCIE) | (1 << RXEN) | (1 << TXEN));
UCSRC = ((1 << UCSZ1) | (1 << UCSZ0));
//Baud rate till 19200.
UBRRL = 25;
UBRRH = 0;
}
void uart_sendchar(char c) {
while(!(UCSRA & (1<<UDRE)));
UDR = c;
}
void uart_printstring(char * str) {
while (*str) {
uart_sendchar(*str++);
}
}
void main(void) {
DDRC = 0x00;
init_UART();
ADC_init();
while(1)
{
if (PINC & 0x01) //If PortC pin 0 is True, program stops.
{
ADC_read();
char buffer[8];
itoa(ADC, buffer, 10);
uart_printstring(buffer);
uart_printstring("\r\n");
_delay_ms(1500);
}
}
}
Code C - [expand] 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 /***************************************************************************** ** Function name: read_adc ** ** Descriptions: makes an ADC measurment in the specified channel ** ** parameters: unsigned char adc_input: channel number ** Returned value: unsigned int: returns 0-1023 ** *****************************************************************************/ unsigned int read_adc(unsigned char adc_input) { // clear the channel bits ane set the new channel ADMUX = (ADMUX & 0xF8) | adc_input; // Delay needed for the stabilization of the ADC input voltage _delay_us(10); // Start the AD conversion ADCSRA |= 0x40; // Wait for the AD conversion to complete while (ADCSRA & (1 << ADSC)) // return result return ADCW; }
uint16_t x,y,z;
x=read_adc(0);
y=read_adc(1);
z=read_adc(2);
itoa(ADC, buffer, 10);
#define LONGOUT 15
char buffer[LONGOUT]="0000 0000 0000";
itoa(x, buffer, 10);
itoa(y, &buffer[5], 10);
itoa(z, &buffer[10], 10);
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#define LONGOUT 15
char buffer[LONGOUT]="0000 0000 0000";
void adc_init(void);
unsigned int adc_read(void);
void adc_conversion(uint16_t);
unsigned char x;
unsigned char y;
unsigned char z;
// ADC configuration
void ADC_init(void) {
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
uint16_t ADC_get_reading(void) {
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
return ADC;
}
unsigned int read_adc(unsigned char adc_input)
{
// clear the channel bits ane set the new channel
ADMUX = (ADMUX & 0xF8) | adc_input;
// Delay needed for the stabilization of the ADC input voltage
_delay_us(10);
// Start the AD conversion
ADCSRA |= 0x40;
// Wait for the AD conversion to complete
while (ADCSRA & (1 << ADSC))
// return result
return ADCW;
}
void init_UART(void)
{
UCSRB = ((1 << RXCIE) | (1 << RXEN) | (1 << TXEN));
UCSRC = ((1 << UCSZ1) | (1 << UCSZ0));
//Baud rate till 19200.
UBRRL = 25;
UBRRH = 0;
}
void uart_sendchar(char c) {
while(!(UCSRA & (1<<UDRE)));
UDR = c;
}
void uart_printstring(char * str) {
while (*str) {
uart_sendchar(*str++);
}
}
void main(void) {
DDRC = 0x00;
init_UART();
ADC_init();
while(1)
{
x=read_adc(0);
y=read_adc(1);
z=read_adc(2);
char buffer[8];
itoa(x, buffer, 10);
itoa(y, &buffer[5], 10);
itoa(z, &buffer[10], 10);
uart_printstring(buffer);
uart_printstring("\r\n");
_delay_ms(1500);
}
}
char buffer[LONGOUT] = "0000 0000 0000";
char buffer[8];
#define LONGOUT 15
char buffer[LONGOUT]="0000 0000 0000";
itoa(x, buffer, 10);
buffer[4]=' ';
itoa(y, &buffer[5], 10);
buffer[9]=' ';
itoa(z, &buffer[10], 10);
#include <stdio.h>
//and use
sprintf(buffer,"%.4u %.4u %.4u ",x,y,z);
while(1)
{
x=read_adc(0);
y=read_adc(1);
z=read_adc(2);
itoa(x, buffer, 10);
buffer[4]=' ';
itoa(y, &buffer[5], 10);
buffer[9]=' ';
itoa(z, &buffer[10], 10);
sprintf(buffer,"%.4u %.4u %.4u ",x,y,z);
//uart_printstring(buffer);
//uart_printstring("\r\n");
_delay_ms(1500);
}
}
itoa(x, buffer, 10);
buffer[4]=' ';
itoa(y, &buffer[5], 10);
buffer[9]=' ';
itoa(z, &buffer[10], 10);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?