我这儿有一个基于ADI的DSP开发板,我想控制上面的键盘按钮。按键是用的CH452,我仿照网上的单片机上的例子改了一下,可是就是进不了中断。
下面是我写的程序
用Visual Dsp++5.0编写的。
//#ifdef _STANDALONE_ // use this to run standalone tests
int main()
{
CH452_Write(0x422); //dsp向CH452写入控制命令;0x402是设定系统参数中设置为按键操作。
Setup_Ints1();
*pSYSCTL &= (~MSEN);
while(1)
{
;
/*SRU(HIGH,PBEN02_I);//assign pin 19 low so it is an input
SRU(HIGH,PBEN03_I);//assign pin 20 low so it is an input
SRU(LOW,DAI_PB02_I);//assign pin buffer 19 low so it is an input
SRU(LOW,DAI_PB03_I); //assign pin buffer 20 low so it is an input
*/
}
}
//#endif //#ifdef _STANDALONE_
void CH452_Write(unsigned short cmd) //写命令
{
CH452_I2c_Start(); //启动总线
// CH452_I2c_WrByte((unsigned char)(cmd>>7)&CH452_I2C_MASK|CH452_I2C_ADDR1); // CH452的ADDR=1时(默认)
CH452_I2c_WrByte((unsigned char)cmd); //发送数据
CH452_I2c_Stop(); //结束总线
}
void CH452_I2c_Start(void) // 操作起始
{
//sysreg_bit_clr(sysreg_IRPTL, (IRQ1I|IRQ0I) );
//interrupt(SIG_IRQ1,SIG_IGN); //DISABLE_KEY_INTERRUPT; 禁止键盘中断,防止开始时被CH452中断而进入中断服务程序中的START
SRU2(HIGH,DPI_PBEN11_I); //CH452_SDA_D_OUT; 设置SDA为输出方向
SRU2(HIGH,DPI_PB11_I); //CH452_SDA_SET; 发送起始条件的数据信号
SRU2(HIGH,DPI_PBEN12_I); //CH452_SCL_D_OUT; 设置SCL为输出方向
SRU2(HIGH,DPI_PB12_I) //CH452_SCL_SET
TimeDelay1(40);
SRU2(LOW,DPI_PB11_I) //CH452_SDA_CLR; 发送起始信号
TimeDelay1(40);
SRU2(LOW,DPI_PB12_I) //钳住I2C总线,准备发送或接收数据
TimeDelay1(40);
}
void CH452_I2c_Stop(void) // 操作结束
{
SRU2(LOW,DPI_PB11_I);//CH452_SDA_CLR;
SRU2(HIGH,DPI_PBEN11_I);//CH452_SDA_D_OUT; /* 设置SDA为输出方向 */
TimeDelay1(40);
SRU2(HIGH,DPI_PB12_I);//CH452_SCL_SET;
TimeDelay1(40);
SRU2(HIGH,DPI_PB11_I);//CH452_SDA_SET; /*发送I2C总线结束信号*/
TimeDelay1(40);
SRU2(LOW,DPI_PBEN12_I);//CH452_SDA_D_IN; /* 设置SDA为输入方向 */
TimeDelay1(40);
sysreg_bit_set(sysreg_IMASK, (IRQ1I|IRQ0I) );
}
void CH452_I2c_WrByte(unsigned char dat) //写一个字节数据
{
unsigned char i;
SRU2(HIGH,DPI_PBEN11_I); //CH452_SDA_D_OUT; 设置SDA为输出方向
for(i=0;i!=12;i++) // 输出8位数据
{
if(dat&0x800) {SRU2(HIGH,DPI_PB11_I);} //CH452_SDA_SET
else {SRU2(LOW,DPI_PB11_I);} //CH452_SDA_CLR;
TimeDelay1(40);
SRU2(HIGH,DPI_PB12_I); //CH452_SCL_SET;
dat<<=1;
TimeDelay1(40);
TimeDelay1(40);
SRU2(LOW,DPI_PB12_I); //CH452_SCL_CLR;
TimeDelay1(40);
}
SRU2(LOW,DPI_PBEN11_I); //CH452_SDA_D_IN; 设置SDA为输入方向
SRU2(HIGH,DPI_PB11_I); //CH452_SDA _SET;
TimeDelay1(40);
SRU2(HIGH,DPI_PB12_I); //CH452_SCL_SET; // 接收应答
TimeDelay1(40);
TimeDelay1(40);
SRU2(LOW,DPI_PB12_I); // CH452_SCL_CLR;
TimeDelay1(40);
}
unsigned char CH452_I2c_RdByte(void) //读一个字节数据
{
unsigned char dat,i;
SRU2(HIGH,DPI_PB11_I); //CH452_SDA_SET;
SRU2(LOW,DPI_PBEN11_I); //CH452_SDA_D_IN; 设置SDA为输入方向
dat=0;
for(i=0;i!=12;i++) // 输入8位数据
{
SRU2(HIGH,DPI_PB12_I); //CH452_SCL_SET;
TimeDelay1(40);
TimeDelay1(40);
dat<<=1;
if(SRU2_INPUT1&BIT_20) dat++; // 输入1位 CH452_SDA_IN)
SRU2(LOW,DPI_PB12_I); //CH452_SCL_CLR;
TimeDelay1(40);
}
SRU2(HIGH,DPI_PB11_I); //CH452_SDA_SET;
TimeDelay1(40);
SRU2(HIGH,DPI_PB12_I); //CH452_SCL_SET; // 发出无效应答
TimeDelay1(40);
TimeDelay1(40);
SRU2(LOW,DPI_PB12_I); //CH452_SCL_CLR;
TimeDelay1(40);
return(dat);
}
unsigned char CH452_Read(void) //读取按键
{
unsigned char keycode;
CH452_I2c_Start(); /*启动总线*/
CH452_I2c_WrByte((unsigned char)CH452_GET_KEY);
//CH452_I2c_WrByte((unsigned char)(CH452_GET_KEY>>7)&CH452_I2C_MASK|0x01|CH452_I2C_ADDR1); // 若有两个CH452并连,当ADDR=0时,需修改为CH452_I2C_ADDR0
keycode=CH452_I2c_RdByte(); /*读取数据*/
CH452_I2c_Stop(); /*结束总线*/
return(keycode);
}
void Setup_Ints1(void)
{
// Config_SRU_INTS1();
// (*pDAI_IRPTL_PRI) = (SRU_EXTMISCA1_INT | SRU_EXTMISCA2_INT); //unmask individual interrupts
// (*pDAI_IRPTL_RE) = (SRU_EXTMISCA1_INT | SRU_EXTMISCA2_INT); //make sure interrupts latch on the rising edge
//Set up interrupt priorities
// sysreg_bit_set(sysreg_IMASK, DAIHI); //make DAI interrupts high priority
(*pSYSCTL) |= (IRQ0EN|IRQ1EN);
sysreg_bit_set(sysreg_MODE2, (IRQ0E|IRQ1E) );
sysreg_bit_clr(sysreg_IRPTL, (IRQ1I|IRQ0I) );
sysreg_bit_set(sysreg_IMASK, (IRQ1I|IRQ0I) ); //enable IRQ interrupts
sysreg_bit_set(sysreg_MODE1, IRPTEN ); //enable global interrupts
//interrupt(SIG_DAIH,DAIroutine);
// interrupt(SIG_IRQ0,IRQ0_routine);
interrupt(SIG_IRQ1,IRQ1_routine1);
}
void Cleanup_SRU_INTS1(void)
{
// interrupt(SIG_DAIH,SIG_IGN);
// interrupt(SIG_IRQ0,SIG_IGN);
interrupt(SIG_IRQ1,SIG_IGN);
*pSYSCTL |= MSEN;
}
void IRQ1_routine1(int sig_int)
{
unsigned char keycode;
keycode=CH452_Read(); /*读取数据*/
Cleanup_SRU_INTS1();
SRU(HIGH,PBEN02_I);//assign pin 19 low so it is an input
SRU(HIGH,PBEN03_I);//assign pin 20 low so it is an input
SRU(LOW,DAI_PB02_I);//assign pin buffer 19 low so it is an input
SRU(LOW,DAI_PB03_I); //assign pin buffer 20 low so it is an input
}
void TimeDelay1(unsigned int Timers)
{
unsigned int i;
while(Timers)
{
Timers--;
for(i=0;i<1000;i++) ;
}
}
此帖出自
小平头技术问答
一周热门 更多>