NSS08Kit-R1开发板上有一个3位的共阴7段数码管,采用74hc595串行方式连接到mc9s08AC16上,引脚链接关系如下:
74hc595引脚 MC9S08AC16引脚
PIN10 PIN32
PIN11 PIN31
PIN12 PIN28
PIN14 PIN27
原理图如下:
下面上程序,动态扫描方式
/*************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<
nicrosystem@gmail.com>
@ date:2009-04-06
**************************/
/***************
@说明:
本程序测试开发板上的3位7段数码管,数码管是共阴的
我们用单片机的定时器产生1秒间隔的节拍,每1秒我们更新下
7段数码管的显示,数码管显示自运行以来经历的时间(以秒计)
*****************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define HC595_SDI PTDD_PTDD0
#define HC595_STCLK PTDD_PTDD1
#define HC595_SHCLK PTDD_PTDD2
#define HC595_RESET PTDD_PTDD3
#define SEVENLED1 PTBD_PTBD3
#define SEVENLED2 PTBD_PTBD2
#define SEVENLED3 PTBD_PTBD1
unsigned int g_timeSecond;
volatile unsigned char g_timeOF;
//初始化定时器,每1秒中断一次
void init_tpm_timer()
{
TPM1MODH=0x00;
TPM1MODL=0x00;//set tpm1 as free running timer
TPM1MODH=0x7A;
TPM1MODL=0x12;
TPM1SC=0x4f; //enable timer overflow interrupt
}
//设置系统时钟模式,为FEE,8MHZ的系统主频,BUS CLK为4MHZ
void init_system_clock()
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
interrupt VectorNumber_Vtpm1ovf void tmp1_overflow_isr()
{
g_timeOF=1;
if(g_timeSecond>=1000)
g_timeSecond=0;
else
g_timeSecond++;
TPM1SC_TOF=0;
}
void init_7seg_port()
{
PTDDD_PTDDD0=1;
PTDDD_PTDDD1=1;
PTDDD_PTDDD2=1;
PTDDD_PTDDD3=1;
PTBDD_PTBDD3=1;
PTBDD_PTBDD1=1;
PTBDD_PTBDD2=1;
PTDDS_PTDDS0=1;
PTDDS_PTDDS1=1;
PTDDS_PTDDS2=1;
PTDDS_PTDDS3=1;
PTBDS_PTBDS0=1;
PTBDS_PTBDS1=1;
PTBDS_PTBDS2=1;
HC595_RESET=1;
HC595_RESET=0;
HC595_RESET=1;
}
void write_74hc595(unsigned char dat)
{
unsigned char i;
HC595_STCLK=0;
for(i=0;i<8;i++)
{
HC595_SHCLK=0;
HC595_SDI=dat>>7;
HC595_SHCLK=1;
dat<<=1;
}
HC595_STCLK=1;
}
void display_7seg(unsigned char no,unsigned char dat)
{
unsigned char code;
unsigned char charmap[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67
};
code="charmap"[dat];
switch(no)
{
case 1:
write_74hc595(code);
SEVENLED1=1;
SEVENLED2=0;
SEVENLED3=0;
break;
case 2:
write_74hc595(code);
SEVENLED2=1;
SEVENLED1=0;
SEVENLED3=0;
break;
case 3:
write_74hc595(code);
SEVENLED3=1;
SEVENLED1=0;
SEVENLED2=0;
break;
default:
SEVENLED3=0;
SEVENLED2=0;
SEVENLED1=0;
HC595_RESET=1;
HC595_RESET=0;
HC595_RESET=1;
break;
}
}
void main(void) {
unsigned char i,buf[5];
EnableInterrupts; /* enable interrupts */
/* include your code here */
SOPT_COPE=0;//disable cop watchdog
g_timeSecond=0;
g_timeOF=0;
for(i=0;i<5;i++)
{
buf
=0;
}
init_system_clock();
init_tpm_timer();
init_7seg_port();
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
if(g_timeOF)
{
buf[0]=g_timeSecond/100;
buf[1]=(g_timeSecond%100)/10;
buf[2]=g_timeSecond%10;
g_timeOF=0;
}
for(i=0;i<3;i++)
{
display_7seg(i+1,buf);
}
} /* loop forever */
/* please make sure that you never leave main */
}
一周热门 更多>