亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? spi.c

?? 基于cc1010的設計實例
?? C
字號:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                         SPI                          *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * The program demonstrates the use of the SPI macros and functions:         *
 * - SPI_CONTROL()                                                           *
 * - MOSI_OE()                                                               *
 * - SPI_DATA                                                                *
 * - SPI_IS_ACTIVE                                                           *
 * - halSpiTransferBlock()                                                   *
 *                                                                           *
 * The example program requires the X25020 SPI Serial EEPROM wired to a flat *
 * test cable inserted on the CC1010 evaluation board. However, the program  *
 * is written primarily as an example on how to use the SPI functions and    *
 * the methods are easily extended to other external SPI slave chips.        *
 *                                                                           *
 * The X25020 has the following pins and connections besides VDD and GND:    *
 * CS_N -> P0.3                HOLD_N -> VDD                                 *
 * SO   -> P0.2                SCK    -> P0.0                                *
 * WP_N -> VDD                 SI     -> P0.1                                *
 *****************************************************************************
 * Author:              ARR                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/08/20      First public release                                 *
 *                                                                           *
 * $Log: spi.c,v $
 * Revision 1.2  2002/11/19 15:41:40  kht
 * Added startup macros
 *
 * Revision 1.1  2002/10/14 11:11:31  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/hal.h>
#include <chipcon/cc1010eb.h>
#include <stdlib.h>
#include <string.h>

// Define X25020 instructions
#define INST_WREN 0x06
#define INST_WRDI 0x04
#define INST_RDSR 0x05
#define INST_WRSR 0x01
#define INST_READ 0x03
#define INST_WRIT 0x02

// Define the addresses
#define RAMBUF1_ADDRESS     0x0000
#define RAMBUF2_ADDRESS     0x0100
#define BUF_SIZE            256

// EEPROM access function prototypes
void eepromReadByte(byte address); // one byte only
void eepromReadBytes(byte address, byte xdata *buffer, word length);
void eepromWriteByte(byte address, byte databyte); // one byte only
bool eepromWriteBytes(byte address, byte xdata *buffer, word length); // 1-256 bytes
void eepromReadStatus(void);
void eepromWriteStatus(byte databyte);
void eepromWriteEnable(void);
void eepromWriteDisable(void);
void eepromWait(void);
void eepromWriteWait(void);

// Set up properly aligned RAM buffers
byte xdata ramBuf1[BUF_SIZE] _at_ RAMBUF1_ADDRESS;
byte xdata ramBuf2[BUF_SIZE] _at_ RAMBUF2_ADDRESS;

// Array which will be initialized with random bytes
byte xdata rndData[BUF_SIZE];



