#include <avr/io.h>
#include <avr/delay.h>
#define PIN_RXD 0 //PD0 RXD
#define PIN_TXD 1 //PD1 TXD
#define BAUDRATE 9600 //baudrate
//#define F_CPU 8000000 //the frequency of the global clock
unsigned int advalue;
void init_USART(void);
void put_s(unsigned char *ptr);
void put_c(unsigned char c);
int main(void)
{
//上電默認DDRx=0x00,PORTx=0x00 輸入,無上拉電阻
PORTA=0XFF; //上拉
PORTB =0xFF; //不用的管腳使能內部上拉電阻。
PORTC=0XFF;
DDRD =(1<<PIN_TXD); //TXD為輸出
PORTD =0xFF;
init_USART();
while(1)
{
unsigned char highvalue,lowvalue;
ADMUX=(1<<REFS0); //AVCC基準電壓,通道0,10bits ad value
/*----------------------------------------------------------------
ADMUX
7,6 REFS1:0--------Rrference Selection Bits
00-------------AREF,Internal Vref truned off
01-------------AVCC with external capacior at AREF pin
10-------------Reserved
11-------------internal 2.56V Voltage Reference with external
capacitior at AREF pin
5 ADELAR---------ADC Left Adjust Result
1--------------Left Adjust-----8 bits resolution
0--------------Right Adjust----10 bits resolution
4:0 MUX4:0---------Analog Channel and Gain Selection Bits
00000----------ADC0
00001----------ADC1
||||||||||||||||||||||||||||||||||||
00111----------ADC7
|||||||||||||||||||||||||||||||||||||
----------------------------------------------------------------*/
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //AD使能,單次轉換,128分頻
/*--------------------------------------------------------------------
ADCSRA
7 ADEN-----------ADC Enable
6 ADSC-----------ADC Start Comversion
5 ADATE----------ADC Auto Trigger Enable
4 ADIF-----------ADC Interrupt Flag
3 ADIE-----------ADC Interrupt Enable
2:0 ADPS2:0--------ADC Prescaler Select Bits
000------------2
001------------2
010------------4
011------------8
100------------16
101------------32
110------------64
111------------128
---------------------------------------------------------------------*/
while(!(ADCSRA & (1 << ADIF))); /*等待*/
advalue=ADC;
ADCSRA&=(~(1<<ADEN))|(~(1<<ADSC));
advalue=advalue*4.89;
highvalue=(unsigned char)(advalue/100);
lowvalue=(unsigned char)(advalue%100);
put_s("AD轉換電壓值為:");
put_c(highvalue/10+0x30);
put_c('.');
put_c(highvalue%10+0x30);
put_c(lowvalue/10+0x30);
put_c(lowvalue%10+0x30);
put_s("V\n");
unsigned char i;
for(i=0;i<255;i++)
_delay_ms(20);
}
}
/*---------------------------------------------------------------------------
function send an unsigned char to the uart
----------------------------------------------------------------------------*/
void put_c(unsigned char c) //發送采用查詢方式
{
while( !(UCSRA & (1<<UDRE)) ); //wait until the uart is empty
UDR=c; //write data to uart
}
/*----------------------------------------------------------------------------
function send a string to the uart and with return back
-----------------------------------------------------------------------------*/
void put_s(unsigned char *ptr)
{
while (*ptr)
{
put_c(*ptr++);
}
put_c(0x0D);
put_c(0x0A); //結尾發送回車換行
}
/*---------------------------------------------------------------------------
fuction initialize the uart unit
----------------------------------------------------------------------------*/
void init_USART(void)//USART 初始化
{
//USART 9600 8, n,1 PC上位機軟件(超級終端等)也要設成同樣的設置才能通訊
UCSRC = (1<<URSEL) | 0x06;
//異步,8位數據,無奇偶校驗,一個停止位,無倍速
/*
UBRRH與UCSRC共用I/O 地址。因此訪問該地址時需注意以下問題。
寫訪問
當在該地址執行寫訪問時, USART 寄存器選擇位(URSEL)控制被寫入的寄存器。
若URSEL為0,對UBRRH值更新;若URSEL為1,對UCSRC設置更新
讀訪問
對UBRRH 或UCSRC 寄存器的讀訪問則較為復雜。但在大多數應用中,基本不需要讀這些寄存器
沒有UBRR這個16位寄存器,因為UBRRL(0x09)/UBRRH(0x20)的地址不連續,而且UBRRH跟UCSRC共用地址
*/
//U2X=0時的公式計算
UBRRL= (F_CPU/BAUDRATE/16-1)%256;
UBRRH= (F_CPU/BAUDRATE/16-1)/256;
//U2X=1時的公式計算
//UBRRL= (F_CPU/BAUDRATE/8-1)%256;
//UBRRH= (F_CPU/BAUDRATE/8-1)/256;
//也可根據數據手冊的[波特率設置的例子]查得
//UBRRL = 0x2F; //set baud rate lo
//UBRRH = 0x00; //set baud rate hi
UCSRA = 0x00; //無倍速
UCSRB = (1<<RXEN)|(1<<TXEN);
//使能接收,使能發送
}