/*Main.c*/
/*SPI-TLC5620 DA转换程序*/
#include "global.c"
#define SetCS PEDATDIR = PEDATDIR | 0x40
#define ClrCS PEDATDIR = PEDATDIR & 0xFFBF
void SystemInit();
void Timer1Init();
void KickDog();
void SPI_Init();
const unsigned int LedCode[]={0xC0C0,0xF9F9,0xA4A4,0xB0B0,0x9999,0x9292,0x8282,0xF8F8,0x8080,0x9090,0x8888,0x8383,0xC6C6,0xA1A1,0x8686,0x8E8E,0xFFFF};
//const unsigned char LedCode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
int numled0=200;
int LedCount = 0;
main()
{
SystemInit(); //系统初始化
MCRC=MCRC & 0xFF00; //IOPE0-7设为IO口模式
PEDATDIR=0xFF00; //所有LED=0,
SPI_Init();
Timer1Init(); /*定时器初始化*/
asm(" CLRC INTM ");
while(1);
}
void SystemInit()
{
asm(" SETC INTM "); /* 关闭总中断 */
asm(" CLRC SXM "); /* 禁止符号位扩展 */
asm(" CLRC CNF "); /* B0块映射为 on-chip DARAM*/
asm(" CLRC OVM "); /* 累加器结果正常溢出*/
SCSR1=0x87FE; /* 系统时钟CLKOUT=20*2=40M */
/* 打开ADC,EVA,EVB,CAN和SCI的时钟*/
WDCR=0x006F; /* 禁止看门狗,看门狗时钟64分频 */
KickDog(); /* 初始化看门狗 */
IFR=0xFFFF; /* 清除中断标志 */
IMR=0x0003; /* 打开中断2*/
}
void Timer1Init()
{
EVAIMRA=0x0080; // 定时器1周期中断使能
EVAIFRA=0xFFFF; // 清除中断标志
GPTCONA=0x0000;
T1PR=2500; // 定时器1初值,定时0.4us*2500=1ms
T1CNT=0;
T1CON=0x144E; //增模式, TPS系数40M/16=2.5M,T1使能
}
void SPI_Init() //SPI-DA初始化
{
MCRB=MCRB | 0x0014; //SPISIMO,SPICLK特殊功能方式
SetCS;
SPICCR=0x004F; //8bit数据
SPICTL=0x0006; //禁止中断
SPIBRR=0x0027; //1M波特率,40M/40=1M
SPICCR=SPICCR | 0x80;
}
void c_int2() /*定时器1中断服务程序*/
{
unsigned char flag=0;
unsigned char i;
if(PIVR!=0x27)
{ asm(" CLRC INTM ");
return;
}
T1CNT=0;
numled0--;
if(numled0==0)
{
numled0=200;
if((PEDATDIR & 0x0002)==0x0002)
PEDATDIR=PEDATDIR & 0xFFE0; //LED灭
else
PEDATDIR=PEDATDIR |0x001F; //LED亮
SPITXBUF = LedCode[LedCount];
while(1)
{
flag=SPISTS & 0x40;
if(flag==0x40) break;
}
SPIRXBUF=SPIRXBUF;/*虚读寄存器以清除中断标志*/
ClrCS;
SetCS;
LedCount++;
if (LedCount>15) LedCount = 0;
}
EVAIFRA=0x80;
asm(" CLRC INTM ");
}
void KickDog() /*踢除看门狗 */
{
WDKEY=0x5555;
WDKEY=0xAAAA;
}