数码管驱动
//smg.c 使用PCA
#include <STC12C5A60S2.H>
#include "intrins.h"
#define FOSC 12000000L
#define T400Hz (FOSC / 12 / 400)
sfr PCAPWM0 = 0xf2;
sfr PCAPWM1 = 0xf3;
unsigned int PCA_TEMP;
unsigned char code SMG_TRANSFORM[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xFF};
unsigned char SMG_DATA[4]={1,2,3,4};
void PCA_isr() interrupt 7
{
static unsigned char SMG_TEMP;
CCF0 = 0; //Clear interrupt flag
CCAP0L = PCA_TEMP;
CCAP0H = PCA_TEMP >> 8; //Update compare PCA_TEMP
PCA_TEMP += T400Hz;
if(SMG_TEMP==0)P2=0xFE;else if(SMG_TEMP==1)P2=0xFD;else if(SMG_TEMP==2)P2=0xFB;else if(SMG_TEMP==3)P2=0xF7;
P0=SMG_TRANSFORM[SMG_DATA[SMG_TEMP]];
SMG_TEMP=(SMG_TEMP+1)%4;
//P2=0x0F;
}
void SMG_SPRINT(unsigned char SMG_COM,unsigned char SMG_NUM)
{
SMG_DATA[SMG_COM]=SMG_NUM;
}
void SMG_INIT(void)
{
CCON = 0; //Initial PCA control register
//PCA timer stop running
//Clear CF flag
//Clear all module interrupt flag
CL = 0; //Reset PCA base timer
CH = 0;
CMOD = 0x00; //Set PCA timer clock source as Fosc/12
//Disable PCA timer overflow interrupt
PCA_TEMP = T400Hz;
CCAP0L = PCA_TEMP;
CCAP0H = PCA_TEMP >> 8; //Initial PCA module-0
PCA_TEMP += T400Hz;
CCAPM0 = 0x49; //PCA module-0 work in 16-bit timer mode and enable PCA interrupt
CR = 1; //PCA timer start run
EA = 1;
}
//smg.h
void SMG_INIT(void);
void SMG_SPRINT(unsigned char SMG_COM,unsigned char SMG_NUM);