Renamed some variables
Fixed comments Changed clock-speed to 16MHz (Onboard Oscillator is 16MHz)
This commit is contained in:
		
							parent
							
								
									dc36f3c7da
								
							
						
					
					
						commit
						aa6be9199c
					
				@ -8,7 +8,7 @@
 | 
				
			|||||||
 * MController		:	AtMega328P
 | 
					 * MController		:	AtMega328P
 | 
				
			||||||
 * Board			:	Arduino Nano
 | 
					 * Board			:	Arduino Nano
 | 
				
			||||||
 */ 
 | 
					 */ 
 | 
				
			||||||
#define F_CPU 20000000UL 				// Clock speed: 20 MHz	- Maximum of AtMega328P
 | 
					#define F_CPU 16000000UL 				// Clock speed: 16 MHz	- Maximum of AtMega328P
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Macros
 | 
					// Macros
 | 
				
			||||||
#define bit_get(p,m) ((p) & (m))
 | 
					#define bit_get(p,m) ((p) & (m))
 | 
				
			||||||
@ -23,33 +23,34 @@
 | 
				
			|||||||
#include <avr/io.h>
 | 
					#include <avr/io.h>
 | 
				
			||||||
#include <avr/interrupt.h>
 | 
					#include <avr/interrupt.h>
 | 
				
			||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Global variable declaration
 | 
					// Global variable declaration
 | 
				
			||||||
uint32_t currTick = 0;		// Todo: Method/Interrupt to get the current-tick
 | 
					uint32_t currTick = 0;		// Todo: Method/Interrupt to get the current-tick
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool enable_boost = false;
 | 
					bool boost_enable = false;
 | 
				
			||||||
bool boost_highVoltage_nextHigh = true;
 | 
					bool boost_highVoltage_nextHigh = true;
 | 
				
			||||||
uint32_t boost_highVoltage_nextTick = 0;
 | 
					uint32_t boost_highVoltage_nextTick = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool enable_signalOutput = false;
 | 
					bool signalOutput_enable = false;
 | 
				
			||||||
bool signal_Output_nextHigh = true;
 | 
					bool signalOutput_nextHigh = true;
 | 
				
			||||||
uint32_t signal_Output_nextTick = 0;
 | 
					uint32_t signalOutput_nextTick = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main()
 | 
					int main()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    DDRD &= ~(1<<2); 					// Activate PD2
 | 
					    DDRD &= ~(1<<2); 					// Activate input PD2
 | 
				
			||||||
    DDRD &= ~(1<<3);					// Activate PD3
 | 
					    DDRD &= ~(1<<3);					// Activate input PD3
 | 
				
			||||||
										
 | 
															
 | 
				
			||||||
	PORTD |= (1<<2);					// Enable pull-up-resistor Pin-D2
 | 
						PORTD |= (1<<2);					// Enable pull-up-resistor D2
 | 
				
			||||||
	PORTD |= (1<<3);					// Enable pull-up-resistor Pin-D3
 | 
						PORTD |= (1<<3);					// Enable pull-up-resistor D3
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// Interrupt for INT0	Pin-D2	High-voltage check
 | 
						// Interrupt for INT0	Pin-D2	High-voltage check
 | 
				
			||||||
    EICRA |= (0 << ISC01)|(1 << ISC00); // Only at any edge		[Code: 01]
 | 
					    EICRA |= (0 << ISC01)|(1 << ISC00); // On at any edge		[Code: 01]
 | 
				
			||||||
    EIMSK |= (1 << INT0);				// Activate Interrupt INT0
 | 
					    EIMSK |= (1 << INT0);				// Activate Interrupt INT0
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
	// Interrupt for INT1	Pin-D3	Counter-click
 | 
						// Interrupt for INT1	Pin-D3	Counter-click
 | 
				
			||||||
    EICRA |= (1 << ISC11)|(1 << ISC10);	// Only at rising edge	[Code: 11]
 | 
					    EICRA |= (1 << ISC11)|(1 << ISC10);	// On rising edge		[Code: 11]
 | 
				
			||||||
    EIMSK |= (1 <<INT1);				// Activate Interrupt INT1
 | 
					    EIMSK |= (1 <<INT1);				// Activate Interrupt INT1
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
    sei();		// Activate global interrupts
 | 
					    sei();		// Activate global interrupts
 | 
				
			||||||
