Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
Code: [Select][code]int main(void){ //I/O Initialization DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs PORTB=0; //All initialized to 0 //UART UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); UBRRL = BAUD_PRESCALE; //Sets UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers UCSRB |= (1 << RXCIE); //Enable USART-interrupt Sometimes (always?) the order of access matters for 16 bit data registers. The data sheet shows UBRRH being set before UBRRL. Try it that way, you may not be getting the baud rate you think you are.Just guessing here, I don't have time to verify this at the moment.Joe
[code]int main(void){ //I/O Initialization DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs PORTB=0; //All initialized to 0 //UART UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); UBRRL = BAUD_PRESCALE; //Sets UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers UCSRB |= (1 << RXCIE); //Enable USART-interrupt
#define F_CPU 7372800UL //Defines clock speed
#define F_CPU 14745600UL //Defines clock speed#include <avr/io.h>#include <avr/interrupt.h>#define USART_BAUDRATE 9600 //Baudrate for serial comm.#define BAUD_PRESCALE 95 //95 sad in Atmega16 datasheet for 14745600UL oscil//Serial com. Interrupt Service Routine (runs each time a byte is received)ISR(USART_RXC_vect){ char ReceivedByte; ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module) UDR = ReceivedByte; // Echoes it back for fun switch (ReceivedByte) //Which ASCII character was received? { case 'p': PORTB = 0b00000100;//l break; case 'n': PORTB = 0b00000010;//k break; case 'r': PORTB = 0;//m break; case 'l': PORTB = 0b00000001;//j break; default: break; } } int main(void){ //I/O Initialization DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs //PORTB=0; //All initialized to 0 //UART UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); UBRRL = BAUD_PRESCALE; //Sets UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers UCSRB |= (1 << RXCIE); //Enable USART-interrupt sei(); //Enable global interrupt /* This program is completely interrupt driven, so nothing goes on in while loop*/ while(1); //Never gets out from here! return 0; //Never reaches this point!}
#define F_CPU 1000000UL //Defines clock speed#include <avr/io.h>#include <avr/interrupt.h>#define USART_BAUDRATE 4800 //Baudrate for serial comm.//#define BAUD_PRESCALE 95 //95 sad in Atmega16 datasheet for 14745600UL oscil#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)//Serial com. Interrupt Service Routine (runs each time a byte is received)ISR(USART_RXC_vect){ char ReceivedByte; ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module) UDR = ReceivedByte; // Echoes it back for fun switch (ReceivedByte) //Which ASCII character was received? { case 'p': PORTB = 0b00000100;//l break; case 'n': PORTB = 0b00000010;//k break; case 'r': PORTB = 0;//m break; case 'l': PORTB = 0b00000001;//j break; default: break; } } int main(void){ //I/O Initialization DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs //PORTB=0; //All initialized to 0 //UART UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers UBRRL = BAUD_PRESCALE;//Sets UCSRB |= (1 << RXCIE); //Enable USART-interrupt sei(); //Enable global interrupt /* This program is completely interrupt driven, so nothing goes on in while loop*/ while(1); //Never gets out from here! return 0; //Never reaches this point!}
No I do not change fuses. Why should I change fuses, if internal clock setting are at 1Mhz?
#define F_CPU 1000000UL //Put clock speed here#define USART_BAUDRATE 9600 //Put desired Baud Rate here#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) //This calculates Baud Prescale value for You
If I want to use 1Mhz internal clock i must change fuses, but now when all is working, it will not give even worse result?
#include <avr/io.h>#include <avr/interrupt.h>#define F_CPU 1000000UL#define USART_BAUDRATE 4800#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)int main (void){ UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UCSRB |= (1 << RCXIE); // Enable the USART Recieve Complete interrupt (USART_RXC) sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed for (;;) // Loop forever { // Do nothing - echoing is handled by the ISR instead of in the main loop } }ISR(USART_RXC_vect){ char ReceivedByte; ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived" UDR = ReceivedByte; // Echo back the received byte back to the computer}
UCSRB |= (1 << RCXIE); // Enable the USART Recieve Complete interrupt (USART_RXC)
UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx
#define F_CPU 1000000UL #define USART_BAUDRATE 4800#define BAUD_PRESCALE 5
"newInRobotics" I tried it, butCode: [Select] UCSRB |= (1 << RCXIE); // Enable the USART Recieve Complete interrupt (USART_RXC) compiler shows error, I change it with thisCode: [Select]UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rxbuild and upload to MCU.
UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC)
Yes with change build was successful, but when i typed - abcdefghijklmnoprst... I get this
Take a look at the tutorial at AVRFreaks. Looks like the same code NIR posted (including the typo), but the following comments might give some other things to look at.
#define F_CPU 1000000UL#define USART_BAUDRATE 4800#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)#include <avr/io.h>#include <avr/interrupt.h>int main(void){ UCSRB |= (1 << RXEN) | (1 << TXEN); //Turn ON transmission and reception circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); //Data length of 8-Bit UBRRH = (BAUD_PRESCALE >> 8); //Load upper 8-bits of BAUD_PRESCALE value UBRRL = BAUD_PRESCALE; UCSRB |= (1 << RXCIE); //Enable Rx Complete interrupt; sei(); //Enable global interrupts; for(;;) {}}ISR(USART_RX_vect) //Byte received interrupt routine{ UDR = UDR;}
Thank You [...]
Maybe You do something like it?