diff --git a/Basic_Timer_setting_for_40kHz_UNO/Basic_Timer_setting_for_40kHz_UNO.ino b/Basic_Timer_setting_for_40kHz_UNO/Basic_Timer_setting_for_40kHz_UNO.ino deleted file mode 100644 index 39ed2a2..0000000 --- a/Basic_Timer_setting_for_40kHz_UNO/Basic_Timer_setting_for_40kHz_UNO.ino +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include - -#define N_PORTS 1 -#define N_DIVS 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") -#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") -*/ - -#define OUTPUT_WAVE(pointer, d) PORTC = pointer[d*N_PORTS + 0] - -#define N_BUTTONS 5 -//half a second -#define BUTTON_SENS 10000 -#define N_FRAMES 29 - -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; - - 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 - - for (int i = 2; i < 8; ++i){ //pin 2 to 7 (D2 to D7) are inputs for the buttons - pinMode(i, INPUT_PULLUP); - } - - // generate a sync signal of 40khz in pin 10 - 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; - interrupts(); // enable all interrupts - - // disable everything that we do not need - ADCSRA = 0; // ADC - power_adc_disable (); - power_spi_disable(); - power_twi_disable(); - power_timer0_disable(); - power_usart0_disable(); - //Serial.begin(115200); -} - -void loop() { - -} diff --git a/DriverNano_Tractor/DriverNano_Tractor.ino b/Interrupt.ino similarity index 84% rename from DriverNano_Tractor/DriverNano_Tractor.ino rename to Interrupt.ino index 2ac23b8..b065653 100644 --- a/DriverNano_Tractor/DriverNano_Tractor.ino +++ b/Interrupt.ino @@ -1,23 +1,24 @@ -// #include #include -#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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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}, @@ -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); @@ -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 @@ -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(); @@ -155,6 +144,4 @@ void setup() } -void loop(){ - -} \ No newline at end of file +void loop(){} diff --git a/README.md b/README.md index a029cfa..afbc874 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/interrupt_2.ino b/interrupt_2.ino new file mode 100644 index 0000000..9280fc2 --- /dev/null +++ b/interrupt_2.ino @@ -0,0 +1,148 @@ +#include +#include +#include +#include + +#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,0x6,0x6,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,0x6,0x6,0xc}, + {0x6,0x6,0x6,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6,0x6}, + {0x6,0x6,0x6,0x6,0x6,0x6,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0x6,0x6,0x6,0x6,0x6}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,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc}, + {0xc,0xc,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,0xc,0x6,0x6,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9}, + {0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0xc,0xc,0x6,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9}, + {0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0x6,0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3}, + {0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0xc,0x6,0x6,0x3,0x3,0x3,0x3,0x3}, + {0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0xc,0xc,0x6,0x3,0x3,0x3}, + {0x6,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x9,0x9,0x9,0x9,0x9,0x9,0x9,0x9,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; +} +*/ \ No newline at end of file