#include "spi.h"
#include "delay.h"
SPI_InitTypeDef SPI_InitStructure;
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //选择了串行时钟的稳态:时钟悬空高
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
//SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
}
//SPI 速度设置函数
//SpeedSet:
//SPI_BaudRatePrescaler_2 2分频 (SPI 36M
@sys 72M)
//SPI_BaudRatePrescaler_8 8分频 (SPI 9M@sys 72M)
//SPI_BaudRatePrescaler_16 16分频 (SPI 4.5M@sys 72M)
//SPI_BaudRatePrescaler_256 256分频 (SPI 281.25K@sys 72M)
void SPI1_SetSpeed(u8 SpeedSet)
{
SPI_InitStructure.SPI_BaudRatePrescaler = SpeedSet ;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
}
void oledcmd(u16 data)
{
dc=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,data);
dc=1;
}
void oleddata(u16 data)
{
dc=1;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,data);
dc=0;
}
void clear()
{
unsigned char yy,xx;
for(yy=0;yy<8;yy++)
{
oledcmd(0xb0+yy);
oledcmd(0x00);
oledcmd(0x10);
for(xx=0;xx<128;xx++)
oleddata(0xff);
}
}
void oledinit()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_14; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
res=0;
delay_ms(10);
res=1;
oledcmd(0xae);//--turn off oled panel
oledcmd(0x00);//---set low column address
oledcmd(0x10);//---set high column address
oledcmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
oledcmd(0x81);//--set contrast control register
oledcmd(0xff);
oledcmd(0xcf); // Set SEG Output Current Brightness
oledcmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
oledcmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
oledcmd(0xa6);//--set normal display
oledcmd(0xa8);//--set multiplex ratio(1 to 64)
oledcmd(0x3f);//--1/64 duty
oledcmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
oledcmd(0x00);//-not offset
oledcmd(0xd5);//--set display clock divide ratio/oscillator frequency
oledcmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
oledcmd(0xd9);//--set pre-charge period
oledcmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
oledcmd(0xda);//--set com pins hardware configuration
oledcmd(0x12);
oledcmd(0xdb);//--set vcomh
oledcmd(0x40);//Set VCOM Deselect Level
oledcmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
oledcmd(0x02);//
oledcmd(0x8d);//--set Charge Pump enable/disable
oledcmd(0x14);//--set(0x10) disable
oledcmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
oledcmd(0xa6);// Disable Inverse Display On (0xa6/a7)
oledcmd(0xaf);//--turn on oled panel
clear();
}
#ifndef __SPI_H
#define __SPI_H
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//Mini STM32开发板
//SPI 驱动函数
//正点原子@ALIENTEK
//技术论坛:
www.openedv.com
//修改日期:2010/6/13
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 正点原子 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
#define res PAout(8)
#define dc PAout(14)
void SPI1_Init(void); //初始化SPI口
void SPI1_SetSpeed(u8 SpeedSet); //设置SPI速度
void clear(void);
void oledinit(void);
void oledcmd(u16 data);
void oleddata(u16 data);
#endif
#include "stm32f10x.h"
#include "spi.h"
int main(void)
{
SPI1_SetSpeed(SPI_BaudRatePrescaler_256);
SPI1_Init();
oledinit();
while(1)
{
}
}
不客气
一周热门 更多>