/**************************************************************************** * FUNCTION: main() - local routine * ****************************************************************************/ int main(int argc, char *argv[]) { int key_entered, Ch; intro(); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 4; Ch++) OUTPORT(base + (Ch * 2), Cal(0x000, Ch)); inb(base+0xA); // set automatic update mode inb(base+0XF); // release zero latch do { CLRSCR(); write_DAC(); CPRINTF("\n\nWould you like to output another value (Y or N)?\n"); key_entered = GETCH(); } while((key_entered != 'N') && (key_entered != 'n')); /* Set all 16 buffers to 0 */ for (Ch = 0; Ch < 4; Ch++) OUTPORT(base + (Ch * 2), Cal(0x000, Ch)); CLRSCR(); PUTS("DA12-16 Sample1 complete."); } /* end main */
void rgbled_init(void (*func)(void)) { refresh_func = func; led_states = (led_state*)malloc(LEDS_COUNT * sizeof(led_state)); DDRPORT(A0_PORT) |= 0x01 << A0_PIN; OUTPORT(A0_PORT) &= ~(0x01 << A0_PIN); DDRPORT(A1_PORT) |= 0x01 << A1_PIN; OUTPORT(A1_PORT) &= ~(0x01 << A1_PIN); DDRPORT(A2_PORT) |= 0x01 << A2_PIN; OUTPORT(A2_PORT) &= ~(0x01 << A2_PIN); DDRPORT(A3_PORT) |= 0x01 << A3_PIN; OUTPORT(A3_PORT) &= ~(0x01 << A3_PIN); DDRPORT(A4_PORT) |= 0x01 << A4_PIN; OUTPORT(A4_PORT) &= ~(0x01 << A4_PIN); DDRPORT(A5_PORT) |= 0x01 << A5_PIN; OUTPORT(A5_PORT) &= ~(0x01 << A5_PIN); DDRPORT(A6_PORT) |= 0x01 << A6_PIN; OUTPORT(A6_PORT) &= ~(0x01 << A6_PIN); DDRPORT(A7_PORT) |= 0x01 << A7_PIN; OUTPORT(A7_PORT) &= ~(0x01 << A7_PIN); DDRPORT(D0_PORT) |= 0x01 << D0_PIN; DDRPORT(D1_PORT) |= 0x01 << D1_PIN; DDRPORT(D2_PORT) |= 0x01 << D2_PIN; DDRPORT(D3_PORT) |= 0x01 << D3_PIN; DDRPORT(D4_PORT) |= 0x01 << D4_PIN; DDRPORT(D5_PORT) |= 0x01 << D5_PIN; DDRPORT(D6_PORT) |= 0x01 << D6_PIN; DDRPORT(D7_PORT) |= 0x01 << D7_PIN; }
/**************************************************************************** * FUNCTION: write_DAC() - local routine * * PURPOSE: Prompts the user for DAC number and voltage, then calculates * * the actual output voltage based on resolution, displays it * * and writes the value to the DAC. * ****************************************************************************/ void write_DAC(void) { float volt_value_entered,volt_value_expected; unsigned counts_entered,dac_number; /* prompt for the DAC number and desire voltage. */ CPRINTF("Enter the DAC number (0 through 15 only)\n: "); scanf("%u",&dac_number); CPRINTF("\n\nEnter a voltage between 0.000v to 9.997v\n: "); scanf("%f",&volt_value_entered); /* convert entered voltage to associated number of DAC counts. Scale factor for 12 bit resolution of DA12-16 is 0.002442. */ volt_value_entered /= 0.002442; /* compute the digital output needed for this value and the actual voltage that will be expected, and display the voltage. */ counts_entered=(int) volt_value_entered; volt_value_expected = (float) counts_entered * 0.002442; CPRINTF("\nDue to the 12-bit resolution of the DAC, you should expect\n"); CPRINTF("to see a voltage of %4.3f.",volt_value_expected); /* write to the DAC */ OUTPORT(base + (2 * dac_number), Cal(counts_entered, dac_number)); /* write low byte */ } /* end write_DAC */
// Write 32 bits to display register and latch void writeDisplayData(uint32_t data) { // shift out data for (uint8_t i = 0; i < 4; i++) { // send byte over SPI SPDR = data >> (8 * (3 - i)); // wait for transmission complete while(!(SPSR & (1 << SPIF))); } // latch register data sbit(OUTPORT(DISP_LATCH_PORT), DISP_LATCH_BIT); _delay_us(10); cbit(OUTPORT(DISP_LATCH_PORT), DISP_LATCH_BIT); }
int SERVO_close(nDEV numdev, nOPT option){ OUTPORT(PIN); TIMER_MODE_DISABLE(); return TRUE; }
BOOLEAN mpuWrite( PSOUND_HARDWARE pHw, UCHAR value ) /*++ Routine Description: Write a command or data to the MPU Arguments: pHw - Pointer to the device extension data value - the value to be written Return Value: TRUE if written correctly , FALSE otherwise --*/ { ULONG uCount; ASSERT(pHw->Key == HARDWARE_KEY); uCount = 100; while (uCount--) { int InnerCount; HwEnter(pHw); // // Inner count loop protects against dynamic deadlock with // midi. // for (InnerCount = 0; InnerCount < 10; InnerCount++) { if (!(INPORT(pHw, MPU_STATUS_PORT) & 0x40)) { // is it ok to send data (bit 6 clear)? - drude OUTPORT(pHw, MPU_DATA_PORT, value); break; } KeStallExecutionProcessor(1); // 1 us } HwLeave(pHw); if (InnerCount < 10) { return TRUE; } } dprintf1(("mpuWrite:Failed to write %x to mpu", (ULONG)value)); return FALSE; }
int SERVO_open(nDEV numdev, nOPT option){ OUTPORT(POUT); TIMER_MODE_ENABLE(); TIMER_OUT_SEL(); TIMER_PRESCALER(); TIMERVAL_INIT(); return 0; }
// Setup MCU input/output void init() { // Display connection pins output sbit(DDR(DISP_DATA_PORT), DISP_DATA_BIT); sbit(DDR(DISP_CLK_PORT), DISP_CLK_BIT); sbit(DDR(DISP_LATCH_PORT), DISP_LATCH_BIT); sbit(DDR(DISP_OE_PORT), DISP_OE_BIT); // SPI configuration: master, sck=fclk/128=125kHz SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0); // LEDs output sbit(DDR(LED1_PORT), LED1_BIT); sbit(DDR(LED2_PORT), LED2_BIT); sbit(DDR(LED3_PORT), LED3_BIT); sbit(DDR(LED4_PORT), LED4_BIT); // Button pull-ups sbit(OUTPORT(BUTTON1_PORT), BUTTON1_BIT); sbit(OUTPORT(BUTTON2_PORT), BUTTON2_BIT); sbit(OUTPORT(BUTTON3_PORT), BUTTON3_BIT); sbit(OUTPORT(BUTTON4_PORT), BUTTON4_BIT); // Buzzer timer output sbit(DDR(BUZZER_PORT), BUZZER_BIT); TCCR1A = (1<<COM1A1) | (1<<COM1A0) | (1<<WGM11); TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11); ICR1 = 0; // timer top value OCR1A = 500; // output compare value // ADC initialization // - ADC clock frequency: F_CPU / 128 (125kHz @ 16MHz) // - reference: AVcc with external cap ADMUX = (1<<REFS0); ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // init I2C interface i2c_init(); }
BOOLEAN mpuWriteNoLock( PSOUND_HARDWARE pHw, UCHAR value ) /*++ Routine Description: Write a command or data to the MPU. The call assumes the caller has acquired the spin lock Arguments: pHw - Pointer to the device extension data value - the value to be written Return Value: TRUE if written correctly , FALSE otherwise --*/ { int uCount; ASSERT(pHw->Key == HARDWARE_KEY); uCount = 1000; while (uCount--) { if (!(INPORT(pHw, MPU_STATUS_PORT) & 0x40)) { // is it ok to send data (bit 6 clear)? - drude OUTPORT(pHw, MPU_DATA_PORT, value); break; } KeStallExecutionProcessor(1); // 1 us } if (uCount >= 0) { return TRUE; } dprintf1(("mpuWriteNoLock:Failed to write %x to mpu", (ULONG)value)); return FALSE; }
static void set_clock(uint8_t addr_pin) { switch (addr_pin) { case 0: OUTPORT(A0_PORT) |= 0x01 << A0_PIN; OUTPORT(A0_PORT) &= ~(0x01 << A0_PIN); break; case 1: OUTPORT(A1_PORT) |= 0x01 << A1_PIN; OUTPORT(A1_PORT) &= ~(0x01 << A1_PIN); break; case 2: OUTPORT(A2_PORT) |= 0x01 << A2_PIN; OUTPORT(A2_PORT) &= ~(0x01 << A2_PIN); break; case 3: OUTPORT(A3_PORT) |= 0x01 << A3_PIN; OUTPORT(A3_PORT) &= ~(0x01 << A3_PIN); break; case 4: OUTPORT(A4_PORT) |= 0x01 << A4_PIN; OUTPORT(A4_PORT) &= ~(0x01 << A4_PIN); break; case 5: OUTPORT(A5_PORT) |= 0x01 << A5_PIN; OUTPORT(A5_PORT) &= ~(0x01 << A5_PIN); break; case 6: OUTPORT(A6_PORT) |= 0x01 << A6_PIN; OUTPORT(A6_PORT) &= ~(0x01 << A6_PIN); break; case 7: OUTPORT(A7_PORT) |= 0x01 << A7_PIN; OUTPORT(A7_PORT) &= ~(0x01 << A7_PIN); break; } }
static void set_data_pin(uint8_t data_pin, uint8_t value) { switch (data_pin) { case 0: if (value) OUTPORT(D0_PORT) |= 0x01 << D0_PIN; else OUTPORT(D0_PORT) &= ~(0x01 << D0_PIN); break; case 1: if (value) OUTPORT(D1_PORT) |= 0x01 << D1_PIN; else OUTPORT(D1_PORT) &= ~(0x01 << D1_PIN); break; case 2: if (value) OUTPORT(D2_PORT) |= 0x01 << D2_PIN; else OUTPORT(D2_PORT) &= ~(0x01 << D2_PIN); break; case 3: if (value) OUTPORT(D3_PORT) |= 0x01 << D3_PIN; else OUTPORT(D3_PORT) &= ~(0x01 << D3_PIN); break; case 4: if (value) OUTPORT(D4_PORT) |= 0x01 << D4_PIN; else OUTPORT(D4_PORT) &= ~(0x01 << D4_PIN); break; case 5: if (value) OUTPORT(D5_PORT) |= 0x01 << D5_PIN; else OUTPORT(D5_PORT) &= ~(0x01 << D5_PIN); break; case 6: if (value) OUTPORT(D6_PORT) |= 0x01 << D6_PIN; else OUTPORT(D6_PORT) &= ~(0x01 << D6_PIN); break; case 7: if (value) OUTPORT(D7_PORT) |= 0x01 << D7_PIN; else OUTPORT(D7_PORT) &= ~(0x01 << D7_PIN); break; } }
int main(void) { // init hardware setup init(); // setup temperature resolution // write temperature register pointer i2c_start(TEMPSENS_ADDRESS, I2C_WRITE); i2c_write(0x01); i2c_write(0b01100000); i2c_stop(); while(1) { // change mode by pressed button if (button1Pressed()) { mode = MODE_1; } else if (button2Pressed()) { mode = MODE_2; } else if (button3Pressed()) { mode = MODE_3; } // mode1 if (mode == MODE_1) { // read analog value from potentiometer uint32_t pot = readAnalog(POT_CHANNEL); pot *= 4888; pot += 500; pot /= 1000; if (++mydelay >= 10) { writeDisplayNumber(pot, 0); mydelay = 0; } // led1 on sbit(OUTPORT(LED1_PORT), LED1_BIT); } else { // led1 off cbit(OUTPORT(LED1_PORT), LED1_BIT); } // mode2 if (mode == MODE_2) { int16_t temp = readTemperature(); writeDisplayTemp(temp, 2); // led2 on sbit(OUTPORT(LED2_PORT), LED2_BIT); } else { // led2 off cbit(OUTPORT(LED2_PORT), LED2_BIT); } // show potentiometer value if (mode == MODE_3) { // led3 on sbit(OUTPORT(LED3_PORT), LED3_BIT); mode3(); /* if (mydelay < 12) { // turn-on buzzer // frequency 2kHz ICR1 = 2000; OCR1A = 1000; // write to display writeDisplayData(convertBcd(DIGIT_MINUS, DIGIT_MINUS, DIGIT_BLANK, DIGIT_BLANK, 255)); } else { // turn-on buzzer // frequency 2kHz ICR1 = 4000; OCR1A = 2000; // write to display writeDisplayData(convertBcd(DIGIT_BLANK, DIGIT_BLANK, DIGIT_MINUS, DIGIT_MINUS, 255)); } if (++mydelay >= 25) mydelay = 0; */ } else { // turn-off buzzer ICR1 = 0; // led3 off cbit(OUTPORT(LED3_PORT), LED3_BIT); } // wait until all mode buttons are released while (button1Pressed() || button2Pressed() || button3Pressed()); _delay_ms(20); } }
void mode3() { uint8_t t = 100; uint8_t esc[3] = {255, 255, 255}; // wait for start writeDisplayNumber(t, 1); while (!button4Pressed()); uint8_t swlast = 0; while (1) { t--; writeDisplayNumber(t, 1); if (button4Pressed() && swlast == 0) { swlast = 1; esc[2] = esc[1]; esc[1] = esc[0]; esc[0] = t; // deactivate if (esc[2] - esc[0] < 10) break; } else if (!button4Pressed() && swlast == 1) { swlast = 0; } uint8_t f = t % 10; OCR1A = 500; switch (f) { case 0: // turn-on buzzer ICR1 = 1000; break; case 9: // turn-off buzzer ICR1 = 0; break; case 5: if (t < 50 && t > 20) { ICR1 = 1000; } if (t < 20) { ICR1 = 0; } break; case 4: if (t < 50 && t > 20) { ICR1 = 0; } if (t < 20) { ICR1 = 1000; } break; case 8: case 6: case 2: if (t < 20) { ICR1 = 1000; } break; case 7: case 3: case 1: if (t < 20) { ICR1 = 0; } break; } if (t == 0) break; _delay_ms(100); } if (t == 0) { sbit(OUTPORT(LED4_PORT), LED4_BIT); } else { ICR1 = 0; } while (!button3Pressed()); }
BOOLEAN mpuReset( PSOUND_HARDWARE pHw ) /*++ Routine Description: Reset the MPU to UART mode. Arguments: pHw - pointer to the device extension data Return Value: TRUE if we succeeded, FALSE if we failed. --*/ { // // try for a reset - note that midi output may be running at this // point so we need the spin lock while we're trying to reset // BYTE Ack = 0; int i; HwEnter(pHw); // dump the current hardware midi input buffer // (We might have data availible if the midi devices // connected have sent anything at all.) mpuDumpBufferNoLock(pHw); // reset the card OUTPORT(pHw, MPU_COMMAND_PORT, 0xFF); // set to smart mode first // wait for the acknowledgement - drude // NOTE: When the Ack arrives, it will trigger an interrupt. // Normally the DPC routine would read in the ack byte and we // would never see it, however since we have the hardware locked (HwEnter), // we can read the port before the DPC can and thus we receive the // Ack. for(i = 10000; i > 0; i--) // some times it takes a really long time { if (!(INPORT(pHw, MPU_STATUS_PORT) & 0x80)) { // do we have data waiting (bit 7 clear)? Ack = INPORT(pHw, MPU_DATA_PORT); // read the ack byte break; } KeStallExecutionProcessor(25); } // NOTE: We cannot check the ACK byte because if the card was already in // UART mode it will not send an ACK but it will reset. // reset the card again OUTPORT(pHw, MPU_COMMAND_PORT, 0x3F); // set to UART mode // wait for the acknowledgement Ack = 0; for(i = 10000; i > 0; i--) { if (!(INPORT(pHw, MPU_STATUS_PORT) & 0x80)) { // do we have data waiting (bit 7 clear)? Ack = INPORT(pHw, MPU_DATA_PORT); // read the ack byte break; } KeStallExecutionProcessor(25); } HwLeave(pHw); // did we succeed? - drude // (if we did not receive the second ACK, // something is wrong with the hardware.) if(Ack != 0xFE) { dprintf1(("mpuReset:reset hardware failed: %x", (ULONG)Ack)); return FALSE; } else { dprintf1(("mpuReset:reset hardware OK")); return TRUE; } }