#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
}
Previous article:LPC1788's internal EEPROM uses
Next article:External interrupt and GPIO interrupt of LPC1788
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- USB Type-C® and USB Power Delivery: Designed for extended power range and battery-powered systems
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- RAQ #223: How to measure and determine soft-start timing without a soft-start equation?
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- GigaDevice's full range of automotive-grade SPI NOR Flash GD25/55 wins ISO 26262 ASIL D functional safety certification
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Application of AVRX--AVR RTOS system
- ZigBee transmission based on self-powered switch
- Data searchRSL10-SENSE-DB-GEVK
- Car body dent repairer
- KiCad-FPGA programmable logic device chip XC7Z030-2FBG676I motherboard 12-layer PCB file
- Question about 74HC14 input pin
- MSP430FR2311 LaunchPad Development Kit
- BlueNRG-2 Bluetooth low energy wireless single chip related information
- PCB advantages
- [High-speed barcode scanner based on Maix Bit] Material unboxing-Sipeed Maix Bit Kit RISC-V AI+ loT