Meeting - Changed Set/Remove and fixed EVERYTHING

master
Alexander B 6 years ago
parent d9bd21f0c6
commit 323d3b4ccb

@ -10,41 +10,38 @@
*/ */
#define F_CPU 16000000UL // Clock speed: 16 MHz - Speed from onboard oscillator #define F_CPU 16000000UL // Clock speed: 16 MHz - Speed from onboard oscillator
// Macros
#define bit_get(p,m) ((p) & (m))
#define bit_set(p,m) ((p) |= (m))
#define bit_clear(p,m) ((p) &= ~(m))
#define bit_flip(p,m) ((p) ^= (m))
#define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m))
#define BIT(x) (0x01 << (x))
#define LONGBIT(x) ((unsigned long)0x00000001 << (x))
// Imports // Imports
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h>
#include <time.h>
// Global variable declaration // Global variable declaration
uint16_t boost_frequency = 2000; // 2kHz frequency uint16_t boost_frequency = 2000; // 2kHz frequency
bool boost_enable = false; uint8_t boost_enable = 0;
bool boost_highVoltage_nextHigh = true; uint8_t boost_highVoltage_nextHigh = 1;
clock_t boost_highVoltage_nextTick = 0; clock_t boost_highVoltage_nextTick = 0;
bool signalOutput_enable = false; // Todo [CRITICAL]: Clock does not exist! Use timer.
bool signalOutput_nextHigh = true;
uint8_t signalOutput_enable = 0;
uint8_t signalOutput_nextHigh = 1;
clock_t signalOutput_nextTick = 0; clock_t signalOutput_nextTick = 0;
int main() int main()
{ {
DDRD &= ~(1<<2); // Activate input PD2 // Inputs
DDRD &= ~(1<<3); // Activate input PD3 DDRD &= (0 << PORTD2); // Activate input PD2
DDRD &= (0 << PORTD3); // Activate input PD3
PORTD |= (1<<2); // Enable pull-up-resistor D2 //PORTD |= (1 << PORTD2); // Enable pull-up-resistor D2
PORTD |= (1<<3); // Enable pull-up-resistor D3 //PORTD |= (1 << PORTD3); // Enable pull-up-resistor D3
// Todo [CRITICAL]: External pull-down resistors 10kOhm
// Outputs
DDRD |= (1 << PORTD4);
DDRD |= (1 << PORTD5);
DDRD |= (1 << PORTD6);
// Interrupt for INT0 Pin-D2 High-voltage check // Interrupt for INT0 Pin-D2 High-voltage check
EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge [Code: 01] EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge [Code: 01]
@ -60,32 +57,32 @@ int main()
while (1) while (1)
{ {
// HighVoltage boosting // HighVoltage boosting
if(boost_enable) if(boost_enable == 1)
{ {
if(clock() >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed if(clock() >= boost_highVoltage_nextTick) // If we are on or after the tick it should be executed
{ {
// Set pin according to next exec // Set pin according to next exec
if(boost_highVoltage_nextHigh) // Set it high if(boost_highVoltage_nextHigh == 1) // Set it high
{ {
bit_set(PD4, 1); PORTD |= (1 << PORTD4);
boost_highVoltage_nextHigh = false; // Next is low boost_highVoltage_nextHigh = 0; // Next is low
} }
else // Set it low else // Set it low
{ {
bit_set(PD4, 0); PORTD |= (0 << PORTD4);
boost_highVoltage_nextHigh = true; // Next is high boost_highVoltage_nextHigh = 1; // Next is high
} }
// Calculate when next high/low should be set // Calculate when next high/low should be set
boost_highVoltage_nextTick = clock() + F_CPU * 1/(boost_frequency/2) // Half of time it should be high/low boost_highVoltage_nextTick = clock() + F_CPU * 1/(boost_frequency/2); // Half of time it should be high/low
} }
}else }else
{ {
if(boost_highVoltage_nextTick > 0) // If boosting is deactivated, but the nextTick was not reset yet if(boost_highVoltage_nextTick > 0) // If boosting is deactivated, but the nextTick was not reset yet
{ {
// Reset boost-state // Reset boost-state
bit_set(PD4, 0); PORTD |= (0 << PORTD4);
boost_highVoltage_nextHigh = true; boost_highVoltage_nextHigh = 1;
boost_highVoltage_nextTick = 0; boost_highVoltage_nextTick = 0;
} }
} }
@ -97,23 +94,23 @@ int main()
if(clock() >= signalOutput_nextTick) // If we are on or after the tick it should be executed if(clock() >= signalOutput_nextTick) // If we are on or after the tick it should be executed
{ {
// Set pin according to next exec // Set pin according to next exec
if(signalOutput_nextHigh) // Set it high if(signalOutput_nextHigh == 1) // Set it high
{ {
bit_set(PD5, 1); PORTD |= (1 << PORTD5);
bit_set(PD6, 1); PORTD |= (1 << PORTD6);
signalOutput_nextHigh = false; // Next is low signalOutput_nextHigh = 0; // Next is low
} }
else // Set it low else // Set it low
{ {
bit_set(PD5, 0); PORTD |= (0 << PORTD5);
bit_set(PD6, 0); PORTD |= (0 << PORTD6);
signalOutput_nextHigh = true; // Next is high signalOutput_nextHigh = 1; // Next is high
signalOutput_enable = false; // Disable signalOutput signalOutput_enable = 0; // Disable signalOutput
} }
// When the signal should stop // When the signal should stop
signalOutput_nextTick = clock() + F_CPU * 0.2 // nextTick is in 0.2s signalOutput_nextTick = clock() + F_CPU * 0.2; // nextTick is in 0.2s
} }
} }
} }
@ -127,15 +124,15 @@ int main()
// Handles: Over/Below working voltage // Handles: Over/Below working voltage
ISR(INT0_vect) ISR(INT0_vect)
{ {
if(PIND&0x20 == 0x00) // Falling edge if(!(PIND & (1 << PORTD2))) // Falling edge
{ {
// Below ~400V, activate booster // Below ~400V, activate booster
boost_enable = true; boost_enable = 1;
} }
else // Rising edge else // Rising edge
{ {
// Reached ~400V, deactivate booster // Reached ~400V, deactivate booster
boost_enable = false; boost_enable = 0;
} }
reti(); // Exit interrupt-handler reti(); // Exit interrupt-handler
@ -146,7 +143,7 @@ ISR(INT0_vect)
ISR(INT1_vect) ISR(INT1_vect)
{ {
// Tick detected, signalOutput // Tick detected, signalOutput
signalOutput_enable = true; signalOutput_enable = 1;
// Execute immediately // Execute immediately
signalOutput_nextTick = 0; signalOutput_nextTick = 0;

Loading…
Cancel
Save