⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个在51单片机下PS/2键盘的应用
💻 C
字号:
/*http://www.wzlab.com *小型终端 LCD:6963  CPU:atmega16 ps2键盘 7.3728晶振 * */#include <compat/ina90.h>#include <avr/delay.h>#include "6963c.h"#include "keyboard.h"#define set_bit(x,y)  (x|=(1<<y))	//对位操作的宏定义#define clr_bit(x,y)  (x&=~(1<<y))#define cpl_bit(x,y)   (x^=(1<<y))#define RT	0x0D  //定义回车符#define UART_CAP	500unsigned char uart_buf[UART_CAP];volatile unsigned char uart_head=0;unsigned char uart_tail=0;/*				微妙级延时			*/void delay_us(unsigned int time){	unsigned int i;  	for(i=0;i<time;i++);}/*	  		    毫秒级延时程序			*/	 void delay_ms(unsigned int time){ 	unsigned int i;	for(i=0;i<time;i++){		delay_us(1000);	}}/*		UART接受中断			*/SIGNAL(SIG_UART_RECV){	unsigned char temp_buf;	temp_buf=UDR;	uart_buf[uart_head]=temp_buf;	uart_head=(uart_head<(UART_CAP-1))?(uart_head+1):0;}/*		从UART缓冲区读数据		*/int uart_getchar(void){    unsigned char temp;    if(uart_head==uart_tail){	return 0;    }    else{	temp=uart_buf[uart_tail];	uart_tail=(uart_tail<(UART_CAP-1))?(uart_tail+1):0;	return temp;	      }	     }/*		通过传口发送出字符		*/int uart_putchar(char c){	/*     	if(c=='\n'){		uart_putchar('\r');	}	loop_until_bit_is_set(UCSRA,UDRE);	UDR=c;	return 0;	*/	while (!(UCSRA&(1<<UDRE)));	UDR=c;	while (!(UCSRA&(1<<TXC)));	UCSRA|=(1<<TXC);	return 0;}/*			UART初始化				*/	void uart_init(void){    UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);//允许发送和接收    UBRRL=23;	//外部7.3728M晶振 9600bps    UBRRH=0;	    UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位 无校验位    UCSRA&=0xFD;}/*			初始化io口 		*/void init_devices(void){    DDRA  = 0xFF;    PORTA = 0xDF;    DDRB  = 0x00;    PORTC = 0x00; //m103 output only        DDRC  = 0x00;    PORTD = 0x40;    DDRD  = 0x83;    //stop errant interrupts until set up    cli(); //disable all interrupts        MCUCR = 0x00;    GICR  = 0x00;    TIMSK = 0x00; //timer interrupt sources    sei();	 //re-enable interrupts    //all peripherals are now initialised}int main(void){    unsigned char i,rchar;		//for循环,接收字符    unsigned int cp_offset;		//拷屏偏移量    unsigned int mem_point,mem_offset; 	//显存头指针,显存偏移量        init_devices();    keyboard_init();    uart_init();        fdevopen(uart_putchar,uart_getchar,0);	//重新定义标准输入输出    DDRD |=(1<<6);			//打开背光 背光有PD6控制    PORTD |=(1<<6);    lcm_init();				//初始化 液晶屏    cls_txt();				//清除文本方式 的现存    cls();				//清除图形方式的现存        cp_offset=0;    mem_offset=0;    mem_point=TEXT_ADD;        cursor_p(0,0);			//初始化光标    w2para(LC_ADD_POS,TEXT_ADD%256,TEXT_ADD/256);	//初始化显存指针        while(1)    {					rchar=(unsigned char)keyboard_getchar();								if(0!=rchar)				{						putchar(rchar);				}				rchar=(unsigned char)getchar();								if((0!=rchar)&&(0x0a!=rchar))				{								//接受字符是0或0x0A,都不处理	    	    			if(0x0d==rchar){	//回车符处理		if((mem_offset%30)==29){	//回车符在末尾处理		    w1para(LC_INC_WR,' '-32);		}		else{		    mem_offset+=30-mem_offset%30;	//回车符不在末尾		    w2para(LC_ADD_POS,(mem_point+mem_offset)%256,(mem_point+mem_offset)/256);		    mem_offset-=1;		}	    }	    else{		w1para(LC_INC_WR,rchar-32);	//输入字符不是回车	    }	    	    if(mem_offset<(30*LCD_LINE-1)){	//如果是新的一行,处理偏移量mem_offset		mem_offset+=1;	    }	    else{		mem_offset-=29;		if(mem_point<(TEXT_END_ADD)){	//清除随后两行内容为空		    mem_point+=30;		    w2para(LC_ADD_POS,(mem_point+30*(LCD_LINE))%256,(mem_point+30*(LCD_LINE))/256);    // 置地址指针		    w0para(LC_AUT_WR);      	// 自动写		    for(i=0;i<30;i++){   	// 清一屏						wdata(0x00);      	// 写数据,0x0		    }		    w0para(LC_AUT_OVR);      	// 自动写结束		    w2para(LC_ADD_POS,(mem_point+mem_offset)%256,(mem_point+mem_offset)/256);  	// 重置地址指针		    }		else{   //将要跳到现存开始  清除现存开始屏末两行		    mem_point=0;		    w2para(LC_ADD_POS,(30*(LCD_LINE-1))%256,(30*(LCD_LINE-1))/256);    // 置地址指针		    w0para(LC_AUT_WR);      	// 自动写		    for(i=0;i<60;i++){   	// 清一屏						//fnSTA3();//检查状态			wdata(0x00);      	// 写数据,0x0		    }		    w0para(LC_AUT_OVR);      	// 自动写结束		    w2para(LC_ADD_POS,30*(LCD_LINE-1)%256,30*(LCD_LINE-1)/256);  	// 重置地址指针		}		w2para(LC_TXT_STP,mem_point%256,mem_point/256);	    }	    	    cursor_p(mem_offset%30,mem_offset/30);	    if(mem_point>(TEXT_END_ADD-30*LCD_LINE-1)){ //到显存的末尾,拷贝一屏到开始		w2para(LC_ADD_POS,cp_offset%256,cp_offset/256);		if(0x0d==rchar){ //判断回车符		    if((cp_offset%30)==29){			w1para(LC_INC_WR,' '-32);		    }		    else{			if(0==cp_offset%30){ //回车符再行首			    cp_offset+=30;			    for(i=0;i<30;i++){				w1para(LC_INC_WR,' '-32);			    }			}else{			    while(0!=cp_offset%30){ //回车符不在行首				w1para(LC_INC_WR,' '-32);				cp_offset+=1;			    }			}		    }		}		else{		    w1para(LC_INC_WR,rchar-32);            //不是回车符		    cp_offset+=1;		}		w2para(LC_ADD_POS,(mem_point+mem_offset)%256,(mem_point+mem_offset)/256); //显示指针还原	    }	    else{		cp_offset=0;	    }	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -