main.cpp

来自「MSP430上实现5110液晶显示程序(性价比很高的彩屏)」· C++ 代码 · 共 593 行 · 第 1/2 页

CPP
593
字号

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*--  文字:     --*/
/*--  宋体9;  此字体下对应的点阵为:宽x高=12x12   --*/
/*--  高度不是8的倍数,现调整为:宽度x高度=12x16  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

/*--------------------------------------------
LCD_write_byte: 使用SPI接口写数据到LCD
输入参数:dt:写入的数据;
command :写数据/命令选择;
编写日期:20080918 
----------------------------------------------*/
void LCD_write_byte(unsigned char dt, unsigned char command)
{
	unsigned char i; 
	sce0;	
	if(command==1)
        {dc1;	}
        else {dc0;}
	for(i=0;i<8;i++)
	{ 
		if(dt&0x80)
                {sdin1;}
		else
                {sdin0;}
		dt=dt<<1;		
		sclk0; 		
		sclk1; 
	}	
	dc1;	
	sce1;	
	sdin1;
}
/*---------------------------------------
LCD_init: 3310LCD初始化
编写日期:20080918 
-----------------------------------------  */
void LCD_init(void)
{
	res0;  	
  	delay_ms(1);
  	res1;  
	LCD_write_byte(0x21,0);//初始化Lcd,功能设定使用扩充指令
	LCD_write_byte(0xd0,0);//设定液晶偏置电压
	LCD_write_byte(0x20,0);//使用基本指令
	LCD_write_byte(0x0C,0);//设定显示模式,正常显示
}
/*-------------------------------------------
LCD_set_XY: 设置LCD坐标函数
输入参数:X:0-83  Y:0-5
编写日期:20080918 
---------------------------------------------*/
void LCD_set_XY(unsigned char X, unsigned char Y)
{
	LCD_write_byte(0x40 | Y, 0);// column
	LCD_write_byte(0x80 | X, 0);// row
} 
/*------------------------------------------
LCD_clear: LCD清屏函数
编写日期:20080918 
--------------------------------------------*/
void LCD_clear(void)
{
	unsigned char t;
	unsigned char k;
	LCD_set_XY(0,0);
	for(t=0;t<6;t++)
	{ 
		for(k=0;k<84;k++)
		{ 
			LCD_write_byte(0x00,1);
				
		} 
	}
}
/*---------------------------------------------
LCD_write_shu: 显示8(宽)*16(高)点阵列数字字母符号等半角类
输入参数:c:显示的字符;
编写日期:20080918 
-----------------------------------------------*/
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
	unsigned char i;  	
	
	LCD_set_XY(row*6, page);// 列,页 
	for(i=0; i<6;i++) 
	{
		LCD_write_byte(zimu[c*12+i],1); 
	}
	
    LCD_set_XY(row*6, page+1);// 列,页 
	for(i=6; i<12;i++) 
	{
		LCD_write_byte(zimu[c*12+i],1);
	}	 	
}
/*---------------------------------------------
LCD_write_hanzi: 显示12(宽)*16(高)点阵列汉字等半角类
输入参数:c:显示的字符;
编写日期:20080918 
-----------------------------------------------*/
void LCD_write_hanzi(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页 dd:字符
{
	unsigned char i;  	
	
	LCD_set_XY(row*6, page);// 列,页 
	for(i=0; i<12;i++) 
	{
		LCD_write_byte(hanzi[c*24+i],1); 
	}
	
    LCD_set_XY(row*6, page+1);// 列,页 
	for(i=12; i<24;i++) 
	{
	LCD_write_byte(hanzi[c*24+i],1);
	}	
}
/********打点********/
void dadian(unsigned int x,unsigned int y)
{
  unsigned int y1,y2,y3,y4=1,i;
  y1=y/8;y2=1+y-8*y1;y3=5-y1;
  for(i=0;i<(8-y2);i++)
  {
    y4*=2;
  }
  LCD_set_XY(x,y3);
  LCD_write_byte(y4,1);
}
/********************************************************
* 名称:Line()
      采用布兰森汉姆(Bresenham)算法画线
* 功能:任意两点间的直线。根据硬件特点,实现加速。
* 入口参数:x0       直线起点所在行的位置
*         y0       直线起点所在列的位置
*         x1     直线终点所在行的位置
‘       y1     直线终点所在列的位置
* 出口参数:   无
* 说明:操作失败原因是指定地址超出缓冲区范围。
*********************************************************/
void Line(double x0,double y0,double x1,double y1)
{
int temp;
int dx,dy;               //定义起点到终点的横、纵坐标增加值
int s1,s2,status,i;
int Dx,Dy,sub;

dx=x1-x0;
if(dx>=0)                 //X的方向是增加的
  s1=1;
else                     //X的方向是降低的
  s1=-1;     
dy=y1-y0;                 //判断Y的方向是增加还是降到的
if(dy>=0)
  s2=1;
else
  s2=-1;
  
Dx=fabs(x1-x0);             //计算横、纵标志增加值的绝对值
Dy=fabs(y1-y0);
if(Dy>Dx)                 //               
  {                     //以45度角为分界线,靠进Y轴是status=1,靠近X轴是status=0 
  temp=Dx;
  Dx=Dy;
  Dy=temp;
  status=1;
  } 
else
  status=0;

/********判断垂直线和水平线********/
if(dx==0)                   //横向上没有增量,画一条水平线
  Line(x0,y0,y1,1);
if(dy==0)                   //纵向上没有增量,画一条垂直线
  Line(x0,y0,x1,1);


/*********Bresenham算法画任意两点间的直线********/ 
  sub=2*Dy-Dx;                 //第1次判断下个点的位置
  for(i=0;i<Dx;i++)
  { 
    dadian(x0,y0);           //画点 
    if(sub>=0)                               
    { 
    if(status==1)               //在靠近Y轴区,x值加1
      x0+=s1; 
    else                     //在靠近X轴区,y值加1               
      y0+=s2; 
    sub-=2*Dx;                 //判断下下个点的位置 
    } 
    if(status==1)
  y0+=s2; 
    else       
    x0+=s1; 
    sub+=2*Dy; 
        
  } 
}
/***********TA初始化*********/
void init_TA()
{
  TACCTL0 = CCIE;                           // TACCR0 interrupt enabled
  TACCR0 = 20000;
  TACTL = TASSEL_2 + MC_1;                  // SMCLK, upmode
}
/************ADC10初始化************/
void init_ADC10()
{
  ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
  ADC10AE0 |= 0x01;                         // P2.0 ADC option select
}
main()
{	
        WDTCTL = WDTPW + WDTHOLD;
        DCOCTL=CALDCO_1MHZ;
        BCSCTL1=CALBC1_1MHZ;
        P1DIR=0XFF;
  	res0;
	delay_us(100);
	res1;	  	
  	LCD_init();  //初始化LCD模块 
	LCD_clear(); //清屏幕
        init_TA();
        init_ADC10();
        __bis_SR_register(GIE); 
	/*LCD_write_shu(2,4,9);//j
        LCD_write_shu(3,4,20);//u
        LCD_write_shu(4,4,18);//s
        LCD_write_shu(5,4,19);//t
        LCD_write_shu(6,4,26);//.
        LCD_write_shu(7,4,4);//e
        LCD_write_shu(8,4,3);//d
        LCD_write_shu(9,4,20);//u
        LCD_write_shu(10,4,26);//.
        LCD_write_shu(11,4,2);//c
        LCD_write_shu(12,4,13);//n*/
        while(1)
        {
         ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
         ad_value=47*ad_value/1023;
          dadian(t,ad_value);  
        }
}
#pragma vector = ADC10_VECTOR             //ADC10中断服务
__interrupt void adc_isr(void)
{
  ad_value=ADC10MEM;                      //将AD采样值存入ad_value
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
  t+=1;
  if(t>83)
  {
    t=0;
    LCD_clear();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?