HCS08入门之——LED数码管实验
0赞本实验采用分时扫描的方式驱动8个发光二极管和4位数码管,共阴极接法,段驱动采用74HC245,位选驱动用NPN三极管。做本实验时需将拨码开关“SW2”和“SW5”的所有位拨到“ON”位置。实验现象是8个发光二极管轮流点亮,数码管显示“2010”。见下图:
程序流程图如下:
实验代码如下:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define LED_DAT PTBD
#define LED_CS0 PTCD_PTCD3
#define LED_CS1 PTCD_PTCD4
#define LED_CS2 PTCD_PTCD5
#define LED_CS3 PTCD_PTCD6
#define LED_CS4 PTCD_PTCD7
#define CS_ON 1
#define CS_OFF 0
typedef unsigned char uchar;
uchar tab[]={
0x3F,/*0*/
0x06,/*1*/
0x5B,/*2*/
0x4F,/*3*/
0x66,/*4*/
0x6D,/*5*/
0x7D,/*6*/
0x07,/*7*/
0x7F,/*8*/
0x6F/*9*/
};//0-9,-,全灭
//============================
//函数名:Init_io初始化I/O
//作用:初始化I/O
//输入参数:无
//返回参数:无
//=============================
void Init_io(void)
{
//io初始值都为0
PTBD = 0x00;
PTCD_PTCD3=0;
PTCD_PTCD4=0;
PTCD_PTCD5=0;
PTCD_PTCD6=0;
PTCD_PTCD7=0;
//io为输出
PTBDD = 0xFF;
PTCDD_PTCDD3=1;
PTCDD_PTCDD4=1;
PTCDD_PTCDD5=1;
PTCDD_PTCDD6=1;
PTCDD_PTCDD7=1;
//高驱动强度
PTBDS = 0xFF;
PTCDS_PTCDS3=1;
PTCDS_PTCDS4=1;
PTCDS_PTCDS5=1;
PTCDS_PTCDS6=1;
PTCDS_PTCDS7=1;
}
//===================================================
//函数名:delay()
//作用:延时,该函数浪费CPU时间,在实际工程中酌情使用
//输入参数:无
//输出参数:无
//====================================================
void delay(void)
{
word i;
i=0x1ff;
while(i--)__RESET_WATCHDOG();//喂狗;
}
void display(uchar d0 , uchar d1 , uchar d2 , uchar d3 , uchar d4) {
//8个发光二极管
LED_DAT = 0;
LED_CS0 = CS_ON;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d0;
delay();
//第四个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_ON;
LED_DAT = d1;
delay();
//第三个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_ON;
LED_CS4 = CS_OFF;
LED_DAT = d2;
delay();
//第二个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_ON;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d3;
delay();
//第一个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_ON;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d4;
delay();
}
void display_2010(void) {
uchar cnt,i;
for( cnt =1 ; cnt < 0x80 ; cnt <<= 1){
for( i= 0 ; i<50 ; i++){
display(cnt , tab[2] , tab[0] , tab[1] , tab[0]);
}
}
for( i= 0 ; i<50 ; i++){
display(cnt , tab[2] , tab[0] , tab[1] , tab[0]);
}
}
void main(void) {
EnableInterrupts; /* enable interrupts */
/* include your code here */
Init_io();
for(;;) {
display_2010();
//__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}


