使用的是科嵌的stm8s103开发板,以下数码管电路.
相关引脚是PD1,PA2,PA1,其中
PD1连接串行数据输入口DIO,图中两个74HC595串联,超过八位的数据,高位会溢出(相当于左边74HC595存储第八位,右边74HC595存储高八位数据)
PA1 数据输入时钟线SCLK,给一个上升沿,数据转移到输出存储器锁存时钟线RCLK中。
PA2 连接输出存储器锁存时钟线RCLK,上升沿触发,输出高低电平。
实现数码管每秒加1,记时
#include"iostm8s103f3.h"
unsigned long count;
unsigned char table[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
//01234567
//89ABCDEF
//共阳数码管段码“0123456789AbCdEF熄灭-”
unsigned char wei[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
#define DIO PD_ODR_ODR1
#define RCLK PA_ODR_ODR2
#define SCLK PA_ODR_ODR1
void CLKInit()
{
CLK_CKDIVR = 0x00; //0预分频
CLK_ICKR_bit.LSIEN = 1;
while(CLK_ICKR_bit.LSIRDY == 0);
}
void delay(unsigned long Count)
{
while(Count--);//Count形参控制延时次数
}
void Key1_Init()
{
PA_DDR_bit.DDR3 = 0;
PA_CR1_bit.C13 = 1;
PA_CR2_bit.C23 = 1;
EXTI_CR1_bit.PAIS = 2;
}
void Timer2_Init()
{
TIM2_PSCR = 0x00;
TIM2_ARRH = 0x3e; //16000000 1ms
TIM2_ARRL = 0x80;
TIM2_CNTRH = 0;
TIM2_CNTRL = 0;
TIM2_IER = 0x01;
TIM2_SR1 = 0x00;
TIM2_CR1 = 0x81;
}
void BeepInit()
{
BEEP_CSR_bit.BEEPDIV = 0x05;
BEEP_CSR_bit.BEEPSEL = 0x00;
}
void LED596_Init()
{
//HC595 CLK
PA_DDR_bit.DDR1 = 1; //设置端口PA->1的输入输出方向寄存器为输出方向
PA_CR1_bit.C11 = 1; //设置PA1为推挽输出
PA_CR2_bit.C21 = 1; //设置PA1的输出最大速度为10MHZ
//HC595 RTCK
PA_DDR_bit.DDR2 = 1; //设置端口PA->2的输入输出方向寄存器为输出方向
PA_CR1_bit.C12 = 1; //置PA2为推挽输出
PA_CR2_bit.C22 = 1; //设置PA2的输出最大速度为10MHZ
//HC595 DAT
PD_DDR_bit.DDR1 = 1; //设置端口PD->1的输入输出方向寄存器为输出方向
PD_CR1_bit.C11 = 1; //设置PD1为推挽输出
PD_CR2_bit.C21 = 1; //设置PD1的输出最大速度为10MHZ
SCLK = 0;
RCLK = 0;
}
void LED_OUT(unsigned char table,unsigned char wei)
{
unsigned int i;
unsigned int tempdata;
tempdata = (unsigned int)(wei);
tempdata <<= 8;
tempdata |= table;
RCLK=0;
for(i=0;i<16;i++)//循环8次
{
SCLK=0;
if (tempdata & 0x8000)//逐一取出最高位
{
DIO=1;//送出“1”
}
else
{
DIO=0;//送出“0”
}
SCLK=1;//SCLK产生上升沿
delay(1);
tempdata<<=1;//执行左移一位操作
}
SCLK=0;
delay(1);
RCLK=1;//RCLK产生上升沿
}
int main(void)
{
asm("sim");
Timer2_Init();
LED596_Init();
Key1_Init();
CLKInit();
BeepInit();
// LED_OUT(table[2],wei[3]);
BEEP_CSR_bit.BEEPEN = 1;
delay(50000);
BEEP_CSR_bit.BEEPEN = 0;
delay(5000);
BEEP_CSR_bit.BEEPEN = 1;
delay(50000);
BEEP_CSR_bit.BEEPEN = 0;
while(1)
{
if(PA_IDR_bit.IDR3 == 0)
{
asm("rim");
}
}
}
#pragma vector =5
__interrupt void KEY1_IRQHander()
{
if(PA_IDR_bit.IDR3 == 0)
{
delay(10);
BEEP_CSR_bit.BEEPEN = 1;
delay(50000);
BEEP_CSR_bit.BEEPEN = 0;
delay(5000);
BEEP_CSR_bit.BEEPEN = 1;
delay(50000);
BEEP_CSR_bit.BEEPEN = 0;
count=0;
}
delay(10);
}
#pragma vector =15
__interrupt void TIM2_Overflow_IRQHandler(void)
{
TIM2_SR1_UIF = 0;
count++;
unsigned int ge = 0;
unsigned int shi = 0;
unsigned int bai = 0;
unsigned int tp = 0;
unsigned int qian = 0;
tp = count/1000;
ge = tp%10;
shi = (tp/10)%10;
bai = (tp/100)%10;
qian = (tp/1000)%10;
LED_OUT(table[ge],wei[3]);
LED_OUT(table[shi],wei[2]);
LED_OUT(table[bai],wei[1]);
LED_OUT(table[qian],wei[0]);
}