int main() {

    int i;

    // Turn off watchdog timer
    WDT_ENABLE(FALSE);

    // Set optimum settings for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);
    
    // All I/Os are inputs after reset. Make I/Os connected to LEDs outputs
    RLED=YLED=GLED=BLED=LED_OFF;
    RLED_OE(TRUE); YLED_OE(TRUE); GLED_OE(TRUE); BLED_OE(TRUE);

    // Set up P0.3 as output to CS_N
    PORTDIRBIT(0,3,POUT);

    // Fill rndData with random contents.
    srand(0xF012);
    for (i=0; i<BUF_SIZE; i++)
        rndData[i]=rand()%0xFF;
    memcpy(ramBuf1, rndData, BUF_SIZE);

    // Set up SPI for transfer. MSB first, sample on posedge, 
    // output on negedge, SCK = CLK/16, three-wire protocol
    SPI_CONTROL(SPI_SPM_DISABLED | SPI_ENABLE | SPI_MSB_FIRST | 
        SPI_POSEDGE_IDLE0 | SPI_CLK_DIV_64);
    MOSI_OE(TRUE);

    // Set write enable latch
    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_WREN;
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    for (i=0;i<8;i++);

    // Read status register
    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_RDSR;
    while (SPI_IS_ACTIVE);
    SPI_DATA = 0x00; // clock in answer
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    for (i=0;i<8;i++);

    // Write and read back 2 bytes
    eepromWriteByte(0x00, 0xA5);
    eepromWriteByte(0xE1, 0x73);
    eepromReadByte(0x00);
    if (SPI_DATA != 0xA5)
        YLED = LED_ON;
    eepromReadByte(0xE1);
    if (SPI_DATA != 0x73)
        GLED = LED_ON;

    // Write and read a buffer in EEPROM, wraparound address
    eepromWriteBytes(0xFB, ramBuf1, BUF_SIZE);
    eepromReadBytes(0xFB, ramBuf2, BUF_SIZE);
    if (memcmp(ramBuf1, ramBuf2, BUF_SIZE))
        RLED = LED_ON;
    else
        GLED = LED_ON;

    // Write and read another buffer, different address size
    eepromWriteBytes(0x49, ramBuf1, 27);
    eepromReadBytes(0x49, ramBuf2, 27);
    if (memcmp(ramBuf1, ramBuf2, 27))
        RLED = LED_ON;
    else
        GLED = LED_ON;

    // Write protect 1/4 and read back status register
    eepromWriteStatus(0x04);
    eepromReadStatus();

    // Attempt to write protected part
    eepromWriteByte(0xC0, 0xAA);
    eepromReadByte(0xC0);
    if (SPI_DATA != 0xAA)
        RLED = LED_ON;
    else
        YLED = LED_ON;

    // Attempt to write unprotected part
    eepromWriteByte(0xBF, 0xCC);
    eepromReadByte(0xBF);
    if (SPI_DATA != 0xCC)
        RLED = LED_ON;
    else
        YLED = LED_ON;

    // Switch off LEDs
    RLED = LED_OFF;
    YLED = LED_OFF;
    GLED = LED_OFF;

    // Disable protection, write contiguous buffer and check
    eepromWriteStatus(0x00);
    eepromWriteBytes(0xBE, ramBuf1, 4);
    eepromReadBytes(0xBE, ramBuf2, 4);
    if (memcmp(ramBuf1, ramBuf2, BUF_SIZE))
        RLED = LED_ON;
    else
        GLED = LED_ON;

    // Check disabling of write
    eepromWriteEnable();
    eepromReadStatus();
    if (SPI_DATA != 0x02)
      RLED = LED_ON;
    else
      YLED = LED_ON;
    eepromWriteDisable();
    eepromReadStatus();

    if (SPI_DATA)
      RLED = LED_ON;
    else
      BLED = LED_ON;

    // Disable SPI
    SPI_CONTROL(SPI_DISABLE);

    // Infinite loop
    while(1);
}

// EEPROM access functions

// Function reads a byte at the specified address location
// SPDR will contain the read byte
void eepromReadByte(byte address) {

    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_READ; // write instruction
    while (SPI_IS_ACTIVE);
    SPI_DATA = address; // write address
    while (SPI_IS_ACTIVE);
    SPI_DATA = 0x00; // clock in answer
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
}

// Function reads _length_ bytes from the specified address 
// location and puts them in _buffer_
void eepromReadBytes(byte address, byte xdata *buffer, word length) {

    word i;

    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_READ; // write instruction
    while (SPI_IS_ACTIVE);
    SPI_DATA = address; // write address
    while (SPI_IS_ACTIVE);
    for (i=0; i<length; i++) {
        SPI_DATA = 0x00; // clock in answer
        while (SPI_IS_ACTIVE);
        *buffer = SPI_DATA;
        buffer++;
    }
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
}

// Function writes specified byte to the specified address location
void eepromWriteByte(byte address, byte databyte) {

    eepromWriteEnable(); // Must enable write latch first
    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_WRIT; // write instruction
    while (SPI_IS_ACTIVE);
    SPI_DATA = address; // write address
    while (SPI_IS_ACTIVE);
    SPI_DATA = databyte; // write data
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
    eepromWriteWait();
}

// Function writes _length_ bytes to the specified address location
bool eepromWriteBytes(byte address, byte xdata *buffer, word length) {

    word bytes_to_write;
    byte page_index_address = address;

    if (!length)
      return FALSE;

    // The following algorithm divides the data buffer into 4-byte
    // page entities. If the supplied address is not on a page 
    // boundary, 1-3 bytes are written first, then full pages are
    // written until the end of the buffer.
    while (length) {
      eepromWriteEnable(); // Must enable write latch first

      // Pre-processing
      PORTBIT(0,3) = 0; // Set CS_N low
      SPI_DATA = INST_WRIT; // write instruction
      while (SPI_IS_ACTIVE);
      SPI_DATA = page_index_address; // write address
      while (SPI_IS_ACTIVE);

      // Decide number of bytes in page
      switch (page_index_address & 0x03) {
          case 0x00: bytes_to_write = 4; break;
          case 0x01: bytes_to_write = 3; break;
          case 0x02: bytes_to_write = 2; break;
          case 0x03: bytes_to_write = 1; break;
          default: break;
      }

      // Write data bytes, 1-4
      if (length<bytes_to_write) {// true only for last page
        halSpiTransferBlock(buffer, length, FALSE);
        length = 0;
      } 
      else {
        halSpiTransferBlock(buffer, bytes_to_write, FALSE);
        buffer+=bytes_to_write;
        length-=bytes_to_write;
      }

      // Address adjustment
      page_index_address += bytes_to_write;

      // Post-processing
      PORTBIT(0,3) = 1; // Set CS_N high
      eepromWait();
      eepromWriteWait();
    }
    return TRUE;
}

