Commit 76c99f17 authored by Martin Vítek's avatar Martin Vítek
Browse files

Lot of refractoring to separate classes

parent 5ec6dad3
......@@ -38,6 +38,13 @@ set(SOURCE_FILES
gpio.h
HD44780.cpp
HD44780.h
encoder.h
irq.cpp
irq.h
SafeSolderingStation.cpp
SafeSolderingStation.h
Uart.cpp
Uart.h
)
add_avr_executable(safe-soldering-station ${SOURCE_FILES})
#include "SafeSolderingStation.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <util/delay.h>
const HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PIN6_bm, PIN7_bm};
//Global instance
SafeSolderingStation sss;
SafeSolderingStation::SafeSolderingStation() : led(PORTD, PIN0_bm),
backlight(PORTD, PIN1_bm),
heating(PORTC, PIN0_bm),
enc(),
lcd(lcd_config),
uart()
{
}
void SafeSolderingStation::clock_init()
{
//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;
}
void SafeSolderingStation::misc_pin_init()
{
//Low 4 data bits on LCD OUTPUT/LOW
PORTC.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
PORTC.OUTCLR = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
}
void SafeSolderingStation::irq_init()
{
//Enable all priority of irqs
PMIC.CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
sei();
}
void SafeSolderingStation::init()
{
clock_init();
misc_pin_init();
uart.init();
lcd.init();
irq_init();
backlight.on();
}
void SafeSolderingStation::wellcome()
{
uart.send_string("\n\rSafe Soldering Station v1.0\n\r");
lcd.gotoxy_new(0,0);
lcd.write_text("Safe Soldering");
lcd.gotoxy_new(1,4);
lcd.write_text("Station v1.0");
_delay_ms(750);
lcd.clear();
lcd.gotoxy_new(0,0);
lcd.write_text("by OK1KVK.cz");
lcd.gotoxy_new(1,12);
lcd.write_text("2016");
_delay_ms(750);
lcd.clear();
}
\ No newline at end of file
#pragma once
#include <avr/io.h>
#include "gpio.h"
#include "HD44780.h"
#include "encoder.h"
#include "Uart.h"
class SafeSolderingStation
{
private:
//Objects
output led;
output backlight;
output heating;
encoder enc;
HD44780 lcd;
Uart uart;
//Methods
void clock_init();
void misc_pin_init();
void irq_init();
public:
SafeSolderingStation();
void init();
void wellcome();
};
//Global instance
extern SafeSolderingStation sss;
#include "Uart.h"
#include <avr/io.h>
Uart::Uart()
{
}
void Uart::init()
{
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;
}
void Uart::send_char(const unsigned char data)
{
//Wait for ongoing transmition to complete
while (!(USARTC0.STATUS & USART_DREIF_bm)) ;;
USARTC0.DATA = data;
}
unsigned char Uart::get_char()
{
//Wait for data to be received
while (!(USARTC0.STATUS & USART_RXCIF_bm)) ;;
return USARTC0.DATA;
}
void Uart::send_string(const char *text)
{
while (*text)
{
send_char(*text);
text++;
}
}
\ No newline at end of file
#pragma once
class Uart
{
public:
Uart();
void init();
void send_char(const unsigned char data);
void send_string(const char *text);
unsigned char get_char();
};
#pragma once
#include <avr/io.h>
#include <stdint.h>
struct encoder_pin
{
PORT_t port;
uint8_t pin;
};
struct encoder_config
{
encoder_pin pinA;
encoder_pin pinB;
encoder_pin button;
};
class encoder
{
private:
encoder_config config;
volatile bool button;
public:
encoder(encoder_config &config)
{
}
encoder()
{
//Inputs
PORTA.DIRCLR = PIN2_bm;
PORTB.DIRCLR = PIN2_bm | PIN3_bm;
//Pull-Up, IRQ on both edges, inverted readout
PORTA.PIN2CTRL = PORT_ISC_BOTHEDGES_gc | PORT_OPC_PULLUP_gc | PORT_INVEN_bm;
PORTCFG.MPCMASK = PIN2_bm | PIN3_bm;
PORTB.PIN2CTRL = PORT_ISC_BOTHEDGES_gc | PORT_OPC_PULLUP_gc | PORT_INVEN_bm;
//Enable IRQs at low priority
PORTA.INTCTRL = PORT_INT0LVL_LO_gc;
PORTA.INT0MASK = PIN2_bm;
PORTB.INTCTRL = PORT_INT0LVL_LO_gc;
PORTB.INT0MASK = PIN2_bm | PIN3_bm;
}
bool get_button() { return button; }
void Ahandler()
{
}
void B_handler()
{
}
void button_handler()
{
};
};
#include "irq.h"
#include <avr/interrupt.h>
#include "encoder.h"
//Encoder
ISR(PORTA_INT0_vect)
{
}
ISR(PORTB_INT0_vect)
{
}
ISR(PORTB_INT1_vect)
{
}
ISR(BADISR_vect)
{
reti();
}
\ No newline at end of file
#pragma once
#include <avr/interrupt.h>
//Encoder
ISR(PORTA_INT0_vect);
ISR(PORTB_INT0_vect);
ISR(PORTB_INT1_vect);
ISR(BADISR);
......@@ -3,63 +3,13 @@
#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);
static void wellcome();
gpio led(PORTD, PIN0_bm);
ISR(PORTA_INT0_vect)
{
led.toggle();
}
ISR(BADISR_vect)
{
reti();
};
const HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PIN6_bm, PIN7_bm};
HD44780 lcd(lcd_config);
#include "SafeSolderingStation.h"
int main()
{
led.set_direction(OUTPUT);
gpio backlight(PORTD, PIN1_bm);
backlight.set_direction(OUTPUT);
backlight.on();
gpio button(PORTA, PIN2_bm);
button.set_direction(INPUT);
//button.set_mode(TOTEM_PULL_UP);
//button.set_invert(true);
PORTA.INTCTRL = PORT_INT0LVL_HI_gc;
PORTA.INT0MASK = PIN2_bm;
PORTA.PIN2CTRL |= PORT_ISC_RISING_gc | PORT_OPC_PULLUP_gc | PORT_INVEN_bm;
PMIC.CTRL = PMIC_HILVLEN_bm | PMIC_RREN_bm;
PORTC.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
PORTC.OUTCLR = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
clock_ini();
uart_ini();
lcd.init();
sei();
wellcome();
sss.init();
sss.wellcome();
for(;;)
{
......@@ -72,85 +22,3 @@ int main()
*/
}
}
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++;
}
}
static void wellcome()
{
uart_string("\n\rSafe Soldering Station v1.0\n\r");
lcd.gotoxy_new(0,0);
lcd.write_text("Safe Soldering");
lcd.gotoxy_new(1,4);
lcd.write_text("Station v1.0");
_delay_ms(750);
lcd.clear();
lcd.gotoxy_new(0,0);
lcd.write_text("by OK1KVK.cz");
lcd.gotoxy_new(1,12);
lcd.write_text("2016");
}
\ No newline at end of file
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