打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CCS5.4+Proteus8的F28027实践课十一、串行12864

周末出去玩了两天,刚到家,昨天有个同学咨询串行12864的东西,真不好意思,现在才以博客的形式来解答。
所谓12864的串行控制,只用到了三根线:CS、SID、SCLK,其中CS信号用来选择控制数据和指令的输入,SID就是数据线,SCLK使能信号线。


了解了几个引脚,我们再来看下时序图:

从时序图可以看出来,每一次操作都要写入三个字节:控制字节、高四位、低四位。其中写指令的时候是0xf8+cmd&0xf0+(cmd<<4)&0xf0,写数据是0xfa+data&0xf0+(data<<4)&0xf0。另外还有一点就是,数据是下降沿写入,昨天那位同学应该是这里出错了。
好了,需要了解的理论知识就这么多,我们现在开始写程序。
先写F2802x_LCD12864.h文件

// auther: wangdingfa
// Checkin $Date: July 31, 2016   22:45:31 $
//###########################################################################
//
// FILE:    F2802x_LCD12864.h
//
// TITLE:    LCD12864 Initialization & Support Functions.
//

#ifndef F2802x_LCD12864_H
#define F2802x_LCD12864_H

#define CS GpioDataRegs.GPADAT.bit.GPIO16
#define SID GpioDataRegs.GPADAT.bit.GPIO17
#define SCLK GpioDataRegs.GPADAT.bit.GPIO18

void SendByte_LCD12864(unsigned char byte);
void WRITEDATA_LCD12864(unsigned char data);
void WRITECMD_LCD12864(unsigned char cmd);
void InitLCD12864(void);

#endif  // end of F2802x_LCD12864_H definition

//===========================================================================
// End of file.
//===========================================================================

再写F2802x_LCD12864.c文件

// auther: wangdingfa
// Checkin $Date: July 31, 2016   22:45:31 $
//###########################################################################
//
// FILE:    F2802x_LCD12864.c
//
// TITLE:    LCD12864 Initialization & Support Functions.
//

#include "F2802x_Device.h"     // Headerfile Include File
#include "F2802x_Examples.h"   // Examples Include File

void SendByte_LCD12864(unsigned char byte)
{
    char i;
    CS=1;
    for(i = 0;i < 8;i ++)
    {
        if((byte<<i) & 0x80)
        {
            SID = 1;
        }
        else
        {
            SID = 0;
        }
        DELAY_US(1);
        SCLK = 1;
        DELAY_US(1);
        SCLK = 0;
        DELAY_US(1);
    }
    DELAY_US(10);
}

//---------------------------------------------------------------------------
// WRITEDATA_LCD12864:
//---------------------------------------------------------------------------
// This function writes data to LCD12864
void WRITEDATA_LCD12864(unsigned char data)
{
    CS = 1; //打开片选
    DELAY_US(10);
    SendByte_LCD12864(0xfa);//第一字节
    DELAY_US(10);
    SendByte_LCD12864(data & 0xf0);  //第二字节
    DELAY_US(10);
    SendByte_LCD12864((data << 4) & 0xf0);//第三字节
    DELAY_US(10);
    CS = 0;
    DELAY_US(100);
}

//---------------------------------------------------------------------------
// WRITECMD_LCD12864:
//---------------------------------------------------------------------------
// This function writes cmd to LCD12864
void WRITECMD_LCD12864(unsigned char cmd)
{
    CS = 1;//打开片选,高电平有效
    DELAY_US(10);
    SendByte_LCD12864(0xf8); //第一字节
    DELAY_US(10);
    SendByte_LCD12864(cmd & 0xf0);     //第二字节
    DELAY_US(10);
    SendByte_LCD12864((cmd << 4) & 0xf0);//第三字节
    DELAY_US(10);
    CS = 0;
    DELAY_US(100);
}


//---------------------------------------------------------------------------
// InitLCD12864:
//---------------------------------------------------------------------------
// This function initializes the LCD12864 to a known (default) state.
// such as FUNCTION SET,DSIPLAY SET,CLEAR SCREEN

void InitLCD12864(void)
{
    DELAY_US(10000);
    WRITECMD_LCD12864(0x30);
    DELAY_US(1000);
    WRITECMD_LCD12864(0x30);
    DELAY_US(100);
    WRITECMD_LCD12864(0x0c);
    DELAY_US(1000);
    WRITECMD_LCD12864(0x01);
    DELAY_US(10000);
    WRITECMD_LCD12864(0x06);
    DELAY_US(10000);
}

//===========================================================================
// End of file.
//===========================================================================

最后还是主函数,这个主函数跟上次并行的一样,都是测试语句而已

#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

void main(void)
{

// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
   InitSysCtrl();

// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
   InitGpio();

// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
   DINT;

// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
   InitPieCtrl();

// Disable CPU interrupts and clear all CPU interrupt flags:
   IER = 0x0000;
   IFR = 0x0000;

// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example.  This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
   InitPieVectTable();


// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example

// Step 5. User specific code:

   InitLCD12864();

   WRITECMD_LCD12864(0x80);
   WRITEDATA_LCD12864('a');
   WRITEDATA_LCD12864('b');
   WRITEDATA_LCD12864('c');
   WRITECMD_LCD12864(0x90);
   WRITEDATA_LCD12864('1');
   WRITEDATA_LCD12864('2');
   WRITEDATA_LCD12864('3');
   WRITECMD_LCD12864(0x88);
   WRITEDATA_LCD12864('x');
   WRITEDATA_LCD12864('y');
   WRITEDATA_LCD12864('z');
   WRITECMD_LCD12864(0x98);
   WRITEDATA_LCD12864('7');
   WRITEDATA_LCD12864('8');
   WRITEDATA_LCD12864('9');

   while(1)
   {
//     GpioDataRegs.GPATOGGLE.all=0x000000ff;
//       DELAY_US(1000);
   }
}

下载测试,结果正确,那说明昨天那位同学出错的地方就是数据写入跳变沿选择错了,应该是下降沿有效,而不是上升沿。


洗洗睡了,昨晚喝酒喝太多了,现在头还有点晕晕的,老了,恢复的比较慢。
F28027菜鸟交流qq群107691092

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
12864设置CGRAM显示自定义图形(或文字)
基于PCF8563时钟芯片的万年历制作
520了,用32做个简单的小程序
使用74LS164和74LS165实现简单I/O扩展
74hc164驱动共阴数码管与共阳数码管
基于51单片机的SPI总线
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服