pic32 spi驱动pmodcls lcd液晶屏

  1. ps: pmodcls这种东西确实不如1602好用,想当时就不该申请这个,随便别的什么配件都比这个给力。考虑到之后一定会使用spi接口就拿这个lcd练手。弄了两天出了很多问题,最后还得抱爽哥的大腿~~~

    我们使用申请的pmodcls液晶显示模块。pmodcls框图如下:

    基本参数:

  • 16x2字符显示屏
  • 通过使用UART、SPI或者TWI接口来灵活连接
  • 简单的终端显示接口
  • 尺寸大小:3.75英寸x 1.75英寸
  • 套件中附带一个6英寸6针下载线和一个6针接口

工作方式的描述如下:

The module is capable of executing a variety of instructions, such as erasing specific characters,

setting different display modes, scrolling, and displaying user-defined characters. These instructions

are specified using escape sequences to send commands to the board.s embedded Atmel ATmega48

microcontroller.

感觉跟1602是一样的东西。。。

Communication with the PmodCLS is using a UART, SPI, or TWI (Phillips I2C compatible) serial connection.

1、通信方式的设置。通过设置跳线MD0-2实现各种不同模式的选择。(我居然没有分DE系列的lcd板子,结果这个地方跳线连反了,后来经爽哥摆弄才发现) 

2、电源选项

      The PmodCLS can be powered through  the boards 6-pin headers J1 or J2, or through connectors J6 or J7.

  我闷使用SPI方式驱动液晶屏。

  话说pmodcls强大啊,居然还有个atmega48

3开发板的连接:直接将SPI #1齐刷刷的接到pmodclsJ1接口即可。

4spi的工作模式:个人以为像一个16位寄存器,每一个spi周期发送完一个字节以后,立即返回一个字节。对于spi来说接受和发送是同一个函数。

When one “transmits” data, the incoming data must be read before attempting to

transmit again.  If the incoming data is not read, then the data will be lost and the

SPI module may become disabled as a result.  Always read the data after a transfer

has taken place, even if the data has no use in your application.

Data is always “exchanged” between devices.  No device can just be a “transmitter”

or just a “receiver” in SPI.  However, each device has two data lines, one for input

and one for output. 

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;

最后贴一下代码:

/*
 * 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();
}
/******************************************************************************
 *
 *
 ******************************************************************************/
main()
{
//u8 value=0;
    char *str= "good! ";                        //{'g','o','o','o','d','!',0};

//init of led and spi
    led_init();
    spi_init();
//led on
    PORTBSET = ((1 << 10)|(1<<11)  );

    PmodCLS_sendCmd("[3e ");                        // Set Backlight on
    PmodCLS_DisplyString(str);                         // sends the string
    while(1)
    {

    }
}
原文地址:https://www.cnblogs.com/yixiaoyang/p/1965369.html