📄 ps2small.c
字号:
#include<iom128v.h>
#include<macros.h>
#define DATA1 0x20
#define fosc 16000000 //晶振8MHZ
#define baud 9600 //波特率
#define N 10
// #define PHASE_N 32
// #define FREQ_M 32
//#define SYS_CLOCK 120000000
#define N2 35.79109
void initail_ocmj(void);
void write_a(unsigned char x,unsigned char y,unsigned char asc_p);
void ocmj_write(unsigned char adata);
void DisplayListChar(unsigned char x,unsigned char y);
void write_lcd_word(unsigned char cmd ,unsigned char x,unsigned char y,unsigned char *pp);
void send_data(unsigned char *arry);
void initial_port(void);
unsigned char dat=0,flag=0,sign=0;
unsigned char Key_UP=0,count=0,sbuff[8];
const unsigned char table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,
0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};//,0x47,0x48};
unsigned char arry[][2]={
0x70, '0',
0x69, '1',
0x72, '2',
0x7A, '3',
0x6B, '4',
0x73, '5',
0x74, '6',
0x6C, '7',
0x75, '8',
0x7D, '9',
0x71, 'D',
0x5a, 'E'
};
/***********************************
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);//8bit+1bit stop
UBRR0L=(fosc/16/(baud+1))%256;
UBRR0H=(fosc/16/(baud+1))/256;
UCSR0B =(1<<RXEN0)|(1<<TXEN0);//RXCEN TXCEN
}
/****************************/
//键盘输入数字转换
unsigned long int convert(unsigned char *arry,unsigned char length)
{
unsigned char i;
unsigned long int num;
num = arry[0];
for(i = 1; i < length; i++)
{
num *= 10;
num += arry[i];
}
return num;
}
/********************************************************/
//频率,控制字转换
void calcultor(unsigned long int frequn_value)
{
unsigned long int fm;
char i;
fm=(float)(frequn_value) * N2;
for(i=8;i>=1;i--)
{
sbuff[i-1]=fm&0x0f;
fm>>=4;
}
}
/*
void putchar0(unsigned char c)
{
while (!(UCSR0A&(1<<UDRE0)));
UDR0=c;
}*/
/***********************************/
#pragma interrupt_handler PS2_KEY:6
void PS2_KEY(void)
{
if((count>0)&&(count<9))
{
dat=dat>>1;
if(PINE&DATA1)
dat|=0x80;
}
count++;
while(!(PINE&0X10));
if(count>10)
{
count=0;
flag=1;
EIMSK=0x00;
}
}
/*****************************/
unsigned char Decode(unsigned char ScanCode)
{
unsigned char TempCyc,KEY;
if (!Key_UP) //当键盘松开时
switch (ScanCode)
{
case 0xF0 : Key_UP=1; break; // 当收到0xF0,Key_UP置1表示断码开始
case 0xe0 : break;
default:
for(TempCyc = 0;(arry[TempCyc][0]!=ScanCode)&&(TempCyc<11); TempCyc++); //查表显示
if(arry[TempCyc][0] == ScanCode)
{
sign=1;
KEY=arry[TempCyc][1] ;
}
break;
}
else
Key_UP = 0;
flag = 0; //标识字符处理完了
if(sign)
{
sign=0;
return KEY;
}
else
return('%');
}
/***********************************/
void initial_Exinterup(void)
{
DDRE&=~BIT(4);
DDRE&=~BIT(5);
PORTE|=BIT(4);
PORTE|=BIT(5);
SREG=0X80;
MCUCR=0X00;
EICRB=0X00;
EIMSK=0X10;
}
/*****************************************/
void main(void)
{
unsigned char i,j,sdata,buff[N],arry_data[8],arry11_data[5]={0x09};
unsigned long int sum;
i=0;
initail_ocmj();
initial_Exinterup();
ocmj_write(0xf4);
initial_port();
write_lcd_word(0xf0,0,0,"输入频率");
send_data(arry11_data); ////所有寄存器清零
write_lcd_word(0xf0,0,2,"频率控制字");
while(1)
{
if(flag) /////1 if start
{
sdata = Decode(dat);
if(sdata=='D')
{
sdata='%';
i=0;
DisplayListChar(0,16); ///clr lcd
}
if((sdata!='%')&&(sdata!='E')) //3if start
{
write_a(i,16,sdata);
buff[i]=sdata-0x30;
i++;
} ///3if end
if( sdata=='E') ///4if start
{
sum=convert(buff,i); ///键盘直转化为频率
calcultor(sum); ////转化8位的16进制显示 0x0x;
DisplayListChar(0,48);
for(j=0;j<8;j++)
{
arry_data[j]=sbuff[j];
write_a(j,48,table[arry_data[j]]); ///显示频率控制字
if(j%2)
{
arry11_data[j/2+1]=(arry_data[j-1]<<4)|arry_data[j];
} ////8个0x0x转化为4个8位频率控制字
}
send_data(arry11_data); ///输出32位频率控制字
}///4if end
} ///1 if end
else
EIMSK=0x10;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -