下边是我的dsp2808 sci通信程序:
//////*******************MADE BY WEI XIA ****************앞에 포트3 나 커넥터 포트6******************//////
////////////access using assembler///////////////
#define DINT asm(" setc INTM") // Role: remove interrupt
#define EINT asm(" clrc INTM") // restore interrupt
#define EALLOW asm(" EALLOW") // remove protection
#define EDIS asm(" EDIS") // restore protection
//////////watchdog///////////////
#define XCLK 0x7010
#define WDCR 0x7029 // Name: Watchdog control register
#define WDKEY 0x7025 // Watchdog reset key register
#define PLLCR 0x7021 // Pll control register
#define PCLKCR0 0x701C
#define PCLKCR1 0x701D // Peripheral Clock Control Register
#define HISPCP 0x701A // High-Speed Peripheral Clock Prescaler Register for HSPCLK clock
#define LOSPCP 0x701B // Low-Speed Peripheral Clock Prescaler Register for LSPCLK clock
//////////SCI-A Registers///////
#define SCICCRA 0x7050
#define SCICTL1A 0x7051
#define SCIHBAUDA 0x7052
#define SCILBAUDA 0x7053
#define SCICTL2A 0x7054
#define SCIRXSTA 0x7055
#define SCIRXBUFA 0x7057
#define SCITXBUFA 0x7059
/////////////GPIO//////////////
#define GPAMUX1 0x6F86
#define GPAMUX2 0x6F88
#define GPADIR 0x6F8A
#define GPADAT 0x6FC0
/*
#define GPFMUX 0x70D4
#define GPFDIR 0x70D5
#define GPFDAT 0x70F4
*/
#define BIT_MASK(x) (1 << (x))
#define GetBit(val, bit) (((val) & BIT_MASK(bit))>>(bit))
#define SetBit(val, bit) ((val) |= BIT_MASK(bit))
//#define Togglebit(val, bit) (( val) ^ = BIT_MASK(bit) )
void InitSysCtrl(void) /// initialize system function ///
{
int iVol;
asm(" EALLOW") ; // EALLOW;
// ENABLE WATCH-DOG
*(volatile unsigned int *)WDCR |= 0x0068;
// *(volatile unsigned int *)WDCR |= 0x002D;
// *(volatile unsigned int *)WDKEY = 0x0055;
// *(volatile unsigned int *)WDKEY = 0x00AA;
// PLLCR -> 0x0A == x5 ==> Sysclk : 20MHzx5 = 100MHz
*(volatile unsigned int *)PLLCR = 0x0A;
// Initialize the peripheral clocks [InitPeripheralClocks]
*(volatile unsigned int *)HISPCP = 0x0003;
// McBSP,SCI,SPI
// Our System don't care
// McBSP,SCI는 사용하지 않고, SPC는 외부 Clock을 사용하므로 상관없다.
*(volatile unsigned int *)LOSPCP = 0x0002; //20*5/(2*2)
//LSPCLK의 분주 비율을 정하는 레지스터,리셋후 기분값은010으로 CPU클럭을 4분주해서사용한다. divide 4 ( P5-49)
// bit8: SPI, bit3: ADC, SCIA(10),SCIB(11)
// bit1: EVT.B, bit0: EVT.A
*(volatile unsigned int *)XCLK|=0x0003;//XCLKOUT off
*(volatile unsigned int *)PCLKCR0= 0xFFFF;
*(volatile unsigned int *)PCLKCR1= 0xFFFF; // 0101 1101 0000 1011
/* SysCtrlRegs.PCLKCR.bit.ECANENCLK=1;//14bit
SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;//0bit
SysCtrlRegs.PCLKCR.bit.EVBENCLK=1;//1bit
SysCtrlRegs.PCLKCR.bit.SCIAENCLK=1;//10bit
SysCtrlRegs.PCLKCR.bit.SCIBENCLK=1;//11bit
SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=1;//12bit
SysCtrlRegs.PCLKCR.bit.SPIENCLK=1;//8bit
SysCtrlRegs.PCLKCR.bit.ADCENCLK=1;//3bit
*/
asm(" EDIS"); //EDIS;
for(iVol= 0; iVol< 5000; iVol++);
}
InitScia_for_RS232C(void)
{
///////핀 설정////////
asm(" EALLOW") ;//EALLOW;
*(volatile unsigned long *)GPAMUX2 |=0x05000000;
*(volatile unsigned long *)GPAMUX2 &=0xF5FFFFFF;
asm(" EDIS"); //EDIS;
////// SCI 설정 //////////
*(volatile unsigned int *)SCICCRA=0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
*(volatile unsigned int *)SCICTL1A=0x0003; // enable RX, TX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE,
*(volatile unsigned int *)SCIHBAUDA = 0x01; //BRR 0x0144=324 통신속도(bps)= 9600 bps
*(volatile unsigned int *)SCILBAUDA = 0x44;
SetBit(*(volatile unsigned int *)SCICTL1A,5); // Relinquish SCI from Reset
}
void scia_Tx(char);
char scia_Rx(void);
// Global variables
char temp;
void main(void)
{
asm(" setc INTM"); // DINT;
InitSysCtrl();
InitScia_for_RS232C(); // Init SCI-A
asm(" EALLOW") ; // EALLOW; // remove protection
*(volatile unsigned int *)GPAMUX1 = 0x0055; // GPIO A, 0 ~ 15, I/O (0=GPIO, 1= Not GPIO)
*(volatile unsigned int *)GPADIR = 0xFFFF; // GPIO A, 0 ~ 7, Output (0=input, 1=output)
*(volatile unsigned int *)GPADAT = 0xF4FF; // 0 ~ 7, Output → High로 Set. (0=Low, 1=High)
asm(" EDIS"); // EDIS;
asm(" clrc INTM");//EINT;
while(1)
{
temp = scia_Rx();
if(temp== 'a')
(*(volatile unsigned int *)GPADAT)^=0xFFFF;//A포트 0~7비트 반전 시킨다.
scia_Tx(temp);
}
}
char scia_Rx(void)
{
char data;
while(!(GetBit(*(volatile unsigned int *)SCIRXSTA, 6))); //SCIRXST( 수신 상태 레지스터),RXRDY=0,수신 버퍼에 새론운 데이터가 없다.
data = *(volatile unsigned int *)SCIRXBUFA; //SCIRXBUF(수신 데이터 버퍼 레지스터)는 수신한 데이터가 저장된는 공간이다. Read data
return data;
}
void scia_Tx(char data)
{
while(!(GetBit(*(volatile unsigned int *)SCICTL2A,7))); //SCICTLA(SCI제어 레지스터),TXRDY=0,SCITXBUF에 전송해야 할 데이터가 남아 있음.
*(volatile unsigned int *)SCITXBUFA = data & 0x00FF; //SCITXBUFA(전송 데이터 버퍼 레지스터)는 전송할 데이터를 넣어두는 공간이다.
}
//===========================================================================
// No more.
//===========================================================================
希望大家给予帮助~~ |