检一2103做的产品,想拿来学学。查查资料写了个SPI的程序,用示波器看怎么连SCK信号都没有?郁闷了好几天了,芯片没坏,我串口都能出数据。请高手帮忙看看,谢谢! #include "config.h"
#define PWM0CON (*((volatile unsigned long *) 0xE0004074)) /* flash */ #define FlashCmd1 0x03; unsigned char inbuff[32]; unsigned char outbuff[32]; unsigned char in_num; unsigned char out_num; unsigned char out_ptr;
uint8 temp;
unsigned int rec_num; unsigned char flag; uint32 step_num;
void Delay(uint32 dly);
//uint8 rcv_buf[8]; // UART0数据接收缓冲区 //volatile uint8 rcv_new; // 接收新数据标志 /***********FLASH 函数*************/ //spi //dig=(dig%100)+160+((dig/100)+160)*256
#define MSTR (1<<5) // #define CPOL (1<<4) // #define CPHA (1<<3) // #define LSBF (1<<6) //
#define MSPI_MODE (MSTR|CPOL|CPHA) /*接口模式MSTR = 1,CPOL = 1,CPHA = 0,LSBF = 0 */ #define SSPI_MODE (CPHA)
#define SPI_CS (1<<18)
/* void SSpiIni(void) { S1PCR = SSPI_MODE;
} void SSendData(unit8 data) { S1PDR = data; while(0 == (S1PSR&0X80));
} unit8 SRcvData(void) { while(0 == (S1PSR&0x80)); return(S1PDR); }
*/
///////////////////////////////////////////////////////////////////////////////////////////////
void MSPI_Init(uint8 fdiv) //初始化SPI接口,设置为主机fdiv 大于8的偶数 { if(fdiv<8) fdiv = 8; S0PCCR = fdiv&0xfe; //2103没有SPI0和SPI1 !!!!!设置SPI时钟分频 Delay(5555); IOSET|= 1<<9; //deng mei Delay(5555); S0PCR = MSPI_MODE; // S0SPCR?????? // S0PCR = 0x38; IOCLR|= 1<<9; Delay(5555); } uint8 MSendData(uint8 data) { IOCLR = SPI_CS;
S0PDR = data; while(0==(S0PSR&0X80));
IOSET = SPI_CS;
return(S0PDR); }
///////////////////////////////////////////
void Feed(unsigned long step) { step_num = step; T0TCR = 0x01; //启动定时器 IOSET |= 1<<30; //打开电动机电源 }
void __irq InterUart0() { if((U0LSR & 0x01)==1) { IOSET |= 1<<9; inbuff[in_num] = U0RBR; // U0THR = inbuff[in_num]; U0THR = temp; //
in_num++; if(in_num > 32) { in_num=0; } } VICVectAddr = 0; Feed(200); }
void __irq InterTime0() { T0IR = 0x01; //CR3中断;p0.18 复位中断; T0TCR = 0x00; //关闭定时器; // U0THR = 'o'; while( (U0LSR&0x40)==0); VICVectAddr = 0; if(flag==0) { if((IOSET&(1<<11))!=0) { IOCLR |= 1<<11; } else { IOSET |= 1<<11; } flag = 1; } else { if((IOSET&(1<<12))!=0) { IOCLR |= 1<<12; } else { IOSET |= 1<<12; } flag = 0; } if(--step_num!=0) { T0TCR = 0x01; } else { uint32 loop=0xff; while(loop--!=0); IOCLR |= 1<<30; } }
void Uart0Init() { unsigned int fdiv; VICIntSelect &= 0xbf; VICIntEnable |= 1<<6; VICVectCntl1 = 0x26; VICVectAddr1 = (uint32)InterUart0; U0LCR = 0X83; //允许访问除数寄存器在DLAB==1 fdiv = (Fpclk/16)/9600; // U0DLM = fdiv>>8; U0DLL = fdiv&0xff; U0FCR = 0x01; //FIFO控制,BIT U0LCR = 0x03; U0IER = 0x01; //必须在DLAB==0时允许接受中断 }
void Time0Init(void) { T0TC = 0; T0PR = 0; //最大值 T0MCR =0x03; //设置匹配后,复位T0TC,并产生中断; T0MR0 = Fpclk/200; T0TCR = 0x01; //启动定时器;// VICIntEnable |= 1<<4; VICVectAddr2 = (uint32)InterTime0; VICVectCntl2 = 0x24; } void Delay(uint32 dly) { uint32 i; for(;dly > 0;dly--) { for(i=0;i<5000;i++); } }
int main (void) { do { // PINSEL0 = 0x05; PINSEL0 = 0X5505; IODIR |= 0xfff<<9; //p0.9-p0.12为输出模式 IOCLR |= 1<<9; //DENG liang Delay(5555); IOSET |= 1<<9; //DENG mei Delay(5555); //
IODIR |= 1<<30; MSPI_Init(52); ////?????????
// MSendData(0x03); temp = MSendData(0x03); // Feed(temp);
Delay(9999); IOSET |= 1<<30; U0FCR = 0x81; //8bit 可改 使能FIFO,并设置触发点为8字节,就是Rx触发 U0IER = 0x01; IRQEnable(); //IRQ 中断必须开 Uart0Init(); // VICSoftInt =1<<6; Time0Init(); U0IIR = 0x00; IOSET |= 1<<10; //打开1838使能 Feed(400); } while(1); return 0; }
|