硬件之路-串行LCD12864之MSP430简单实现


串行LCD12864引脚接法:

180144_s4QJ_1413857.jpg

实现代码(MSP430):

/*
 * 12864.c
 *  Created on: 2014-7-15
 *      Author: bazingagain
 */
#include <msp430.h>
#include "12864.h"

/**
 * 函数名:delay
 * 功能:延时函数,延时时间为100us * x
 * 参数:x
 * 返回值:无
 */
void delay(uint x)
{
	uint i,j;
	for(j=0;j<x;j++)
		for(i=0;i<10;i++);
}
/**
 * 函数名:sendByte
 * 功能:向LCD12864串行发送1BYET数据
 * 参数:command
 * 返回值:无
 */
void sendByte(uchar command)
{
	uchar i;
	for(i=0;i<8;i++)
	{
		if((command<<i) & 0x80)
		{
			P2OUT |= BIT1;  //sid=1;
		}
		else
		{
			P2OUT &= (~BIT1); //sid=0;
		}
		P2OUT &= (~BIT2);  //SCLK=0
		P2OUT |= BIT2;  //SCLK=1

	}
}
/**
 * 函数名:writeCom
 * 功能:向LCD1264写指令函数
 * 参数:command
 * 返回值:无
 */
void writeCom(unsigned char command)
{
//	P2OUT |= BIT0;  //CS =P20  CS=1
	sendByte(0xf8);  //传送指令
	sendByte(command & 0xf0);
	sendByte((command << 4) & 0xf0);
	delay(2);
}
/**
 * 函数名:writeData
 * 功能:向LCD1286写数据函数
 * 参数:data
 * 返回值:无
 */
void writeData(unsigned char data)
{
//	P2OUT |= BIT0;  //CS=1;
	sendByte(0xfa);
	sendByte(data & 0xf0);
	sendByte((data << 4) & 0xf0);
	delay(2);
}
/**
 * 函数名:lcd_init
 * 功能:初始化LCD12864函数
 * 参数:无
 * 返回值:无
 */
void lcd_init(void)
{
	P2DIR |= BIT1 + BIT2; //SID , SCLK
	/*delay(20000);
	writeCom(0x30);  //设置8位数据接口,基本指令模式
	delay(50);
	writeCom(0x0c);  //整体显示开,游标关,反白关
	delay(50);*/
	writeCom(0x30);  //设置8位数据接口,基本指令模式
	writeCom(0x20);  //清DDRAM
	writeCom(0x06);  //游标及显示右移一位
	writeCom(0x0c);  //整体显示开,游标关,反白关
	writeCom(0x01);  //写入空格清屏
	writeCom(0x80);  //设置首次显示位置

}
/**
 * 函数名:displayOn12864
 * 功能:LCD12864显示字符函数
 * 参数:*s, addr
 * 返回值:无
 */
void displayOn12864(char *s, unsigned char addr)
{
	writeCom(addr);
	while(*s>0)
	{
		writeData(*s);
		s++;
		delay(50);
	}
}

51 mcu 写法:

#include <reg51.h>

/*
 * 12864.c
 *  Created on: 2015-3-31
 *      Author: bazingagain
 */
#include "12864.h"

sbit RW = P1^0;
sbit EN = P1^1;
 
/**
 * 函数名:delay
 * 功能:延时函数,延时时间为100us * x
 * 参数:x
 * 返回值:无
 */
void delay(uint x)
{
    uint i,j;
    for(j=0;j<x;j++)
        for(i=0;i<10;i++);
}
/**
 * 函数名:sendByte
 * 功能:向LCD12864串行发送1BYET数据
 * 参数:command
 * 返回值:无
 */
void sendByte(uchar command)
{
    uchar i;
    for(i=0;i<8;i++)
    {
        if((command<<i) & 0x80)
        {
            RW = 1;  //sid=1;
        }
        else
        {
            RW = 0; //sid=0;
        }
        EN = 0;  //SCLK=0
        EN = 1;  //SCLK=1
 
    }
}
/**
 * 函数名:writeCom
 * 功能:向LCD1264写指令函数
 * 参数:command
 * 返回值:无
 */
void writeCom(unsigned char command)
{
	RW = 1;
    sendByte(0xf8);  //传送指令
    sendByte(command & 0xf0);
    sendByte((command << 4) & 0xf0);
    delay(2);
}
/**
 * 函数名:writeData
 * 功能:向LCD1286写数据函数
 * 参数:data
 * 返回值:无
 */
void writeData(unsigned char ddata)
{
    RW = 1;
    sendByte(0xfa);
    sendByte(ddata & 0xf0);
    sendByte((ddata << 4) & 0xf0);
    delay(2);
}
/**
 * 函数名:lcd_init
 * 功能:初始化LCD12864函数
 * 参数:无
 * 返回值:无
 */
void lcd_init(void)
{
    
    /*delay(20000);
    writeCom(0x30);  //设置8位数据接口,基本指令模式
    delay(50);
    writeCom(0x0c);  //整体显示开,游标关,反白关
    delay(50);*/
    writeCom(0x30);  //设置8位数据接口,基本指令模式
    writeCom(0x20);  //清DDRAM
    writeCom(0x06);  //游标及显示右移一位
    writeCom(0x0c);  //整体显示开,游标关,反白关
    writeCom(0x01);  //写入空格清屏
    writeCom(0x80);  //设置首次显示位置
 
}
/**
 * 函数名:displayOn12864
 * 功能:LCD12864显示字符函数
 * 参数:*s, addr
 * 返回值:无
 */
void displayOn12864(char *s, unsigned char addr)
{
    writeCom(addr);
    while(*s>0)
    {
        writeData(*s);
        s++;
        delay(50);
    }
}


转载于:https://my.oschina.net/xlazhh/blog/300854

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值