void doLoopThing(int j){ // This FOR Loop picks the starting point for looking up the data in the "output" array // It starts at i=j and goes to i=8. This excludes any values for i that are less than j. int i; for (i=j; i<8; i++) { REGISTER_BIT(PORTB,0) = output[i]; // Send data from the "output" array out to the Serial Input Pin on the 595 via PB0 // Whatever state (hi or low) the Data Pin (PB0) is in when the shift clock is hit, is the state that is stored in the Shift Register shift_in(); // toggle Shift Clock Pin on 595 to shift current bit into SR } // end for // This FOR Loops picks up where the other FOR loop left off, but addressing the data that was less than j. // This loop effectively "wraps" around the array, so when i=8 and there is still room in the SR, the Loop fills in // the remaining spots starting back at i=0. This is useful for making a "ring counter" to // "scan" the columns of a Scrolling Dot Matrix LED display. for (i=0; i<j; i++) // this for loop actually picks the addresses in the "output" array to put out. { REGISTER_BIT(PORTB,0) = output[i]; // Send data from the "output" array out to the Serial Input Pin on the 595 via PB0 // Whatever state (hi or low) the Data Pin (PB0) is in when the shift clock is hit, is the state that is stored in the Shift Register shift_in(); // toggle Shift Clock Pin on 595 to shift current bit into SR } // end for latch_in(); // After 8 new bits have been clocked into the SR, Toggle the Latch Clock Pin on the 595 to present the 8 bits in the SR to th 595 Ouput Pins }
int main(void){ usart_init(); shift_init(); while(1){ uint8_t data = shift_in(); usart_transmit_byte(data); _delay_ms(500); } return 1; }
// The cog. void avrISP_Run(){ unsigned int value[12]; // Chip Select is asserted when it is low, so we make sure to put it high to start with high(pins.CS); // Set the Clock pin low low(pins.CLK); while(1){ low(pins.CS); // Assert the CS // Set the LCD backlight level shift_out(pins.MOSI, pins.CLK, MSBFIRST, 8, 0x57); // 'W' shift_out(pins.MOSI, pins.CLK, MSBFIRST, 8, LCD_Backlight); // Ask the AVR for data shift_out(pins.MOSI, pins.CLK, MSBFIRST, 8, 0x52); // 'R' // Save the returned data into an array for(int i = 0; i < 12; i++){ value[i] = shift_in(pins.MISO, pins.CLK, MSBPRE, 8); } high(pins.CS); // De-assert the CS // Get the data and stick it into the data struct // Buttons are 'corrected' so a high means the button is down inputs.buttonA = !((value[0] >> 4) & 0x01); inputs.buttonB = !((value[0] >> 5) & 0x01); inputs.leftStick.button = !((value[0] >> 6) & 0x01); inputs.rightStick.button = !((value[0] >> 7) & 0x01); inputs.battStat2 = !((value[0] >> 1) & 0x01); inputs.battStat1 = !(value[0] & 0x01); inputs.battVolts = (value[10] << 8) | value[11]; inputs.rotary = value[1]; inputs.leftStick.X = (value[2] << 8) | value[3]; inputs.leftStick.Y = (value[4] << 8) | value[5]; inputs.rightStick.X = (value[6] << 8) | value[7]; inputs.rightStick.Y = (value[8] << 8) | value[9]; // Pause for a bit pause(20); } }
int main(void) { shift_in_init(); uart_init(); stdout = &uart_output; stdin = &uart_input; uint8_t register_value; while (1) { /* Read in parallel input by setting SH/LD low. */ //digital_write(LATCH, LOW); //_delay_ms(10); /* Freeze data by setting SH/LD high. When SH/LD is high data enters */ /* to reqisters from SER input and shifts one place to the right */ /* (Q0 -> Q1 -> Q2, etc.) with each positive-going clock transition. */ //digital_write(LATCH, HIGH); shift_in_latch(); /* Read in first 74HC165 data. */ register_value = shift_in(); printf("%d \n", register_value); /* Read in second 74HC165 data. */ register_value = shift_in(); printf("%d \n", register_value); _delay_ms(2000); } return 0; }
void imu_SPIreadBytes(unsigned char csPin, unsigned char subAddress, unsigned char *dest, unsigned char count) { // To indicate a read, set bit 0 (msb) of first byte to 1 unsigned char rAddress = 0x80 | (subAddress & 0x3F); // Mag SPI port is different. If we're reading multiple bytes, // set bit 1 to 1. The remaining six bytes are the address to be read if ((csPin == __pinM) && count > 1) rAddress |= 0x40; low(csPin); shift_out(__pinSDIO, __pinSCL, MSBFIRST, 8, rAddress); for (int i=0; i<count; i++) { dest[i] = shift_in(__pinSDIO, __pinSCL, MSBPRE, 8); } high(csPin); }
t_stat cpu_one_inst (uint32 opc, uint32 ir) { uint32 ea, op, dat, res, dev, sh4, ch; t_bool ovf_this_cycle = FALSE; t_stat reason = 0; op = I_GETOP (ir); /* opcode */ ea = I_GETEA (ir); /* address */ switch (op) { /* case on opcode */ /* Loads, stores, transfers instructions */ case OP_B: /* bring */ A = Read (ea); /* A <- M[ea] */ delay = I_delay (opc, ea, op); break; case OP_H: /* hold */ Write (ea, A); /* M[ea] <- A */ delay = I_delay (opc, ea, op); break; case OP_C: /* clear */ Write (ea, A); /* M[ea] <- A */ A = 0; /* A <- 0 */ delay = I_delay (opc, ea, op); break; case OP_Y: /* store address */ dat = Read (ea); /* get operand */ dat = (dat & ~I_EA) | (A & I_EA); /* merge address */ Write (ea, dat); delay = I_delay (opc, ea, op); break; case OP_R: /* return address */ dat = Read (ea); /* get operand */ dat = (dat & ~I_EA) | (((PC + 1) & AMASK) << I_V_EA); Write (ea, dat); delay = I_delay (opc, ea, op); break; case OP_U: /* uncond transfer */ PCQ_ENTRY; PC = ea; /* transfer */ delay = I_delay (opc, ea, op); break; case OP_T: /* conditional transfer */ if ((A & SIGN) || /* A < 0 or */ ((ir & SIGN) && t_switch)) { /* -T and Tswitch set? */ PCQ_ENTRY; PC = ea; /* transfer */ } delay = I_delay (opc, ea, op); break; /* Arithmetic and logical instructions */ case OP_A: /* add */ dat = Read (ea); /* get operand */ res = (A + dat) & DMASK; /* add */ if ((~A ^ dat) & (dat ^ res) & SIGN) /* calc overflow */ ovf_this_cycle = TRUE; A = res; /* save result */ delay = I_delay (opc, ea, op); break; case OP_S: /* sub */ dat = Read (ea); /* get operand */ res = (A - dat) & DMASK; /* subtract */ if ((A ^ dat) & (~dat ^ res) & SIGN) /* calc overflow */ ovf_this_cycle = TRUE; A = res; delay = I_delay (opc, ea, op); break; case OP_M: /* multiply high */ dat = Read (ea); /* get operand */ A = (Mul64 (A, dat, NULL) << 1) & DMASK; /* multiply */ delay = I_delay (opc, ea, op); break; case OP_N: /* multiply low */ dat = Read (ea); /* get operand */ Mul64 (A, dat, &res); /* multiply */ A = res; /* keep low result */ delay = I_delay (opc, ea, op); /* total delay */ break; case OP_D: /* divide */ dat = Read (ea); /* get operand */ if (Div32 (A, dat, &A)) /* divide; overflow? */ ovf_this_cycle = TRUE; delay = I_delay (opc, ea, op); break; case OP_E: /* extract */ dat = Read (ea); /* get operand */ A = A & dat; /* and */ delay = I_delay (opc, ea, op); break; /* IO instructions */ case OP_P: /* output */ if (Q_LGP21) { /* LGP-21 */ ch = A >> 26; /* char, 6b */ if (ir & SIGN) /* 4b? convert */ ch = (ch & 0x3C) | 2; dev = I_GETTK (ir); /* device select */ } else { /* LGP-30 */ ch = I_GETTK (ir); /* char, always 6b */ dev = Q_OUTPT? DEV_PT: DEV_TT; /* device select */ } reason = op_p (dev & DEV_MASK, ch); /* output */ delay = I_delay (sim_grtime (), ea, op); /* next instruction */ break; case OP_I: /* input */ if (Q_LGP21) { /* LGP-21 */ ch = 0; /* initial shift */ sh4 = ir & SIGN; /* 4b/6b select */ dev = I_GETTK (ir); /* device select */ } else { /* LGP-30 */ ch = I_GETTK (ir); /* initial shift */ sh4 = Q_IN4B; /* 4b/6b select */ dev = Q_INPT? DEV_PT: DEV_TT; /* device select */ } if (dev == DEV_SHIFT) /* shift? */ A = shift_in (A, 0, sh4); /* shift 4/6b */ else reason = op_i (dev & DEV_MASK, ch, sh4); /* input */ delay = I_delay (sim_grtime (), ea, op); /* next instruction */ break; case OP_Z: if (Q_LGP21) { /* LGP-21 */ if (ea & 0xF80) { /* no stop? */ if (((ea & 0x800) && !bp32) || /* skip if any */ ((ea & 0x400) && !bp16) || /* selected switch */ ((ea & 0x200) && !bp8) || /* is off */ ((ea & 0x100) && !bp4) || /* or if */ ((ir & SIGN) && !OVF)) /* ovf sel and off */ PC = (PC + 1) & AMASK; if (ir & SIGN) /* -Z? clr overflow */ OVF = 0; } else { /* stop */ lgp21_sov = (ir & SIGN)? 1: 0; /* pending sense? */ reason = STOP_STOP; /* stop */ } } else { /* LGP-30 */ if (out_done) /* P complete? */ out_done = 0; else if (((ea & 0x800) && bp32) || /* bpt switch set? */ ((ea & 0x400) && bp16) || ((ea & 0x200) && bp8) || ((ea & 0x100) && bp4)) ; /* don't stop or stall */ else if (out_strt) /* P pending? stall */ reason = STOP_STALL; else reason = STOP_STOP; /* no, stop */ } delay = I_delay (sim_grtime (), ea, op); /* next instruction */ break; /* end switch */ }
void processSPI(void) { uint8_t b, c; b = spi_xfer(0); switch (b) { case 0x80: // set brightness c = spi_xfer(0); set_brightness(c); eeprom_write_byte(&b_brightness, c); break; case 0x82: // clear clear_screen(); counter = 0; dots = 0; break; case 0x83: // set scroll mode c = spi_xfer(0); if (c == 0) scroll_mode = ROTATE; else scroll_mode = SCROLL; break; case 0x84: // receive segment data c = spi_xfer(0); /* if (scroll_mode == ROTATE) { set_segments_at(c, counter++); if (counter >= 4) counter = 0; } else { shift_in_segments(c); } */ break; case 0x85: // set dots (the four bits of the second byte controls dots individually) dots = spi_xfer(0); break; case 0x88: // display integer { uint8_t i1 = spi_xfer(0); uint8_t i2 = spi_xfer(0); uint16_t i = (i2 << 8) + i1; set_number(i); } break; case 0x89: // set position (only valid for ROTATE mode) counter = spi_xfer(0); break; case 0x8a: // get firmware revision spi_xfer(3); break; case 0x8b: // get number of digits spi_xfer(8); break; default: if (b >= 0x80) break; // anything above 0x80 is considered a reserved command and is ignored if (scroll_mode == ROTATE) { set_char_at(b, counter++); if (counter >= 8) counter = 0; } else { shift_in(b); } break; } }