51单片机驱动PS2摇杆1602显示程序(1602用6根线驱动- /********************************************************
- 功 能:51单片机通过PS2摇杆模块驱动4个LED
- 并用1602显示
- (1602使用6根线驱动)
- 单片机:STC12C2052AD
- 晶 振:12M
- 注 释:摇杆的x轴接单片机P10.摇杆的Y轴接单片机P11.
- Z轴接P17
- 作 者:苏义江改编自网络
- 时 间:2016-10-12
- *******************************************************/
- #include<stc12c2052ad.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //#define TINER_HIGH 0XFF//24M
- //#define TINE_LOW 0XD8
- #define TINER_HIGH 0XFf//12M 100us
- #define TINE_LOW 0X9c
- #define ADC_POWER 0x80 //定义ADC电源常量
- #define ADC_FLAG 0x10 //定义ADC转换结束标志位
- #define ADC_START 0x08 //定义ADC转换开始控制位
- #define ADC_SPEEDLL 0x00 //1080个时钟转换一次
- #define ADC_SPEEDL 0x20 //810个时钟转换一次
- #define ADC_SPEEDH 0x40 //540个时钟转换一次
- #define ADC_SPEEDHH 0x60 //270个时钟转换一次
- sbit led_red =P1^6;
- sbit led_green =P1^5;
- sbit led_blue =P1^4;
- sbit led_yellow=P1^3;
- sbit z_zhou =P1^7;//Z轴
- uchar red=0x00,green=0x00,blue=0x00,yellow=0x00;
- uchar counter=0;
- typedef struct
- {
- char x;
- char y;
- char drift;
- }pos;
- pos rocker;
- void delay(uint t)
- {
- uint y,i; //定义变量
- for(y=t;t>0;t--) //如果t大于0,t减1(外层循环)
- for(i=600;i>0;i--); //i等于600,如果i大于0,i减1
- }
- ////////STC12C5608AD转换定义在哪个IO口进行并转换//////////////
- uchar Read5608AD (uchar CHA)
- {
- uchar AD_FIN=0; //存储A/D转换标志;若在函数外定义此变量则不能得到连续变化的模拟量的显示
- /////////以下为ADC初始化程序////////////
- CHA &= 0x03;
- //选择ADC的8个接口中的一个(0000 0011 清0高6位)
- ADC_CONTR = ADC_SPEEDHH; //ADC转换的速度(0XX0 0000 其中XX控制速度,请根据数据手册设置)
- _nop_();
- ADC_CONTR |= CHA; //选择A/D当前通道
- _nop_();
- ADC_CONTR |= ADC_POWER; //启动A/D电源(1000 0000)
- delay(1); //使输入电压达到稳定(1ms即可?
- /////以下为ADC执行程序///////////////////
- ADC_CONTR |= ADC_START; //启动A/D转换(0000 1000 令ADCS = 1)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while (AD_FIN ==0) //等待A/D转换结束
- {
- AD_FIN = (ADC_CONTR & ADC_FLAG); //0001 0000测试A/D转换结束否
- }
- ADC_CONTR &= 0xE7; //1111 0111 清ADC_FLAG位, 关闭A/D转换,
- return (ADC_DATA); //返回A/D转换结果(8位)
- }
- /*
- ///////////STC12C2052AD转换//////////////
- uchar Read2052AD (uchar CHA)
- {
- uchar AD_FIN=0; //存储A/D转换标志;若在函数外定义此变量则不能得到连续变化的模拟量的显示
- //////以下为ADC初始化程序/////////////////
- CHA &= 0x07; //选择ADC的8个接口中的一个(0000 0111 清0高5位)
- ADC_CONTR = 0x60; //ADC转换的速度(0XX0 0000 其中XX控制速度,请根据数据手册设置)
- _nop_();_nop_();_nop_();
- ADC_CONTR |= CHA; //选择A/D当前通道
- _nop_();_nop_();_nop_();
- ADC_CONTR |= 0x80; //启动A/D电源
- delay(2); //使输入电压达到稳定(1ms即可?
- //////////以下为ADC执行程序////////////////////
- ADC_CONTR |= 0x08; //启动A/D转换(0000 1000 令ADCS = 1)
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while (AD_FIN ==0)
- { //等待A/D转换结束
- AD_FIN = (ADC_CONTR & 0x10); //0001 0000测试A/D转换结束否
- }
- ADC_CONTR &= 0xE7; //1111 0111 清ADC_FLAG位, 关闭A/D转换,
- return (ADC_DATA); //返回A/D转换结果(8位)
- }
- */
- pos rocker_read(int drift)
- {
- pos buffer;
- buffer.x=128-Read5608AD (1);
- buffer.y=128-Read5608AD (0);
- buffer.drift=drift;
- return (buffer);
- }
- void timer0_init()
- {
- TMOD=0X01;
- TH0=TINER_HIGH ;
- TL0=TINE_LOW;
- ET0=1;
- TR0=1;
- EA=1;
- }
- void timer0_irpt() interrupt 1
- {
- TH0=TINER_HIGH ;
- TL0=TINE_LOW;
- counter++;
- if(counter<red) led_red=0; else led_red=1;
- if(counter<green) led_green=0; else led_green=1;
- if(counter<blue) led_blue=0; else led_blue=1;
- if(counter<yellow) led_yellow=0; else led_yellow=1;
- }
- //6根线驱动LCD1602接口定义
- #define LCD_DB P2 // - - P2 = DB4~DB7
- sbit LCD_RS=P2^2; // - - p2.2 = RS
- //sbit LCD_RW=P0^6; // - - p2.1 = RW 接地
- sbit LCD_E=P2^3; // - - p2.3 = E
- // - - 定义子程序函数
- void LCD_init(void); // - - 初始化LCD1602函数
- void LCD_write_H4bit_command(uchar dat);
- void LCD_write_L4bit_command(uchar dat);
- void LCD_write_4bit_command(uchar command); // - - 向LCD1602写指令函数
- void LCD_write_4bit_data(uchar dat); // - - 向LCD1602写数据函数
- void LCD_set_xy(uchar x,uchar y); // - - 设置LCD1602显示位置 X(0-16),y(1-2)
- void LCD_disp_char(uchar x,uchar y,uchar dat); // - - 在LCD1602上显示一个字符
- void LCD_disp_string(uchar X,uchar Y,uchar *s); // - - 在LCD1602上显示一个字符串
- //void LCD_check_busy(void);//检查忙函数。我没用到此函数,因为通过率极低。
- void LCD_delay_10us(uint n); // - - 10微秒的延时子程序
- void LCD_delay_50us(uint n); // - - 50微秒的延时子程序
- void LCD_delay_10us(uint n) // - - 10微秒的延时子程序
- {
- uint i,j;
- for(i=n*10;i>0;i--) // - - 晶振及单片机修改设置
- for(j=2;j>0;j--);
- }
- void LCD_delay_50us(uint n) // - - 50微秒的延时子程序
- {
- uint i,j;
- for(i=n*10;i>0;i--) // - - 晶振及单片机修改设置
- for(j=22;j>0;j--);
- }
- // - - 初始化LCD1602
- void LCD_init(void)
- {
- LCD_delay_50us(50);
- LCD_RS=0; // - - 指令
- // LCD_RW=0; // - - 写入
- LCD_E=0; // - - 使能
- LCD_write_L4bit_command(0x03); // - - 设置4位格式,2行,5x7
- LCD_delay_50us(20);
- LCD_write_L4bit_command(0x03); // - - 设置4位格式,2行,5x7
- LCD_delay_50us(20);
- LCD_write_L4bit_command(0x02); // - - 设置4位格式,2行,5x7
- LCD_delay_50us(2);
- LCD_write_4bit_command(0x28); // - - 设置4位格式,2行,5x7
- LCD_delay_10us(10);
- LCD_write_4bit_command(0x28); // - - 设置4位格式,2行,5x7
- LCD_delay_10us(10);
- LCD_write_4bit_command(0x0c); // - - 整体显示,关光标,不闪烁
- LCD_delay_10us(10);
- LCD_write_4bit_command(0x06); // - - 设定输入方式,增量不移位
- LCD_delay_10us(10);
- LCD_write_4bit_command(0x01); // - - 清除屏幕显示
- LCD_delay_50us(40);
- }
- //********************************
- // - - 向LCD1602写指令
- void LCD_write_4bit_command(uchar dat)
- {
- LCD_delay_10us(50);
- LCD_RS=0; // - - 指令
- // LCD_RW=0; // - - 写入
- LCD_write_H4bit_command(dat);
- LCD_write_L4bit_command(dat);
- }
- // - - 向LCD1602写高四位指令
- void LCD_write_H4bit_command(uchar dat)
- {
- LCD_delay_10us(250);
- LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
- LCD_delay_10us(50);
- LCD_E=1; // - - 允许
- LCD_delay_10us(250);
- LCD_E=0;
- }
- // - - 向LCD1602写低四位指令
- void LCD_write_L4bit_command(uchar dat)
- {
- dat<<=4;
- LCD_delay_10us(50);
- LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
- LCD_delay_10us(50);
- LCD_E=1; // - - 允许
- LCD_delay_10us(250);
- LCD_E=0;
- }
- // - - 向LCD1602写数据
- void LCD_write_4bit_data(uchar dat)
- {
- LCD_delay_10us(50);
- LCD_RS=1; // - - 数据
- // LCD_RW=0; // - - 写入
- LCD_write_H4bit_command(dat);
- LCD_write_L4bit_command(dat);
- }
- // - - 设置显示位置
- void LCD_set_xy(uchar x,uchar y)
- {
- uchar address;
- if(y==1)
- {
- address=0x80+x; // - - 第一行位置
- }
- else
- {
- address=0xc0+x; // - - 第二行位置
- }
- LCD_delay_10us(50);
- LCD_write_4bit_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(5);
- LCD_write_4bit_data(dat);
- }
- // - - 显示一个字符串函数
- void LCD_disp_string(uchar x,uchar y,uchar *s)
- {
- LCD_set_xy(x,y);
- LCD_delay_10us(5);
- while(*s!='\0')
- {
- LCD_write_4bit_data(*s);
- s++;
- }
- }
- void display(uint num )//显示数字
- {
- uchar qian,bai,shi,ge;
- qian=num/1000;
- bai=num%1000/100;
- shi=num%100/10;
- ge=num%10;
- LCD_set_xy(3,0);
- LCD_write_4bit_data(qian+0x30);
- LCD_write_4bit_data(bai+0x30);
- LCD_write_4bit_data(shi+0x30);
- LCD_write_4bit_data(ge+0x30);
- }
- void display1(uint num )//显示数字
- {
- uchar qian,bai,shi,ge;
- qian=num/1000;
- bai=num%1000/100;
- shi=num%100/10;
- ge=num%10;
- LCD_set_xy(11,0);
- LCD_write_4bit_data(qian+0x30);
- LCD_write_4bit_data(bai+0x30);
- LCD_write_4bit_data(shi+0x30);
- LCD_write_4bit_data(ge+0x30);
- }
- void main()
- {
- LCD_init();
- timer0_init();
- LCD_disp_string(0,0,"X=");
- LCD_disp_string(8,0,"Y=");
- LCD_disp_string(4,1,"Z=");
- while(1)
- {
- if(z_zhou==0)
- {
- LCD_disp_string(7,1,"O N");
- }
-
- else
- {
- // LCD_write_4bit_command(0x01);
- LCD_disp_string(7,1,"OFF");
- }
- rocker=rocker_read(0);
- if(rocker.x<-3)
- {
- rocker.x=-rocker.x+1;
- blue=rocker.x<<1;
- display(blue);
- }
- else
- {
- if(rocker.x>3)
- {
- green=rocker.x<<1;
- display(green);
- }
- else
- {
- green=blue=0;
- }
- }
- if(rocker.y<-3)
- {
- rocker.y=-rocker.y+1;
- yellow=rocker.y<<1;
- display1(yellow);
- }
- else
- {
- if(rocker.y>3)
- {
- red=rocker.y<<1;
- display1(red);
- }
- else
- {
- yellow=red=0;
- }
- }
- }
- }
复制代码
)
|