|
msp430g2231在lcd1602显示自定义字符
[复制链接]
自己又蛋疼写的程序。主要是在1602上写入自定义字符,可以用h1838遥控,没有实际意义,做个测试文件,以后自己好再调试。msp430g2231只有8个io口(可以拓展为10个)怎么利用这些io口,我想了个简单的办法。红外遥控占用一个io口,1602只是需要4根线(164驱动的)当然可以节省到2个io口,共占6个io口的,本来打算做个遥控车的,用PWM驱动,但肯跌的是自己不能解决电机反向电流的干扰。一上电机程序就崩溃,无奈,只好这样了。不过写入自定义字符确实是件不错的乐趣。
补充内容:
图片暂时无法上传,看了很多开发板的原理图,我发现高明的设计不在于用了什么高端的芯片,而是精明的设计,虽然我还不具备这样的能力,也不知道我理解的怎么样?先写下我的思路。
部分程序预览:
- #include"config.h"
- uchar const pic[8][8]={
- {0x04,0x0E,0x15,0x04,0x04,0x04,0x04,0x00},//↑
- {0x00,0x04,0x04,0x04,0x04,0x15,0x0E,0x04},//↓
- {0x00,0x04,0x08,0x1F,0x08,0x04,0x00,0x00},//←
- {0x00,0x04,0x02,0x1F,0x02,0x04,0x00,0x00},//→
- {0x04,0x04,0x0A,0x1F,0x1F,0x0A,0x04,0x04},//占位符
- {0x00,0x1B,0x1B,0x1B,0x1B,0x1B,0x1B,0x00},/*暂停*/
- {0x11,0x13,0x15,0x19,0x15,0x13,0x11,0x00},/*后退*/
- {0x11,0x19,0x15,0x13,0x15,0x19,0x11,0x00},/*"前进*/
- };
- uchar const pic1[8][8]={
- {0x00,0x0E,0x1F,0x11,0x11,0x11,0x11,0x1F},/*电量空*/
- {0x00,0x0E,0x1F,0x11,0x11,0x11,0x1F,0x1F},/*一格电*/
- {0x00,0x0E,0x1F,0x11,0x11,0x1F,0x1F,0x1F},/*2格电*/
- {0x00,0x0E,0x1F,0x11,0x1F,0x1F,0x1F,0x1F},/*3格电*/
- {0x00,0x0E,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},/*4格电满电*/
- {0x00,0x0F,0x09,0x0D,0x0B,0x09,0x11,0x00},/*凡*/
- {0x11,0x0E,0x1F,0x11,0x1F,0x04,0x1B,0x04},/*机器人*/
- {0x00,0x1F,0x0A,0x0A,0x0A,0x11,0x00,0x00},/*pi*/
- };
- //音量大小设置1-8
- uchar const pic3[8][8]={
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f},/*"未命名文件",1*/
- {0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x1f},/*"未命名文件",2*/
- {0x00,0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f},/*"未命名文件",3*/
- {0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f},/*"未命名文件",4*/
- {0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f},/*"未命名文件",5*/
- {0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f},/*"未命名文件",6*/
- {0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f},/*"未命名文件",7*/
- {0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f},/*"未命名文件",8*/
- };
- uchar flag,flag1,flag2;
- uchar flag3,flag4;
- //extern void read_xyz_normol();
- void extern_1m()
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- if (CALBC1_1MHZ==0xFF) // If calibration constant erased
- {
- while(1); // do not load, trap CPU!!
- }
- DCOCTL = 0; // Select lowest DCOx and MODx settings
- BCSCTL1 = CALBC1_1MHZ;
- DCOCTL = CALDCO_1MHZ; // Load 8MHz constants
- }
- void sendbyte(uchar byte)
- {
- uchar c,num;
- num=byte;
- for(c=0;c<8;c++)
- {
- P1OUT&=~0x80;
- CLK0;
- P1OUT|=num&0x80; //(0x80即十进制的128, 二进制的10000000 按位发送
- CLK1;
- num<<=1;
- }
- }
- // - - 向LCD1602写指令
- void LCD_write_command(uchar dat)
- {
- LCD_RS_LOW; // - - 指令
- //LCD_RW_LOW; // - - 写入
- sendbyte(dat);
- //delay_us(10);
- LCD_EN_HIGH; // - - 允许
- //delay_us(1);
- LCD_EN_LOW;
- }
- // - - 向LCD1602写数据
- void LCD_write_data(uchar dat)
- {
- LCD_RS_HIGH;// - - 数据
- //LCD_RW_LOW;// - - 写入
- sendbyte(dat);
- //delay_us(10);
- LCD_EN_HIGH;// - - 允许
- //delay_us(1);
- LCD_EN_LOW;
- }
- // - - 设置显示位置
- void LCD_set_xy(uchar x,uchar y)
- {
- uchar address;
- if(y==1)
- {
- address=0x80+x; // - - 第一行位置
- } else {
- address=0xc0+x; // - - 第二行位置
- }
- // LCD_delay_10us(1);
- LCD_write_command(address);
- }
- // - - 显示一个字符函数
- void LCD_disp_char(uchar x,uchar y,uchar dat) // - - LCD_disp_char(0,1,0x38); // - - 显示8
- {
- LCD_set_xy(x,y);
- // LCD_delay_10us(1);
- LCD_write_data(dat);
- }
- // - - 显示一个字符串函数
- void LCD_disp_string(uchar x,uchar y,char *s)
- {
- LCD_set_xy(x,y);
- // LCD_delay_10us(1);
- while(*s!='\0')
- {
- LCD_write_data(*s);
- s++;
- }
- }
- void LCD_init(void)
- {
- RSEN_INIT;
- CLKDAT_INIT;
- LCD_READ_DIR;
- LCD_write_command(0x38); // - - 设置8位格式,2行,5x7
- //delay_us(20);
- LCD_write_command(0x0c); // - - 整体显示,关光标,不闪烁
- //delay_us(20);
- LCD_write_command(0x06); // - - 设定输入方式,增量不移位
- //delay_us(20);
- LCD_write_command(0x01); // - - 清除屏幕显示
- //delay_us(200);
- }
- void lcd1602_write_pic(unsigned char add,uchar const *pic_num)
- {
- unsigned char i;
- add=add<<3;
- for(i=0;i<8;i++)
- {
- LCD_write_command(0x40|add+i);
- LCD_write_data(*pic_num++);
- }
- }
- void lcd1602_write_pic_all()//显示跳变所有显示
- {
- uchar i;
- switch(flag)
- {
- case 0:flag=1;break;
- case 1:flag=0;break;
- default:flag=0;break;
- }
- if(flag==0)
- {
- for(i=0;i<8;i++)
- lcd1602_write_pic(i,pic);
- LCD_disp_string(0,1," ");
- for(i=0;i<8;i++)
- LCD_disp_char(i,0,i);
- }
- else{
- for(i=0;i<8;i++)
- lcd1602_write_pic(i,pic1);
- LCD_disp_string(0,0," ");
- for(i=0;i<8;i++)
- LCD_disp_char(i,1,i);
- }
- }
- void lcd1602_write_pic_baterry() //自定义数据1
- {
- if(flag>=5)
- flag=0;
- lcd1602_write_pic(1,pic1[flag]);
- LCD_disp_char(15,0,1);
- }
- void lcd1602_write_pic_robot() //自定义数据0
- {
- if(++flag1>=15)
- {
- flag1=0;
- LCD_disp_char(14,0,' ');
- }
- else
- LCD_disp_char(flag1-1,0,' ');
- lcd1602_write_pic(0,pic1[6]);
- LCD_disp_char(flag1,0,0);
- }
- void lcd1602_write_pic_dian(unsigned char add) //自定义数据0
- {
- if(++flag2==8)
- flag2=0;
- lcd1602_write_pic(add,pic[flag2]);
- LCD_disp_char(10,1,add);
- }
- void lcd1602_write_pic_volum(unsigned char add)
- {
- if(flag3>=8)
- flag3=0;
- lcd1602_write_pic(add,pic3[flag3]);
- LCD_disp_char(15,1,add);
- }
- void lcd1602_write_pic_qu(unsigned char add)
- {
- if(++flag4>=8)
- flag4=0;
- uchar i;
- for(i=0;i<4;i++)
- {
- lcd1602_write_pic(add+i,pic3[(i+flag4)%8]);
- LCD_disp_char(i,1,add+i);
- }
- }
- void sendbyte_4wire(char date)
- {
- uchar i;
- for(i=0;i<2;i++)
- {
- LCD_EN_HIGH;
- sendbyte(date);
- LCD_EN_LOW;
- date<<=4;
- }
- }
- void lcd_4wire(uchar cd,uchar date)
- {
- if(cd==0)
- LCD_RS_LOW;
- else LCD_RS_HIGH;
- sendbyte_4wire(date);
- }
- void lcd_4wire_init()
- {
- RSEN_INIT;
- CLKDAT_INIT;
- lcd_4wire(0,0x28);
- lcd_4wire(0,0x0f);
- lcd_4wire(0,0x06);
- lcd_4wire(0,0x01);
- lcd_4wire(0,0x80);
- }
- void lcd_4wire_setxy(uchar x,uchar y)
- {
- uchar address;
- if(y==1)
- {
- address=0x80+x; // - - 第一行位置
- } else {
- address=0xc0+x; // - - 第二行位置
- }
- // LCD_delay_10us(1);
- lcd_4wire(0,address);
- }
- void LCD_4wire_disp_char(uchar x,uchar y,uchar dat) // - - LCD_disp_char(0,1,0x38); // - - 显示8
- {
- lcd_4wire_setxy(x,y);
- // LCD_delay_10us(1);
- lcd_4wire(1,dat);
- }
- // - - 显示一个字符串函数
- void lcd_4wire_disp_string(uchar x,uchar y,char *s)
- {
- lcd_4wire_setxy(x,y);
- // LCD_delay_10us(1);
- while(*s!='\0')
- {
- lcd_4wire(1,*s);
- s++;
- }
- }
- void init_TA()
- {
- TACCTL0 = CCIE; // TACCR0 interrupt enabled
- TACCR0 = 50000; //400ms中断一次
- TACTL = TASSEL_2 + MC_1+ID_3; // SMCLK, upmode
- }
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void Timer_A (void)
- {
- // LCD_disp_char(0,0,'F');
- // lcd_4wire_disp_string(0,0,"wo shi");
- // lcd1602_write_pic_all();
- lcd1602_write_pic_baterry();
- lcd1602_write_pic_robot();
- lcd1602_write_pic_dian(2);
- lcd1602_write_pic_volum(3);
- lcd1602_write_pic_qu(4);
- //read_xyz_normol();
- }
|
|