//Program for ADC to read from channel 0 and show the 8 bit o/p on PORTB
// send the reading from ADC to Serial Port
#include <mega16.h>
#include <stdlib.h>
#include <delay.h>
#include <io.h>
#define ADC_VREF_TYPE 0x40
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((12000000/ (USART_BAUDRATE*16))) - 1)
//------------------------------------------------
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // faktor pembagi 128
}
void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception
circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes
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
}
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
}
/*void USART_transmit(unsigned char data)
{
while((UCSRA & 0x20)==1)
{
UDR=data; }
}*/
/*void SEND_reading(unsigned char abc)
{
unsigned char k,l,m;
k=abc/100;
l=(abc/10)%10;
m=abc%10;
USART_transmit(k);
USART_transmit(l);
USART_transmit(m);
USART_transmit(',');
} */
/*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 ADCW;
} */
// Read the AD conversion result
unsigned int ADC_read(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0b01000000;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==1);
ADCSRA|=0b00000;
return ADCW;
}
void main(void)
{
unsigned int value;
DDRB=0xFF;
DDRD=0x03;
ADC_init(); // Initialization of ADC
usart_init();
// ch=0;
while(1)
{
value=ADC_read(0);
value=value*500/1024;
PORTB=value;
//SEND_reading(value);
usart_putch(value);
delay_ms(10);
}
}
Connect the RxD of Virtual Terminal (VT) to TxD of mcu and connect TxD of VT to RxD of mcu in Proteus and simulate.
k=abc/100;
l=(abc/10)%10;
m=abc%10;
k=abc/100 + 48;
l=(abc/10)%10 + 48;
m=abc%10 + 48;
//Program for ADC to read from channel 0 and show the 8 bit o/p on PORTB
// send the reading from ADC to Serial Port
#include <mega16.h>
#include <stdlib.h>
#include <delay.h>
#include <io.h>
#define ADC_VREF_TYPE 0x40
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((12000000/ (USART_BAUDRATE*16))) - 1)
//------------------------------------------------
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // faktor pembagi 128
}
void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes
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
}
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
}
// Read the AD conversion result
unsigned int ADC_read(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0b01000000;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==1);
ADCSRA|=0b00000;
return ADCW;
}
void main(void)
{
unsigned int value;
DDRB=0xFF;
DDRD=0x03;
ADC_init(); // Initialization of ADC
usart_init();
// ch=0;
while(1)
{
value=ADC_read(0);
value=value*500/1024;
PORTB=value;
//SEND_reading(value);
usart_putch(value);
delay_ms(10);
}
}
usart_putch('C');
void main(void)
{
unsigned int value;
DDRB=0xFF;
DDRA=0xFE;
ADC_init(); // Initialization of ADC
usart_init();
// ch=0;
while(1)
{
value=ADC_read(0);
value=value*500/1024;
PORTB=value;
value=(char)value;
//SEND_reading(value);
usart_putch(value);
delay_ms(10);
}
}
value=(char)value;
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?