16,577
社区成员
发帖
与我相关
我的任务
分享#include"stc8a8k64s4a12.h"
#include<intrins.h>
#define uint8_t
unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int
#define LED_wei P6
#define LED_duan P7
sbit left_up=P0^0;//左方+1
sbit left_down=P0^1;//左方-1sbit right_up=P0^2;//右方+1
sbit right_down=P0^3;//右方-1
sbit game_over=P0^4;//本局结束
sbit game_middle_time=P0^5;//局中歇
sbit game_space_time=P0^6;//局间歇
sbit match_over=P0^7;//整场比赛结束
sbit DAT=P1^2;//串行移位寄存器的数据输入端
sbit CLK=P1^3;//串行移位寄存器的时钟输入端
void Init();//初始化函数,主要包括时钟的设置与端口的设置
void Delay_ms(uint32_t ms);//延时函数,for 循环实现
void Display();//显示函数
void Set_score();//设置函数,主要包括 8 个按键的功能实现
void Sendbyte(uint8_t byte);//发送字节函数,主要目的用来给两个级联的 74164 发
送数据
uint16_t code tab[] =
{0xfcfc,0x60fc,0xdafc,0xf2fc,0x66fc,0xb6fc,0xbefc,0xe0fc,0xfefc,0xf6fc,
0xfc60,0x6060,0xda60,0xf260,0x6660,0xb660,0xbe60,0xe060,0xfe60,0xf660,
0xfcda,0x60da,0xdada,0xf2da,0x66da,0xb6da,0xbeda,0xe0da,0xfeda,0xf6da,
0xfcf2,0x60f2,0xdaf2,0xf2f2,0x66f2,0xb6f2,0xbef2,0xe0f2,0xfef2,0xf6f2,
0xfc66,0x6066,0xda66,0xf266,0x6666,0xb666,0xbe66,0xe066,0xfe66,0xf666,
0xfcb6,0x60b6,0xdab6,0xf2b6,0x66b6,0xb6b6,0xbeb6,0xe0b6,0xfeb6,0xf6b6,
0xfcbe,0x60be,0xdabe,0xf2be,0x66be,0xb6be,0xbebe,0xe0be,0xfebe,0xf6be,
0xfce0,0x60e0,0xdae0,0xf2e0,0x66e0,0xb6e0,0xbee0,0xe0e0,0xfee0,0xf6e0,
0xfcfe,0x60fe,0xdafe,0xf2fe,0x66fe,0xb6fe,0xbefe,0xe0fe,0xfefe,0xf6fe,
0xfcf6,0x60f6,0xdaf6,0xf2f6,0x66f6,0xb6f6,0xbef6,0xe0f6,0xfef6,0xf6f6,
0x0202};//“00”-“99”与“- -”在静态数码管上显示的 16
进制数
uint8_t code
led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};//定义一个
一维数组 led,用来存放共阳极数码管加反相器后“0~9”与“-”和“全灭”的显示字型
码
uint8_t count=0; //中断次数变量
int left_score=0; //左方得分,涉及到加减值,需要设置为 int 型
int right_score=0;//右方得分,同上
uint8_t left_game_score=0;//左方局分
uint8_t right_game_score=0;//右方局分
int time=0; //暂停时间初值
uint8_t middle_time_signal=0;//局中歇标志位
uint8_t space_time_signal=0;//局间歇标志位
void Init(void)
{
Delay_ms(300);
//延时等待上电稳定
SetBits(P_SW2,EAXFR);
//允许访问特殊功能寄存器
SetBits(IRC24MCR,ENIRC24M);
//使能内部 24M IRC
while (!(IRC24MCR & IRC24MST));
//等待时钟稳定
ClrBits(CKSEL,Bits_ALL);
//选择内部 24M IRC,时钟不输
出
ClrBits(CLKDIV,Bits_ALL);
//主时钟不分频
ClrBits(P_SW2,EAXFR);
//关闭访问特殊功能寄存器//复位脚用作 IO 口 P54
ClrBits(RSTCFG,P54RST);
//根据硬件设计配置端口
//P0 端口设置为准双向口
P0_Mode_PullUp(PIN_ALL);
//P12、P13 端口设置为推挽输出,用于驱动静态数码管
P1_Mode_OUT_PP(PIN_2|PIN_3);
//P1 其他端口设置为高阻
P1_Mode_HighZ(PIN_0|PIN_1|PIN_4|PIN_5|PIN_6|PIN_7);
//P2 端口设置为高阻
P2_Mode_HighZ(PIN_ALL);
//P4 端口设置为高阻
P4_Mode_HighZ(PIN_ALL);
//P5 端口设置为高阻
P5_Mode_HighZ(PIN_ALL);
//P6 端口设置为推挽输出
P6_Mode_OUT_PP(PIN_ALL);
//P7 端口设置为推挽输出
P7_Mode_OUT_PP(PIN_ALL);
//将 P30 P31 设置为准双向口,用于下载
P3_Mode_PullUp(PIN_0|PIN_1);
//P3 其他端口设置为高阻
P3_Mode_HighZ(PIN_2|PIN_3|PIN_4|PIN_5|PIN_6|PIN_7);
}
void Delay_ms(uint32_t ms) //延时函数
{
uint32_t i;
while( (ms--) != 0)
{
for(i = 0; i < 1850; i++);
}
}
void Display()//数码管动态显示函数
{
这里怎么写
}void Timer_0_Init()//定时器 0 中断初始化函数
{
这里怎么搞
}
void Timer_0() interrupt 1 //定时器 0 中断服务函数,每中断 50 次为 1s
{
count++;
if(count==50)
{
count=0;
time--; //满一秒,time 值减 1
}
}
void Sendbyte(uint8_t byte) //发送字节函数,一次性发送两个字节
{
uint16_t num;
uint8_t c;
num = tab[byte];
//取段码
for(c = 0; c < 16; c++)
//发送 16 位段码
{ CLK = 0;
DAT = num&0x0001;
//发送最低位数据
CLK = 1;
num>>=1;
//数据右移一位
}
}
void Set_score()//设置函数,包含了 8 个独立式按键对应的动作
{
if(left_up==0)//左方+1
{
Delay_ms(2);
if(left_up==0)
{
left_score++;
if(left_score==100)
{
left_score=0;
}
}
while(!left_up)
{
Display();
}
}
if(left_down==0)//左方-1
{
Delay_ms(2);
if(left_down==0)
{
left_score--;if(left_score==-1)
{
left_score=0;
}
}
while(!left_down)
{
Display();
}
}
if(right_up==0)//右方+1
{
Delay_ms(2);
if(right_up==0)
{
right_score++;
if(right_score==100)
{
right_score=0;
}
}
while(!right_up)
{
Display();
}
}
if(right_down==0)//右方-1
{
Delay_ms(2);
if(right_down==0){
right_score--;
if(right_score==-1)
{
right_score=0;
}
}
while(!right_down)
{
Display();
}
}
if(game_over==0)//本局结束
{
Delay_ms(2);
if(game_over==0)
{
if(left_score>right_score)
{
left_score=0;
right_score=0;
left_game_score++;
if(left_game_score==10)
{
left_game_score=0;
}
}
if(left_score<right_score)
{
left_score=0;right_score=0;
right_game_score++;
if(right_game_score==10)
{
right_game_score=0;
}
}
if(left_score==right_score)
{
_nop_();
}
}
while(!game_over)
{
Display();
}
}
if(game_middle_time==0)//局中歇
{
Delay_ms(2);
if(game_middle_time==0)
{
middle_time_signal++;
time=30;
}
while(!game_middle_time)
{
Display();
}
while(middle_time_signal){
TR0=1;
Sendbyte(time);
Display();
if(time==-1)
{
time=0;
TR0=0;
middle_time_signal=0;
}
}
}
if(game_space_time==0)//局间歇
{
Delay_ms(2);
if(game_space_time==0)
{
space_time_signal++;
time=60;
}
while(!game_space_time)
{
Display();
}
while(space_time_signal)
{
TR0=1;
Sendbyte(time);
Display();
if(time==-1){
time=0;
TR0=0;
space_time_signal=0;
}
}
}
if(match_over==0)//本场比赛结束
{
Delay_ms(2);
if(match_over==0)
{
left_score=0;
right_score=0;
left_game_score=0;
right_game_score=0;
time=0;
}
while(!match_over)
{
Display();
}
}
}
void main()//主函数,仅有调用
{
Init();
Timer_0_Init();
Sendbyte(0);
while(1){
Display();
Set_score();
}
}