赞
踩
#include "Driver_SPI.h" void Driver_SPI_Init(void) { /* 1. 开启GPIO时钟 PA和PC*/ RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPAEN); /* 2. 设置引脚的工作模式 */ /* 2.1 cs: 推挽输出 PC13* CNF=00 MODE=11 */ GPIOC->CRH &= ~GPIO_CRH_CNF13; GPIOC->CRH |= GPIO_CRH_MODE13; /* 2.2 sck: 推挽输出 PA5*/ /* 2.3 mosi: 推挽输出 PA7*/ GPIOA->CRL &= ~(GPIO_CRL_CNF5 | GPIO_CRL_CNF7); GPIOA->CRL |= (GPIO_CRL_MODE5 | GPIO_CRL_MODE7); /* 2.4 miso: 浮空输入 PA6 CNF=01 MODE=00*/ GPIOA->CRL &= ~(GPIO_CRL_CNF6_1 | GPIO_CRL_MODE6); GPIOA->CRL |= GPIO_CRL_CNF6_0; /* 3. spi的模式0 sck空闲状态是 0 */ SCK_LOW; /* 4. 片选默认不选中 */ CS_HIGH; /* 5. 延时 */ SPI_DELAY; } void Driver_SPI_Start(void) { CS_LOW; // SPI_DELAY; } void Driver_SPI_Stop(void) { CS_HIGH; // SPI_DELAY; } uint8_t Driver_SPI_SwapByte(uint8_t byte) { uint8_t rByte = 0x00; for (uint8_t i = 0; i < 8; i++) { //(byte & 0x80) ? MOSI_HIGH : MOSI_LOW; /* 1. 先把数据放入到MOSI上 */ if (byte & 0x80) { MOSI_HIGH; } else { MOSI_LOW; } byte <<= 1; // SPI_DELAY; /* 2. 拉高时钟 (第一个跳变)*/ SCK_HIGH; // SPI_DELAY; /* 3. 读取miso (第一个跳变采样)*/ rByte <<= 1; // MISO_READ ? rByte|= 0x01 : rByte; if (MISO_READ) { rByte |= 0x01; } /* 4. 拉低时钟 */ SCK_LOW; // SPI_DELAY; } return rByte; }
#ifndef __DRIVER_SPI_H #define __DRIVER_SPI_H #include "stm32f10x.h" #include "Delay.h" #define CS_HIGH (GPIOC->ODR |= GPIO_ODR_ODR13) #define CS_LOW (GPIOC->ODR &= ~GPIO_ODR_ODR13) #define SCK_HIGH (GPIOA->ODR |= GPIO_ODR_ODR5) #define SCK_LOW (GPIOA->ODR &= ~GPIO_ODR_ODR5) #define MOSI_HIGH (GPIOA->ODR |= GPIO_ODR_ODR7) #define MOSI_LOW (GPIOA->ODR &= ~GPIO_ODR_ODR7) #define MISO_READ (GPIOA->IDR & GPIO_IDR_IDR6) #define SPI_DELAY Delay_us(5) void Driver_SPI_Init(void); void Driver_SPI_Start(void); void Driver_SPI_Stop(void); uint8_t Driver_SPI_SwapByte(uint8_t byte); #endif
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。