Code is:
#define F_CPU 8000000UL
#include <avr/io.h>
#include "simon_atmega328_avr_c.h"
#include "tacho.h"
#include <avr/wdt.h>
#include <avr/interrupt.h>
#define speed0 950 //PCM value for a stopped motor
#define speed1 50 //timing value for speed1
#define speed2 33 //timing value for speed2
#define PCM_VALUE OCR1A // OCR1A holds the PWM duty value for the PCM output
#define PCM PB1
#define S1_o PB2
#define S2_o PD4
#define Fault_o PB5
#define S1_i PB6
#define S2_i PB7
#define Fault_i PB4
#define scavenge PD1
#define bounce 100 //time in ms to wait for switch de-bounce
int main(void)
{
uint16_t pwm1 = 1500; //starting PCM value for speed1
uint16_t pwm2 = 1800; //starting PCM value for speed2
sei(); //globally enable interrupts
bit_s(DDRB, PB0); //set output for startup test signal
bit_s (PORTB, PB0); //turn on test pin
_delay_ms(1000); //wait 1000ms to get a pulse that is easy to detect
bit_c(PORTB, PB0); //turn test pin off
wdt_enable(WDTO_2S); //enable watchdog timer with a 2S timeout
bit_s(DDRB, PCM); //PCM output
bit_s(DDRB, S1_o); //speed1 output
bit_s(DDRD, S2_o); //speed2 output
bit_s(DDRB, Fault_o); //fault output
bit_s(DDRD, scavenge); //scavenge output
bit_s(PORTB, S1_i); //enable pull-up for S1_i
bit_s(PORTB, S2_i); //enable pull-up for S2_i
bit_s(PORTB, Fault_i); //enable pull-up for fault in
bit_s(EICRA, ISC01); //set interrupt0 to fire on a falling edge
bit_c(EICRA, ISC00);
counter0_CTC(50);
bit_s(EIMSK, INT0); //enable external interrupt 0
pwm16_fast_setup();
PCM_VALUE = speed0;
wdt_reset ( );
while(1)
{
wdt_reset ( );
time_calc();
//if both speed inputs are high set PWM at 1000 (1.5mS pulse) reversed logic with mcu pull-up's
if ( (bit_get (PINB, S2_i)) && (bit_get (PINB, S1_i)) )
{
delay_ms(bounce); //wait for switch bounce
if ( (bit_get (PINB, S2_i)) && (bit_get (PINB, S1_i)) ) //recheck state
{
PCM_VALUE = speed0 ;
bit_c (PORTB, S1_o) ;
bit_c (PORTD, S2_o) ;
bit_c (PORTD, scavenge); // Turn off scavenge fan
}
}
//Set speed 1
//if speed 1 low AND speed 2 high set speed1 pulse reversed logic with mcu pull-up's
if ( (bit_get (PINB, S2_i)) && (!bit_get (PINB, S1_i)) )
{
_delay_ms(bounce); //wait for switch bounce
if ( (bit_get (PINB, S2_i)) && (!bit_get (PINB, S1_i)) ) //recheck state
{
bit_s (PORTB, S1_o) ;
bit_c (PORTD, S2_o) ;
if (time > speed1)
pwm1++;
if (time < speed1)
pwm1--;
PCM_VALUE = pwm1;
bit_s (PORTD, scavenge); //Turn on Scavenge fan
}
}
//Set speed 2
//if speed 2 low AND speed 1 high set speed2 pulse reversed logic with mcu input pull-up's
if ( (!bit_get (PINB, S2_i)) && (bit_get (PINB, S1_i)) )
{
_delay_ms(bounce); //wait for switch bounce
if ( (!bit_get (PINB, S2_i)) && (bit_get (PINB, S1_i)) ) //recheck state
{
bit_c (PORTB, S1_o) ;
bit_s (PORTD, S2_o) ;
if (time > speed2)
pwm2++;
if (time < speed2)
pwm2--;
PCM_VALUE = pwm2;
bit_s (PORTD, scavenge); //Turn on Scavenge fan
}
}
//fault signal
//if the fault input is 1 then turn on fault output else turn it off
if (bit_get (PINB, Fault_i))
bit_s(PORTB, Fault_o);
else
bit_c(PORTB, Fault_o);
}
}