| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /******************************************************************************/
- /* -- 居加居-- */
- /*
- // 文件名:
- // 说明:
- // 编写人员:kinve
- // 编写日期:2018-8-21
- // 程序维护:
- // 维护记录:
- //
- //
- */
- // 免责声明:
- // (c) Anxinke Corporation. All rights reserved.
- /******************************************************************************/
- #include <string.h>
- #include "Common.h"
- #include "Delay.h"
- #include "Timer.h"
- #include "Interrupt.h"
- #include "Flash.h"
- #include "Version.h"
- #define USE_2401_API
- #ifdef USE_2401_API
- #include "nRF24L01_API.h"
- #else
- #include "NRF24L01_inte.H"
- #define TX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
- #define RX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
- #endif
- unsigned char P12_Led_state = 0;
- //***********************************************************************************************************
- // Application: SPI Function
- // Master send 0x90 and recevie 0x4E
- // Master send 0x01 and recevie 0x55
- // Master send 0x02 and recevie 0x56
- // Master send 0x03 and recevie 0x4F
- // Master send 0x04 and recevie 0x54
- //
- // Master recevie 0x4E and 0x4F form slave after transmitting
- //***********************************************************************************************************
- //-----------------------------------------------------------------------------------------------------------
- void SPI_Error ( void )
- {
- DBG ( "\nSPI error.\n" );
- while ( 1 ) // SPI error and P0.7 flash/
- {
- P07 = 1;
- Timer0_Delay1ms ( 500 );
- P07 = 0;
- Timer0_Delay1ms ( 500 );
- }
- }
- //-----------------------------------------------------------------------------------------------------------
- void SPI_Initial ( void )
- {
- clr_SPIEN; //禁止SPI
- P15_Quasi_Mode; // P15 (SS) Quasi mode
- P10_Quasi_Mode; // P10(SPCLK) Quasi mode
- P00_Quasi_Mode; // P00 (MOSI) Quasi mode
- P01_Quasi_Mode; // P22 (MISO) Quasi mode
- //SS仅在从机有效
- set_DISMODF; // 1 = 禁止模式错误检测. SS的特征依赖SSOE 位
- clr_SSOE; // SS 作为 0.普通IO 1.自动拉低
- clr_LSBFE; // 0.MSB first
- clr_CPOL; // 0.SPI空闲时SCL为低
- clr_CPHA; // 0.在第1个边沿采样
- set_MSTR; // 主设备
- //SPICLK_DIV16; // Select SPI clock=1Mhz
- SPICLK_DIV4; // 16/4=4Mhz, 24l01速度不能太快,最大的数据传输率为 10Mbps
- set_SPIEN; // 使能SPI
- clr_SPIF; // 清标志位
- }
- //-----------------------------------------------------------------------------------------------------------
- void Start_Sending_SPI ( uint8 *pu8MID, uint8 *pu8DID )
- {
- SS = 0;
- SPDR = 0x90;
- Timer3_Delay10us ( 1 ); // Send 0x90 to Slave
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- if ( SPDR != 0x4E )
- SPI_Error();
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0x01; // Send 0x01 to Slave
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- if ( SPDR != 0x55 )
- SPI_Error();
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0x02; // Send 0x02 to Slave
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- if ( SPDR != 0x56 )
- SPI_Error();
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0x03; // Send 0x03 to Slave
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- if ( SPDR != 0x4F )
- SPI_Error();
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0x04; // Send 0x04 to Slave
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- if ( SPDR != 0x54 )
- SPI_Error();
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0xFF;
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- *pu8MID = SPDR; // Receive Slave 1st DATA from Slave
- DBG ( "\nSlave Return %c!\n", SPDR );
- SPDR = 0xFF;
- Timer3_Delay10us ( 1 );
- while ( ! ( SPSR & SET_BIT7 ) );
- clr_SPIF;
- *pu8DID = SPDR; // Receive Slave 2nd DATA from Slave
- DBG ( "\nSlave Return %c!\n", SPDR );
- SS = 1;
- }
- //1.关闭POR (TA 保护) 上电复位(检测电源上升到系统可以工作的电压)
- #ifdef N76E003_IAR
- __sfr __no_init volatile unsigned char PORDIS @ 0XFD;
- #else
- sfr PORDIS = 0XFD;
- #endif
- void CLOSE_POR ( void )
- {
- TA = 0XAA;
- TA = 0X55;
- PORDIS = 0X5A;
- TA = 0XAA;
- TA = 0X55;
- PORDIS = 0XA5;
- }
- /******************************************************************************
- This code illustrates how to do APROM and CONFIG IAP from LDROM.
- APROM are re-programmed by the code to output P1 as 55h and P0 as aah.
- The CONFIG2 is also updated to disable BOD reset.
- User needs to configure CONFIG0 = 0x7F, CONFIG1 = 0xFE, CONFIG2 = 0xFF.
- ******************************************************************************/
- //2.关闭BOD 欠压检测 (监测运行期间VDD电平)
- #ifdef N76E003_IAR
- __sfr __no_init volatile unsigned char BODCON0 @ 0XA3;
- __sfr __no_init volatile unsigned char BODCON1 @ 0XAB;
- //__sfr __no_init volatile unsigned char CONFIG2 @ 0XFD;
- #else
- //sfr BODCON0 = 0XA3;
- //sfr BODCON1 = 0XAB;
- #endif
- void CLOSE_BOD ( void )
- {
- BODCON0 &= ~ ( 1 << 7 ); //0 =禁用欠压检测电路
- }
- enum
- {
- UMKNOW_RESET,
- POWER_ON_RESET, //上电复位
- BROWN_OUT_RESET, //欠压复位
- RST_PIN_RESET, //RST引脚复位
- HARD_FAULT_RESET, //硬件故障复位
- WATCHGOD_TIMER_RESET, //看门狗定时器复位
- SOFTWARE_RESET, //软件复位
- MAX_RESET
- };
- /*
- RST pin reset
- Brown-out reset
- Software reset
- Power-on reset
- Watchgod Timer reset
- Hard Fault reset
- */
- unsigned char Reset_Reason ( void )
- {
- unsigned char res = UMKNOW_RESET;
- if ( PCON & ( 1 << 4 ) )
- {
- PCON &= ~ ( 1 << 4 );
- res = POWER_ON_RESET;
- }
- if ( BODCON0 & ( 1 << 1 ) )
- {
- BODCON0 &= ~ ( 1 << 1 );
- res = BROWN_OUT_RESET;
- }
- if ( AUXR1 & ( 1 << 6 ) )
- {
- AUXR1 &= ~ ( 1 << 6 );
- res = RST_PIN_RESET;
- }
- if ( AUXR1 & ( 1 << 5 ) )
- {
- AUXR1 &= ~ ( 1 << 5 );
- res = HARD_FAULT_RESET;
- }
- if ( WDCON & ( 1 << 3 ) )
- {
- WDCON &= ~ ( 1 << 3 );
- res = WATCHGOD_TIMER_RESET;
- }
- if ( AUXR1 & ( 1 << 7 ) )
- {
- AUXR1 &= ~ ( 1 << 7 );
- res = SOFTWARE_RESET;
- }
- return res;
- }
- void ResetSys ( void )
- {
- AUXR1 &= ~ ( 1 << 7 ); //software reset flag clear
- EA = 0;
- TA = 0xAA;
- TA = 0x55;
- CHPCON |= ( 1 << 7 ); //software reset
- }
- void P12_Flash ( unsigned char num )
- {
- unsigned char i;
- for ( i = 0; i < num && num < 5 ; i++ )
- {
- P12 = 1;
- Timer3_Delay100ms ( 1 );
- P12 = 0;
- Timer3_Delay100ms ( 1 );
- }
- P12_Led_state = P12;
- }
- #if 0
- unsigned char flag;
- //NRF24L01中断处理
- #ifdef N76E003_IAR
- #pragma vector=0x3B
- __interrupt void NRF24L01_Handler ( void )
- #else
- void NRF24L01_Handler ( void ) interrupt 7 //interrupt address is 0x001B
- #endif
- {
- clr_EPI; //关闭管脚中断7
- flag = PIF;
- PIF = 0x00; //清除全部标志
- DBG ( "\nNRF24L01 interrupt!\n" );
- set_EPI; //开启管脚中断7
- }
- void NRF24L01_Interrupt_P03 ( void )
- {
- //P0M1|=SET_BIT3;P0M2&=~SET_BIT3; //input
- PICON &= ~ ( SET_BIT0 | SET_BIT1 );
- PICON |= ( 0x00 ); //[1:0]=0.端口0
- PICON |= SET_BIT5; //通道3=1.边沿触发
- PINEN |= SET_BIT3; // 1 = 低电平/ 下降沿触发中断
- PIPEN &= ~SET_BIT3; //0 = 关闭中断(高电平/ 上升沿触发中断)
- EIE |= SET_BIT1; //管脚中断,最多8个,中断号7
- EA = 1; //总中断
- }
- #endif
- //96位 UID与128位UCID
- unsigned char xdata UIDBuf[12]; //存储96bit的UID内容
- UINT8 UID_BYTE ( UINT8 Addr )
- {
- UINT8 DATATEMP;
- set_IAPEN;
- IAPAL = Addr;
- IAPAH = 0x00;
- IAPCN = READ_UID;
- set_IAPGO;
- DATATEMP = IAPFD;
- clr_IAPEN;
- return DATATEMP;
- }
- //读出芯片的96bit的UID
- void ReadUID_UCID ( UINT8 UID, UINT8 *out )
- {
- UINT8 i;
- for ( i = 0; i < 12; i++ )
- {
- out[i] = UID_BYTE ( i + UID ); //ucid=0x20
- }
- }
- //最长4byte
- #define GET_VER_POS(NUM,VER) ((VER>>(8*NUM))&0xff)
- //-----------------------------------------------------------------------------------------------------------
- void main ( void )
- {
- unsigned char reset_num, i;
- unsigned char datatemp[8];
- unsigned char TxBuf[TX_PLOAD_WIDTH] = {'>', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //
- unsigned char RxBuf[RX_PLOAD_WIDTH] = {0};
- //uint8 u8MID, u8DID;
- reset_num = Reset_Reason();
- CLOSE_POR();
- CLOSE_BOD();
- Set_All_GPIO_Quasi_Mode;
- //NRF24L01_Interrupt_P03();
- //定时器
- Timer0_init ( TIMER_SCAN ); //1ms
- //ResetSys();
- InitialUART0_Timer1 ( 115200 ); /* 115200 Baud Rate*/
- Timer3_Delay10us(1000);
- DBG ( "\nHome Light %s, Build by Kinve in %ld\n", THIS_VER, CUSTOM_VER );
- ReadUID_UCID ( 0x00, UIDBuf ); //0x00=uid 0x20=ucid
- DBG ( "UID=%x%x%x%x\n", UIDBuf[0], UIDBuf[1], UIDBuf[2], UIDBuf[3] );
- //call read byte
- datatemp[5] = Read_APROM_BYTE ( 0x3885 );
- if ( datatemp[5] != 0x4A )
- {
- //call write byte
- for ( i = 0; i < sizeof ( datatemp ); i++ )
- {
- if ( i == 5 ) Write_DATAFLASH_BYTE ( 0x3880 + i, 0x4A );
- else Write_DATAFLASH_BYTE ( 0x3880 + i, GET_VER_POS ( i, CUSTOM_VER ) );
- datatemp[i] = Read_APROM_BYTE ( ( UINT16 code * ) ( 0x3880 + i ) );
- }
- }
- SPI_Initial();
- #ifdef USE_2401_API
- NRF24L01_Wait_Check(); // 等待检测到NRF24L01,程序才会向下执行
- NRF24L01_RT_Init ();
- #else
- NRF24L01_Init();
- DBG ( "NRF24L01 Check Reg: %s\n", ( NRF24L01_FLAG & NRF24L01_CHECK )?("SUCCESS"):("FAIL"));
- #endif
- DBG ( "NRF24L01 init OK!\n" );
- while ( 1 )
- {
- if ( System_Flag_1s )
- {
- System_Flag_1s = 0;
- P12_Led_state = !P12_Led_state;
- P12 = P12_Led_state;
- }
- if ( System_Flag_5s )
- {
- System_Flag_5s = 0;
- //TxBuf[1] = TxBuf[1] == '1' ? '2' : '1';
- #ifdef USE_2401_API
- SEND_BUF ( TxBuf, 10 );
- #else
- NRF24L01_SendFrame ( TxBuf, 10 );
- #endif
- DBG ( "Send: %s\n", TxBuf );
- }
- #ifdef USE_2401_API
- if ( NRF_IRQ == 0 ) // 如果无线模块接收到数据
- {
- if ( NRF24L01_RxPacket ( RxBuf, sizeof(RxBuf) ) )
- {
- P12_Flash ( 2 );
- DBG ( "Recv: %s\n", RxBuf );
- }
- }
- #else
- if ( NRF24L01_FLAG & NRF24L01_TX_ACCESS )
- {
- NRF24L01_FLAG &= ~NRF24L01_TX_ACCESS; //清除发送成功标志
- P12_Flash ( 1 );
- DBG ( "Send: OK\n" );
- }
- if ( NRF24L01_FLAG & NRF24L01_RECIVE )
- {
- NRF24L01_FLAG &= ~NRF24L01_RECIVE; //清除接收成功标志
- NRF24L01_RecvFrame ( RxBuf );
- P12_Flash ( 2 );
- DBG ( "Recv: %s\n", RxBuf );
- }
- #endif
- }
- }
- //-----------------------------------------------------------------------------------------------------------
|