打印

LCM1602四位总线模式,计数到99就复位

[复制链接]
1944|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
seacore_2008|  楼主 | 2012-11-22 20:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位前辈帮分析一下,这个计数器怎么每次计数到99 , 999......就会调用一下show_log这个函数?谢谢!程序如下;
#device PIC16F87
#fuses HS, NOWDT,PUT, NOBROWNOUT, NOLVP, NOPROTECT, NOCPD,NOWRT
#use delay (clock = 4000000)
typedef unsigned char  uchar;
typedef unsigned int8 uint;
typedef int1     bit;
#include "pic16F87.h"
const uchar display[]={"  Welcom to use "};
const uchar display1[]={"TEST Tool!"};
const uchar display2[]={"Voltage:  V"};
const uchar disp_count[]={"Count:"};
const uchar disp_hex[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//--------------------------------------------------------------

#define  LCD_RS RB5
#define  LCD_EN RB6
//-------------初始化端口-----------------------------------------
void init_misc(void)
{ TRISA = 0x00;
      TRISB = 0x0;
      TRISC = 0x0;
      
      PORTA = 0x00;  //
      PORTB = 0x00;
      PORTC = 0x00;  //
      ADCON1 = 0x07;//
     
}
//----------写指令--------------------------------------
void write_com(uchar com)
{
PORTC = com&0xf0; // 传送高4?
LCD_RS = 0;   // 指令
LCD_EN = 1;  
delay_us(20) ;
LCD_EN = 0;  // 产生下降沿,写时序

PORTC = (com&0x0f)<<4; // 传送低4位
LCD_EN = 1;  
delay_us(20) ;
LCD_EN = 0;  // 产生下降沿,写时序
}
//---------------写数据
void write_dat( uchar lcd_data)
{
PORTC = lcd_data&0xf0; // 传送高4位
LCD_RS = 1;   // 数据
LCD_EN = 1;
delay_us(20);
LCD_EN = 0;  // 产生下降沿,写时序

PORTC = (lcd_data&0x0f)<<4; // 传送高4位
LCD_EN = 1;
delay_us(20);
LCD_EN = 0;  // 产生下降沿,写时序
}
void init_lcd()
{
delay_ms(30);
write_com(0x30);//
delay_ms(20);
write_com(0x30);//
delay_ms(20);
write_com(0x30);//
delay_ms(20);
write_com(0x20);//
delay_ms(2);
write_com(0x28);//DL=0:四位,N=1;双显,F=0?5*7点阵显示
delay_ms(2);
write_com(0X0C);//显示开,光标不显示,不闪烁//0x0c
delay_ms(2);
write_com(0x01);//清屏
delay_ms(2);
write_com(0x06);//光标不动,字符右移
delay_ms(2);
}
void show_log()
{uchar i;
write_com(0x80);              //写地址第一行0地址开始
delay_ms(5);
for(i=0;i<15;i++)
{
write_dat(display[i]);
delay_ms(20);
}
delay_ms(500);
write_com(0x0C3);           //第二行地址0x80+0X40
for(i=0;i<12;i++)
{
write_dat(display1[i]);
delay_ms(20);
}
delay_ms(1000);
write_com(0x01);              //清屏
delay_ms(10);
write_com(0x80);              //写地址
for(i=0;i<6;i++)            
{
write_dat(disp_count[i]);
delay_ms(50);
}

}
void disp_lcd(uchar a0,a1,a2,a3,a4,a5,a6,a7)
{
write_com(0x87);           
write_dat(disp_hex[a0]);
delay_ms(1);
write_com(0x88);
write_dat(disp_hex[a1]);
delay_ms(1);
write_com(0x89);
write_dat(disp_hex[a2]);
delay_ms(1);
write_com(0x8a);
write_dat(disp_hex[a3]);
delay_ms(1);
write_com(0x8b);
write_dat(disp_hex[a4]);
delay_ms(1);
write_com(0x8c);
write_dat(disp_hex[a5]);
delay_ms(1);
write_com(0x8d);
write_dat(disp_hex[a6]);
delay_ms(1);
write_com(0x8e);
write_dat(disp_hex[a7]);
delay_ms(1);
}
//------------------------------------------------------
void main(void)
{
uint R1,a0,a1,a2,a3,a4,a5,a6,a7;
R1=a1=a2=a3=a4=a5=a6=a7=a0=0;
init_misc();
init_lcd();
show_log();
while(1)
{
restart_wdt();
R1++;
delay_ms(1);
while(a0>=10)
{a0-=10;
while(1);
}
while(a1>=10)
{a1-=10;a0++;}
while(a2>=10)
{a2-=10;a1++;}
while(a3>=10)
{a3-=10;a2++;}
while(a4>=10)
{a4-=10;a3++;}
while(a5>=10)
{a5-=10;a4++;}
while(a6>=10)
{a6-=10;a5++;}
while(R1>=10)
{R1-=10;a6++;}
a7=R1;
disp_lcd(a0,a1,a2,a3,a4,a5,a6,a7);

}
}
沙发
kvdong| | 2012-11-23 11:32 | 只看该作者
写的太乱了。
1,你的问题描述不是很明确,是想每显示到99或999时显示**呢还是不想显示呢?
2,看你那堆while()头疼。能不能把while换成if()啊,在你程序里效果一样,而且更明确。
3,如果空间、时间够的话(仅仅你现有的任务),建议你这样写:
void main(void)
{
    unsigned long R1;
    unsigned char a[8];

    init_misc();
    init_lcd();
    show_log();

    R1=0;
    while(1)
    {
        restart_wdt();

        a[0]=( R1/10000000)%10;
        a[1]=( R1/1000000)%10;
        a[2]=( R1/100000)%10;
        a[3]=( R1/10000)%10;
        a[4]=( R1/1000)%10;
        a[5]=( R1/100)%10;
        a[6]=( R1/10)%10;
        a[7]=R1%10;
      
        disp_lcd(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]);
        R1++;
    }
}

