正文

PC213X之LCM1010 C++程序源代码2008-02-22 22:31:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/niao0311/32875.html

分享到:

/*--------------------------------------------------------------------------
LPC213X之LCM1010 C++程序源代码

HotPower@126.com 2006.8.29

-------------------------------------------------------------------------------*/

#i nclude <LPC213XDEF.H>


#define WDI     P0_11//
#define LCMDATA P0_6 //LCM1010数据端
#define LCMWR   P0_7 //LCM1010写
#define LCMCS   P0_8 //LCM1010片选


#define LedSegS1 0x20
#define LedSegS2 0x40
#define LedSegS3 0x80

#define LedSegS4 0x02
#define LedSegS5 0x04
#define LedSegS6 0x08

 

//HCBD GAEF
#define LedSegA  0x04
#define LedSegB  0x20
#define LedSegC  0x40
#define LedSegD  0x10
#define LedSegE  0x02
#define LedSegF  0x01
#define LedSegG  0x08
#define LedSegH  0x80

#define LedChar0 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF          )
#define LedChar1 (          LedSegB + LedSegC           )
#define LedChar2 (LedSegA + LedSegB           + LedSegD + LedSegE           + LedSegG)
#define LedChar3 (LedSegA + LedSegB + LedSegC + LedSegD                     + LedSegG)
#define LedChar4 (          LedSegB + LedSegC                     + LedSegF + LedSegG)
#define LedChar5 (LedSegA           + LedSegC + LedSegD           + LedSegF + LedSegG)
#define LedChar6 (LedSegA           + LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedChar7 (LedSegA + LedSegB + LedSegC           )
#define LedChar8 (LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedChar9 (LedSegA + LedSegB + LedSegC + LedSegD           + LedSegF + LedSegG)
#define LedCharA (LedSegA + LedSegB + LedSegC           + LedSegE + LedSegF + LedSegG)
#define LedCharB (                    LedSegC + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedCharC (LedSegA                     + LedSegD + LedSegE + LedSegF    )
#define LedCharD (          LedSegB + LedSegC + LedSegD + LedSegE           + LedSegG)
#define LedCharE (LedSegA                     + LedSegD + LedSegE + LedSegF + LedSegG)
#define LedCharF (LedSegA                               + LedSegE + LedSegF + LedSegG)


static const unsigned char LedTab[] =
{
  LedChar0,
  LedChar1,
  LedChar2,
  LedChar3,
  LedChar4,
  LedChar5,
  LedChar6,
  LedChar7,
  LedChar8,
  LedChar9,
  LedCharA,
  LedCharB,
  LedCharC,
  LedCharD,
  LedCharE,
  LedCharF
};

 


void _delay_loop_(unsigned int);
void _delay_loop_(unsigned int val)
{
unsigned int i;
  for(i = 0; i < (val * 60); i ++);
}


class SystemObj {//系统类
public:
  SystemObj(void);//SystemObj的构造函数
public:
  void SystemInit(void);
  void WdtInit(void);
  void ClrWdt(void);
};

class LcdObj {//系统显示菜单类
public:
  LcdObj(void);//LcdObj的构造函数
public:
  void LcdInit(void);
  void LcdBuffersClear(void);
  void LcdClear(void);
  void LcdOpen(void);
  void LcdClose(void);
  void LcdWrite(unsigned char, unsigned char);
  void LcdWriteCommand(unsigned char);
  void LcdWriteData(unsigned char, unsigned char);
  void LcdDisplay(unsigned char, unsigned char);
  void LcdDisplay(unsigned long);
  void LcdDisplay(unsigned short);
public:
  unsigned char LcdBuffers[32];
};

SystemObj::SystemObj(void)
{
  SystemInit();
}

void SystemObj::SystemInit(void)
{
  PINSEL->PIN_SEL0 = 0;
  PINSEL->PIN_SEL1 = 0;
  PINSEL->PIN_SEL2 = 0;
  P0->IODIR = 0;
  P1->IODIR = 0;
  WdtInit();
}

void SystemObj::WdtInit(void)
{
  P0->IODIR |= (1 << WDI);      // 设置WDI控制口为输出 
}

void SystemObj::ClrWdt(void)
{
  P0->IOPIN ^= (1 << WDI);
}

LcdObj::LcdObj(void)
{
  LcdInit();
}

void LcdObj::LcdClear(void)
{
int i;
  for(i = 0;i < 32;i ++) {
    LcdWriteData(i, 0);
  }
}

void LcdObj::LcdBuffersClear(void)
{
int i;
  for(i = 0;i < 32;i ++) {
    LcdBuffers[i] = 0;
  }
}

void LcdObj::LcdInit(void)
{
  P0->IODIR |= (1 << LCMDATA) | (1 << LCMWR) | (1 << LCMCS);
  P0->IOSET = (1 << LCMDATA) | (1 << LCMWR) | (1 << LCMCS);
  LcdBuffersClear();
  LcdWriteCommand(0x24);
  LcdWriteCommand(0x18);
  LcdWriteCommand(0x01);
  LcdWriteCommand(0x03);
  LcdClear();
}

void LcdObj::LcdOpen(void)
{
  P0->IOSET = (1 << LCMDATA) | (1 << LCMWR) | (1 << LCMCS);
  _delay_loop_(1);
  P0->IOCLR = (1 << LCMCS);//打开片选信号
  _delay_loop_(1);
}

