视频:https://video.kuaishou.com/short-video/3xtkh23yf64tzk6?authorId=3xmydwifg7bk3yk&streamSource=profile&area=profilexxnull/*****************pfc161+tm1651********************/
/******************20210608************************/
/****************qq:774145445**********************/
/****************快手:共同学习stm8*****************/
/**************************************************/
// _______ ______
// dio-|1 pb7 \_/ pb010|-clk
// |2 vdd pfc161gnd 9|
// cs-||-|3 pa7 pa0 8|-hong
// tk8-|4 pa6 pa4 7|-lv
// tk9-|5_pa5_______pa3_6|-lan
//
// tm1651
// | 5v
// pb0-|-clk
// pb7-|-dio
// | gnd
/**************************************************/
#include "extern.h"
tm1651_clk bit pb.0;
tm1651_dio bit pb.7;
led_hong bit pa.0;
led_lv bit pa.4;
led_lan bit pa.3;
/**************************************************/
/**************************************************/ //tm1651
byte tm1651_a0;
byte tm1651_a1; //千位
byte tm1651_a2; //百位
byte tm1651_a3; //十位
byte tm1651_a4; //个位
void tm1651_yanshi(); //延时
void tm1651_start(); //起始信号
void tm1651_data(); //数据
void tm1651_ack(); //应答
void tm1651_stop(); //结束
void tm1651_xianshi(); //显示
/**************************************************/ //显示
byte xianshi_a1; //千位
byte xianshi_a2; //百位
byte xianshi_a3; //十位
byte xianshi_a4; //个位
byte xianshi_qumo_a1;
void xianshi(); //显示
void xianshi_qumo(); //取模
/**************************************************/
void yanshi(); //延时
/**************************************************/
byte xianshi_c1;
byte xianshi_c2;
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/4 // SYSCLK=IHRC/4
$ tm1651_clk out,high; //输出,高
$ tm1651_dio out,high;
$ led_hong out,low;
$ led_lv out,low;
$ led_lan out,low;
/**************************************************/
//0x7e;0x30;0x6d;0x79;0x33;0x5b;0x5f;0x70;0x7f;0x7b;0x77;0x1f;0x4e;0x3d;0x4f;0x47;
xianshi_c1 =0x00;
xianshi_c2 =0xff;
while (1)
{
xianshi();
yanshi();
//if(led_hong){led_hong=0;} else{led_hong=1;}
//if(led_lv){led_lv=0;} else{led_lv=1;}
//if(led_lan){led_lan=0;} else{led_lan=1;}
xianshi_c1++;
xianshi_c2--;
}
}
/**************************************************/
void tm1651_yanshi() //延时
{
.delay 50;
}
/**************************************************/
void tm1651_start() //起始信号
{
tm1651_clk =1; tm1651_dio =1; tm1651_yanshi();
tm1651_clk =1; tm1651_dio =0; tm1651_yanshi();
}
/**************************************************/
void tm1651_data() //数据
{
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.0){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop; //太快检测不到需要加延时
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.1){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.2){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.3){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.4){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.5){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.6){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_clk =0; tm1651_yanshi();
if(tm1651_a0.7){tm1651_dio =1;} else{tm1651_dio =0;}
nop;nop;nop;
tm1651_clk =1; tm1651_yanshi();
tm1651_ack();
}
/**************************************************/
void tm1651_ack() //应答
{
byte tm1651_ack_a1;
tm1651_ack_a1 =0;
$ tm1651_dio in,pull;
tm1651_clk =0; tm1651_yanshi(); //第八个时钟的下降沿
while(tm1651_dio==1) //检测低电平
{
tm1651_ack_a1 ++;
if(tm1651_ack_a1>150) //退出条件(死机)
{
$ tm1651_dio out,low;
return;
}
}
tm1651_clk =1; tm1651_yanshi();
$ tm1651_dio out,low;
}
/**************************************************/
void tm1651_stop() //结束
{
tm1651_clk =0; tm1651_dio =0; tm1651_yanshi();
tm1651_clk =1; tm1651_dio =0; tm1651_yanshi();
tm1651_clk =1; tm1651_dio =1; tm1651_yanshi();
}
/**************************************************/
void tm1651_xianshi() //显示
{
tm1651_start();
tm1651_a0 =0b0100_0000; tm1651_data(); //写数据到显示寄存器,自动地址增加
tm1651_stop();
tm1651_start();
tm1651_a0 =0b1100_0000; tm1651_data(); //设置地址00H
tm1651_a0 =tm1651_a1; tm1651_data();
tm1651_a0 =tm1651_a2; tm1651_data();
tm1651_a0 =tm1651_a3; tm1651_data();
tm1651_a0 =tm1651_a4; tm1651_data();
tm1651_stop();
tm1651_start();
tm1651_a0 =0b1000_1011; tm1651_data(); //显示开,10/16
tm1651_stop();
}
/**************************************************/
void xianshi() //显示
{
/*
xianshi_a1 =tkch >>4;
xianshi_a2 =tkch &0b0000_1111;
xianshi_a3 =tkcl >>4;
xianshi_a4 =tkcl &0b0000_1111;
*/
xianshi_a1 =xianshi_c1 >>4;
xianshi_a2 =xianshi_c1 &0b0000_1111;
xianshi_a3 =xianshi_c2 >>4;
xianshi_a4 =xianshi_c2 &0b0000_1111;
xianshi_qumo_a1 =xianshi_a1;
xianshi_qumo();
xianshi_a1 =xianshi_qumo_a1;
xianshi_qumo_a1 =xianshi_a2;
xianshi_qumo();
xianshi_a2 =xianshi_qumo_a1;
xianshi_qumo_a1 =xianshi_a3;
xianshi_qumo();
xianshi_a3 =xianshi_qumo_a1;
xianshi_qumo_a1 =xianshi_a4;
xianshi_qumo();
xianshi_a4 =xianshi_qumo_a1;
tm1651_a1 =xianshi_a1;
tm1651_a2 =xianshi_a2;
tm1651_a3 =xianshi_a3;
tm1651_a4 =xianshi_a4;
tm1651_xianshi();
}
/**************************************************/
//0x7e;0x30;0x6d;0x79;
//0x33;0x5b;0x5f;0x70;
//0x7f;0x7b;0x77;0x1f;
//0x4e;0x3d;0x4f;0x47;
void xianshi_qumo() //取模
{
if(xianshi_qumo_a1 ==0x00){xianshi_qumo_a1 =0x7e; return;} //0
if(xianshi_qumo_a1 ==0x01){xianshi_qumo_a1 =0x30; return;} //1
if(xianshi_qumo_a1 ==0x02){xianshi_qumo_a1 =0x6d; return;} //2
if(xianshi_qumo_a1 ==0x03){xianshi_qumo_a1 =0x79; return;} //3
if(xianshi_qumo_a1 ==0x04){xianshi_qumo_a1 =0x33; return;} //4
if(xianshi_qumo_a1 ==0x05){xianshi_qumo_a1 =0x5b; return;} //5
if(xianshi_qumo_a1 ==0x06){xianshi_qumo_a1 =0x5f; return;} //6
if(xianshi_qumo_a1 ==0x07){xianshi_qumo_a1 =0x70; return;} //7
if(xianshi_qumo_a1 ==0x08){xianshi_qumo_a1 =0x7f; return;} //8
if(xianshi_qumo_a1 ==0x09){xianshi_qumo_a1 =0x7b; return;} //9
if(xianshi_qumo_a1 ==0x0a){xianshi_qumo_a1 =0x77; return;} //a
if(xianshi_qumo_a1 ==0x0b){xianshi_qumo_a1 =0x1f; return;} //b
if(xianshi_qumo_a1 ==0x0c){xianshi_qumo_a1 =0x4e; return;} //c
if(xianshi_qumo_a1 ==0x0d){xianshi_qumo_a1 =0x3d; return;} //d
if(xianshi_qumo_a1 ==0x0e){xianshi_qumo_a1 =0x4f; return;} //e
if(xianshi_qumo_a1 ==0x0f){xianshi_qumo_a1 =0x47; return;} //f
}
/**************************************************/
void yanshi() //延时
{
.delay 2000000;
}
/**************************************************/
/**************************************************/
|