我自己/***************************************************************/
/* 读数据程序没能调出,应该是5v 与3v之间的传输问题。如果接收可用*
* 则可用查忙函数代替延时 *
* *
* *
* *
* *
***************************************************************/
#include <msp430g2553.h>
#define uchar unsigned char
#define uint unsigned int
#define SID BIT1
#define SCK_1 P1OUT|=BIT4
uchar Table[] = "2014111111111111111";
void write_SID(uchar byte);
void write_data(uchar data);
void write_com(uchar com);
uchar read_SID(void);
uchar read_data(void);
uchar read_com(void);
uchar read_busy(void);
void check_busy(void);
void delay_us(uint a);
void delay_ms(uint a);
void init(void);
void display(uchar address,uchar *p,uint num);
uchar lcd;
/*********主函数*************/
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
while(1);
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
P1DIR |= BIT4 + BIT3; //1.3片选 1.4时钟
P2DIR |= BIT1; //2.1数据传输
P1OUT = 0xff;
P2OUT = 0xff;
init();
display(0x80,Table,16);
display(0x90,Table+16,16);
display(0x88,Table+32,16);
display(0x98,Table+48,16);
LPM4;
}
/*******us延时*************/
void delay_us(uint a)
{
uint i;
for(i=a;i>0;i--);
}
/*******ms延时*************/
void delay_ms(uint a)
{
uint n=1000;
for(;a>0;a--)
for(;n>0;n--);
}
/*******写一个字节*************/
void write_SID(uchar byte)
{
uchar i;
P1OUT &= ~BIT4;
P1OUT |= BIT3; //片选使能
for(i=0;i<8;i++)
{
P1OUT &= ~BIT4;
if(byte&0x80)
P2OUT |= BIT1;
else
P2OUT &= ~BIT1;
P1OUT |= BIT4;
byte <<= 1;
}
P1OUT &= ~BIT3; //片选失能
P1OUT &= ~BIT4;
}
/*******读忙*************/
uchar read_busy(void)
{
uchar BF;
write_SID(0xfc);
P2DIR &= ~BIT1;
P1OUT &= ~BIT4;
P1OUT |= BIT3;
P1OUT &= ~BIT4;
BF = P2IN & BIT1;
P1OUT |= BIT4;
P1OUT &= ~BIT4;
P1OUT &= ~BIT3;
P2DIR |= BIT1;
return(BF);
}
/*******读一字节*************/
uchar read_SID(void)
{
uchar i,data;
P2DIR &= ~BIT1;
P1OUT &= ~BIT4;
P1OUT |= BIT3;
for(i=0;i<8;i++)
{
P1OUT &= ~BIT4;
if(P2IN&BIT1)
{
data <<= 1;
data |= BIT0;
}
else
{
data <<= 1;
data &= ~BIT0;
P1OUT |= BIT4;
P1OUT &= ~BIT4;
}
}
P1OUT &= ~BIT3;
P2DIR |= BIT1;
return(data);
}
/*******读一字节数据*************/
uchar read_data(void)
{
uchar h_data,l_data,data;
write_SID(0xfe);//第一个字节
h_data = read_SID();//收前半
l_data = read_SID();//收后半
data = h_data | l_data;//合并
return(data);
}
/*******读一字节指令*************/
uchar read_com(void)
{
uchar h_com,l_com,com;
write_SID(0xfc);
h_com = read_SID();
l_com = read_SID();
com = h_com | l_com;
return(com);
}
/*******判忙*************/
void check_busy(void)
{
while (read_busy() & 0xff);
}
/*******写一字节命令*************/
void write_com(uchar com)
{
write_SID(0xf8);//写第一个字节
write_SID(com & 0xf0);//第二个字节前半
write_SID((com<<4) & 0xf0);//第二个字节后半
}
/*******写一字节数据*************/
void write_data(uchar data)
{
write_SID(0xfa);
write_SID(data & 0xf0);
write_SID((data<<4) & 0xf0);
}
/*******初始化*************/
void init(void)
{
write_com(0x30);
check_busy();
write_com(0x08);//关显示
check_busy();
write_com(0x0c);//开显示,关光标
check_busy();
write_com(0x01);//清屏
check_busy();
write_com(0x06);//第一行显示位置 位址归位
check_busy();
}
/*******写字符串*************/
void display(uchar address,uchar * p,uint num)
{
uint i;
write_com(address);//写地址
check_busy();
for(i=0;i<num;i++)
{
write_data(*(p++));//写数据
check_busy();
}
}
写的可以写进去,就是读不出来,请教各位大侠??? |