📄 myhpitest.c
字号:
#include <hpitest.h>
void C51_Init(void);
void HPI_Init(void);
void HPIC_Write(void);
void HPID_Write(void);
void HPID_Read(void);
void HPIDN_Write(void);
void HPIDN_Read(void);
void HPIA_Write(void);
void delay(unsigned int x);
void SENDCHAR(unsigned char x);
void SENDVAL(unsigned char x);
void SENDHEXDATA(unsigned char x);
unsigned char HEXTOASCII(unsigned char x);
unsigned char i=0,GetData[2];
bit Flag_Hint;
unsigned char flag=0;
unsigned char senddata[20];
unsigned char readdatah[10];
unsigned char readdatal[10];
unsigned char data tt1=0x12;
unsigned char data tt2=0x00;
/*
void uart00_int () interrupt 4 //中断方式接收数据。
{
if(RI==1)
{
flag=SBUF;
RI=0;
TI=0;
}
if(TI==0) //将接收的数据发送到串口。
{
TI=0;
SBUF=flag; //Transmission is initiated by writing to SBUF.
while(TI==0); //wait for the data sending.When the TI=1 indicate finishing send.
}
}
*/
//==============================================================================
void main(void)
{
P0=0;
P1=0xff;
for(i=0;i<20;i++)senddata[i]=i;
flag=0;
//-----------------------------------
//initialize
C51_Init();
HPI_Init();
//enable all the interrupt
EA=1;
GetData[0]=0x07; //dsp 存储数据地址 0x1208
GetData[1]=0x12;
/*
//-------------------------------------UART TEST2--------------------------------------
while (1)
{
unsigned char i,y;
for(i=0;i<256;i++)
{
SENDVAL(i);
SENDCHAR(' ');
if(i==255)
{
SENDCHAR('_');
SENDCHAR('E');
SENDCHAR('N');
SENDCHAR('D');
SENDCHAR('_');
}
}
}
*/
//-------------------------------------------------------------------------------
while(1) //only for test
{
delay(500);
//1 写数据到DSP中去。。
GetData[0]=0x07; //dsp 存储数据地址 0x1208 写前会预加1
GetData[1]=0x12;
HPIA_Write();
for(i=0;i<0xff;i++)
{
GetData[1]=i+1; //高字节
GetData[0]=i; //低字节
HPID_Write();
}
//2 从DSP中读数据出来。
GetData[0]=0x08; //dsp 存储数据地址 0x1208 读后加1
GetData[1]=0x12;
HPIA_Write();
for(i=0;i<10;i++)
{
HPID_Read();
readdatah[i]=GetData[1]; //1高字节
readdatal[i]=GetData[0]; //0高字节
SENDVAL(GetData[1]);
SENDVAL(GetData[0]);
SENDCHAR(' ');
}
//3 写数据到DSP中去。
GetData[0]=0x7f; //dsp 存储数据地址 0x1280
GetData[1]=0x12;
HPIA_Write();
for(i=0;i<10;i++)
{
GetData[1]=readdatah[i]; //高字节
GetData[0]=readdatal[i]; //低字节
HPID_Write();
}
}
while(1)
{
if(Flag_Hint==1)
{
unsigned int i;
int l_data[16];
GetData[0]=0x08; //dsp 存储数据地址 0x1208
GetData[1]=0x12;
HPIA_Write();
for(i=0;i<0x8;i++) //将DSP中的数据读到C51的l_data 中。
{
HPID_Read();
l_data[2*i+1]=GetData[1]; //高字节
l_data[2*i]=GetData[0]; //低字节
TI=0;
SBUF=l_data[2*i+1]; //Transmission is initiated by writing to SBUF.
while(TI==0); //wait for the data sending.When the TI=1 indicate finishing send.
TI=0;
SBUF=55; //Transmission is initiated by writing to SBUF.
while(TI==0); //wait for the data sending.When the TI=1 indicate finishing send.
}
HPIA_Write();
for(i=0;i<0x8;i++) //将51中的数据l_data 写到DSP中去。
{
GetData[1]=l_data[2*i+1];
GetData[0]=l_data[2*i];
HPID_Write();
}
//clear HPI interrupt and trigger DSPINT interrupt
GetData[0]=0x0c;
GetData[1]=0x0c;
HPIC_Write();
//5410为HPI-8 ,正面为主机写HPI
//-----------------------------------------------------------------------
//HPIC: | 15-12 | 11 | 10 | 9 | 8 |7 -4 | 3 | 2 | 1 | 0 |
//----------------------------------------------------------------------|
// | X | HINT | 0 | SMOD| BOB | X | HINT| 0 | SMOD| BOB|
// 1 1 0 0 0 1 1 0 0 0 =0x0c0c
//======================================= 5410===================================================
//--------------------------------5402 OR 5410--主机C51 写 HIPC---------------------------------
//HPIC: | 15-13 | 12 | 11 | 10 | 9 | 8 |7 -5 | 4 | 3 | 2 | 1 | 0
//---------------------------------------------------------|-------------------------------
// | X | XHPIA | HINT | DSPINT| X | BOB | X | XHPIA| HINT | DSPINT| X |BOB|
// 0 0 1 1 0 0 | 0 0 1 1 0 0 =0C0C
//BOB=0 第一个字节为高位(主机读/写);
//主机写1到 DSPINT,则DSP产生一个中断 ,写0没有效果
//主机往DSP里的 HINT 写1清除中断,此HINT只能由DSP置1,由主机清0(也是写1)
// XHPIA=0时,主机写HPIA[15:0]
//HPIENA 5410没有这位,
//reset the Flag
Flag_Hint=0;
}
}
} //main() end
void C51_Init(void)
{
//-------------------------------------------------------------------------------
PLLCON=0x50; //12.5892M
//Configure UART
T3CON = 0x86; //9600 Baud rate
T3FD = 0x12;
SCON = 0x52; //0101 0000 : old=0x52
// SCON = 0x90;
//----------------------------------------------------------------------------------------------
// 7 6 5 4 3 2 1 0
// sm0sm1=01 | sm2=0 (方式2、3) | ren=1 | TB8=0 |RB8=0 |TI=1 |RI
// 方式1;|接收到字符,TI=1,没有停止位;|允许串行接收|为发送的第9位数据|接收到的第9位| 发送中断标志|接收的中断标志
EA = 1; //Enable Global Interrupts
ES=1; //open uart Interrupt
//--------------------------------------------------------------------------------------
//enable int1 and configure the interrupt type
IT0=0; //low level trigger
EX1=1; //Enable int1
EX0=1; //Enable int0
Flag_Hint=0; //initialize the flags
}
void HPI_Init(void)
{
//???? HOE=0; //to enable the boot
//to do:initialize the HPIC
GetData[1]=0x08;
GetData[0]=0x08;
HPIC_Write(); //clear the initial HPIC interrupt flag and set BOB
//------------------------------------54X--------------------------------
//HPIC: | 15-12 | 11 | 10 | 9 | 8 |7 -4 | 3 | 2 | 1 | 0 |
//----------------------------------------------------------------------|---
// | X | HINT | 0 | SMOD| BOB | X | HINT| DSPINT | X | BOB|
// 0 1 0 0 0 0 1 0 0 0 =0x0808
//主机往DSP里的HINT写1清除中断,此HINT只能由DSP置1,由主机清0
//BOB=0第一个字节为高位(主机读/写);SMOD=0共享SAM便能(主机读)
//=============================================================
//--------------------------------5402 OR 5410--主机C51 写 HIPC---------------------------------
//HPIC: | 15-13 | 12 | 11 | 10 | 9 | 8 |7 -5 | 4 | 3 | 2 | 1 | 0
//---------------------------------------------------------|-------------------------------
// | X | XHPIA | HINT | DSPINT| X | BOB | X | XHPIA| HINT | DSPINT| X |BOB|
// 0 0 1 0 0 0 | 0 0 1 0 0 0 =0808
//BOB=0 第一个字节为高位(主机读/写);
//主机写1到 DSPINT,则DSP产生一个中断 ,写0没有效果
//主机往DSP里的 HINT 写1清除中断,此HINT只能由DSP置1,由主机清0(也是写1)
// XHPIA=0时,主机写HPIA[15:0]
//HPIENA 5410没有这位,
}
void int1_isr(void) interrupt INT1_VECT //int1
{
Flag_Hint=1; //set the interrupt flag
GetData[1]=0x08;
GetData[0]=0x08;
HPIC_Write(); //clear the intial HPIC interrupt flag
/*
//judge if it is a valid interrupt
// if(GetData[0]==0x01&&GetData[1]==0x00)
// {
Flag_Hint=1; //set the interrupt flag
GetData[1]=0x08;
GetData[0]=0x08;
HPIC_Write(); //clear the intial HPIC interrupt flag
*/
// }
}
// A15--------A3---------A2----------A1------A0
// HCS-------HRW-------HCNIL0------HCNIL1---HBIL
// 0---------0/1---------0-----------0-------0/1-------HPIC
// 0---------0/1---------1-----------0-------0/1-------HPIA
// 0---------0/1---------0-----------1-------0/1-------HPIDA
// 0---------0/1---------1-----------1-------0/1-------HPID
//HRW =0 表示主机写HPI,HRW=1表示读HPI
//HBIL=0 表示第一个字节,HBIL=1表示第二个字节
//HDS1=WR/,读写指令时,会自动产生。
void HPID_Read(void)
{
HRW=1;
HDS1=1;
HRW=1;
HBIL=0;
HCNTL1=0; //连续地址访问方式
HCNTL0=1;
P0=0x0ff;
HDS1=0; //下降沿锁存
ACC=0; //delay 1 clockS
while(HRDY==0);
GetData[1]=P0; //read the high byte
ACC=0;
ACC=0;
ACC=0; //delay 3 clockS
HDS1=1;
HRW=1;
HBIL=1;
HCNTL1=0;
HCNTL0=1;
HDS1=0;
ACC=0; //delay 1 clockS
while(HRDY==0);
GetData[0]=P0; //read the lower byte
HDS1=1;
// HCS-------HRW-------HCNIL0------HCNIL1---HBIL
// 0---------0/1---------0-----------0-------0/1-------HPIC
// 0----------1----------0-----------1--------0--------HPIDA
}
void HPIDN_Read(void)
{
HRW=1;
HDS1=1;
HRW=1;
HBIL=0;
HCNTL1=1; //无连续地址访问方式
HCNTL0=1;
HDS1=0;
ACC=0; //delay a clock
GetData[1]=P0; //read the high byte
HDS1=1;
HRW=1;
HBIL=1;
HCNTL1=1;
HCNTL0=1;
HDS1=0;
ACC=0; //delay a clock
GetData[0]=P0; //read the lower byte
HDS1=1;
// HCS-------HRW-------HCNIL0------HCNIL1---HBIL
// 0---------0/1---------0-----------0-------0/1-------HPIC
// 0----------1----------0-----------1--------0--------HPIDA
}
void HPIC_Write(void)
{
HRW=0;
HDS1=1;
HRW=0;
HBIL=0;
HCNTL1=0;
HCNTL0=0;
P0= GetData[1];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
HRW=0;
HBIL=1;
HCNTL1=0;
HCNTL0=0;
P0=GetData[0];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
}
void HPID_Write(void)
{
HRW=0;
HDS1=1;
HRW=0;
HBIL=0;
HCNTL1=0;
HCNTL0=1;
P0= GetData[1];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
HRW=0;
HBIL=1;
HCNTL1=0;
HCNTL0=1;
P0=GetData[0];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
}
void HPIDN_Write(void)
{
HRW=0;
HDS1=1;
HRW=0;
HBIL=0;
HCNTL1=1;
HCNTL0=1;
P0= GetData[1];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
HRW=0;
HBIL=1;
HCNTL1=1;
HCNTL0=1;
P0=GetData[0];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
}
void HPIA_Write(void)
{
HRW=0;
HDS1=1;
HRW=0;
HBIL=0;
HCNTL1=1;
HCNTL0=0;
P0= GetData[1];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
HRW=0;
HBIL=1;
HCNTL1=1;
HCNTL0=0;
P0=GetData[0];
HDS1=0;
ACC=0; //delay a clock
HDS1=1;
}
void delay(unsigned int x)
{ unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<x;j++)
{;}
}
//==================================================================================
void SENDCHAR(unsigned char x) // sends ASCII value contained in A to UART
{
while(TI==0);
TI=0;
SBUF=x;
}
void SENDVAL(unsigned char x)
{
unsigned char YL,YH,YCL,YCH;
YL=x&0x0f;
YH=(x>>4)&0x0f;
SENDHEXDATA(YH);
SENDHEXDATA(YL);
}
void SENDHEXDATA(unsigned char x)
{
unsigned char y;
y=HEXTOASCII(x);
SENDCHAR(y);
}
unsigned char HEXTOASCII(unsigned char x) //将半字节数据转换成-0-A的ASCII
{
unsigned char y;
if((x>=0)&&(x<=9))
{ y=x+0x30; }
if((x>=10)&&(x<=15))
{ y=x+0x37;}
if(x>=16) y=0x2b; //'+'
if(x<0) y=0x2d; //'-'
return(y);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -