- #include <msp430x14x.h>
- #include "hpi.h"
- #define HPI_CONTROL_REG 0x00
- #define HPI_DATA_WITHADDR_INC 0x04
- #define HPI_ADDR_REG 0x08
- #define HPI_DATA_REG 0x0C
- #define SET_BOB 0x101
- #define CLR_HINT 0x909
- #define INT_DSP 0x505
- int pBuf[128];
- char nFlag;
- void Init_Port(void)
- {
- //将P1 P2口所有的管脚在初始化的时候设置为输入方式
- P1DIR = 0;
- P2DIR = 0;
- //将P1 P2 口所有的管脚设置为一般I/O口
- P1SEL = 0;
- P2SEL = 0;
- //P2.0作为输出管脚
- P2DIR |= BIT0;
- //P2.1作为输出管脚
- P2DIR |= BIT1;
- //P2.2作为输出管脚
- P2DIR |= BIT2;
- //P2.3作为输出管脚
- P2DIR |= BIT3;
- //P2.4作为输出管脚
- P2DIR |= BIT4;
- //HDS1输出高电平
- P2OUT = 0x10;
- // 管脚P2.7使能中断
- P2IE |= BIT7;
- // 对应的管脚由高到低电平跳变使相应的标志置位
- P2IES |= BIT7;
- //打开中断
- _EINT();
- nFlag = 0;
- return;
- }
- void Init_CLK(void)
- {
- unsigned int i;
- BCSCTL1 = 0X00; //将寄存器的内容清零
- //XT2震荡器开启
- //LFTX1工作在低频模式
- //ACLK的分频因子为1
- do
- {
- IFG1 &= ~OFIFG; // 清除OSCFault标志
- for (i = 0x20; i > 0; i--);
- }
- while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1
- BCSCTL2 = 0X00; //将寄存器的内容清零
- BCSCTL2 += SELM1; //MCLK的时钟源为TX2CLK,分频因子为1
- BCSCTL2 += SELS; //SMCLK的时钟源为TX2CLK,分频因子为1
- }
- void HPI_Write(int nValue,char nRegCode)
- {
- char hi;
- char lo;
- char temp;
- hi = (char)((nValue >> 8) & 0x0ff);
- lo = (char)(nValue & 0x0ff);
- //P1口为输出方向
- P1DIR = 0xFF;
- //传输第一个字节
- P1OUT = lo;
- //设置HDS1为高
- temp = (char)(nRegCode | 0x10);
- //往P2口输出控制信号
- P2OUT = temp;
- //设置HDS1为低,开始HPI
- P2OUT &= ~(BIT4);
- //设置HDS1为高,结束HPI
- P2OUT |= BIT4;
- //传输第一个字节
- P1OUT = hi;
- //设置HDS1为高、HBIL为高
- temp = (char)(nRegCode | 0x12);
- //往P2口输出控制信号
- P2OUT = temp;
- //设置HDS1为低,开始HPI
- P2OUT &= ~(BIT4);
- //设置HDS1为高,结束HPI
- P2OUT |= BIT4;
- return;
- }
- int HPI_Read(char nRegCode)
- {
- char hi;
- char lo;
- char temp;
- int res;
- //P1口为输入方向
- P1DIR = 0x00;
- //设置HDS1为高,HR/W为高
- temp = (char)(nRegCode | 0x11);
- //往P2口输出控制信号
- P2OUT = temp;
- //设置HDS1为低,开始HPI
- P2OUT &= ~(BIT4);
- lo = P1IN;
- //设置HDS1为高,结束HPI
- P2OUT |= BIT4;
- //读第2个字节
- //设置HDS1为高、HBIL为高
- temp = (char)(nRegCode | 0x13);
- //往P2口输出控制信号
- P2OUT = temp;
- //设置HDS1为低,开始HPI
- P2OUT &= ~(BIT4);
- hi = P1IN;
- //设置HDS1为高,结束HPI
- P2OUT |= BIT4;
- res = (int)(hi);
- res <<= 8;
- res = res + lo;
- return res;
- }
- interrupt [PORT2_VECTOR] void R_B_ISR(void)
- {
- if(P2IFG & BIT7)
- {
- // 清除中断标志位
- P2IFG &= ~(BIT7);
- //清除DSP中断
- HPI_Write(CLR_HINT,HPI_CONTROL_REG);
- //从地址为62开始读数
- //先确定起始地址
- HPI_Write(0x62,HPI_ADDR_REG);
- //读数
- pBuf[0] = HPI_Read(HPI_DATA_REG);
- //设置标志
- nFlag = 1;
- }
- }
- void main(void)
- {
- // 关闭看门狗
- WDTCTL = WDTPW + WDTHOLD;
- // 关闭中断
- _DINT();
- Init_CLK();
- Init_Port();
- //初始化HPI
- HPI_Write(SET_BOB,HPI_CONTROL_REG);
- //初始地址
- HPI_Write(0x60,HPI_ADDR_REG);
- //往DSP写入一个数据
- HPI_Write(12,HPI_DATA_REG);
- //往DSP写入一个数据,地址增加
- HPI_Write(12,HPI_DATA_WITHADDR_INC);
- //中断DSP
- HPI_Write(INT_DSP,HPI_CONTROL_REG);
- for(;;)
- {
- //等待DSP返回
- if(nFlag == 1) break;
- }
- }