搜索
bottom↓
回复: 6

各位好心的大哥大姐们,帮小弟看看我的LCD1602程序~

[复制链接]

出0入0汤圆

发表于 2009-12-17 20:42:22 | 显示全部楼层 |阅读模式
小弟是AVR单片机超级菜鸟新手,以前直接触过c语言,所以这次搞1602显示遇到很多问题~现在我的LCD初始化都不成功,更别提显示东西了,郁闷啊~下面把我的程序和头文件发上来~各位高手麻烦看下~帮个忙指点下~万分感谢~(*^__^*) 我自己怀疑是延时不准?我用的M16,晶振1M
//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
#include <abc_lcd.h>



/*------宏定义------*/
#define uchar        unsigned char
#define uint        unsigned int
#define BIT(x)        (1<<(x))
#define NOP()        asm("nop")
#define WDR()         asm("wdr")


//端口初始化
void port_init(void)
{
        PORTA = 0x00;
        DDRA  = 0x00;
        PORTB = 0x00;
        DDRB  = 0x00;
        PORTC = 0x00;
        DDRC  = 0x00;
        PORTD = 0x00;
        DDRD  = 0x00;
}


void init_devices(void)
{
        cli(); //禁止所有中断
        MCUCR  = 0x00;
        MCUCSR = 0x80;//禁止JTAG
        GICR   = 0x00;
        port_init();
lcd_init();
        sei();//开全局中断
}
//主函数
int main(void)
{        init_devices();
unsigned char x,y,sdata,a,b;
x=0x01;
y=0x00;
sdata=0xff;
a=1;
b=0;
//在这继续添加你的代码
        while(1)
        {

lcd_write_char(x,y,sdata);
lcd_locate(x,y);
lcd_print_number(sdata,a,b);

NOP();
        }
        return 0;
}





以下为abc_lcd.h头文件/*
* ----------------------------------------------------------------------------
* Project Charger SE
*
* file: abc_lcd.h
*
* 创建者:  Trinove
* 创建日期: 2008-1-13
*
* 最后修改者:martin7wind       
* 最后修改日期:2008-2-3
*
* 当前版本: 0.01
*
* ----------------------------------------------------------------------------
*
*
*  液晶显示的基本支持函数
*
*/


#ifndef _LCD_H_
#define _LCD_H_ 1


// 位操作

#define BIT(x)        (1<<(x))

#define                BIT_SET(a,b)        a|=BIT(b)
#define                BIT_CLR(a,b)               a&=~BIT(b)
#define                BIT_INV(a,b)           a^=BIT(b)
#define                BIT_STATUS(a,b) a&BIT(b)
#define  xtal 1

//四线模式
/*----------------------------------------------------------------------------
LCD   1602
PIN0~7  RW E RS
连线如下:
PIN7 --> PB4
PIN6 --> PD2
PIN5 --> PD3
PIN4 --> PD6
RW   --> GND (R/W = 0 write)
E      --> PA6 (E = 1 enable )
RS    --> PA7 (RS = 0 CMD;RS = 1 DATA )

---------------------------------------------------------------------------*/
#define uint        unsigned int
#define LCD_DATA7_DDR DDRB
#define LCD_DATA6_DDR DDRD
#define LCD_DATA5_DDR DDRD
#define LCD_DATA4_DDR DDRD

#define LCD_DATA7_PIN PINB
#define LCD_DATA6_PIN PIND
#define LCD_DATA5_PIN PIND
#define LCD_DATA4_PIN PIND

#define LCD_DATA7_PORT PORTB
#define LCD_DATA6_PORT PORTD
#define LCD_DATA5_PORT PORTD
#define LCD_DATA4_PORT PORTD

#define LCD_DATA7 PB4
#define LCD_DATA6 PD2
#define LCD_DATA5 PD3
#define LCD_DATA4 PD6

#define LCD_E_DDR  DDRA
#define LCD_E_PORT  PORTA
#define LCD_E  PA6