void LcdObj::LcdClose(void)
{
  P0->IOSET = (1 << LCMDATA) | (1 << LCMWR) | (1 << LCMCS);
}

void LcdObj::LcdWrite(unsigned char Data, unsigned char Count)
{
int i;
unsigned char val;
  val = 1 << (Count - 1);
  for(i = 0; i < Count; i ++) {
    P0->IOCLR = (1 << LCMWR);//拉低读写时钟
    if (Data & val) {
      P0->IOSET = (1 << LCMDATA);//发送串行数据'1'
 }
 else {
      P0->IOCLR = (1 << LCMDATA);//发送串行数据'0'
 }
    _delay_loop_(1);//等待LCM1010读
    P0->IOSET = (1 << LCMWR);//抬高读写时钟,打入串行数据
    _delay_loop_(1);//等待LCM1010读
 Data <<= 1;
  }
}

void LcdObj::LcdWriteCommand(unsigned char Command)
{
  LcdOpen();
  LcdWrite(0x04, 3);
  LcdWrite(Command, 8);
  LcdWrite(0x00, 1);
  LcdClose();
}

void LcdObj::LcdWriteData(unsigned char Addr, unsigned char Data)
{
  LcdOpen();
  LcdWrite(0x05, 3);
  LcdWrite(Addr, 5);
  LcdWrite(Data, 4);
  LcdClose();
}

void LcdObj::LcdDisplay(unsigned char LcdBit, unsigned char LcdData)
{
unsigned char Addr;
  Addr = LcdBit;
  LcdOpen();
  LcdWrite(0x05, 3);
  LcdWrite(Addr, 6);
  if (Addr) {
    LcdWrite((LcdData >> 4) & 0x0f, 4);//HCB0
    LcdWrite((LcdData >> 1) & 0x0f, 4);//DGA0
    LcdWrite((LcdData << 1) & 0x06, 4);//0EF0
  }
  else {
    LcdWrite(LcdData >> 4, 4);//S1S2S3 0
    LcdWrite(LcdData, 4);//S1S2S3 0
  }
  LcdClose();

}

void LcdObj::LcdDisplay(unsigned long val)
{
unsigned char LcdData, temp;
int i;
  LcdOpen();
  LcdWrite(0x05, 3);
  LcdWrite(0x0e, 6);
  for(i = 0; i < 6; i ++) {
    temp = val % 10;
 val /= 10;
    LcdData = LedTab[temp];
    LcdWrite((LcdData >> 4) & 0x0f, 4);//HCB0
    LcdWrite((LcdData >> 1) & 0x0f, 4);//DGA0
    LcdWrite((LcdData << 1) & 0x06, 4);//0EF0
  }
  LcdClose();
}

void LcdObj::LcdDisplay(unsigned short val)
{
unsigned char LcdData, temp;
int i, div;
  div = 1000;
  LcdOpen();
  LcdWrite(0x05, 3);
  LcdWrite(0x02, 6);
  for(i = 0; i < 4; i ++) {
    temp = (val / div) % 10;
 div /= 10;
    LcdData = LedTab[temp];
 if (i >= 2) {
   LcdData |= LedSegH;
 }
    LcdWrite((LcdData >> 4) & 0x0f, 4);//HCB0
    LcdWrite((LcdData >> 1) & 0x0f, 4);//DGA0
    LcdWrite((LcdData << 1) & 0x06, 4);//0EF0
  }
  LcdClose();
}

SystemObj Sys;
LcdObj    Lcd;

int main(void)
{
unsigned char K = LedSegS1;
unsigned long S = 1234;
unsigned short V = 0;
//  Lcd.LcdDisplay(0x00,  LedSegS1 + LedSegS4 + LedSegS6);//S1

/*
  Lcd.LcdDisplay(0x02, LedChar1);//下LED1
  Lcd.LcdDisplay(0x05, LedChar2);//下LED2
  Lcd.LcdDisplay(0x08, LedSegH + LedChar3);//下LED3
  Lcd.LcdDisplay(0x0b, LedSegH + LedChar4);//下LED4
*/
/*
  Lcd.LcdDisplay(0x0e, LedCharF);//上LED5
  Lcd.LcdDisplay(0x11, LedCharE);//上LED6
  Lcd.LcdDisplay(0x14, LedCharD);//上LED7
  Lcd.LcdDisplay(0x17, LedCharC);//上LED8
  Lcd.LcdDisplay(0x1a, LedCharB);//上LED9
  Lcd.LcdDisplay(0x1d, LedCharA);//上LED10
*/
  Lcd.LcdDisplay(0x00,  K);
  Lcd.LcdDisplay(S);
  Lcd.LcdDisplay(V);
  while(1) {
    S ++;
    Lcd.LcdDisplay(S);
 if ((S % 100) == 0) {
   V ++;
      Lcd.LcdDisplay(V);
      if ((S % 500) == 0) {
     if (K == LedSegS1) {
       K = LedSegS2;
     }
     else if (K == LedSegS2) {
       K = LedSegS3;
     }
     else if (K == LedSegS3) {
       K = LedSegS4;
     }
     else if (K == LedSegS4) {
       K = LedSegS5;
     }
     else if (K == LedSegS5) {
       K = LedSegS6;
     }
     else K = LedSegS1;
     Lcd.LcdDisplay(0x00,  K);
   }
 }
 Sys.ClrWdt();
    _delay_loop_(500);
    __nop();
  }
}

阅读(2497) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册