Posting code in the allowed 5-minute intervals
BLDC:
/* bldc.h
Header intended to contain all functions that keep the motor running
*/
#include <defines.h>
void bldc_move();
void set_bemf(int phase, int type);
void motor_start(void);
void beep(int time, int freq);
void reset_motor(void);
void AH_BL(void);
void AH_CL(void);
void BH_CL(void);
void BH_AL(void);
void CH_AL(void);
void CH_BL(void);
// All functions in the header follow below and are separated by lines like the one below this one:
////////////////////////////////////////////////////////////////
void bldc_move() // Controlling which bridge leg of MOSFETs get the regulated PWM.
{
//noInterrupts();
switch(bldc_step) // OCR0B = phase A, OCR1B = phase C, OCR1A = phase = B.
{
case 0:
AH_BL();
set_bemf(C, FALLING);
break;
case 1:
AH_CL();
set_bemf(B, RISING);
break;
case 2:
BH_CL();
set_bemf(A, FALLING);
break;
case 3:
BH_AL();
set_bemf(C, RISING);
break;
case 4:
CH_AL();
set_bemf(B, FALLING);
break;
case 5:
CH_BL();
set_bemf(A, RISING);
}
//interrupts();
}
////////////////////////////////////////////////////////////////
void AH_BL() // TCCR1A = OCR1n, TCCR0A = OCR0n.
{
TCCR1A = ~((1 << COM1A1) | (1 << COM1B1));
TCCR0A |= (1 << COM0B1);
TRANSPORTREG = 0b00010000;
}
////////////////////////////////////////////////////////////////
void AH_CL()
{
TCCR1A &= ~((1 << COM1A1) | (1 << COM1B1));
TCCR0A |= (1 << COM0B1);
TRANSPORTREG = 0b00001000; // 0x08
}
////////////////////////////////////////////////////////////////
void BH_CL()
{
CLEAR_BIT(TCCR1A, WGM11);
CLEAR_BIT(TCCR1B, WGM13);
TCCR1A |= (1 << COM1A1);
TCCR1A &= ~((1 << COM1B1));
TCCR0A &= ~((1 << COM0B1));
TRANSPORTREG = 0b00000100; // 0x04
}
////////////////////////////////////////////////////////////////
void BH_AL()
{
CLEAR_BIT(TCCR1A, WGM11);
CLEAR_BIT(TCCR1B, WGM13);
TCCR1A |= (1 << COM1A1);
TCCR1A &= ~((1 << COM1B1));
TCCR0A &= ~((1 << COM0B1));
TRANSPORTREG = 0b00010000;
}
////////////////////////////////////////////////////////////////
void CH_AL()
{
CLEAR_BIT(TCCR1A, WGM11);
CLEAR_BIT(TCCR1B, WGM13);
TCCR1A &= ~((1 << COM1A1));
TCCR1A |= (1 << COM1B1);
TCCR0A &= ~((1 << COM0B1));
TRANSPORTREG = 0b00001000; // 0x08
}
////////////////////////////////////////////////////////////////
void CH_BL()
{
CLEAR_BIT(TCCR1A, WGM11);
CLEAR_BIT(TCCR1B, WGM13);
TCCR1A &= ~((1 << COM1A1));
TCCR1A |= (1 << COM1B1);
TCCR0A &= ~((1 << COM0B1));
TRANSPORTREG = 0b00000100; // 0x04
}
////////////////////////////////////////////////////////////////
void ALL_OFF()
{
TCCR1A &= ~((1 << COM1A1) | (1 << COM1B1));
TCCR0A &= ~((1 << COM0B1));
TRANSPORTREG = 0b00011100; // 0x1C
}
////////////////////////////////////////////////////////////////
void set_bemf(int phase, int type)
{
switch (phase)
{
case A:
if (type == RISING)
{
PCMSK1 = 0x04; // Interrupt at pin 0b00000100 which corresponds to pin PC2.
pin_state = 0x04; // Desired value for the pin to have changed to stored in this variable.
}
else
{
PCMSK1 = 0x04; // Interrupt at pin 0b00000100 which corresponds to pin PC2.
pin_state = 0;
}
break;
case B:
if (type == RISING)
{
PCMSK1 = 0x08; // Interrupt at pin 0b00000100 which corresponds to pin PC3.
pin_state = 0x08;
}
else
{
PCMSK1 = 0x08; // Interrupt at pin 0b00000100 which corresponds to pin PC3.
pin_state = 0;
}
break;
case C:
if (type == RISING)
{
PCMSK1 = 0x10; // Interrupt at pin 0b00000100 which corresponds to pin PC4.
pin_state = 0x10;
}
else
{
PCMSK1 = 0x10; // Interrupt at pin 0b00000100 which corresponds to pin PC4.
pin_state = 0;
}
break;
}
}
////////////////////////////////////////////////////////////////
void reset_motor()
{
STATUS.motorON = False;
STATUS.emergency = False;
ALL_OFF();
bldc_step = AB;
set_bemf(A, RISING);
}
Set up:
// 328.h Contains a commented set - up of the ATMEGA 328P microcontroller.
void init_328p(void)
{
init();
STATUS.allflags = False; // All flags to zero
ADMUX |= (1 << REFS0) | (1 << ADLAR); // External reference and 8 - bit ADC
ADCSRA = 0x87; // ADC-module turned on. 0b10000111
// Initiating inhibit transistor control pins.
CLEAR_BIT(DDRC, DDC2);
CLEAR_BIT(DDRC, DDC3);
CLEAR_BIT(DDRC, DDC4);
CLEAR_BIT(PORTC, PORTC2);
CLEAR_BIT(PORTC, PORTC3);
CLEAR_BIT(PORTC, PORTC4);
CLEAR_BIT(DDRC, DDC1);
SET_BIT(PORTC, PORTC1); // For break-checking
DDRD = 0b00111100;
SET_BIT(DDRB, DDB1);
SET_BIT(DDRB, DDB2);
CLEAR_BIT(PORTB,PORTB1);
CLEAR_BIT(PORTB,PORTB2);
PORTD = 0; // Setting standard mode for PWM-pins to ground.
// Setting three pins to PWM-Out. OC0B OC1B OC1A
noInterrupts();
// Using timer 2 for timekeeping
TCCR0A |= (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); // Setting tyoe of PWM for OC0B.
TCCR0B |= (1 << CS01); // (1 << WGM02);
TCCR1B |= (1 << WGM12) | (1 << CS11);
CLEAR_BIT(TCCR1B,CS10);
CLEAR_BIT(TCCR1B,CS12);
TCCR1A |= (1 << WGM10);
TCCR1A |= (1 << COM1A1)|(1 << COM1B1);
//TCCR1A = 0x01; // Fixing PWM frequency.
//TCCR1B = 0x09;
TCCR0B &= ~((1 << CS00) | (1 << CS02));
// Interrupts for BEMF sensing.
PCICR = 0x02; // PCICR = 0x03
PCMSK1 = 0x04; // Setting first intrerrupt at PCINT10. 0b00000100
//PCMSK0 = 0x08;
// Timekeeping
TCCR2A = 0x02; // CTC mode
SET_BIT(TCCR2B, CS21); // Prescaling 8
CLEAR_BIT(TCCR2B, CS22);
CLEAR_BIT(TCCR2B, CS20);
TIMSK2 = 0x02;// Enabling CTC with OCR2A
TCNT2 = 0;
OCR2A = 200;
interrupts();
}