//正弦波+方波显示程序
#include <msp430G2553.h>
#include <math.h>
#define LCD_RS_High P2OUT|=BIT2 // LCD_RS 高电平 显示数据 P62
#define LCD_RS_Low P2OUT&=~BIT2 // LCD_RS 低电平 指令数据
#define LCD_RW_High P2OUT|=BIT1 // LCD_RW 高电平 数据读到DB0_DB7 P64
#define LCD_RW_Low P2OUT&=~BIT1// LCD_RW 低电平 DB0_DB7 数据写入IR DR
#define LCD_EN_High P2OUT|=BIT0 // LCD_EN 高电平 高有效 P63
#define LCD_EN_Low P2OUT&=~BIT0 // LCD_EN 低电平 使能信号
#define LCD_PSB_High P3OUT&=0xff // 并口 高电平
#define LCD_PSB_Low P3OUT&=0xfe; // 串口 低电平 P30
//P67 悬空
#define uchar unsigned char
unsigned char pic1[]= // 图片代码
{
0x50,0x50,0xF0,0x00,0x70,0x50,0xD0,0x02,0xFF,0x42,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xA1,0xA1,0xE1,0x00,0xE1,0xA1,0xA1,0x00,0xFF,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x03,0x02,0x02,0x00,0x02,0x02,0x03,0x00,0xFF,0x21,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0x00,0x17,0x15,0x1D,0x00,0xFF,0x84,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x5C,0x54,0x74,0x00,0xFF,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFB,0x8A,0xFA,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0xFB,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xEA,0xAB,0xBA,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0xAA,0xAB,0xFA,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x3A,
0x23,0xFA,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xBA,0xAB,0xEA,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0xFA,0xAB,0xEA,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x0A,0x0B,0xFA,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xFA,0xAB,0xFA,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0xBA,0xAB,0xFA,0x02,0x02,0x02,0x02,0x02,0x02,0xFA,0x02,0xFB,0x8A,0xFA,0x02,
0x02,0x02,0x02,0x02,0x02,0xFA,0x03,0x02,0xFA,0x02,0x02,0x02,0x02,0x07,0x02,0x00
};
void delay_ms(unsigned int timer)
{
unsigned int i;
for(i=0;i<1000;i--)
for(;timer>0;timer--)
;
}
unsigned char lcd_busy()
{
unsigned char result;
P1DIR=0x00;
LCD_RS_Low; // 指令数据
LCD_RW_High;
LCD_EN_High;
result = P1IN&0x80; // 读取数据口数据
LCD_EN_Low;
return(result);
}
/*写命令到液晶*/
void LCD_WCMD(unsigned char cmd)
{
while(lcd_busy());
P1DIR=0xff; // OUT;
LCD_RS_Low; // 指令数据
LCD_RW_Low;
// LCD_EN_Low;
LCD_EN_High;
P1OUT = cmd;
LCD_EN_Low;
}
/*写数据到液晶*/
void LCD_WDAT(unsigned char dat)
{
while(lcd_busy());
P1DIR=0xff; // OUT;
LCD_RS_High; // 数据
LCD_RW_Low;
// LCD_EN_Low;
LCD_EN_High;
P1OUT = dat;
LCD_EN_Low;
}
unsigned char ReadByte(void)
{
unsigned char byReturnValue ;
while(lcd_busy());
P1DIR=0xff;
P1OUT = 0xff;
P1DIR=0x00;
LCD_RS_High; ;
LCD_RW_High; ;
LCD_EN_Low ;
LCD_EN_High ;
byReturnValue=P1IN ;
LCD_EN_Low ;
return byReturnValue ;
}
/*显示的字符串*/
void Display(unsigned char X,unsigned char Y,unsigned char *string)
{
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
LCD_WCMD(X+Y);
while(*string )
{ //显示字符
LCD_WDAT(*string++);
}
}
void Display_number(unsigned char X,unsigned char Y,unsigned char number)
{
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
LCD_WCMD(X+Y);
LCD_WDAT(48+number);
}
/*时钟初始化*/
void Clock_init()
{
unsigned char t;
BCSCTL1 = RSEL0 + RSEL1 + RSEL2; // XT2on, max RSEL
BCSCTL1&=~XT2OFF; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (t = 255; t > 0; t--); // 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0); // 判断XT2是否起振
BCSCTL2 |= SELS+SELM_2; // SMCLK = XT2
}
/*液晶初始化*/
void LCD_init()
{
LCD_WCMD(0x34); //扩充指令操作
LCD_WCMD(0x30); //基本指令操作
LCD_WCMD(0x0C); //显示开,关光标
LCD_WCMD(0x01); //清除LCD的显示内容
}
/*------------------------------------------------
清屏
------------------------------------------------*/
void ClrScreen()
{
LCD_WCMD(0x01);
delay_ms(15);
}
/*------------------------------------------------
设置到绘图模式
------------------------------------------------*/
void SetGraphicMode()
{
LCD_WCMD(0x36); //选择8bit数据流 图形模式
delay_ms(20);
}
/*------------------------------------------------
显示图片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char *img)
{
int i,j;
//显示上半屏内容设置
for(i=0;i<32;i++)
{
LCD_WCMD(0x80 + i); //SET 垂直地址 VERTICAL ADD
LCD_WCMD(0x80); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
LCD_WDAT(*img);
img++;
}
}
//显示下半屏内容设置
for(i=0;i<32;i++)
{
LCD_WCMD(0x80 + i); //SET 垂直地址 VERTICAL ADD
LCD_WCMD(0x88); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
LCD_WDAT(*img);
img++;
}
}
}
/*-------------------------------------------------------------------
增加画点子程序
-------------------------------------------------------------------*/
void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color)
{
unsigned char Row,Tier,Tier_bit ;
unsigned char ReadOldH,ReadOldL ;
LCD_WCMD(0x34);
LCD_WCMD(0x36);
Tier=X>>4 ;
Tier_bit=X&0x0f ;
if(Y<32)
{
Row=Y ;
}
else
{
Row=Y-32 ;
Tier+=8 ;
}
LCD_WCMD(Row+0x80);
LCD_WCMD(Tier+0x80);
ReadByte();
ReadOldH=ReadByte();
ReadOldL=ReadByte();
LCD_WCMD(Row+0x80);
LCD_WCMD(Tier+0x80);
if(Tier_bit<8)
{
switch(Color)
{
case 0 :
ReadOldH&=(~(0x01<<(7-Tier_bit)));
break ;
case 1 :
ReadOldH|=(0x01<<(7-Tier_bit));
break ;
case 2 :
ReadOldH^=(0x01<<(7-Tier_bit));
break ;
default :
break ;
}
LCD_WDAT(ReadOldH);
LCD_WDAT(ReadOldL);
}
else
{
switch(Color)
{
case 0 :
ReadOldL&=(~(0x01<<(15-Tier_bit)));
break ;
case 1 :
ReadOldL|=(0x01<<(15-Tier_bit));
break ;
case 2 :
ReadOldL^=(0x01<<(15-Tier_bit));
break ;
default :
break ;
}
LCD_WDAT(ReadOldH);
LCD_WDAT(ReadOldL);
}
LCD_WCMD(0x30);
}
//----------------------------------------------
// 清整个GDRAM空间
//-----------------------------------------------
void clrgdram()
{
unsigned char x,y ;
for(y=0;y<64;y++)
for(x=0;x<16;x++)
{
LCD_WCMD(0x34);
LCD_WCMD(y+0x80);
//行地址
LCD_WCMD(x+0x80);
//列地址
LCD_WCMD(0x30);
LCD_WDAT(0x00);
LCD_WDAT(0x00);
}
}
//////////////////////////
/////////////////////////
void juxingbo(void)//显示矩形波
{
uchar i,j,colour=1,flag=0;
uchar zhan=12;//为电平'1'的时间长度
uchar high=25;//高电平的位置
uchar low=40;//低电平的位置
for(i=0;i<128;i++)
{
if(i%zhan==0)
{
for(j=high;j<=low;j++)
DrawPoint(i,j,colour);
if(flag==0)
flag=1;
else
flag=0;
}
else
{
if(flag==0)
j=high;
else
j=low;
DrawPoint(i,j,colour);
}
delay_ms(5000);
delay_ms(5000);
}
}
void main( void )
{
//char i;
//unsigned char x1,y1,colour=1 ;//////////////////////////
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer to prevent time out reset
Clock_init(); // 时钟初始化
P2DIR|=BIT2+BIT1+BIT0;
LCD_PSB_High; // 液晶显示 并口方式
LCD_init();
//-----------------------------------------------------------------------------
// 显示字符
//---------------------------------------------------------------------------
Display(0,1,"西安理工大学");
Display(1,1,"电子实验小组");
Display(2,1,"正弦波显示");
Display(3,0,"2014年07月25日"); // 第四行要显示的内容
while(1)
{
clrgdram();
ClrScreen();
/*for(x1=0;x1<128;x1++) //正弦波显示
{
// y1=10*sin(0.314*x1)+20; //T=2pi/w
y1=10*sin(0.314*x1*0.5)+20; //T=2pi/w
DrawPoint(x1,y1,colour);
Display(2,1,"正弦波绘制");
Display(3,1,"数字:");
delay_ms(5000);
}*/
juxingbo(); //方波显示
delay_ms(5000);
//_BIS_SR(LPM0_bits);
}
}
|