Цитата
how I can transfer this 10 bit once time to pc through rs232
ATmega's UART allows sending from 5 to 9 data bits at once (in one frame). Best frame format for communication with PC (using hardware RS232 or USB-TO-COM adapter) is 8 data bits and 1 stop bit. You should send ADC conversion result in two frames using "USART Data Register
Empty (UDRE) Interrupt" (Page 150 in datasheet). Example:
Код
/*****************************************************
Chip type : ATmega16
Program type : Application
Clock frequency : 14,745600 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
register unsigned char ADCH_, ADCL_; //Plase variables in working registers for fast access to them
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
ADCL_ = ADCL;
ADCH_ = ADCH; //Right order to read ADC result!
//Preparing data to transmission
ADCH_<<=3;
ADCH_|=ADCL_>>5;
ADCL_&=0x1F;
//Now we have 5 higher bits of the result in ADCH_ and 5 lower bits of result in ADCL_;
ADCH_|=0x80; //Set MSB in ADCH_ in 1 for correct handling recived data on PC
//Start Transmission!
UDR=ADCH_;
UCSRB=0x28;//UDRE interrupt enabled
}
// USART Transmitter interrupt service routine
interrupt [USART_DRE] void usart_tx_udre(void)
{
UDR=ADCL_;
UCSRB=0x08; //UDRE interrupt disabled
ADCSRA|=0x40; //Start next ADC conversion!!;
}
void main(void)
{
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On, UDRE int. On
// USART Mode: Asynchronous
// USART Baud Rate: 57600
UCSRA=0x00;
UCSRB=0x08; //TXEN=1;
UCSRC=0x86; //8-bit frame
UBRRH=0x00;
UBRRL=0x0F;
//ADC initialization
ADMUX=0xC0; // ADC Voltage Reference: Int., cap. on AREF, INPUT=PORTA.0
ADCSRA=0xCF; // ADC Clock frequency: 115,200 kHz, START FIRST CONVERSION
#asm("sei")
while (1);
}
P. S. Van Duong, are you from Vietnam? I glad to see vietnamese man on this forum!
My e-mail is KT829A[dog]yahoo[dot]com. Also i use YahooMessanger with this Yahoo ID. Any messages welcome!