@ -58,7 +59,7 @@ int main()
 | 
				
			|||||||
    while (1) 
 | 
					    while (1) 
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
		// HighVoltage boosting
 | 
							// HighVoltage boosting
 | 
				
			||||||
		if(enable_boost)
 | 
							if(boost_enable)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if(currTick >= boost_highVoltage_nextTick)	// If we are on or after the tick it should be executed
 | 
								if(currTick >= boost_highVoltage_nextTick)	// If we are on or after the tick it should be executed
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
@ -90,26 +91,26 @@ int main()
 | 
				
			|||||||
		
 | 
							
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// Signal output
 | 
							// Signal output
 | 
				
			||||||
		if(enable_signalOutput)
 | 
							if(signalOutput_enable)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if(currTick >= signal_Output_nextTick)	// If we are on or after the tick it should be executed
 | 
								if(currTick >= 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(signal_Output_nextHigh)		// Set it high
 | 
									if(signalOutput_nextHigh)		// Set it high
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					bit_set(PD5, 1);
 | 
										bit_set(PD5, 1);
 | 
				
			||||||
					bit_set(PD6, 1);
 | 
										bit_set(PD6, 1);
 | 
				
			||||||
					signal_Output_nextHigh = false;	// Next is low
 | 
										signalOutput_nextHigh = false;	// Next is low
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				else  								// Set it low
 | 
									else  								// Set it low
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					bit_set(PD5, 0);
 | 
										bit_set(PD5, 0);
 | 
				
			||||||
					bit_set(PD6, 0);
 | 
										bit_set(PD6, 0);
 | 
				
			||||||
					signal_Output_nextHigh = true;	// Next is high
 | 
										signalOutput_nextHigh = true;	// Next is high
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				
 | 
									
 | 
				
			||||||
				// When the signal should stop
 | 
									// When the signal should stop
 | 
				
			||||||
				signal_Output_nextTick = currTick + 101		// Todo: Add calculation for signalOutput low time
 | 
									signalOutput_nextTick = currTick + 101		// Todo: Add calculation for signalOutput low time
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -119,32 +120,33 @@ int main()
 | 
				
			|||||||
 * Interrupt-handler
 | 
					 * Interrupt-handler
 | 
				
			||||||
 * ISR - Interrupt service routine
 | 
					 * ISR - Interrupt service routine
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
// Address: 0x001	INT0	-	Only at any edge	[Code: 01]
 | 
					// Address: 0x001	INT0	-	On any edge		[Code: 01]
 | 
				
			||||||
// Handles:	Over/Below working voltage
 | 
					// Handles:	Over/Below working voltage
 | 
				
			||||||
ISR(INT0_vect)
 | 
					ISR(INT0_vect)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if(PIND&0x20 == 0x00)	// Falling edge
 | 
						if(PIND&0x20 == 0x00)	// Falling edge
 | 
				
			||||||
	{	
 | 
						{	
 | 
				
			||||||
		// Below ~400V, activate booster
 | 
							// Below ~400V, activate booster
 | 
				
			||||||
		enable_boost = true;
 | 
							boost_enable = true;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else					// Rising edge
 | 
						else					// Rising edge
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		// Reached ~400V, deactivate booster
 | 
							// Reached ~400V, deactivate booster
 | 
				
			||||||
		enable_boost = false;
 | 
							boost_enable = false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	reti();		// Exit interrupt-handler
 | 
						reti();		// Exit interrupt-handler
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Address: 0x002	INT1	-	Only at rising edge	[Code: 11]
 | 
					// Address: 0x002	INT1	-	On rising edge	[Code: 11]
 | 
				
			||||||
// Handles:	Counter tube "tick"
 | 
					// Handles:	Counter tube "tick"
 | 
				
			||||||
ISR(INT1_vect)
 | 
					ISR(INT1_vect)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	// Tick detected, signalOutput
 | 
						// Tick detected, signalOutput
 | 
				
			||||||
	enable_signalOutput = true;
 | 
						signalOutput_enable = true;
 | 
				
			||||||
	// Execute immediately
 | 
						// Execute immediately
 | 
				
			||||||
	signal_Output_nextTick = 0;
 | 
						signalOutput_nextTick = 0;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	reti();		// Exit interrupt-handler
 | 
						reti();		// Exit interrupt-handler
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user