// Function reads the EEPROM status register
// SPDR will contain the status byte
void eepromReadStatus(void) {

    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_RDSR; // write instruction
    while (SPI_IS_ACTIVE);
    SPI_DATA = 0x00; // clock in answer
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
}

// Function writes the EEPROM status register
void eepromWriteStatus(byte databyte) {

    eepromWriteEnable(); // Must enable write latch first
    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_WRSR; // write instruction
    while (SPI_IS_ACTIVE);
    SPI_DATA = databyte; // write status byte
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
    eepromWriteWait();
}

// Function sets the write enable latch
void eepromWriteEnable(void) {

    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_WREN; // write instruction
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
}

// Function resets the write enable latch
void eepromWriteDisable(void) {

    PORTBIT(0,3) = 0; // Set CS_N low
    SPI_DATA = INST_WRDI; // write instruction
    while (SPI_IS_ACTIVE);
    PORTBIT(0,3) = 1; // Set CS_N high
    eepromWait();
}

// Function counts 8 clock cycles and waits for write to finish
void eepromWait(void) {

    int i;

    for (i=0; i<8; i++);
}

// Function waits for write to finish
void eepromWriteWait(void) {

    // Wait until finished with write
    while (1) {
        eepromReadStatus();
        if (!(SPI_DATA&0x01))   // Write in progress ?
            break;
    }
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产一区999| 欧美午夜电影在线播放| 久久精品噜噜噜成人av农村| 亚洲精品精品亚洲| √…a在线天堂一区| 久久久久国产精品麻豆| 日韩精品一区二区三区蜜臀| 在线免费观看日本欧美| 91首页免费视频| 99久久精品国产观看| 国产成人av一区| 国产电影精品久久禁18| 国产在线播放一区| 国产裸体歌舞团一区二区| 久久国产精品72免费观看| 五月婷婷激情综合| 亚洲综合色自拍一区| 亚洲色欲色欲www在线观看| 中文字幕日韩一区| 亚洲视频一区二区在线| 日韩毛片视频在线看| 国产精品久久网站| 国产精品国产自产拍在线| 国产精品美女久久久久久久网站| 日韩一区二区视频在线观看| 欧美一区二区三区系列电影| 3d成人h动漫网站入口| 欧美一级午夜免费电影| 欧美一区二区在线不卡| 日韩精品一区二区三区在线播放| 欧美不卡一区二区| 制服丝袜在线91| 日韩欧美资源站| 2020日本不卡一区二区视频| 久久久久国色av免费看影院| 国产精品福利一区| 亚洲激情图片小说视频| 午夜视频一区二区三区| 麻豆精品精品国产自在97香蕉| 蜜乳av一区二区三区| 国模一区二区三区白浆| 丰满放荡岳乱妇91ww| 一本久道中文字幕精品亚洲嫩| 91久久线看在观草草青青| 91久久精品一区二区三| 91精品国产欧美一区二区成人| 欧美精品一区二区在线播放 | 国产精品乱人伦中文| 亚洲视频你懂的| 亚洲午夜久久久久久久久电影网| 日本欧美大码aⅴ在线播放| 国内精品在线播放| 91色在线porny| 欧美日韩在线直播| 国产午夜精品理论片a级大结局 | 婷婷久久综合九色综合伊人色| 久久精品免费观看| av综合在线播放| 欧美二区三区91| 亚洲成人av福利| 成人午夜在线播放| 欧美做爰猛烈大尺度电影无法无天| 欧美日韩免费观看一区三区| 久久综合色婷婷| 亚洲欧美电影一区二区| 老汉av免费一区二区三区| 99久久精品久久久久久清纯| 精品三级av在线| 一区二区三区 在线观看视频| 国产美女在线观看一区| 欧美日韩一区不卡| 精品福利在线导航| 亚洲一区二区三区中文字幕在线| 精东粉嫩av免费一区二区三区| 欧美在线观看视频在线| 久久精品夜色噜噜亚洲aⅴ| 日韩中文欧美在线| 91热门视频在线观看| 日本一区二区综合亚洲| 男人的天堂亚洲一区| 成人免费毛片a| 久久久久久影视| 日本三级韩国三级欧美三级| 色噜噜狠狠色综合欧洲selulu| 久久久夜色精品亚洲| 蜜臀av性久久久久蜜臀aⅴ四虎| 91在线看国产| 1024成人网| 丁香网亚洲国际| 91精品国产免费| 日韩精品亚洲专区| 91精品1区2区| 有坂深雪av一区二区精品| 国产精品中文欧美| 2020国产精品自拍| 美女一区二区视频| 日韩三级免费观看| 免费的国产精品| 国产精品国产三级国产| 国产精品一区在线观看你懂的| 欧美午夜精品久久久| 一区二区高清免费观看影视大全| 粉嫩久久99精品久久久久久夜| 国产亚洲制服色| 国产尤物一区二区在线| 精品国产第一区二区三区观看体验| 午夜精品福利久久久| 欧美日韩一区二区三区四区五区| 亚洲欧美另类小说| 91浏览器打开| 亚洲免费观看高清完整版在线观看| 99久久久国产精品免费蜜臀| 国产午夜精品一区二区三区视频| 捆绑调教美女网站视频一区| 欧美成人vps| 美女视频免费一区| 国产三级精品三级在线专区| 国产精品综合在线视频| 国产精品成人免费在线| 成人h版在线观看| 亚洲美女在线一区| 在线观看亚洲a| 亚洲香肠在线观看| 欧美日韩一区二区三区在线看| 一区二区成人在线观看| 欧美三级一区二区| 亚洲国产成人av| 欧美一区二区私人影院日本| 精品一区二区在线免费观看| 国产香蕉久久精品综合网| 国产99久久久国产精品| 亚洲三级小视频| 欧美午夜精品电影| 美女视频黄免费的久久| 精品久久久网站| 高清国产一区二区| 国产精品久久久久婷婷| 色哟哟精品一区| 国产精品毛片久久久久久| 国产在线播精品第三| 久久久不卡影院| 99视频精品全部免费在线| 国产成人精品aa毛片| 国产精品久久看| 欧美性一区二区| 免费欧美在线视频| 国产视频一区二区在线| 色综合久久久久久久久久久| 亚洲靠逼com| 3d动漫精品啪啪1区2区免费| 极品少妇一区二区| 1区2区3区欧美| 欧美日韩中文字幕一区二区| 偷拍日韩校园综合在线| 精品国产免费视频| 欧美综合色免费| 久久精品国产一区二区三区免费看| 欧美不卡一区二区三区四区| 91官网在线免费观看| 蜜臀久久99精品久久久久久9| 国产精品二区一区二区aⅴ污介绍| 欧美系列亚洲系列| 不卡的av网站| 日本中文字幕不卡| 一区二区三区在线视频播放| 精品国产3级a| 欧美日韩国产大片| 成人激情av网| 精品一区二区日韩| 依依成人综合视频| 国产精品国产自产拍高清av王其| 欧美电影一区二区三区| 91麻豆免费看片| 国产制服丝袜一区| 日韩av在线播放中文字幕| 综合久久一区二区三区| 欧美mv日韩mv国产| 欧美午夜不卡在线观看免费| 五月综合激情网| 久久久www成人免费毛片麻豆| 在线免费av一区| 丁香天五香天堂综合| 三级在线观看一区二区| 成人欧美一区二区三区| 亚洲精品一区二区三区99| 欧美三级视频在线| 久久久精品综合| 色久综合一二码| 国产在线不卡一区| 一区二区三区精品| 国产精品丝袜一区| 欧美色爱综合网| 在线影视一区二区三区| 成人一区在线观看| 国产最新精品精品你懂的| 亚洲伦理在线精品| 国产精品视频第一区| 久久综合色综合88| 欧美三级视频在线观看| 在线观看日韩电影|