📄 master_sci.c
字号:
#include <at898252.h>
#include <intrins.h>
sfr IPH = 0xB7; //
sbit S0 = P3^2; //SPI通讯中从机选择脚0
sbit S1 = P3^3; //SPI通讯中从机选择脚1
sbit DIS = P3^4; //显示内容使能脚
sbit DUAN = P3^5; //位显示使能脚
sbit KEY0 = P2^0; //按键
sbit KEY1 = P2^1;
sbit KEY2 = P2^2;
sbit KEY3 = P2^3;
sbit KEY4 = P2^4;
sbit KEY5 = P2^5;
sbit KEY6 = P2^6;
sbit KEY7 = P2^7;
#define T2_H 0xf8 //定时器2在24MHz晶振下定时时间为1mS
#define T2_L 0x30
#define T1_V 0xff //定时器1在24MHz晶振下设定波特率为1152000bit/S
unsigned char code dis_byte[17] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00};
unsigned char code dis_bit[8] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
unsigned int i;
unsigned char first_key, sec_key, last_key, key;
unsigned char dis_num;
unsigned char dis_buf[8] = {16, 16, 16, 16, 16, 16, 16, 16};
unsigned char times;
unsigned char scan_key_time;
unsigned char sendtimes;
unsigned char se_dat, re_dat, error;
#define send_char_time() {_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();}
#define delay9() {_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();}
#define delay38() {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();\
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();\
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();\
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
void init(void){
IE = 0; //关闭所有中断
P0 = 255; P1 = 255; P3 = 0xf3; P2 = 255; //初始化输入输出端口
/*********SPI设置****************/
// SPCR = 0x11; //CPOL = 0,CPHA = 0,master mode,先高位后低位,6Mbit/s /0x51
// SPCR |= 0x40;
// SPSR &= 0x1c; //clear interrupt mark
// ES = 1;
/*********SCI设置****************/
//设定定时器1为自动再装入模式,定时器0为16位计数器,两定时器均不允许中断
TMOD = 0x20;
TL1 = T1_V; TH1 = T1_V; TR1 = 1; ET1 = 0;
PCON |= 0x80; SCON = 0x50; ES = 1; //设定SMOD为1,串行通讯模式1,允许串行中断
//定时器2设置
RCAP2H = T2_H;
RCAP2L = T2_L;
T2CON = 0X04;
T2MOD &= 0XFF;
ET2 = 1;
IPH = 0x10; //SPI中断优先级高
IP = 0x10;
EA = 1; //开总中断
}
//定时器2的中断服务程序
void system_time(void) interrupt 5 using 1{
TF2 = 0;
if(!scan_key_time--){
first_key = 0; //
//扫描键盘
P2 = 255; KEY4 = 0;
if(!KEY0) first_key = 1;
else if(!KEY1) first_key = 2;
else if(!KEY2) first_key = 3;
else if(!KEY3) first_key = 4;
else{
KEY4 = 1; KEY5 = 0;
if(!KEY0) first_key = 5;
else if(!KEY1) first_key = 6;
else if(!KEY2) first_key = 7;
else if(!KEY3) first_key = 8;
else{
KEY5 = 1; KEY6 = 0;
if(!KEY0) first_key = 9;
else if(!KEY1) first_key = 10;
else if(!KEY2) first_key = 11;
else if(!KEY3) first_key = 12;
else{
KEY6 = 1; KEY7 = 0;
if(!KEY0) first_key = 13;
else if(!KEY1) first_key = 14;
else if(!KEY2) first_key = 15;
else if(!KEY3) first_key = 16;
KEY7 = 1;
}
}
}
if((sec_key != first_key) && (first_key != last_key)){ //有新按键
last_key = first_key;
key = first_key;
times = 50;
}
sec_key = first_key;
if(!times--){sec_key = 0; last_key = 0;} //如按键在一定时间内还未松开,则认为是一新按键
scan_key_time = 10; //10mS扫描一次键盘
}
if(first_key == 0x00){
P0 = dis_byte[dis_buf[dis_num]]; DIS = 1; DIS = 0; //显示位码
P0 = dis_bit[dis_num]; DUAN = 1; DUAN = 0; //显示段码
dis_num++;
if(dis_num == 8) dis_num = 0;
}
}
void send_sci(void) interrupt 4 using 1{
if(TI) TI = 0;w19851122j
if(RI){
dis_buf[7] = SBUF;
RI = 0;
}
if(SPSR & 0x80){ACC = SPSR; re_dat = SPDR;}
}
void receive_sci(void) interrupt 4 using 3{
if(TI) TI=0;
if(RI){
dis_buf[2] = SBUF;
RI=0;
}
if(SPSR & 0x80){ACC = SPSR; re_dat = SPDR;}
}
void main(void){
init();
dis_num = 0;
times = 10;
while(1){
if(key){dis_buf[0] = key; SBUF = key; key = 0;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -