Skip to content
Open

This file was deleted.

67 changes: 27 additions & 40 deletions DriverNano_Tractor/DriverNano_Tractor.ino → Interrupt.ino
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// #include <avr/sleep.h>
#include <avr/power.h>

#define N_PORTS 1
#define N_DIVS 24
#define N_Transducers 24

#define WAIT_LOT(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop");__asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop");__asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop");__asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//14 clock

#define WAIT_MID(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop");__asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop");__asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
#define WAIT_LIT(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//13 clock

#define WAIT_LIT(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//9 clock

#define OUTPUT_WAVE(pointer, d) PORTC = pointer[d*N_PORTS + 0]
#define OUTPUT_WAVE(pointer, d) PORTC = pointer[d]

#define N_BUTTONS 5
//half a second
#define BUTTON_SENS 10000
#define BUTTON_SENS
#define N_FRAMES 29

static byte frame = 19;
static byte animation[N_FRAMES][N_DIVS] =
static byte animation[N_FRAMES][N_Transducers] = //29x24
{{0x6,0xc,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6},
{0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x6,0x6,0x6},
{0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3},
Expand All @@ -36,8 +37,8 @@ static byte animation[N_FRAMES][N_DIVS] =
{0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x6,0x6,0x6,0x6,0x6,0x3},
{0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x6,0x6},
{0xc,0x6,0x6,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc},
{0xc,0xc,0xc,0xc,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0xc,0xc,0xc,0xc,0xc,0xc,0xc}, //19th frame
{0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0xc,0xc,0xc,0xc,0xc},
{0xc,0xc,0xc,0xc,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0xc,0xc,0xc,0xc,0xc,0xc,0xc},
{0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0xc,0xc,0xc,0xc,0xc},
{0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x9,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x6,0xc,0xc},
{0x6,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x6,0x6},
{0x6,0x6,0x6,0x6,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3},
Expand All @@ -46,36 +47,24 @@ static byte animation[N_FRAMES][N_DIVS] =
{0x3,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3},
{0x9,0x9,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9},
{0x9,0x9,0x9,0x9,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9},
{0x9,0x9,0x9,0x9,0x9,0xc,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x3,0x9,0x9,0x9,0x9,0x9,0x9}}; //29th frame
// I think above is the phase information for each transducers.
// But I'm not sure why there is "29" N_FRAEMS..?
{0x9,0x9,0x9,0x9,0x9,0xc,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x3,0x9,0x9,0x9,0x9,0x9,0x9}};

/*
0x6 = 0b 0000 0110
0x3 = 0b 0000 0011
0x9 = 0b 0000 1001
0xc = 0b 0000 1010
*/

