完成单板代码
/*任务5:ZigBee无线模块应用*/
分析ZigBee_Test:
/*******************************************************************************
* Function Name : ZigBee_Test
* Description : ZigBee test.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ZigBee_Test(void)
{
Menu_TypeDef menu; //set a ZigBee_Test menu
uint8_t buf[16]; //give a buffer
uint8_t rd; //the information
uint16_t key, x;
uint32_t i;
/*完成一些初始化工作*/
SPI_IOConfig(1); /*set the SPI I/O config*/
SPI_Init(1, 8, 2); /*init spi*/
SPI752_Init(1, 115200); /*set the SPI*/
menu.max_numb = 3; //set some menu information
menu.numb = 1;
menu.title = "ZigBee";
menu.item[0] = "1.Send Hello ZigBee!";
menu.item[1] = "2.Turn on other FAN";
menu.item[2] = "3.Turn off other FAN";
Dis_Menu(menu);
x = 0;
delay_ms(250);
while(menu.numb)
{
if(UART0_GetChar(&rd)) //if the ZigBee board uart get some information, then use the ZigBee send it out
{
ZigBee_PutChar(rd); //send out by ZigBee
}
if(ZigBee_GetChar(&rd)) //if ZigBee module get some information from another zigbee
{
printf("rd =%d\n",rd);
OLED_DisChar(2-1 + 6, x, 0, rd);
x += 6;
if(x > 120)
{
x = 0;
}
UART0_PutChar(rd); //and send the information out by UART
for(i=0; i<8; i++)
{
buf[i] = buf[i+1];
}
buf[i] = rd;
if((buf[0]=='F') && (buf[1]=='A') //judge the buffer received, this is "FAN On "
&& (buf[2]=='N') && (buf[3]==' ')
&& (buf[4]=='O') && (buf[5]=='n')
&& (buf[6]==' ') && (buf[7]==' '))
{
GPIOSetValue(PORT0, 2, 1); // Turn on Fan
}
else if((buf[0]=='F') && (buf[1]=='A') //judge the buffer received, this is "FAN Off "
&& (buf[2]=='N') && (buf[3]==' ')
&& (buf[4]=='O') && (buf[5]=='f')
&& (buf[6]=='f') && (buf[7]==' '))
{
GPIOSetValue(PORT0, 2, 0); // Turn off Fan
}
}
key = KEY_Read();
switch(key) //key setting
{
case KEY_UP:
if(menu.numb > 1)
menu.numb --;
else
menu.numb = menu.max_numb;
Dis_Menu(menu);
x = 0;
break;
case KEY_DOWN:
if(menu.numb < menu.max_numb)
menu.numb ++;
else
menu.numb = 1;
Dis_Menu(menu);
x = 0;
break;
case KEY_SEL:
switch(menu.numb)
{
case 1:
ZigBee_PutString("Hello ZigBee! ");
printf("Send: Hello ZigBee! \n");
break;
case 2:
ZigBee_PutString("FAN On ");
printf("Send: FAN On \n");
break;
case 3:
ZigBee_PutString("FAN Off ");
printf("Send: FAN Off \n");
break;
default:
break;
}
break;
case KEY_ESC:
menu.numb = 0;
break;
default:
break;
}
if((key!=KEY_NONE) && (menu.numb!=0))
{
delay_ms(250);
}
}
}
void ZigBee_Test(void)
1、SPI_IOConfig(1);
/*****************************************************************************
** Function name: SPI_IOConfig
**
** Descriptions: SPI port initialization routine
**
** parameters: None
** Returned value: None
**
*****************************************************************************/
void SPI_IOConfig(uint8_t portNum)
{
if(portNum == 0)
{
LPC_SYSCON->PRESETCTRL |= (0x1<<0);
LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<11);
LPC_SYSCON->SSP0CLKDIV = 0x02; /* Divided by 2 */
LPC_IOCON->PIO0_8 &= ~0x07; /* SSP I/O config */
LPC_IOCON->PIO0_8 |= 0x01; /* SSP MISO */
LPC_IOCON->PIO0_9 &= ~0x07;
LPC_IOCON->PIO0_9 |= 0x01; /* SSP MOSI */
LPC_IOCON->SCK_LOC = 0x02;
LPC_IOCON->PIO0_6 = 0x02; /* P0.6 function 2 is SSP clock, need to
combined with IOCONSCKLOC register setting */
/* Enable AHB clock to the GPIO domain. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_IOCON->PIO2_4 &= ~0x07; /* SSP SSEL0 is a GPIO pin, for OLED */
/* port2, bit 4 is set to GPIO output and high */
GPIOSetDir( PORT2, 4, 1 );
GPIOSetValue( PORT2, 4, 1 );
LPC_IOCON->PIO2_6 &= ~0x07; /* SSP SSEL1 is a GPIO pin, for FLASH */
/* port2, bit 6 is set to GPIO output and high */
GPIOSetDir( PORT2, 6, 1 );
GPIOSetValue( PORT2, 6, 1 );
LPC_IOCON->PIO2_7 &= ~0x07; /* SSP SSEL2 is a GPIO pin, for RFID */
/* port2, bit 5 is set to GPIO output and high */
GPIOSetDir( PORT2, 7, 1 );
GPIOSetValue( PORT2, 7, 1 );
}
else
/* port number 2, spi use gpio port2 to communicate with the SC16IS752IPW*/
/*MCU port2 for spi */
/*to the SC16IS752IPW*/
/*and transform to the uart */
/*to communicate with the zigbee module*/
{
LPC_SYSCON->PRESETCTRL |= (0x1<<2);
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<18);
LPC_SYSCON->SSP1CLKDIV = 0x02; /* Divided by 2 */
LPC_IOCON->PIO2_2 &= ~0x07; /* SSP I/O config */
LPC_IOCON->PIO2_2 |= 0x02; /* SSP MISO */
LPC_IOCON->PIO2_3 &= ~0x07;
LPC_IOCON->PIO2_3 |= 0x02; /* SSP MOSI */
LPC_IOCON->PIO2_1 &= ~0x07;
LPC_IOCON->PIO2_1 |= 0x02; /* SSP CLK */
/* Enable AHB clock to the GPIO domain. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_IOCON->PIO2_0 &= ~0x07; /* SSP SSEL is a GPIO pin */
/* port2, bit 0 is set to GPIO output and high */
GPIOSetDir( PORT2, 0, 1 );
GPIOSetValue( PORT2, 0, 1 );
}
}
2、SPI_Init(1, 8, 2);
/*****************************************************************************
** Function name: SPI_Init
**
** Descriptions: SPI port initialization routine
**
** parameters: - portNum //at this we have two spi interface, so need init two
** 0: SPI0
** 1: SPI1
** - EvenDiv, speed rate Div, 2-254
** Returned value: None
**
*****************************************************************************/
void SPI_Init(uint8_t portNum, uint8_t Bit, uint8_t EvenDiv)
{
uint8_t i, Dummy=Dummy;
if(portNum == 0)
{
/* Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 15 */
LPC_SSP0->CR0 = 0x0700 | (Bit-1);
/* SSPCPSR clock prescale register, master mode, minimum divisor is EvenDiv(2-254) */
LPC_SSP0->CPSR = EvenDiv;
for ( i = 0; i < SSP_FIFOSIZE; i++ )
{
Dummy = LPC_SSP0->DR; /* clear the RxFIFO */
}
/* Enable the SSP Interrupt */
NVIC_EnableIRQ(SSP0_IRQn);
/* Device select as master, SSP Enabled */
/* Master mode */
LPC_SSP0->CR1 = SSPCR1_SSE;
/* Set SSPINMS registers to enable interrupts */
/* enable all error related interrupts */
LPC_SSP0->IMSC = SSPIMSC_RORIM | SSPIMSC_RTIM;
}
else
{
/* Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 15 */
LPC_SSP1->CR0 = 0x0700 | (Bit-1);
/* SSPCPSR clock prescale register, master mode, minimum divisor is 0x02 */
LPC_SSP1->CPSR = EvenDiv;
for ( i = 0; i < SSP_FIFOSIZE; i++ )
{
Dummy = LPC_SSP1->DR; /* clear the RxFIFO */
}
/* Enable the SSP Interrupt */
NVIC_EnableIRQ(SSP1_IRQn);
/* Device select as master, SSP Enabled */
/* Master mode */
LPC_SSP1->CR1 = SSPCR1_SSE;
/* Set SSPINMS registers to enable interrupts */
/* enable all error related interrupts */
LPC_SSP1->IMSC = SSPIMSC_RORIM | SSPIMSC_RTIM;
}
}
2.1、NVIC_EnableIRQ(SSP0_IRQn);
/**
* @brief Enable Interrupt in NVIC Interrupt Controller
*
* @param IRQn_Type IRQn specifies the interrupt number
* @return none
*
* Enable a device specific interupt in the NVIC interrupt controller.
* The interrupt number cannot be a negative value.
*/
static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
{
NVIC->ISER[0] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* enable interrupt */
}
3、SPI752_Init(1, 115200);
/*******************************************************************************
* Function Name : SPI752_Init
* Description : Set channel 0 & 1 baud rate. The range is 300-230400 Baud.
* The crystal input frequency is 14745600Hz.
* The default value of prescaler after reset is divide-by-1.
* The format is: 8N1
* Input : - Channel : 0 & 1.
* - Baud : 300-230400 Baud.
* Output : None
* Return : None
*******************************************************************************/
void SPI752_Init(uint8_t Channel, uint32_t Baud)
{
uint16_t rd;
// Disable sleep
SPI752_RegWrite(Channel, SPI752_LCR_RW, 0xBF);
rd = SPI752_RegRead(Channel, SPI752_EFR_RW);
SPI752_RegWrite(Channel, SPI752_EFR_RW, rd | 0x10);
SPI752_RegWrite(Channel, SPI752_LCR_RW, 0x03);
rd = SPI752_RegRead(Channel, SPI752_IER_RW);
SPI752_RegWrite(Channel, SPI752_IER_RW, rd & (~0x10));
rd = SPI752_RegRead(Channel, SPI752_MCR_RW);
SPI752_RegWrite(Channel, SPI752_MCR_RW, rd & (~0x80));
// Set baud rate & 8N1 format
SPI752_RegWrite(Channel, SPI752_LCR_RW, 0x83);
rd = (14745600/16) / Baud;
SPI752_RegWrite(Channel, SPI752_DLL_RW, rd);
SPI752_RegWrite(Channel, SPI752_DLH_RW, rd>>8);
SPI752_RegWrite(Channel, SPI752_LCR_RW, 0x03);
SPI752_RegRead(Channel, SPI752_RHR_R);
// use port3_3 as input event, ZigBee interrupt.
GPIOSetDir(PORT3, 3, 0);
// port3_3 interrupt. edge, single trigger, falling edges.
GPIOSetInterrupt(PORT3, 3, 0, 0, 0);
GPIOIntEnable(PORT3, 3);
// Set SPI752 RXDx interrupt Enable.
//rd = SPI752_RegRead(1, SPI752_IER_RW);
//SPI752_RegWrite(Channel, SPI752_IER_RW, rd | 0x01);
SPI752_RegWrite(Channel, SPI752_IER_RW, 0x01);
SPI752_RegRead(Channel, SPI752_RHR_R);
}
3.1、SPI752_RegWrite(Channel, SPI752_LCR_RW, 0xBF);
/*******************************************************************************
* Function Name : SPI752_RegWrite
* Description : Write 8bit data to Registor.
* Input : - Channel : 0 & 1.
* - Reg : 0x00 - 0x0f.
* - Data : 8bit data.
* Output : None
* Return : None
*******************************************************************************/
void SPI752_RegWrite(uint8_t Channel, uint8_t Reg, uint8_t Data)
{
SPI_UART_CS(0);
SPI_PutGet(1, SPI752_WRITE | (Reg<<3) | (Channel<<1));
SPI_PutGet(1, Data);
SPI_UART_CS(1);
}
3.1.1、SPI_UART_CS(0);
#define SPI_UART_CS(x) GPIOSetValue(PORT2, 0, x)
3.1.2、SPI_PutGet(1, SPI752_WRITE | (Reg<<3) | (Channel<<1));
/*****************************************************************************
** Function name: SPI_PutGet
**
** Descriptions: SPI port send & receive
**
** parameters: - portNum
** 0: SPI0
** 1: SPI1
** - SendData, send data
**
** Returned value: - Get data
**
*****************************************************************************/
uint16_t SPI_PutGet(uint8_t portNum, uint16_t SendData)
{
if(portNum == 0)
{
/* Move on only if NOT busy and TX FIFO not full. */
while((LPC_SSP0->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
LPC_SSP0->DR = SendData;
/* Wait until the Busy bit is cleared. */
while(LPC_SSP0->SR & SSPSR_BSY);
/* Wait until the Busy bit is cleared */
while((LPC_SSP0->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE);
return LPC_SSP0->DR;
}
else
{
/* Move on only if NOT busy and TX FIFO not full. */
while((LPC_SSP1->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
LPC_SSP1->DR = SendData;
/* Wait until the Busy bit is cleared. */
while(LPC_SSP1->SR & SSPSR_BSY);
/* Wait until the Busy bit is cleared */
while((LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE);
return LPC_SSP1->DR;
}
}
3.2、rd = SPI752_RegRead(Channel, SPI752_EFR_RW);
/*******************************************************************************
* Function Name : SPI752_RegRead
* Description : Read Registor.
* Input : - Channel : 0 & 1.
* - Reg : 0x00 - 0x0f.
* Output : None
* Return : Registor value.
*******************************************************************************/
uint8_t SPI752_RegRead(uint8_t Channel, uint8_t Reg)
{
uint8_t rd;
SPI_UART_CS(0);
SPI_PutGet(1, SPI752_READ | (Reg<<3) | (Channel<<1));
rd = SPI_PutGet(1, 0);
SPI_UART_CS(1);
return rd;
}
4、UART0_GetChar()
/*******************************************************************************
* Function Name : UART0_GetChar
* Description : print format string.
* Input : fmt
* Output : None
* Return : None
*******************************************************************************/
uint8_t UART0_GetChar(uint8_t *ch)
{
if(UART_op != UARTCount)
{
*ch = UARTBuffer[UART_op];
UART_op ++;
if(UART_op >= UART0_RBUF_SIZE)
UART_op = 0;
return 1;
}
return 0;
}
5、UART0_PutChar()
/*******************************************************************************
* Function Name : UART0_PutChar
* Description : Send a char to uart0 channel.
* Input : c
* Output : None
* Return : None
*******************************************************************************/
void UART0_PutChar(char ch)
{
while(!(LPC_UART->LSR & LSR_THRE));
LPC_UART->THR = ch;
}
6、ZigBee_PutChar()
void ZigBee_PutChar(uint8_t Ch)
{
SPI752_PutChar(1, Ch);
}
6.1、SPI752_PutChar
/*******************************************************************************
* Function Name : SPI752_PutChar
* Description : Use SPI752 channel 0 & 1 send a byte.
* Input : - Channel : 0 & 1.
* - Ch : 8bit data.
* Output : None
* Return : None
*******************************************************************************/
void SPI752_PutChar(uint8_t Channel, uint8_t Ch)
{
while(!(SPI752_RegRead(Channel, SPI752_LSR_R)&0x20));
SPI752_RegWrite(Channel, SPI752_THR_W, Ch);
}
6.1.1、SPI752_RegRead
/*******************************************************************************
* Function Name : SPI752_RegRead
* Description : Read Registor.
* Input : - Channel : 0 & 1.
* - Reg : 0x00 - 0x0f.
* Output : None
* Return : Registor value.
*******************************************************************************/
uint8_t SPI752_RegRead(uint8_t Channel, uint8_t Reg)
{
uint8_t rd;
SPI_UART_CS(0);
SPI_PutGet(1, SPI752_READ | (Reg<<3) | (Channel<<1));
rd = SPI_PutGet(1, 0);
SPI_UART_CS(1);
return rd;
}
6.1.1.1、SPI_UART_CS(0);
#define SPI_UART_CS(x) GPIOSetValue(PORT2, 0, x)
6.1.1.2、SPI_PutGet
/*****************************************************************************
** Function name: SPI_PutGet
**
** Descriptions: SPI port send & receive
**
** parameters: - portNum
** 0: SPI0
** 1: SPI1
** - SendData, send data
**
** Returned value: - Get data
**
*****************************************************************************/
uint16_t SPI_PutGet(uint8_t portNum, uint16_t SendData)
{
if(portNum == 0)
{
/* Move on only if NOT busy and TX FIFO not full. */
while((LPC_SSP0->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
LPC_SSP0->DR = SendData;
/* Wait until the Busy bit is cleared. */
while(LPC_SSP0->SR & SSPSR_BSY);
/* Wait until the Busy bit is cleared */
while((LPC_SSP0->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE);
return LPC_SSP0->DR;
}
else
{
/* Move on only if NOT busy and TX FIFO not full. */
while((LPC_SSP1->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
LPC_SSP1->DR = SendData;
/* Wait until the Busy bit is cleared. */
while(LPC_SSP1->SR & SSPSR_BSY);
/* Wait until the Busy bit is cleared */
while((LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE);
return LPC_SSP1->DR;
}
}
6.1.2、SPI752_RegWrite
/*******************************************************************************
* Function Name : SPI752_RegWrite
* Description : Write 8bit data to Registor.
* Input : - Channel : 0 & 1.
* - Reg : 0x00 - 0x0f.
* - Data : 8bit data.
* Output : None
* Return : None
*******************************************************************************/
void SPI752_RegWrite(uint8_t Channel, uint8_t Reg, uint8_t Data)
{
SPI_UART_CS(0);
SPI_PutGet(1, SPI752_WRITE | (Reg<<3) | (Channel<<1));
SPI_PutGet(1, Data);
SPI_UART_CS(1);
}
7、ZigBee_GetChar()
uint8_t ZigBee_GetChar(uint8_t *Ch)
{
return SPI752_GetChar(1, Ch);
}
7.1、SPI752_GetChar
/*******************************************************************************
* Function Name : SPI752_GetChar
* Description : Use SPI572 channel 0 & 1 receive a byte.
* Input : - Channel : 0 & 1.
* - *Ch : 8bit data.
* Output : None
* Return : - 0, none receive
* - 1, get a byte
*******************************************************************************/
uint8_t SPI752_GetChar(uint8_t Channel, uint8_t *Ch)
{
if(Channel == 0)
{
if(SPI752_rbuf_0_op != SPI752_rbuf_0_ip)
{
*Ch = SPI752_rbuf_0[SPI752_rbuf_0_op];
SPI752_rbuf_0_op ++;
if(SPI752_rbuf_0_op >= SPI752_RBUF_0_NUMB)
SPI752_rbuf_0_op = 0;
return 1;
}
}
else
{
if(SPI752_rbuf_1_op != SPI752_rbuf_1_ip)
{
*Ch = SPI752_rbuf_1[SPI752_rbuf_1_op];
SPI752_rbuf_1_op ++;
if(SPI752_rbuf_1_op >= SPI752_RBUF_1_NUMB)
SPI752_rbuf_1_op = 0;
return 1;
}
}
return 0;
}