How to connect
bluetooth to microcontroller MEGA16
Fig.1. Bluetooth module is connected to serial PORT
PD0 and PD1
Fig. 2. Get analog input from potensiometer and using
XTALL 11.0592 MHz.
#include <mega16.h>
#include <delay.h>
#include <stdlib.h>
// Alphanumeric LCD functions
#include <alcd.h>
// Standard Input/Output functions
#include <stdio.h>
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(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|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
unsigned char temp[4];
unsigned int data_adc;
unsigned char buff[];
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0xFF;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA4;
SFIOR&=0x1F;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 3
// D5 - PORTC Bit 4
// D6 - PORTC Bit 5
// D7 - PORTC Bit 6
// Characters/line: 16
lcd_init(16);
while (PINB.2==1)
{
lcd_clear();
lcd_gotoxy(0,1);
lcd_puts("PRESS ENTER");
delay_ms(10);
}
while (1)
{
delay_ms(1);
sprintf(buff,"!%d#",read_adc(0));
puts(buff);
data_adc=read_adc(0);
itoa(data_adc,temp);
lcd_clear();
lcd_gotoxy(0,1);
lcd_puts(temp);
delay_ms(10);
}
}
|