void setup()
{

/*
for (int i = 0; i < (N_PORTS*N_DIVS); ++i){
animation[frame][i] = 0;
}

for (int i = 0; i < (N_PORTS*N_DIVS/2); ++i){
animation[frame][i] = 0b11111111;
}

for(int i = 0; i < N_DIVS; ++i){
if (i % 2 == 0){
animation[frame][i * N_PORTS] |= 0b00000001;
}else{
animation[frame][i * N_PORTS] &= 0b11111110;
}
}
*/
DDRC = 0b00111111; //A0 to A5 are the signal outputs
PORTC = 0b00000000;
PORTC = 0b00000000;

pinMode(10, OUTPUT); //pin 10 (B2) will generate a 40kHz signal to sync
pinMode(11, INPUT_PULLUP); //pin 11 (B3) is the sync in
//please connect pin 10 to pin 11
//connect pin 10 to pin 11

for (int i = 2; i < 8; ++i){ //pin 2 to 7 (D2 to D7) are inputs for the buttons
pinMode(i, INPUT_PULLUP);
Expand All @@ -85,8 +74,8 @@ void setup()
noInterrupts(); // disable all interrupts
TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare
TCCR1B = bit (WGM12) | bit (WGM13) | bit (CS10); // fast PWM, no prescaler
OCR1A = (F_CPU / 40000L) - 1;
OCR1B = (F_CPU / 40000L) / 2;
OCR1A = (F_CPU / 40000L) - 1; //16MHz / 40kHz -1
OCR1B = (F_CPU / 40000L) / 2; //16MHz / 40kHz /2
interrupts(); // enable all interrupts

// disable everything that we do not need
Expand All @@ -98,16 +87,16 @@ void setup()
power_usart0_disable();
//Serial.begin(115200);

byte* emittingPointer = &animation[frame][0]; //It means the pointer, static byte frame = 19; / & : address of operator
byte* emittingPointer = &animation[frame][0];
byte buttonsPort = 0;

bool anyButtonPressed;
bool buttonPressed[N_BUTTONS]; //Maybe.. N_BUTTONS = 5 means PD2 to PD7, Sensing the button action of DDRD
bool buttonPressed[N_BUTTONS];
short buttonCounter = 0;

LOOP:
while(PINB & 0b00001000); //wait for pin 11 (B3) to go low
// #define OUTPUT_WAVE(pointer, d) PORTC = pointer[d*N_PORTS + 0]

OUTPUT_WAVE(emittingPointer, 0); buttonsPort = PIND; WAIT_LIT();
OUTPUT_WAVE(emittingPointer, 1); anyButtonPressed = (buttonsPort & 0b11111100) != 0b11111100; WAIT_MID();
OUTPUT_WAVE(emittingPointer, 2); buttonPressed[0] = buttonsPort & 0b00000100; WAIT_MID();
Expand Down Expand Up @@ -155,6 +144,4 @@ void setup()

}

void loop(){

}
void loop(){}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
This branch is part of Timer/Counter code for project.
@오준영
Basic idea is generate the 40kHz.
An open source code using the sync operation, so I think that can be the proper way to achieve it.
This branch is part of Interrupt code for project.
@김동현
We are using three external switch to control the each transducers' phase.
D2(PB2) is used for Up shifting the pressure trap.
D3(PB3) is used for Down shifting the pressure trap.
D4(PB4) is used for Reset the pressure trap.
148 changes: 148 additions & 0 deletions interrupt_2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <util/delay.h>

#define N_TRANSDUCERS 24

#define WAIT_LOT(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//14 clock

#define WAIT_MID(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//13 clock

#define WAIT_LIT(a) __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop"); __asm__ __volatile__ ("nop")
//9 clock

#define OUTPUT_WAVE(pointer, d) PORTC = pointer[d]

#define N_BUTTONS 5
#define BUTTON_SENS 50
#define N_FRAMES 29

volatile uint8_t frame = 19;
volatile uint8_t pressed_button = 0;
volatile uint8_t buttonCounter = 0;
volatile bool anyButtonPressed = false;

static uint8_t animation[N_FRAMES][N_TRANSDUCERS] = //29x24
{
{0x6,0xc,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6},
{0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3,0x3,0x6,0x6,0x6},
{0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x3,0x3},
{0x3,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9},
{0x9,0x9,0x9,0x9,0x3,0x3,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0xc,0xc,0x9,0x9,0x9,0x9,0x9,0x9},
{0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x3,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,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9,0x9,0x9,0x9,0x9,0xc,0xc,0x6,0x6},
{0x6,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0xc}
};

void initTimer() {
TCCR0A = (1 << WGM01); // CTC mode
TCCR0B = (1 << CS01) | (1 << CS00); // Prescaler 64
OCR0A = 250; // Compare value for ~1ms interrupt
TIMSK0 = (1 << OCIE0A); // Enable compare interrupt
}

void initButtons() {
// Set buttons as input with pull-up resistors
DDRD &= ~(1 << PD0) & ~(1 << PD1) & ~(1 << PD2) & ~(1 << PD3) & ~(1 << PD4);
PORTD |= (1 << PD0) | (1 << PD1) | (1 << PD2) | (1 << PD3) | (1 << PD4);
}

ISR(TIMER0_COMPA_vect) {
uint8_t buttonState = ~PIND & ((1 << PD0) | (1 << PD1) | (1 << PD2) | (1 << PD3) | (1 << PD4));
if (buttonState) {
if (!anyButtonPressed) {
pressed_button = buttonState;
anyButtonPressed = true;
}
} else {
anyButtonPressed = false;
}
}

void setup() {
// Initialize the microcontroller
cli();
clock_prescale_set(clock_div_1);
DDRC = 0xFF; // All PORTC as output
initButtons();
initTimer();
sei();
}

void loop() {
for (uint8_t c = 0; c < N_TRANSDUCERS; c++) {
switch (c) {
case 7:
case 15:
case 23:
OUTPUT_WAVE(animation[frame], c);
WAIT_LOT(0);
break;
case 3:
case 11:
case 19:
OUTPUT_WAVE(animation[frame], c);
WAIT_MID(0);
break;
default:
OUTPUT_WAVE(animation[frame], c);
WAIT_LIT(0);
break;
}
}

if (anyButtonPressed) {
switch (pressed_button) {
case (1 << PD0):
frame = 0;
break;
case (1 << PD1):
frame = 5;
break;
case (1 << PD2):
frame = 10;
break;
case (1 << PD3):
frame = 15;
break;
case (1 << PD4):
frame = 20;
break;
default:
break;
}
}
}

/*
int main() {
setup();
while (1) {
loop();
}
return 0;
}
*/