使用特权

评论回复
板凳
kvdong| | 2012-11-23 11:34 | 只看该作者
如果想显示**的话,对R1做判断就可以了,不用我再说了吧

使用特权

评论回复
地板
seacore_2008|  楼主 | 2012-11-23 20:32 | 只看该作者
谢谢楼上的师傅,由于用CCSC编译器,求余还不是很熟。现已经搞定了,呵呵
的确是写的乱了一点,呵呵
while(1)
        {
       
        while(a0>=10)
                        {
                                while(1);

                        }

        while(a1>=10)
                        {
                                a1-=10; a0++;
                                write_com(0x87);   
                                write_dat(disp_hex[a0]);

                        }
       
                while(a2>=10)
                        {
                                a2-=10; a1++;
                                write_com(0x88);   
                                write_dat(disp_hex[a1]);

                        }
                while(a3>=10)
                        {
                                a3-=10;a2++;
                                write_com(0x89);   
                                write_dat(disp_hex[a2]);
                        }
                while(a4>=10)
                        {
                                a4-=10; a3++;
                                write_com(0x8a);   
                                write_dat(disp_hex[a3]);
                        }
                while(a5>=10)
                        {
                                a5-=10;
                                a4++;
                                write_com(0x8b);   
                                write_dat(disp_hex[a4]);
                        }
                while(a6>=10)
                        {
                                a6-=10;
                                a5++;
                                write_com(0x8c);   
                                write_dat(disp_hex[a5]);
                       
                        }
                while(a7>=10)
                        {
                                a7-=10;
                                a6++;
                                write_com(0x8d);   
                                write_dat(disp_hex[a6]);
                        }
       
                       
                               
                                write_com(0x8e);   
                                write_dat(disp_hex[a7]);
                                a7++;
                                relay();
                }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

31

帖子

0

粉丝