Commit 61045a14 authored by Martin Vítek's avatar Martin Vítek
Browse files

Add UART and 32MHz clock initializations

parent e378fa84
......@@ -8,7 +8,7 @@ set(AVR_H_FUSE 0xC8)
set(AVR_L_FUSE 0x1F)
#set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(CMAKE_BUILD_TYPE Release)
set(MCU_SPEED "2000000UL")
set(MCU_SPEED "32000000UL")
set(WITH_MCU OFF)
set(WITH_MCU OFF CACHE BOOL "Add the mCU type to the target file name." FORCE)
......
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include "gpio.h"
#include "HD44780.h"
static void clock_ini();
static void uart_ini();
static void uart_tx(const unsigned char data);
static uint8_t uart_rx();
static void uart_string(const char *text);
int main()
{
......@@ -14,6 +21,10 @@ int main()
backlight.set_direction(OUTPUT);
backlight.on();
clock_ini();
uart_ini();
uart_string("\n\rSafe Soldering Station v1.0\n\r");
PORTC.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
PORTC.OUTCLR = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
......@@ -24,7 +35,7 @@ int main()
for (uint8_t i = 0; i < 2; i++)
{
lcd.gotoxy(i+1,1);
lcd.gotoxy_new(i,0);
for (uint8_t j = 0; j < 40; j++)
{
//lcd.gotoxy_new(i,j);
......@@ -37,7 +48,7 @@ int main()
lcd.gotoxy_new(0,0);
lcd.write_char('x');
//lcd.gotoxy(2,1);
//lcd.gotoxy_new(1,2);
//lcd.gotoxy(1,2);
lcd.write_text("Ahoj! :)");
for(;;)
......@@ -48,4 +59,69 @@ int main()
led.toggle();
_delay_ms(500);
}
}
\ No newline at end of file
}
static void clock_ini()
{
//Enable 32MHz oscillator
OSC.CTRL |= OSC_RC32MEN_bm;
//Wait to stabilize
while (!(OSC.STATUS & OSC_RC32MRDY_bm)) ;;
//Select 32MHz oscillator
CCP = 0xD8;
CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
//Enable DFLL on 32MHz
//OSC.DFLLCTRL = 0; // 32kHz as reference
DFLLRC32M.CTRL = DFLL_ENABLE_bm;
//Lock clock settings
CCP = 0xD8;
CLK.LOCK |= CLK_LOCK_bm;
}
static void uart_ini()
{
PORTC.OUTSET = PIN3_bm; //TX high
PORTC.DIRSET = PIN3_bm; //TX output
//Set baudrate 57600 (error 0.79%) http://www.avrcalc.elektronik-projekt.de/xmega/baud_rate_calculator
const uint16_t bsel = 34;
const uint8_t bscale = 0b00000000;
USARTC0.BAUDCTRLA = bsel & 0x00ff;
USARTC0.BAUDCTRLB = ((bsel>>8)&0x0f) | bscale;
//Set asynchronous mode and 8n1 data format
USARTC0.CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_CHSIZE_8BIT_gc;
//Enable receiver and transmitter
USARTC0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
}
static void uart_tx(const unsigned char data)
{
//Wait for ongoing transmition to complete
while (!(USARTC0.STATUS & USART_DREIF_bm)) ;;
USARTC0.DATA = data;
}
static uint8_t uart_rx()
{
//Wait for data to be received
while (!(USARTC0.STATUS & USART_RXCIF_bm)) ;;
return USARTC0.DATA;
}
static void uart_string(const char *text)
{
while (*text)
{
uart_tx(*text);
text++;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment