ud23
Advanced Member level 3
- Joined
- Apr 20, 2011
- Messages
- 926
- Helped
- 185
- Reputation
- 370
- Reaction score
- 180
- Trophy points
- 1,323
- Activity points
- 6,138
hi all i want to send atmega16 internal adc readings to the serial port of pc
i wrote this code but it is sending continuous data to serial port i want to send data when adc reading got change.
Code:
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
void ADC_init(void);
unsigned int ADC_read(unsigned char);
//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(uint16_t ubrr_value)
{
//Set Baud rate
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
/*Set Frame Format
>> Asynchronous mode
>> No Parity
>> 1 StopBit
>> char size 8
*/
UCSRC=(1<<URSEL)|(3<<UCSZ0);
//Enable The receiver and transmitter
UCSRB=(1<<RXEN)|(1<<TXEN);
}
char USARTReadChar()
{
//Wait untill a data is available
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}
//Now USART has got data from host
//and is available is buffer
return UDR;
}
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
//Now write the data to USART buffer
UDR=data;
}
int main()
{
char data;
/*First Initialize the USART with baud rate = 19200bps
*/
USARTInit(51);
ADC_init();
while(1)
{
unsigned int value;
DDRB=0xFF;
DDRD=0x03; // Initialization of ADC
value=ADC_read(0);
PORTB=value;
_delay_ms(1000);
data=value;
USARTWriteChar(data);
}
}
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// Enable ADC and set Prescaler division factor as 128
}
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111; // channel must be b/w 0 to 7
ADMUX |= ch; // selecting channel
ADCSRA|=(1<<ADSC); // start conversion
while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
return (ADC);
}
i wrote this code but it is sending continuous data to serial port i want to send data when adc reading got change.