LPC1788 SPI usage

Publisher:xinyi9008Latest update time:2017-01-13 Source: eefocusKeywords:LPC1788 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#ifndef __SPI_H_

#define __SPI_H_

 

#include "common.h"

#include "delay.h"

 

// cs p1.21

//sck p1.20

//miso p1.23

//mosi p1.24

 

#define SPI_CLOCK   12000000

 

void spi0_select_cs(void);

 

void spi0_disSelect_cs(void);

 

u8 spi0_rw_data(u8 writeByte);

 

void spi0_set_speed(u8 speed);

 

void spi0_init(u8 divide);

 

 

 

#endif

 

 

 

 

 

 

 

 

#include "spi.h"

 

 

void spi0_select_cs(void)

{

    P1low(21) = 1;

}

 

void spi0_disSelect_cs(void)

{

    P1high(21) = 1;

}

 

u8 spi0_rw_data(u8 writeByte)

{

    u8 retry = 200;

    u8 readByte;

    while(!(LPC_SSP0->SR&0x01)) //Wait for sending to complete

    {

        retry--;

        DelayUs(1);

        if(retry == 0)return 1;

    }

    LPC_SSP0->DR = (u16)writeByte;

    retry = 200;

    while(!(LPC_SSP0->SR&0x04))

    {

        retry--;

        DelayUs(1);

        if(retry == 0)return 1;

    }

    readByte = (u8)LPC_SSP0->DR;

   

    return readByte; 

}

 

void spi0_set_speed(u8 divide)

{

    LPC_SSP0->CR1 &= ~(1<<1); //stop spi

    LPC_SSP0->CR0 |= ((SPI_CLOCK/(divide-1))<<8);

    LPC_SSP0->CR1 |= (1<<1); //Start spi

}

 

void spi0_init(u8 divide)

{

    //Turn on the SPI clock and io port clock

    LPC_SC->PCONP |= (1<<15)|(1<<21); //Turn on the clock

    // Placement cs

    LPC_IOCON->P1_21 = 0x00; //Select gpio function, disable hysteresis, non-reverse, normal push-pull

    LPC_IOCON->P1_21 |= (2<<3); //Pull up

    P1dir(21) = 1; // output

    P1high(1) = 1; // high

   

    LPC_IOCON->P1_20 = 0x00; //Select to disable hysteresis and non-reverse normal push-pull

    LPC_IOCON->P1_20 |= (2<<3)|(5<<0);//上拉 ssp clk

   

    LPC_IOCON->P1_23 = 0x00; //Select to disable hysteresis and non-reverse normal push-pull

    LPC_IOCON->P1_23 |= (2<<3)|(5<<0); //Pull up ssp miso

   

    LPC_IOCON->P1_24 = 0x00; //Select to disable hysteresis and non-reverse normal push-pull

    LPC_IOCON->P1_24 |= (2<<3)|(5<<0); //pull up ssp mosi

   

     //spi interface initialization

    LPC_SSP0->CR0 = 0x0000;

    LPC_SSP0->CR1 = 0x00000000; //Normal operation, host mode

    LPC_SSP0->CR0 |= (7<<0)|(1<<6)|(1<<7)|((SPI_CLOCK/(divide-1))<<8);//8位数据 cpol cpoa

    LPC_SSP0->CPSR = ApbClock/SPI_CLOCK;

    LPC_SSP0->IMSC = 0x00000000; //Disable interrupts

   

    LPC_SSP0->CR1 |= (1<<1); //Start spi

}

 


Keywords:LPC1788 Reference address:LPC1788 SPI usage

Previous article:LPC1788's internal EEPROM uses
Next article:External interrupt and GPIO interrupt of LPC1788

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号