I'd like to communicate with the PC via USB without using Serial.begin(), Serial.print() / println().
This is the code for TTL serial, works like a charm:
void USART_Init(unsigned int baud)
{
/* Set baud rate */
UBRR1H = (unsigned char)(baud>>8);
UBRR1L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR1B = (1<<RXEN1)|(1<<TXEN1);
/* Set frame format: 8data, 2stop bit */
UCSR1C = (1<<UMSEL11)|(UMSEL10)|(1<<USBS1)|(3<<UCSZ10);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR1A & (1<<UDRE1)) );
/* Put data into buffer, sends the data */
UDR1 = data;
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
USART_Transmit(*StringPtr);
StringPtr++;
}
}
int main(void)
{
/*------ Arduino initialisation ------
* Only using init() because the arduino SDK doesn't detect the COM correctly*/
init();
#if defined(USBCON)
USBDevice.attach();
#endif
/*------------------------------------*/
setup();
while (1)
{
USART_putstring("Testing");
Serial.println('a');
delay(1000);
//ARDUINO RELATED - Can't send data from the serial monitor to the board otherwise.
//Actually I could if I would implement a receive routine for the USB
if (serialEventRun)
serialEventRun();
}
}
This is the code for the USB:
void USART_Init(unsigned int baud)
{
/* Set baud rate */
UBRR0H = (unsigned char)(baud>>8);
UBRR0L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<UMSEL01)|(UMSEL00)|(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 USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){
USART_Transmit(*StringPtr);
StringPtr++;
}
}
int main(void)
{
/*------ Arduino initialisation ------
* Only using init() because the arduino SDK doesn't detect the COM correctly*/
init();
#if defined(USBCON)
USBDevice.attach();
#endif
/*------------------------------------*/
setup();
while (1)
{
USART_putstring("Testing");
Serial.println('a');
delay(1000);
//ARDUINO RELATED - Can't send data from the serial monitor to the board otherwise.
//Actually I could if I would implement a receive routine for the USB
if (serialEventRun)
serialEventRun();
}
}
The IDE says that all the registers that I'm using are not defined. However I dug up the HardwareSerial library and found (inside of HardwareSerial0.cpp - Location: Arduino\hardware\arduino\avr\cores\arduino):
#if defined(UBRRH) && defined(UBRRL)
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
#else
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#endif
Am I missing something here? Why can't I use the UBRR0H, UBRR0L, UCSR0A, UCSR0B, UCSR0C, UDR0 registers? Should I switch to AtmelStudio (I am aware that the existing bootloader on the uC will go 'ByeBye')? Is this related to the Arduino IDE? These registers exist in the Atmega32u4 documenation (Atmega32u4 - the uC that the Leonardo board is using).
The reason why I'm using the registers is because our teacher doesn't want us to use preexisting Arduino functions / libraries. He wants us to learn how to work with registers.
EDIT 1:
These registers do not exist. I was using the wrong registers. I will post a solution after I will read the documentation of the Atmega32u4 controller, regarding the USB registers.