#define LCD_E_HIGH BIT_SET(LCD_E_PORT, LCD_E)
#define LCD_E_LOW BIT_CLR(LCD_E_PORT, LCD_E)

#define LCD_RS_DDR DDRA
#define LCD_RS_PORT PORTA
#define LCD_RS PA7

#define LCD_DATA_MODE BIT_SET(LCD_RS_PORT, LCD_RS)
#define  LCD_CMD_MODE BIT_CLR(LCD_RS_PORT, LCD_RS)

#define CMD 0
#define DATA 1

#define BIT_POSITION7 (1<<7)
#define BIT_POSITION6 (1<<6)
#define BIT_POSITION5 (1<<5)
#define BIT_POSITION4 (1<<4)

#define LINE_1 0
#define LINE_2 1
void Delay_us(void)                 //1us延时函数
  {
   asm("nop");
  }

void Delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   Delay_us();
  }
void Delay_1ms(void)
{uint j;
for (j=1;j<(uint)(xtal*143-2);j++)
;
}
void Delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int j=0;
   for (j=0;j<n;j++)
   Delay_1ms();
  }


unsigned int temp;

//最初的初始化设置中,写入数据函数
void lcd_write_cmd(unsigned char cmd)
{

        LCD_CMD_MODE;

        if(cmd&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(cmd&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(cmd&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(cmd&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

        //E 信号
        LCD_E_HIGH;

        Delay_nus(5);

        LCD_E_LOW;

}

// 向LCD写入数据
void lcd_write_byte(unsigned char byte, unsigned char type)
{
        if(type)
               
                {LCD_DATA_MODE;}
        else
                {LCD_CMD_MODE;}

        temp = byte;

        if(byte&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(byte&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(byte&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(byte&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

                //E 信号
        LCD_E_HIGH;

        Delay_nus(5);

        LCD_E_LOW;

         Delay_nus(39);//39us!

        byte <<=4;

        if(byte&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(byte&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(byte&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(byte&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

                        //E 信号
        LCD_E_HIGH;

Delay_nus(5);

        LCD_E_LOW;

        Delay_nms(10);
       
}


// 向LCD写入指令
void lcd_write_order(unsigned char order)
{

}



// 从LCD读取标志
unsigned char lcd_read_status(void)
{
unsigned char result;


return result;
}

//清屏
void lcd_cls()
{
        lcd_write_byte(0x01,CMD);
}

/********************************************************
1602 液晶初始化函数
输入:无
输出:无
功能: 设置io状态,初始化1602
********************************************************/

void lcd_init()
{
        // 相关IO配置
         BIT_SET(LCD_DATA7_DDR, LCD_DATA7);

         BIT_SET(LCD_DATA6_DDR, LCD_DATA6);

         BIT_SET(LCD_DATA5_DDR, LCD_DATA5);

         BIT_SET(LCD_DATA4_DDR, LCD_DATA4);
         
        BIT_SET(LCD_E_DDR, LCD_E);

        BIT_SET(LCD_RS_DDR,LCD_RS);

         Delay_nms(15);//上电延时15MS以上
         lcd_write_cmd(0x30);//上电初始化命令
         Delay_nms(5);//delay >5.1MS
         lcd_write_cmd(0x30);
         Delay_nms(5);//delay >100us
         lcd_write_cmd(0x30);
         Delay_nms(15);
          lcd_write_cmd(0x20);//四位数据模式
         Delay_nms(15);
         //功能设定
          lcd_write_byte(0x28,CMD);//0x28=4位数据线,双行显示,5*7字型
         //LCD_write_byte(0x0e,CMD);//0x0e=显示开,光标不闪烁
         lcd_write_byte(0x0c,CMD);//0x0c=显示开,光标不显示
         lcd_write_byte(0x06,CMD);//0x06=每次输入后光标右移一格

        lcd_cls();

}

void lcd_write_char(unsigned char lcd_x,unsigned char lcd_y,unsigned char data)
{
if(lcd_y) //第二行
  lcd_write_byte(0xc0+lcd_x,CMD);
else      //第一行
  lcd_write_byte(0x80+lcd_x,CMD);
  
lcd_write_byte(data,DATA);
}

// 从LCD读取数据
unsigned char lcd_read_data(void)
{
unsigned char result;


return result;
}


// 定位LCD行列位置
// 横坐标 lcd_x
//纵坐标 lcd_y
void lcd_locate(unsigned char lcd_x, unsigned char lcd_y)
{
        if(lcd_y) //第二行
          lcd_write_byte(0xc0+lcd_x,CMD);
         else      //第一行
          lcd_write_byte(0x80+lcd_x,CMD);
}

// 向LCD发送显示文字
//输入参数显示文字指针pstring
void lcd_print_string(unsigned char * pstring)
{
         while(*pstring)
                 {
                         lcd_write_byte(*pstring,DATA);
                         pstring++;
                 }
}

// 向LCD发送显示数据
// number表示数值,为有符号的整数,ca表示显示的位数(1-6),cb表示其中的小数位数,(类似定点的小数)
// 显示整数时,cb置0即可
void lcd_print_number(signed int number , unsigned char ca , unsigned char cb)
{
               
        unsigned char bit[4];   //uint,  最多5位

        unsigned char *pbit;

        unsigned char i;

        if(number>0)
                {
                        temp = number;
                }
        else
                {
                        temp =-number;
                }
//        temp = 12345;

//算出每一位057920


        bit[4] = temp/10000 ;

        bit[3] = temp/1000  - bit[4]*10;

        bit[2] = temp/100  - bit[4]*100 - bit[3]*10;

        bit[1] = temp/10  - bit[4]*1000 - bit[3]*100 - bit[2]*10;

        bit[0] = temp  - bit[4]*10000 - bit[3]*1000 - bit[2]*100 - bit[1]*10;

        if(number<0)     //负值
                {
                        lcd_write_byte('-', DATA);        //先显示负号

                        pbit = &(bit [4]);   //指向最高位

                        for(i=5-ca;i>0;i--)
                                pbit --;
                       
                        while(ca)
                                {
                                        if(ca==cb)
                                                lcd_write_byte('.', DATA);

                                        lcd_write_byte(*pbit+48,DATA);

                                        pbit --;

                                        ca--;
                                }
                }
        else  //正数
                {

                        pbit = &(bit [4]);   //指向最高位

                        for(i=5-ca;i>0;i--)
                                pbit --;

                        while(ca)
                                {
                                        if(ca==cb)
                                                lcd_write_byte('.', DATA);

                                        lcd_write_byte(*pbit+48,DATA);

                                        pbit --;

                                        ca--;
                                }
                }
}

#endif

出0入0汤圆

 楼主| 发表于 2009-12-17 21:32:16 | 显示全部楼层
.....大哥大姐们,看了的指点两句呗~谢谢了~我自己顶下~不能沉啊~

出0入0汤圆

发表于 2009-12-17 21:51:54 | 显示全部楼层
看我的签名,同时消灭零回复。

出0入0汤圆

发表于 2009-12-22 15:14:52 | 显示全部楼层
看不懂。。。让会看的继续了

出0入0汤圆

 楼主| 发表于 2009-12-22 20:46:14 | 显示全部楼层
开心死了啊~哈哈哈~我的问题解决了~不是~好像不是对比度的原因,我又换了个程序~把数据端口设成PA的高四位就成功了~下面发我现在用的程序~也是改我师兄的,服务大家吧~希望以后与大家多多交流LCD1602和其它问题吧~(*^__^*) 嘻嘻……
主函数:
//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
#include <abc_lcd.h>



/*------宏定义------*/
#define uchar        unsigned char
#define uint        unsigned int
#define BIT(x)        (1<<(x))
#define NOP()        asm("nop")
#define WDR()         asm("wdr")


//端口初始化
void port_init(void)
{
        PORTA = 0x00;
        DDRA  = 0x00;
        PORTB = 0x00;
        DDRB  = 0x00;
        PORTC = 0x00;
        DDRC  = 0x00;
        PORTD = 0x00;
        DDRD  = 0x00;
}


void init_devices(void)
{
        cli(); //禁止所有中断
        MCUCR  = 0x00;
        MCUCSR = 0x80;//禁止JTAG
        GICR   = 0x00;
        port_init();
        Delay_nms(500);
lcd_init();

        sei();//开全局中断
}

//主函数
int main(void)
{init_devices();
uint i,j;
        while(1)
        {lcd_locate(0,0);
        lcd_print_string("^^MyLCD1602__@^^");
  lcd_locate(0,1);
lcd_print_string("^^Thanks m7w!!^^");
Delay_nms(5000);
lcd_cls();
        for(i=0x41,j=0;i<0x90;i++)
{lcd_write_char(j,0,i);
j++;
    Delay_nms(5000);}
        }
        return 0;
}


lcd1602驱动程序"abc_lcd.h":


/*
* ----------------------------------------------------------------------------
* Project Charger SE
*
* file: abc_lcd.h
*
* 创建者:  Trinove
* 创建日期: 2008-1-13
*
* 修改者:martin7wind
* 修改日期:2008-2-3
*
* 最后修改者:dxxlovewj
* 修改日期:2009.12.22
* qq:281564713  朋友们可以加我大家共同进步哈~我是新手~
* 当前版本: 0.01
*
* ----------------------------------------------------------------------------
*
*
*  液晶显示的基本支持函数
*
*/


#ifndef _LCD_H_
#define _LCD_H_ 1


// 位操作

#define BIT(x)        (1<<(x))

#define                BIT_SET(a,b)        a|=BIT(b)
#define                BIT_CLR(a,b)               a&=~BIT(b)
#define                BIT_INV(a,b)           a^=BIT(b)
#define                BIT_STATUS(a,b) a&BIT(b)
#define  xtal 1

//四线模式
/*----------------------------------------------------------------------------
LCD   1602
PIN0~7  RW E RS
连线如下:
PIN7 -->   PA7
PIN6 -->   PA6
PIN5 -->   PA5
PIN4 -->   PA4
RW   --> GND (R/W = 0 write)
E      --> PA6 (E = 1 enable )
RS    --> PA7 (RS = 0 CMD;RS = 1 DATA )

---------------------------------------------------------------------------*/
#define uint        unsigned int
#define LCD_DATA7_DDR DDRA
#define LCD_DATA6_DDR DDRA
#define LCD_DATA5_DDR DDRA
#define LCD_DATA4_DDR DDRA

#define LCD_DATA7_PIN PINA
#define LCD_DATA6_PIN PINA
#define LCD_DATA5_PIN PINA
#define LCD_DATA4_PIN PINA

#define LCD_DATA7_PORT PORTA
#define LCD_DATA6_PORT PORTA
#define LCD_DATA5_PORT PORTA
#define LCD_DATA4_PORT PORTA

#define LCD_DATA7 PA7
#define LCD_DATA6 PA6
#define LCD_DATA5 PA5
#define LCD_DATA4 PA4

#define LCD_E_DDR  DDRD
#define LCD_E_PORT  PORTD
#define LCD_E  PD7

#define LCD_E_HIGH BIT_SET(LCD_E_PORT, LCD_E)
#define LCD_E_LOW BIT_CLR(LCD_E_PORT, LCD_E)

#define LCD_RS_DDR DDRC
#define LCD_RS_PORT PORTC
#define LCD_RS PC6

#define LCD_DATA_MODE BIT_SET(LCD_RS_PORT, LCD_RS)
#define  LCD_CMD_MODE BIT_CLR(LCD_RS_PORT, LCD_RS)

#define CMD 0
#define DATA 1

#define BIT_POSITION7 (1<<7)
#define BIT_POSITION6 (1<<6)
#define BIT_POSITION5 (1<<5)
#define BIT_POSITION4 (1<<4)

#define LINE_1 0
#define LINE_2 1
void Delay_us(void)                 //1us延时函数
  {
   asm("nop");
  }

void Delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   Delay_us();
  }
void Delay_1ms(void)
{uint j;
for (j=1;j<(uint)(xtal*143-2);j++)
;
}
void Delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int j=0;
   for (j=0;j<n;j++)
   Delay_1ms();
  }


unsigned int temp;

//最初的初始化设置中,写入数据函数
void lcd_write_cmd(unsigned char cmd)
{

        LCD_CMD_MODE;

        if(cmd&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(cmd&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(cmd&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(cmd&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

        //E 信号
        LCD_E_HIGH;

        Delay_nus(500);

        LCD_E_LOW;

}

// 向LCD写入数据
void lcd_write_byte(unsigned char byte, unsigned char type)
{
        if(type)
               
                {LCD_DATA_MODE;}
        else
                {LCD_CMD_MODE;}

        temp = byte;

        if(byte&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(byte&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(byte&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(byte&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

                //E 信号
        LCD_E_HIGH;

        Delay_nus(500);

        LCD_E_LOW;

                Delay_nms(500);//39us!

        byte <<=4;

        if(byte&BIT_POSITION7)
                {BIT_SET(LCD_DATA7_PORT,LCD_DATA7);}
        else
                {BIT_CLR(LCD_DATA7_PORT,LCD_DATA7);}

        if(byte&BIT_POSITION6)
                {BIT_SET(LCD_DATA6_PORT,LCD_DATA6);}
        else
                {BIT_CLR(LCD_DATA6_PORT,LCD_DATA6);}

        if(byte&BIT_POSITION5)
                {BIT_SET(LCD_DATA5_PORT,LCD_DATA5);}
        else
                {BIT_CLR(LCD_DATA5_PORT,LCD_DATA5);}

        if(byte&BIT_POSITION4)
                {BIT_SET(LCD_DATA4_PORT,LCD_DATA4);}
        else
                {BIT_CLR(LCD_DATA4_PORT,LCD_DATA4);}

                        //E 信号
        LCD_E_HIGH;

        Delay_nms(500);

        LCD_E_LOW;

        Delay_nms(500);
       
}


// 向LCD写入指令
void lcd_write_order(unsigned char order)
{

}



// 从LCD读取标志
unsigned char lcd_read_status(void)
{
unsigned char result;


return result;
}

//清屏
void lcd_cls()
{
        lcd_write_byte(0x01,CMD);
}

/********************************************************
1602 液晶初始化函数
输入:无
输出:无
功能: 设置io状态,初始化1602
********************************************************/

void lcd_init()
{
        // 相关IO配置
         BIT_SET(LCD_DATA7_DDR, LCD_DATA7);

         BIT_SET(LCD_DATA6_DDR, LCD_DATA6);

         BIT_SET(LCD_DATA5_DDR, LCD_DATA5);

         BIT_SET(LCD_DATA4_DDR, LCD_DATA4);
         
        BIT_SET(LCD_E_DDR, LCD_E);

        BIT_SET(LCD_RS_DDR,LCD_RS);

                 Delay_nms(500);//上电延时15MS以上
         lcd_write_cmd(0x30);//上电初始化命令
         Delay_nms(500);//delay >5.1MS
         lcd_write_cmd(0x30);
         Delay_nms(500);//delay >100us
         lcd_write_cmd(0x30);
                Delay_nms(500);
          lcd_write_cmd(0x20);//四位数据模式
        Delay_nms(500);
         //功能设定
          lcd_write_byte(0x28,CMD);//0x28=4位数据线,双行显示,5*7字型
                Delay_nms(500);
           lcd_write_byte(0x28,CMD);//0x28=4位数据线,双行显示,5*7字型
            Delay_nms(500);
             lcd_write_byte(0x28,CMD);//0x28=4位数据线,双行显示,5*7字型
           Delay_nms(500);
           lcd_write_byte(0x28,CMD);//0x28=4位数据线,双行显示,5*7字型
            Delay_nms(500);

         //LCD_write_byte(0x0e,CMD);//0x0e=显示开,光标不闪烁
         lcd_write_byte(0x0c,CMD);//0x0c=显示开,光标不显示
   Delay_nms(500);
  lcd_write_byte(0x0c,CMD);//0x0c=显示开,光标不显示
  Delay_nms(500);
         lcd_write_byte(0x06,CMD);//0x06=每次输入后光标右移一格
         Delay_nms(500);

        lcd_cls();
Delay_nms(500);
}

void lcd_write_char(unsigned char lcd_x,unsigned char lcd_y,unsigned char data)
{
if(lcd_y) //第二行
  lcd_write_byte(0xc0+lcd_x,CMD);
else      //第一行
  lcd_write_byte(0x80+lcd_x,CMD);
  Delay_nms(5000);
lcd_write_byte(data,DATA);
}

// 从LCD读取数据
unsigned char lcd_read_data(void)
{
unsigned char result;


return result;
}


// 定位LCD行列位置
// 横坐标 lcd_x
//纵坐标 lcd_y
void lcd_locate(unsigned char lcd_x, unsigned char lcd_y)
{
        if(lcd_y) //第二行
          lcd_write_byte(0xc0+lcd_x,CMD);
         else      //第一行
          lcd_write_byte(0x80+lcd_x,CMD);
}

// 向LCD发送显示文字
//输入参数显示文字指针pstring
void lcd_print_string(unsigned char * pstring)
{
         while(*pstring)
                 {
                         lcd_write_byte(*pstring,DATA);
                         pstring++;
                 }
}

// 向LCD发送显示数据
// number表示数值,为有符号的整数,ca表示显示的位数(1-6),cb表示其中的小数位数,(类似定点的小数)
// 显示整数时,cb置0即可
void lcd_print_number(signed int number , unsigned char ca , unsigned char cb)
{
               
        unsigned char bit[4];   //uint,  最多5位

        unsigned char *pbit;

        unsigned char i;

        if(number>0)
                {
                        temp = number;
                }
        else
                {
                        temp =-number;
                }
//        temp = 12345;

//算出每一位057920


        bit[4] = temp/10000 ;

        bit[3] = temp/1000  - bit[4]*10;

        bit[2] = temp/100  - bit[4]*100 - bit[3]*10;

        bit[1] = temp/10  - bit[4]*1000 - bit[3]*100 - bit[2]*10;

        bit[0] = temp  - bit[4]*10000 - bit[3]*1000 - bit[2]*100 - bit[1]*10;

        if(number<0)     //负值
                {
                        lcd_write_byte('-', DATA);        //先显示负号

                        pbit = &(bit [4]);   //指向最高位

                        for(i=5-ca;i>0;i--)
                                pbit --;
                       
                        while(ca)
                                {
                                        if(ca==cb)
                                                lcd_write_byte('.', DATA);

                                        lcd_write_byte(*pbit+48,DATA);

                                        pbit --;

                                        ca--;
                                }
                }
        else  //正数
                {

                        pbit = &(bit [4]);   //指向最高位

                        for(i=5-ca;i>0;i--)
                                pbit --;

                        while(ca)
                                {
                                        if(ca==cb)
                                                lcd_write_byte('.', DATA);

                                        lcd_write_byte(*pbit+48,DATA);

                                        pbit --;

                                        ca--;
                                }
                }
}

#endif

出0入0汤圆

 楼主| 发表于 2009-12-22 20:47:23 | 显示全部楼层
谢谢楼上的朋友为我解答~感谢~也感谢点进来的朋友~祝大家圣诞元旦快乐~

出0入0汤圆

发表于 2009-12-26 11:50:14 | 显示全部楼层
mark
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-3 01:18

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表