按键亮灯和跑马灯程序
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*要求:*/
/*利用DSP2812实现*/
/*1、确定CPU时钟100M(外部使用20MHZ晶振)*/
/*2、GPIOA0-7接8个LED灯*/
/*3、GPIOA8-15接8个按键*/
/*每一秒读取按键的值,并点亮相应的灯*/
viod Initsystem()
{
EALLOW;
SysCtrlReg.WDCR=0x0068;
{
InitSysCtrl();/*初始化系统*/
DINT; /*关中断*/
IER = 0x0000;
IFR = 0x0000;
InitPieCtrl(); /*初始化PIE控制寄存器*/
InitPieVectTable(); /*初始化PIE矢量表*/
InitGpio(); /*初始化GPIO*/
InitEva(); /*初始化EV*/
//设置T1PR和T1CMP
EvaRegs.T1PR=37499;
EvaRegs.T1CMPR=15000; //15000/37500=40%
EvaRegs.T1CNT=0;
EvaRegs.T1CON.bit.TENABLE=1; //启动定时器操作,开始产生PWM波
}
// Main函数
void main(void)
GpioDataRegs.GPADAT.all=j;
delay_loop(); // Delay 1[s]
}
}
}
void Gpio_select(void)
{
EALLOW;
GpioMuxRegs.GPAMUX.all = 0x0000; //设置GPIO为I/O功能
GpioMuxRegs.GPADIR.all = 0xFFFF; //设置GPIO为输出
EDIS;
}
//初始化EV
void InitEva(void)
{
//设置定时器控制寄存器T1CON
EvaRegs.T1CON.bit.TMODE=2; //因为要产生不对称的PWM波形,所以将T1的计数方式设定为连续增模式
EvaRegs.T1CON.bit.TPS=1; //这个位是T1输入时钟预定标位,则,T1CLK=HSPCLK/2=75/2=37.5M
// Prototype statements for functions found within this file.
Uint16 j=0x0001;
short i ;
void Gpio_select(void);
void delay_loop(Uint16 loop_time);
void main(void)
CpuTimer0Regs.TCR.bit.TIE = 1;
CpuTimer0.InterruptCount = 0;
}
void main()
{
Initsystem();
InitCPUtimer0();
DINT;
IER = 0x0000;
IFR = 0x0000;
/*初始化PIE*/
InitPieCtrl();
EINT; // Enable Global interrupt INTM
ERTM;// Enable Global realtime interrupt DBGM
}
interrupt void ISRTimer0(void)
{
uint32 a;
PieCtrl.PIEACK.bit.ACK7=1;
a=GpioDataRegs.GPADAT.all;
void InitSysCtrl(void)
{
Uint16 i;
EALLOW;
SysCtrlRegs.WDCR= 0x0068; //禁止开门狗
SysCtrlRegs.PLLCR = 0xA; //初始化PLL模块,系统时钟30M*10/2=150M
for(i= 0; i< 5000; i++);//延时,使得PLL初始化成功
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
void InitEva(void)
//系统时钟初始化
SysCtrlRegs.PLLCR =0x0a;
for(i= 0; i< 5000; i++);
GpioMuxRegs.GPAMUX.all = 0x0000;
GpioMuxRegs.GPFDIR.all= 0x00ff;
EDIS;
}
void ConfigCpuTimer(uint16 a)
{
CpuTimer0Regs.PRD.all = a;
EINT; //使能中断
for(;;)
}
SysCtrlRegs.HISPCP.all = 0x0001; //HSPCLK=150M/2=75M
SysCtrlRegs.LOSPCP.all = 0x0002; //LSPCLK=150M/4=37.5M
SysCtrlRegs.PCLKCR.bit.EVAENCLK=1; //使能EVA的时钟
GpioMuxRegs.GPAQUAL.all = 0x0000; //设置输入采样限定值(不限制)
EDIS;
}
/*延时*/
void delay_loop()
{
short i;
for (i = 0; i < 1000; i++)
}
3
/*定时器T1产生一个不对称的PWM波形,频率为1KHz,占空比为40%。*/
EvaRegs.T1CON.bit.TENABLE=0; //暂时禁止定时器操作,等到全部设定完毕,再启动定时器
//设置GPTCONA
EvaRegs.GPTCONA.bit.TCOMPOE=1; //定时器比较输出T1PWM或者T2PWM由各自的定时器比较逻辑驱动
EvaRegs.GPTCONA.bit.T1PIN=1; //定时器1的比较输出极性是低电平有效
GpioDataRegs.GPADAT.all=j; // First led light ON
delay_loop(); // Delay 1Leabharlann Baidus]
while(1) //
{
for(i=1;i<8;i++) // The second led to eighth one lights by turns
{
InitSysCtrl();//系统初始化
DINT;//关中断
InitPieCtrl();//初始化PIE
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();//初始化PIE中断矢量表
Gpio_select();//初始化GpioGpioDataRegs.GPACLEAR.all=0xFFFF; // All GpioAx to OFF
{
j=0x0001<<(8*i);
GpioDataRegs.GPADAT.all=j;
delay_loop(); // // Delay 1[s]
}
for(i=1;i<8;i++)
{
j=0x1000>>(8*i); // The seventh led to first one lights by turns
EDIS;
}
//编写函数InitGpio()2812的T1PWM引脚和IO是复用的,需设定一下T1PWM这个引脚,使它的功能是输出PWM而不是普通的IO口。
void InitGpio(void)
{
EALLOW;
GpioMuxRegs.GPAMUX.bit.T1PWM_GPIOA6=1; //将GPIOA6脚功能设置为T1PWM,而不是普通的IO口
a>>16;
GpioDataRegs.GPADAT.all=a;
}
2
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
/*初始化PIE中断矢量表*/
InitPieVectTable();
EALLOW;
PieVectTable.TINT0 = &ISRTimer0;
EDIS;
ConfigCpuTimer(100000000);
StartCpuTimer0();
/*开中断*/
IER |= 0x01;
PieCtrl.PIEIER1.bit.INTx7=1;
CpuTimer0Regs.TPR.all = 0;
CpuTimer0Regs.TPRH.all = 0;
CpuTimer0Regs.TCR.bit.TSS = 1;
CpuTimer0Regs.TCR.bit.TRB = 1;
CpuTimer0Regs.TCR.bit.SOFT = 1;
CpuTimer0Regs.TCR.bit.FREE = 1;
/*利用DSP2812实现*/
/*1、确定CPU时钟100M(外部使用20MHZ晶振)*/
/*2、GPIOA0-7接8个LED灯*/
/*3、GPIOA8-15接8个按键*/
/*每一秒读取按键的值,并点亮相应的灯*/
viod Initsystem()
{
EALLOW;
SysCtrlReg.WDCR=0x0068;
{
InitSysCtrl();/*初始化系统*/
DINT; /*关中断*/
IER = 0x0000;
IFR = 0x0000;
InitPieCtrl(); /*初始化PIE控制寄存器*/
InitPieVectTable(); /*初始化PIE矢量表*/
InitGpio(); /*初始化GPIO*/
InitEva(); /*初始化EV*/
//设置T1PR和T1CMP
EvaRegs.T1PR=37499;
EvaRegs.T1CMPR=15000; //15000/37500=40%
EvaRegs.T1CNT=0;
EvaRegs.T1CON.bit.TENABLE=1; //启动定时器操作,开始产生PWM波
}
// Main函数
void main(void)
GpioDataRegs.GPADAT.all=j;
delay_loop(); // Delay 1[s]
}
}
}
void Gpio_select(void)
{
EALLOW;
GpioMuxRegs.GPAMUX.all = 0x0000; //设置GPIO为I/O功能
GpioMuxRegs.GPADIR.all = 0xFFFF; //设置GPIO为输出
EDIS;
}
//初始化EV
void InitEva(void)
{
//设置定时器控制寄存器T1CON
EvaRegs.T1CON.bit.TMODE=2; //因为要产生不对称的PWM波形,所以将T1的计数方式设定为连续增模式
EvaRegs.T1CON.bit.TPS=1; //这个位是T1输入时钟预定标位,则,T1CLK=HSPCLK/2=75/2=37.5M
// Prototype statements for functions found within this file.
Uint16 j=0x0001;
short i ;
void Gpio_select(void);
void delay_loop(Uint16 loop_time);
void main(void)
CpuTimer0Regs.TCR.bit.TIE = 1;
CpuTimer0.InterruptCount = 0;
}
void main()
{
Initsystem();
InitCPUtimer0();
DINT;
IER = 0x0000;
IFR = 0x0000;
/*初始化PIE*/
InitPieCtrl();
EINT; // Enable Global interrupt INTM
ERTM;// Enable Global realtime interrupt DBGM
}
interrupt void ISRTimer0(void)
{
uint32 a;
PieCtrl.PIEACK.bit.ACK7=1;
a=GpioDataRegs.GPADAT.all;
void InitSysCtrl(void)
{
Uint16 i;
EALLOW;
SysCtrlRegs.WDCR= 0x0068; //禁止开门狗
SysCtrlRegs.PLLCR = 0xA; //初始化PLL模块,系统时钟30M*10/2=150M
for(i= 0; i< 5000; i++);//延时,使得PLL初始化成功
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
void InitEva(void)
//系统时钟初始化
SysCtrlRegs.PLLCR =0x0a;
for(i= 0; i< 5000; i++);
GpioMuxRegs.GPAMUX.all = 0x0000;
GpioMuxRegs.GPFDIR.all= 0x00ff;
EDIS;
}
void ConfigCpuTimer(uint16 a)
{
CpuTimer0Regs.PRD.all = a;
EINT; //使能中断
for(;;)
}
SysCtrlRegs.HISPCP.all = 0x0001; //HSPCLK=150M/2=75M
SysCtrlRegs.LOSPCP.all = 0x0002; //LSPCLK=150M/4=37.5M
SysCtrlRegs.PCLKCR.bit.EVAENCLK=1; //使能EVA的时钟
GpioMuxRegs.GPAQUAL.all = 0x0000; //设置输入采样限定值(不限制)
EDIS;
}
/*延时*/
void delay_loop()
{
short i;
for (i = 0; i < 1000; i++)
}
3
/*定时器T1产生一个不对称的PWM波形,频率为1KHz,占空比为40%。*/
EvaRegs.T1CON.bit.TENABLE=0; //暂时禁止定时器操作,等到全部设定完毕,再启动定时器
//设置GPTCONA
EvaRegs.GPTCONA.bit.TCOMPOE=1; //定时器比较输出T1PWM或者T2PWM由各自的定时器比较逻辑驱动
EvaRegs.GPTCONA.bit.T1PIN=1; //定时器1的比较输出极性是低电平有效
GpioDataRegs.GPADAT.all=j; // First led light ON
delay_loop(); // Delay 1Leabharlann Baidus]
while(1) //
{
for(i=1;i<8;i++) // The second led to eighth one lights by turns
{
InitSysCtrl();//系统初始化
DINT;//关中断
InitPieCtrl();//初始化PIE
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();//初始化PIE中断矢量表
Gpio_select();//初始化GpioGpioDataRegs.GPACLEAR.all=0xFFFF; // All GpioAx to OFF
{
j=0x0001<<(8*i);
GpioDataRegs.GPADAT.all=j;
delay_loop(); // // Delay 1[s]
}
for(i=1;i<8;i++)
{
j=0x1000>>(8*i); // The seventh led to first one lights by turns
EDIS;
}
//编写函数InitGpio()2812的T1PWM引脚和IO是复用的,需设定一下T1PWM这个引脚,使它的功能是输出PWM而不是普通的IO口。
void InitGpio(void)
{
EALLOW;
GpioMuxRegs.GPAMUX.bit.T1PWM_GPIOA6=1; //将GPIOA6脚功能设置为T1PWM,而不是普通的IO口
a>>16;
GpioDataRegs.GPADAT.all=a;
}
2
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
/*初始化PIE中断矢量表*/
InitPieVectTable();
EALLOW;
PieVectTable.TINT0 = &ISRTimer0;
EDIS;
ConfigCpuTimer(100000000);
StartCpuTimer0();
/*开中断*/
IER |= 0x01;
PieCtrl.PIEIER1.bit.INTx7=1;
CpuTimer0Regs.TPR.all = 0;
CpuTimer0Regs.TPRH.all = 0;
CpuTimer0Regs.TCR.bit.TSS = 1;
CpuTimer0Regs.TCR.bit.TRB = 1;
CpuTimer0Regs.TCR.bit.SOFT = 1;
CpuTimer0Regs.TCR.bit.FREE = 1;