为什么我使用atmega328传输数据这么慢,每个字节长达1ms。下面是我在arduino IDE环境下写的代码
- #define FOSC 16000000 // Clock Speed
- #define BAUD 9600
- #define MYUBRR FOSC/16/BAUD-1
- long num=0;
- void USART_Init( unsigned int ubrr)
- {
- /*Set baud rate */
- UBRR0H = (unsigned char)(ubrr>>8);
- UBRR0L = (unsigned char)ubrr;
- /*Enable receiver and transmitter RXEN0=4,TXEN0=3*/
- UCSR0B = (1<<RXEN0)|(1<<TXEN0);
- /* Set frame format: 8data, 2stop bit */
- //UCSR0C = (1<<USBS0)|(3<<UCSZ00);
- }
- void USART_Transmit( unsigned char data )
- {
- /* Wait for empty transmit buffer */
- while ( !( UCSR0A & (1<<UDRE0)) )
- ;
- /* Put data into buffer, sends the data */
- UDR0 = data;
- }
- void setup( )
- {
- USART_Init(MYUBRR);
- //Serial.begin(9600);
- pinMode(13,OUTPUT);
- }
- void loop()
- {
- // Serial.println("ok");
- USART_Transmit(0x45);
- USART_Transmit(0x43);
- USART_Transmit(0x42);
- USART_Transmit(0x41);
- USART_Transmit(0x40);
- USART_Transmit(0x39);
- USART_Transmit(0x38);
- USART_Transmit(0x37);
- num++;
- if(num>1000)
- digitalWrite(13,HIGH);
- else
- digitalWrite(13,LOW);
- if(num>2000)
- num=0;
- // delay(100);
- }
复制代码
我测算了下。每次亮灭需要16s。也就是说每次循环占用了8ms。但是我把
USART_Transmit(0x45);
USART_Transmit(0x43);
USART_Transmit(0x42);
USART_Transmit(0x41);
USART_Transmit(0x40);
USART_Transmit(0x39);
USART_Transmit(0x38);
USART_Transmit(0x37);
去掉后,每次循环只有几us。所以一个字节的传输占到了1ms。是不是我使用方式有问题啊?还是这款芯片就这速度?
新手求教
此帖出自
小平头技术问答
一周热门 更多>