这是我dspic33f
芯片显示在12864上的,现在我想通过按键对第一行的值进行设置,也就是通过加和减按键对一个四位数一位一位的加和减,但是单个数字是一个字符,光标闪烁的的地址写进去后是两个字符,不知道怎么进行加减了!求指教!下面是我的显示程序:
#include"p33FJ128MC804.h"
_FBS(BWRP_WRPROTECT_OFF & BSS_NO_FLASH & RBS_NO_RAM); //f80000
_FSS(SWRP_WRPROTECT_OFF & SSS_NO_FLASH & RSS_NO_RAM); //f80002
_FGS(GCP_OFF & GWRP_OFF & GSS_OFF); //f80004
_FOSCSEL(FNOSC_PRIPLL & IESO_ON); //f80006
_FOSC(POSCMD_XT& OSCIOFNC_OFF & IOL1WAY_OFF & FCKSM_CSECMD); //f80008
_FWDT(WDTPOST_PS32768 & WDTPRE_PR128 & WINDIS_OFF & FWDTEN_OFF); //f8000a
_FPOR(FPWRT_PWR128 & AL
ti2C_OFF & LPOL_OFF & HPOL_OFF & PWMPIN_ON); //f8000c
_FICD( ICS_PGD3 & JTAGEN_OFF); //f8000e
#define E PORTBbits.RB13
#define RW PORTAbits.RA10
#define RS PORTAbits.RA7
#define RST PORTBbits.RB8
#define Key_enter PORTAbits.RA0
#define Key_mode PORTAbits.RA1
#define Key_down PORTBbits.RB0
#define Key_up PORTBbits.RB1
#define Key_blank PORTBbits.RB15
#define D0 PORTBbits.RB12
#define D1 PORTBbits.RB11
#define D2 PORTBbits.RB10
#define D3 PORTCbits.RC9
#define D4 PORTCbits.RC8
#define D5 PORTCbits.RC7
#define D6 PORTCbits.RC6
#define D7 PORTBbits.RB9
#define uchar unsigned char
#define uint unsigned int
uchar canshu1[]=("设定转速: rpm");
uchar canshu2[]=("当前转速: rpm");
uchar canshu3[]=("设置功率: KW");
uchar canshu4[]=("母线电压: V");
void System_Colck(void);
void configio(void);
void lcd_init(void);
void chk_busy();
void write_cmd(unsigned char c);
void write_data(unsigned char c);
void display1(void);
void delay_ms (uint t);
void delay_us (uint t);
void yidong(void);
void LED_DAT (uchar d)
{
D0=d&0x01;
D1=(d&0x02)>>1;
D2=(d&0x04)>>2;
D3=(d&0x08)>>3;
D4=(d&0x10)>>4;
D5=(d&0x20)>>5;
D6=(d&0x40)>>6;
D7=(d&0x80)>>7;
}
void System_Colck(void)
{
//Fosc = 80MHz 40MIPS
CLKDIVbits.PLLPRE = 0;//N1 = 2 7.3728MHz/2= 3.6864MHz
PLLFBDbits.PLLDIV = 38;//M = 40 3.6864*43 = 158.5152MHz
CLKDIVbits.PLLPOST = 0;//N2 = 2 158.5152/2 = 79.2576MHz
}
void configio(void)
{
TRISA= 0x0003;//RA0,RA1为输入,其余为输出
TRISB= 0x8003;//RB0,RB1,RB15为输入,其余为输出
TRISC= 0x0001;//RC0为输入,其余为输出
TRISBbits.TRISB8 = 0;//RB8
TRISBbits.TRISB9 = 0;//RB9
TRISBbits.TRISB10 = 0;//RB10
TRISBbits.TRISB11 = 0;//RB11
TRISBbits.TRISB12 = 0;//RB12
TRISBbits.TRISB13 = 0;//RA13
TRISCbits.TRISC6 = 0;//RC6
TRISCbits.TRISC7 = 0;//RC7
TRISCbits.TRISC8 = 0;//RC8
TRISCbits.TRISC9 = 0;//RC9
AD1PCFGL=0x0f;
}
/*------------------------------------------------
12864LCD初始化
------------------------------------------------*/
void lcd_init(void)
{
RST=0; //复位
delay_ms(850);//延迟71ms,大于40ms的延时程序
RST=1; //复位置高
delay_ms(120);//延迟10ms
write_cmd(0x34); //选择基本指令集
delay_us(2500); //延迟166us
write_cmd(0x34); //选择8位数据
delay_us(1000); //延迟66us
write_cmd(0x30);
delay_us(1000); //延时寤惰繜66us
write_cmd(0x0c); //开显示,有游标,游标位置允许反白
delay_us(2500); //延迟166us
write_cmd(0x01); //清除显示,并设定地址指针为00H
delay_ms(250); //延迟20ms
write_cmd(0x06); //指定在数据的读取与写入时,设定游标从右向左加1移动
delay_us(3000); //延迟200us
}
void chk_busy() //查忙
{
TRISB = 0x9e03; //RB9,10,11,12设置为输入
TRISC = 0x3c01; //RC6,7,8,9 设置为输入
E = 0; //使能无效
RS = 0; //指令方式
RW = 1; //读操作
delay_us(800); //延迟50us
while(D7)
{
delay_us(800); //延时500us
E=1;
delay_us(800); //延时500us
E=0;
}
E=0;
TRISB = 0x8003; //RB9,10,11,12还原为输出
TRISC = 0x0001; //RC6,7,8,9 还原为输出
}
/*------------------------------------------------
写指令
------------------------------------------------*/
void write_cmd(unsigned char c)
{
chk_busy();
RS=0;
RW=0;
delay_us(800);//延时50us
E=1;
delay_us(800);//延时50us
LED_DAT (c);
delay_us(8000);//延时500us
E=0;
delay_us(1500);//延时100us
}
/*------------------------------------------------
写数据
------------------------------------------------*/
void write_data(unsigned char c)
{
chk_busy();
RS=1;
RW=0;
delay_us(800);//延迟50us
E=1;
delay_us(800);//延迟50us
LED_DAT (c);
delay_us(8000);//延迟500us
E=0;
delay_us(1500);//延迟100us
}
void delay_ms (unsigned int t)
{
unsigned int i;
for(;t>0;t--)
for(i=0;i<1000;i++);
return;
}
void delay_us (unsigned int t)
{
while(--t);
return;
}
void yidong(void)
{
uchar Key_num;
if(!Key_mode)
{
delay_ms(15);
if(Key_mode==0)
{ while(!Key_mode);
Key_num++;
switch(Key_num)
{
case 0:write_cmd(0x84);break;
case 1:write_cmd(0x85);break;
case 2:write_cmd(0x86);break;
case 3:write_cmd(0x8C);break;
case 4:write_cmd(0x8D);break;
case 5:write_cmd(0x8E);break;
case 6:Key_num=0;write_cmd(0x84);break;
default:break;
}
}
}
}
void display1(void)
{
uchar i;
write_cmd(0x80);
for(i=0;i<16;i++)
{
write_data(canshu1[i]);
delay_ms(100);//寤惰繜8ms
};
write_cmd(0x90);
for(i=0;i<16;i++)
{
write_data(canshu2[i]);
delay_ms(100);//寤惰繜8ms
}
write_cmd(0x88);
for(i=0;i<15;i++)
{
write_data(canshu3[i]);
delay_ms(100);//寤惰繜8ms
}
write_cmd(0x98);
for(i=0;i<14;i++)
{
write_data(canshu4[i]);
delay_ms(100);//寤惰繜8ms
}
write_cmd(0x80+4 );
write_cmd(0x0D);
}
void main ()
{
System_Colck();
configio();
lcd_init();
delay_ms (100);//延迟8ms
display();
write_cmd(0x80+4 );
write_cmd(0x0D);//ڢҪʁ˸ì
Key_down=1;
while(1)
{
yidong();
}
}
-
一周热门 更多>