msp430,AD转换1602显示仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- /******************************************************************
- * 实验名称:LCD1602 显示仿真 (MSP430F249)
- *
- * 实验说明:
- * 通设置系统主时钟(MCLK=8Mhz ) 、辅助时钟(ACLK=8Mhz )、
- * 子系统时钟(SMCLK=8Mhz )
- * 编译软件:Embedded Workbench 5.3
- *
- *仿真软件:proteus7.7 sp 2
- ******************************************************************/
- #include <MSP430x24x.h>
- #include <string.h>
- #define uchar unsigned char
- #define uint unsigned int
- /**************定义接口************************/
- #define LCDIO P1OUT
- #define LCD1602_RS_1 P2OUT|=1
- #define LCD1602_RS_0 P2OUT&=~1
- #define LCD1602_RW_1 P2OUT|=2
- #define LCD1602_RW_0 P2OUT&=~2
- #define LCD1602_EN_1 P2OUT|=4
- #define LCD1602_EN_0 P2OUT&=~4
- /**************定义函数************************/
- void LCD_write_command(unsigned char command);//写入指令函数
- void LCD_write_dat( unsigned char dat);//写入数据函数
- void LCD_set_xy( unsigned char x, unsigned char y );//设置显示位置函数
- void LCD_dsp_char( unsigned char x,unsigned char y, char dat);//显示一个字符函数
- void LCD_dsp_string(unsigned char X,unsigned char Y,const char *s);//显示字符串函数
- void LCD_init(void);//初始化函数
- void delay_nms(unsigned int n);//延时函数
- /********************************************/
- void delayms(uint t)
- {
- uint i;
- while(t--)
- for(i=1330;i>0;i--);//进过参数的调整
- }
- /*******检查忙函数*************/
- void LCD_check_busy() //实践证明,在我的LCD1602上,检查忙指令通过率极低,以
- { //至于不能正常使用LCD。因此我没有再用检查忙函数。而使
-
- P1DIR=0x00;
- LCDIO=0xff;
- LCD1602_RS_0; //要用200次循环便能完成。
- LCD1602_RW_1;
- LCD1602_EN_1;
- while(P1IN&0x80);
- LCD1602_EN_0;
- P1OUT=0x00;
- P1DIR=0xFF;
- }
- /******************************/
- /**************写指令函数********************************/
- void LCD_write_command(unsigned char command)
- {
-
- //LCD_check_busy(); //加上这句仿真无法通过
-
- LCD1602_RS_0;
- LCDIO=command;
- LCD1602_EN_1;
-
- //delayms(1);
- LCD1602_EN_0;
- delayms(1);
-
- }
- /***************************************************/
- /****************写数据函数************************/
- void LCD_write_dat( unsigned char dat)
- {
- //LCD_check_busy(); //加上这句仿真无法通过
- LCD1602_RS_1;
- LCDIO=dat;
- LCD1602_EN_1;
-
- //delayms(1);
- LCD1602_EN_0;
- delayms(1);
- LCD1602_RS_0;
- }
- /****************************************************/
- /***************设置显示位置**************************/
- void LCD_set_xy( unsigned char x, unsigned char y )
- {
- unsigned char address;
- if (y == 1)
- address = 0x80+x;
- else if (y == 2)
- {
- address=0x80+0x40+x;
-
- }
- LCD_write_command(address);
- }
- /***************************************************/
- /****************显示一个字符**********************/
- void LCD_dsp_char( unsigned char x,unsigned char y, char dat)
- {
- LCD_set_xy( x, y );
- LCD_write_dat(dat);
- }
- /**********************************************/
- /***************显示字符串函数***************/
- void LCD_dsp_string(unsigned char X,unsigned char Y,const char *s)
- {
- uchar len,List;
- len=strlen(s);
- LCD_set_xy( X, Y );
- for(List=0;List<len;List++)
- //LCD_dsp_char(X+List,Y,s[List]);
- LCD_write_dat(s[List]);
-
- }
- /***********************************************/
- /********** 延时**********************/
- void delay_nms(unsigned int n)
- {
- unsigned int i=0,j=0;
- for (i=n;i>0;i--)
- for (j=0;j<10;j++);
- }
- /**************************************/
- /************初始化函数****************/
- void LCD_init(void)
- {
- LCD1602_RW_0;
- LCD1602_EN_0;
- //CLEARSCREEN;//clear screen
- LCD_write_command(0x38);//set 8 bit data transmission mode
- delayms(1);
- LCD_write_command(0x38);//set 8 bit data transmission mode
- delayms(1);
- LCD_write_command(0x38);//set 8 bit data transmission mode
- delayms(1);
- LCD_write_command(0x06);//open display (enable lcd display)
- delayms(1);
- LCD_write_command(0x0C);//set lcd first display address
- delayms(1);
- LCD_write_command(0x01);//clear screen
- delayms(1);
- //LCD_write_command(0x80);//clear screen
- //delayms(1);
- }
- /****************************************************/
- void main(void)
- {
-
- WDTCTL=WDTPW + WDTHOLD; // 关闭看门狗
- P1DIR=0xFF; // 设置方向
- P1SEL=0; // 设置为普通I/O 口
-
- P2DIR=0xFF; // 设置方向
- P2SEL=0; // 设置为普通I/O 口
-
- P2OUT=0x00;
- P1OUT=0x00;
- delayms(200);
- LCD_init();
-
- while(1)
- {
-
- LCD_dsp_string(1,1," LCD TEST ");//在第一行第一列显示"LCD TEST"
- LCD_dsp_string(1,2," SUCCSEEE! ");//在第一行第一列显示"LCD TEST"
- _BIS_SR(LPM0_bits+ GIE);//进入低功耗睡眠模式
- }
-
- }
复制代码
所有资料51hei提供下载:
LCD1602.rar
(243.5 KB, 下载次数: 59)
|