149 lines
3.6 KiB
C
149 lines
3.6 KiB
C
/**
|
|
* @file main.c
|
|
* @author Jake G
|
|
* @date 15 June 2024
|
|
* @brief File contains the main function.
|
|
*
|
|
* This file contains the main logic loop and exists to setup the values
|
|
* specific to the micro-controller. All other functions and configuration are
|
|
* extracted into separate source files and headers for configuration.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#define F_CPU 3333333UL
|
|
|
|
// This can prevent issues with utils/delay.h library with the gcc toolchain
|
|
#define __DELAY_BACKWARD_COMPATIBLE__
|
|
|
|
#include "LedController.h"
|
|
#include "RegEdit.h"
|
|
#include "SuperLoop.h"
|
|
#include "config.h"
|
|
#include "timer.h"
|
|
#include <avr/cpufunc.h> /* Required header file */
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
|
|
#define SW1PIN (1 << 5)
|
|
#define SW2PIN (1 << 4)
|
|
#define RELAYPIN (1 << 7)
|
|
#define RELAYREADINGPIN \
|
|
(1 << 6) // It would be better to use PA7 so USART worked
|
|
|
|
//#define LED_BM_PORTA (1 << 6)|(1 << 1)|(1 << 2)|(1 << 3)
|
|
#define LED_BM_PORTA (1 << 1) | (1 << 2) | (1 << 3)
|
|
#define LED_BM_PORTB (1 << 3) | (1 << 2) | (1 << 0) | (1 << 1)
|
|
|
|
// Set the function pointer for the delay func
|
|
void (*Delay_MicroSeconds)(double us) = _delay_us;
|
|
|
|
void SW1_Wait(void) {
|
|
// poll the input.
|
|
while (PORTA.IN & SW1PIN) {
|
|
}
|
|
}
|
|
|
|
void SW2_Wait(void) {
|
|
// poll the input.
|
|
while (PORTA.IN & SW2PIN) {
|
|
}
|
|
}
|
|
|
|
void Activate_Relay(void) { PORTA.OUT |= RELAYPIN; }
|
|
|
|
void Deactivate_Relay(void) { PORTA.OUT &= ~RELAYPIN; }
|
|
|
|
void WaitForRelayConnect(void) {
|
|
while (!(PORTA.IN & RELAYREADINGPIN)) {
|
|
;
|
|
}
|
|
}
|
|
|
|
void WaitForRelayDisconnect(void) {
|
|
while (PORTA.IN & RELAYREADINGPIN) {
|
|
;
|
|
}
|
|
}
|
|
|
|
void LedSetup(LedByte *led_byte) {
|
|
PORTA.DIR |= LED_BM_PORTA;
|
|
PORTB.DIR |= LED_BM_PORTB;
|
|
|
|
// pin 13, bit 0, PA3
|
|
led_byte->leds[0].port = (void *)&PORTA.OUT;
|
|
led_byte->leds[0].pin_num = 3;
|
|
|
|
// pin 12, bit 1, PA2
|
|
led_byte->leds[1].port = &PORTA.OUT;
|
|
led_byte->leds[1].pin_num = 2;
|
|
|
|
// pin 11, bit 2, PA1
|
|
led_byte->leds[2].port = &PORTA.OUT;
|
|
led_byte->leds[2].pin_num = 1;
|
|
|
|
// pin 9, bit 3, PB0
|
|
led_byte->leds[3].port = &PORTB.OUT;
|
|
led_byte->leds[3].pin_num = 0;
|
|
|
|
// pin 8, bit 4, PB1
|
|
led_byte->leds[4].port = &PORTB.OUT;
|
|
led_byte->leds[4].pin_num = 1;
|
|
|
|
// pin 2, bit 5, PB2
|
|
led_byte->leds[5].port = &PORTB.OUT;
|
|
led_byte->leds[5].pin_num = 2;
|
|
|
|
// pin 3, bit 6, PB3
|
|
led_byte->leds[6].port = &PORTB.OUT;
|
|
led_byte->leds[6].pin_num = 3;
|
|
|
|
// pin 3, bit 7, PA6
|
|
led_byte->leds[7].port = &PORTA.OUT;
|
|
// led_byte->leds[7].pin_num = 6;
|
|
led_byte->leds[7].pin_num = 3;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
// We use pin 10 which is the UDPI/RESET pin.
|
|
// This means we have to change it's mode.
|
|
|
|
PORTA.DIR |= RELAYPIN;
|
|
PORTA.DIR &= ~RELAYREADINGPIN;
|
|
PORTA.DIR &= ~SW1PIN;
|
|
PORTA.DIR &= ~SW2PIN;
|
|
|
|
uint16_t make_time = 0;
|
|
uint16_t break_time = 0;
|
|
uint8_t temp = 0;
|
|
|
|
LedByte led_byte = LedController_New(&PORTA.OUT);
|
|
LedSetup(&led_byte);
|
|
|
|
while (true) {
|
|
SW1_Wait();
|
|
Activate_Relay();
|
|
|
|
Timer_Start();
|
|
WaitForRelayConnect();
|
|
Timer_Disable();
|
|
|
|
make_time = Timer_GetOverflowCount();
|
|
|
|
// Output the Make time via LEDS
|
|
temp = (uint8_t)(make_time & 0xFF);
|
|
LedController_ShowByte(&led_byte, temp);
|
|
|
|
SW2_Wait();
|
|
Deactivate_Relay();
|
|
|
|
Timer_Start();
|
|
WaitForRelayDisconnect();
|
|
Timer_Disable();
|
|
|
|
break_time = Timer_GetOverflowCount();
|
|
|
|
// Output the Break time via LEDS
|
|
temp = (uint8_t)(break_time & 0xFF);
|
|
LedController_ShowByte(&led_byte, temp);
|
|
}
|
|
}
|