pic32 spi驱动pmodcls lcd液晶屏(2)
- UID
- 1029342
- 性别
- 男
|
pic32 spi驱动pmodcls lcd液晶屏(2)
5、除此之外,系统时钟设置需要注意。
参考武林大侠那副比较经典点的图示:
/* configure bit setting. Fcy = 72MHZ,Fpb = 9MHZ */
#pragma config POSCMOD = XT, FNOSC = PRIPLL
#pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1
#pragma config FPBDIV = DIV_8, FWDTEN = OFF, CP = OFF, BWP = OFF
即使用的时钟计算方式为:系统频率Fcy = ( ( 8Mhz / 2 ) * 18 ) / 1
=
72MHZ;外围设备总线时钟Fpb = 72 / FPBDIV
= 9 MHZ;
6、源码
/*
* xiaoyang@HIT 2011.1.15
* cool@HIT 2010.1.21 complete
*
*/
#include
#include "spi.h"
#include "types.h"
#include
/* configure bit setting. Fcy = 72MHZ,Fpb = 9MHZ */
#pragma config POSCMOD = XT, FNOSC = PRIPLL
#pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1
#pragma config FPBDIV = DIV_8, FWDTEN = OFF, CP = OFF, BWP = OFF
/* SPI Control & Baud Rate Configuration */
#define SPI_CONF 0x8060 // SPI_ON | SPI_MSTEN
#define SPI_BRG 3 // 9MHZ /8 =
/* SPI Slave Pin Config */
#define SPI_SS
0x200
// RD9 for SS port
#define ACTIVATE()
PORTDCLR=SPI_SS // tris control for CS pin
#define DEACTIVATE() PORTDSET=SPI_SS
/******************************************************************************
* led_init
*
******************************************************************************/
void led_init()
{
TRISB = 0; // LED1-4 output
AD1PCFG = 0xffff; // all PORTB as digital
PORTB = 0x0;
}
/******************************************************************************
* spi_init
*
******************************************************************************/
void spi_init()
{
int rData;
/* Setup SS Pin */
TRISDCLR = SPI_SS; //
DEACTIVATE();
/* Configurate Master */
SPI1CONCLR = SPI_ON; // Stop and Reset
rData = SPI1BUF; // Clr the receive buffer
SPI1BRG = SPI_BRG; // Set SCLK = Fp/32
SPI1CON = SPI_CONF; // Reconfigurated and Ready to Work
}
/******************************************************************************
* spi using the same interface for receive and transport
*
******************************************************************************/
u8 spi1_8TxRx(u8 ch)
{
while(SPI1STATbits.SPIBUSY); // Check
SPI1BUF = ch;
while(!SPI1STATbits.SPIRBF); //waiting for transfer complete
return SPI1BUF;
}
void PmodCLS_DisplyString(char* str)
{
u8 len=0,ch;
ACTIVATE();
spi1_8TxRx(0x1B); // ESC character
spi1_8TxRx(0x5B); // [ character
spi1_8TxRx(0x30); // zero
spi1_8TxRx(0x6A); // j- command for clear display and home cursor
while(*str) {
spi1_8TxRx(*str++);
len++;
}
DEACTIVATE();
return;
}
void PmodCLS_sendCmd(char* cmd)
{
ACTIVATE();
spi1_8TxRx(0x1B);
while(*cmd) {
spi1_8TxRx(*cmd++);
}
DEACTIVATE();
}
/******************************************************************************
*
*
******************************************************************************/ |
|
|
|
|
|