uint8_t W5100_MemReadWord(uint16_t addr, uint16_t *val) { uint8_t low, high; W5100_RequestSPIBus(); W5100_CS_ENABLE(); SPIWriteByte(W5100_CMD_READ); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ high = SPIReadByte(); /* data */ W5100_CS_DISABLE(); addr++; W5100_CS_ENABLE(); SPIWriteByte(W5100_CMD_READ); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ low = SPIReadByte(); /* data */ W5100_CS_DISABLE(); W5100_ReleaseSPIBus(); *val = (high<<8)|low; return ERR_OK; }
uint8_t W5100_MemReadByte(uint16_t addr, uint8_t *val) { W5100_GetBus(); SPIWriteByte(W5100_CMD_READ); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ *val = SPIReadByte(); /* data */ W5100_ReleaseBus(); return ERR_OK; }
uint8_t W5100_MemWriteByte(uint16_t addr, uint8_t val) { W5100_GetBus(); SPIWriteByte(W5100_CMD_WRITE); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ SPIWriteByte(val); /* data */ W5100_ReleaseBus(); return ERR_OK; }
// Write an SPI command (consisting of control byte then data byte) void SPIWriteCommand(Byte control, Byte data, Byte address) { // Pull SS low if (address) { PORTD &= ~SS2; } else { PORTD &= ~SS1; } SPIWriteByte(control); SPIWriteByte(data); // Pull SS back high if (address) { PORTD |= SS2; } else { PORTD |= SS1; } }
/** * Function to read a byte. * \param res - pointer in which to store the result * \return - 0 the operation is successful * \return - 1 the operation is failed. Check internal error for more details */ BOOL SPIReadByte (BYTE * res) { int counter=0; if(SPI2CON1bits.MSTEN==0) { #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Master op in Slave mode, quitting\n"); #endif _intSPIerr=NotMasterMode; return 1; } if(SPIWriteByte(0x00)==0) { while((SPI2STATbits.SPIRBF==0) && (counter!=255)) counter++; if(counter<255) { #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Rx buf ok, reading\n"); #endif *res = SPI2BUF; _intSPIerr=SPI_NO_ERR; return 0; } #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Rx buf empty, NOT READING!\n"); #endif _intSPIerr = RxBufEmpty; return 1; } return 1; }
///////////////////////////////////////////////////////////////////// //功 能:写RC632寄存器 //参数说明:Address[IN]:寄存器地址 // value[IN]:写入的值 ///////////////////////////////////////////////////////////////////// void WriteRawRC(u8 Address, u8 value) { u8 ucAddr; // u8 tmp; CLR_SPI_CS; ucAddr = ((Address<<1)&0x7E); SPIWriteByte(ucAddr); SPIWriteByte(value); SET_SPI_CS; // tmp=ReadRawRC(Address); // // if(value!=tmp) // printf("wrong\n"); }
void calibration() { line2(); writeLCD("DC OFFSET"); while (1) { uint24_t status = readRegister(STATUS_REG); if (status & 0x800000) { break; } } LATC0 = 1; LATC1 = 1; TRISC0 = 0; TRISC1 = 0; SPIWriteByte(0xD9); while (1) { uint24_t status = readRegister(STATUS_REG); if (status & 0x800000) { break; } } writeLCD("AC OFFSET"); SPIWriteByte(0xDD); while (1) { uint24_t status = readRegister(STATUS_REG); if (status & 0x800000) { break; } } LATC0 = 0; LATC1 = 0; line2(); writeLCD("DONE"); }
uint8_t W5100_MemWriteWord(uint16_t addr, uint16_t val) { W5100_RequestSPIBus(); W5100_CS_ENABLE(); SPIWriteByte(W5100_CMD_WRITE); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ SPIWriteByte(val>>8); /* data */ W5100_CS_DISABLE(); addr++; W5100_CS_ENABLE(); SPIWriteByte(W5100_CMD_WRITE); SPIWriteByte(addr>>8); /* high address */ SPIWriteByte(addr&0xff); /* low address */ SPIWriteByte(val&0xff); /* data */ W5100_CS_DISABLE(); W5100_ReleaseSPIBus(); return ERR_OK; }
///////////////////////////////////////////////////////////////////// //功 能:读RC632寄存器 //参数说明:Address[IN]:寄存器地址 //返 回:读出的值 ///////////////////////////////////////////////////////////////////// u8 ReadRawRC(u8 Address) { u8 ucAddr; u8 ucResult=0; CLR_SPI_CS; ucAddr = ((Address<<1)&0x7E)|0x80; SPIWriteByte(ucAddr); ucResult=SPIReadByte(); SET_SPI_CS; return ucResult; }
// transfer line buffer to display using SPI // input: line position where line buffer is rendered void SPIWriteLine(unsigned char line) { P2OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_WR | VCOM); // send command to write line(s) SPIWriteByte(line+1); // send line address UCB0CTL0 |= UCMSB; // switch SPI to MSB first for proper bitmap orientation unsigned char j = 0; while(j < (PIXELS_X/8)) // write pixels / 8 bytes { UCB0TXBUF = LineBuff[j++]; // transfer byte while (UCB0STAT & UCBUSY); // wait for transfer to complete } UCB0CTL0 &= ~UCMSB; // switch SPI back to LSB first for commands SPIWriteWord(0); // send 16 bit to latch buffers and end transfer P2OUT &= ~_SCS; // SCS low, finished talking to display }
// transfer line buffer to display using SPI // input: line position where line buffer is rendered void SPIWriteLine(unsigned char line) { P1OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_WR | VCOM); // send command to write line(s) SPIWriteByte(line+1); // send line address USICTL0 &= ~USILSB; // switch SPI to MSB first for proper bitmap orientation unsigned char j = 0; while(j < (PIXELS_X/8)) // write pixels / 8 bytes { USISRH = LineBuff[j++]; // store low byte USISRL = LineBuff[j++]; // store high byte USICNT |= USISCLREL | USI16B | USICNT4; // release SCLK when done, transfer 16 bits while(!(USICTL1 & USIIFG)); // wait for transfer to complete } USICTL0 |= USILSB; // switch SPI back to LSB first for commands SPIWriteWord(0); // send 16 bit to latch buffers and end transfer P1OUT &= ~_SCS; // SCS low, finished talking to display }
/** * Function to perform a contemporary write and read of a BYTE. * This is used when the slave device supports full-duplex. * In this case while clocking data out, data in is considered valid and saved. * \param data - byte data to be sent * \param res - pointer to store the received data * \return - 0 the operation is successful * \return - 1 the operation is failed. Check internal error for more details */ BOOL SPIWriteReadByte(BYTE data, BYTE * res) { if(SPI2CON1bits.MSTEN==0) { #ifdef SPI_DBG uartwrite(1,"[SPI_DBG] Master op in Slave mode, quitting\n"); #endif _intSPIerr=NotMasterMode; return 1; } //BOOL outcome; if(SPIWriteByte(data)==0) { //outcome = SPIReadByte(res); //return outcome; *res = SPI2BUF; return 0; } return 1; }
int main(void) { // configure WDT WDTCTL = WDTPW | WDTHOLD; // stop watch dog timer P1DIR |= _LED | _SCLK | _SDATA; // set all pins used to output P1OUT &= ~(_LED | _SCLK | _SDATA); // set all outputs low P1SEL |= _SCLK | _SDATA; // connect pins to USCI (secondary peripheral) P1SEL2 |= _SCLK | _SDATA; // connect pins to USCI (secondary peripheral) P2DIR |= _DISP | _SCS; // set all pins used to output P2OUT &= ~(_DISP | _SCS); // set all outputs low // configure UCSI B0 for SPI UCB0CTL1 |= UCSWRST; // reset USCI B0 UCB0CTL0 = UCCKPH | UCMST | UCMODE_0 | UCSYNC; // read on rising edge, inactive clk low, lsb, 8 bit, master mode, 3 pin SPI, synchronous UCB0BR0 = 8; UCB0BR1 = 0; // clock scaler = 8, i.e 2 MHz SPI clock UCB0CTL1 = UCSSEL_2; // clock source SMCLK, clear UCSWRST to enable USCI B0 UCB0CTL1 &= ~UCSWRST; // enable USCI B0 // setup timer A, to keep time and alternate VCOM at 1Hz timeMSec = 0; // initialize variables used by "clock" timeSecond = 0; timeMinute = 0; VCOM = 0; // initialize VCOM, this flag controls LCD polarity // and has to be toggled every second or so TA0CTL = TASSEL_2 + MC_1; // SMCLK (default ~1MHz), up mode TACCR0 = 1000; // trigger every millisecond TACCTL0 |= CCIE; // timer 0 interrupt enabled P2OUT |= _DISP; // turn display on // initialize display P2OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_CM | VCOM); // send clear display memory command SPIWriteByte(0); // send command trailer P2OUT &= ~_SCS; // SCS lo, finished talking to display // write some text to display to demonstrate options printSharp("HELLO,WORLD?",1,0); printSharp(" SHARP",16,DISP_WIDE); printSharp(" MEMORY ",24,DISP_INVERT); printSharp(" DISPLAY!",32,DISP_HIGH); printSharp("123456789012",56,0); while(1) { // show VCOM state on LED1 for visual control if(VCOM == 0) { P1OUT &= ~_LED; } else { P1OUT |= _LED; } // write clock to display TextBuff[0] = ' '; TextBuff[1] = timeMinute / 10 + '0'; TextBuff[2] = timeMinute % 10 + '0'; TextBuff[3] = ':'; TextBuff[4] = timeSecond / 10 + '0'; TextBuff[5] = timeSecond % 10 + '0'; TextBuff[6] = 0; printSharp(TextBuff,72,DISP_HIGH | DISP_WIDE); // put display into low-power static mode P2OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_SM | VCOM); // send static mode command SPIWriteByte(0); // send command trailer P2OUT &= ~_SCS; // SCS lo, finished talking to display // sleep for a while _BIS_SR(LPM0_bits + GIE); // enable interrupts and go to sleep }; }
int main(void) { // configure WDT WDTCTL = WDTPW | WDTHOLD; // stop watch dog timer P1DIR |= _LED | _DISP | _SCLK | _SDATA | _SCS; // set all pins used to output P1OUT &= ~(_LED | _DISP | _SCLK | _SDATA | _SCS); // set all outputs low // setup USI for SPI USICTL0 = USIPE6 | USIPE5 | USILSB // enable SDO and SCLK, LSB sent first | USIMST | USIOE; // master, enable data output USICTL1 = USICKPH; // data captured on first edge USICKCTL = USIDIV_1 | USISSEL_2; // /1 SMCLK (= default ~1MHz), clock low when inactive // setup timer A, to keep time and alternate VCOM at 1Hz timeMSec = 0; // initialize variables used by "clock" timeSecond = 0; timeMinute = 0; VCOM = 0; // initialize VCOM, this flag controls LCD polarity // and has to be toggled every second or so TA0CTL = TASSEL_2 + MC_1; // SMCLK (default ~1MHz), up mode TACCR0 = 1000; // trigger every millisecond TACCTL0 |= CCIE; // timer 0 interrupt enabled P1OUT |= _DISP; // turn display on // initialize display P1OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_CM | VCOM); // send clear display memory command SPIWriteByte(0); // send command trailer P1OUT &= ~_SCS; // SCS lo, finished talking to display // write some text to display to demonstrate options printSharp("HELLO,WORLD?",1,0); printSharp(" SHARP",16,DISP_WIDE); printSharp(" MEMORY ",24,DISP_INVERT); printSharp(" DISPLAY!",32,DISP_HIGH); printSharp("123456789012",56,0); while(1) { // show VCOM state on LED1 for visual control if(VCOM == 0) { P1OUT &= ~_LED; } else { P1OUT |= _LED; } // write clock to display TextBuff[0] = ' '; TextBuff[1] = timeMinute / 10 + '0'; TextBuff[2] = timeMinute % 10 + '0'; TextBuff[3] = ':'; TextBuff[4] = timeSecond / 10 + '0'; TextBuff[5] = timeSecond % 10 + '0'; TextBuff[6] = 0; printSharp(TextBuff,72,DISP_HIGH | DISP_WIDE); // put display into low-power static mode P1OUT |= _SCS; // SCS high, ready talking to display SPIWriteByte(MLCD_SM | VCOM); // send static mode command SPIWriteByte(0); // send command trailer P1OUT &= ~_SCS; // SCS lo, finished talking to display // sleep for a while _BIS_SR(LPM0_bits + GIE); // enable interrupts and go to sleep }; }