6c5e7399cd5533efef658bda6bcbaf576d54e712.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /******************************************************************************/
  2. /* -- 居加居-- */
  3. /*
  4. // 文件名:
  5. // 说明:
  6. // 编写人员:kinve
  7. // 编写日期:2018-8-21
  8. // 程序维护:
  9. // 维护记录:
  10. //
  11. //
  12. */
  13. // 免责声明:
  14. // (c) Anxinke Corporation. All rights reserved.
  15. /******************************************************************************/
  16. #include <string.h>
  17. #include "Common.h"
  18. #include "Delay.h"
  19. #include "Timer.h"
  20. #include "Interrupt.h"
  21. #include "Flash.h"
  22. #include "Version.h"
  23. //#define USE_2401_API
  24. #ifdef USE_2401_API
  25. #include "nRF24L01_API.h"
  26. uchar TX_ADDRESS[TX_ADR_WIDTH] = {0x77, 0x66, 0x55, 0x44, 0x33}; //发送地址
  27. uchar RX_ADDRESS[RX_ADR_WIDTH] = {0x77, 0x66, 0x55, 0x44, 0x33}; //接收地址
  28. uchar rate = 0x0F; //2Mbs, 0dB 低噪声增益开启
  29. #else
  30. #include "NRF24L01_inte.H"
  31. #define TX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
  32. #define RX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
  33. #endif
  34. unsigned char P12_Led_state = 0;
  35. //***********************************************************************************************************
  36. // Application: SPI Function
  37. // Master send 0x90 and recevie 0x4E
  38. // Master send 0x01 and recevie 0x55
  39. // Master send 0x02 and recevie 0x56
  40. // Master send 0x03 and recevie 0x4F
  41. // Master send 0x04 and recevie 0x54
  42. //
  43. // Master recevie 0x4E and 0x4F form slave after transmitting
  44. //***********************************************************************************************************
  45. //-----------------------------------------------------------------------------------------------------------
  46. void SPI_Error ( void )
  47. {
  48. DBG ( "\nSPI error.\n" );
  49. while ( 1 ) // SPI error and P0.7 flash/
  50. {
  51. P07 = 1;
  52. Timer0_Delay1ms ( 500 );
  53. P07 = 0;
  54. Timer0_Delay1ms ( 500 );
  55. }
  56. }
  57. //-----------------------------------------------------------------------------------------------------------
  58. void SPI_Initial ( void )
  59. {
  60. clr_SPIEN; //禁止SPI
  61. P15_Quasi_Mode; // P15 (SS) Quasi mode
  62. P10_Quasi_Mode; // P10(SPCLK) Quasi mode
  63. P00_Quasi_Mode; // P00 (MOSI) Quasi mode
  64. P01_Quasi_Mode; // P22 (MISO) Quasi mode
  65. //SS仅在从机有效
  66. set_DISMODF; // 1 = 禁止模式错误检测. SS的特征依赖SSOE 位
  67. clr_SSOE; // SS 作为 0.普通IO 1.自动拉低
  68. clr_LSBFE; // 0.MSB first
  69. clr_CPOL; // 0.SPI空闲时SCL为低
  70. clr_CPHA; // 0.在第1个边沿采样
  71. set_MSTR; // 主设备
  72. //SPICLK_DIV16; // Select SPI clock=1Mhz
  73. SPICLK_DIV4; // 16/4=4Mhz, 24l01速度不能太快,最大的数据传输率为 10Mbps
  74. set_SPIEN; // 使能SPI
  75. clr_SPIF; // 清标志位
  76. }
  77. //-----------------------------------------------------------------------------------------------------------
  78. void Start_Sending_SPI ( uint8 *pu8MID, uint8 *pu8DID )
  79. {
  80. SS = 0;
  81. SPDR = 0x90;
  82. Timer3_Delay10us ( 1 ); // Send 0x90 to Slave
  83. while ( ! ( SPSR & SET_BIT7 ) );
  84. clr_SPIF;
  85. if ( SPDR != 0x4E )
  86. SPI_Error();
  87. DBG ( "\nSlave Return %c!\n", SPDR );
  88. SPDR = 0x01; // Send 0x01 to Slave
  89. Timer3_Delay10us ( 1 );
  90. while ( ! ( SPSR & SET_BIT7 ) );
  91. clr_SPIF;
  92. if ( SPDR != 0x55 )
  93. SPI_Error();
  94. DBG ( "\nSlave Return %c!\n", SPDR );
  95. SPDR = 0x02; // Send 0x02 to Slave
  96. Timer3_Delay10us ( 1 );
  97. while ( ! ( SPSR & SET_BIT7 ) );
  98. clr_SPIF;
  99. if ( SPDR != 0x56 )
  100. SPI_Error();
  101. DBG ( "\nSlave Return %c!\n", SPDR );
  102. SPDR = 0x03; // Send 0x03 to Slave
  103. Timer3_Delay10us ( 1 );
  104. while ( ! ( SPSR & SET_BIT7 ) );
  105. clr_SPIF;
  106. if ( SPDR != 0x4F )
  107. SPI_Error();
  108. DBG ( "\nSlave Return %c!\n", SPDR );
  109. SPDR = 0x04; // Send 0x04 to Slave
  110. Timer3_Delay10us ( 1 );
  111. while ( ! ( SPSR & SET_BIT7 ) );
  112. clr_SPIF;
  113. if ( SPDR != 0x54 )
  114. SPI_Error();
  115. DBG ( "\nSlave Return %c!\n", SPDR );
  116. SPDR = 0xFF;
  117. Timer3_Delay10us ( 1 );
  118. while ( ! ( SPSR & SET_BIT7 ) );
  119. clr_SPIF;
  120. *pu8MID = SPDR; // Receive Slave 1st DATA from Slave
  121. DBG ( "\nSlave Return %c!\n", SPDR );
  122. SPDR = 0xFF;
  123. Timer3_Delay10us ( 1 );
  124. while ( ! ( SPSR & SET_BIT7 ) );
  125. clr_SPIF;
  126. *pu8DID = SPDR; // Receive Slave 2nd DATA from Slave
  127. DBG ( "\nSlave Return %c!\n", SPDR );
  128. SS = 1;
  129. }
  130. //1.关闭POR (TA 保护) 上电复位(检测电源上升到系统可以工作的电压)
  131. #ifdef N76E003_IAR
  132. __sfr __no_init volatile unsigned char PORDIS @ 0XFD;
  133. #else
  134. sfr PORDIS = 0XFD;
  135. #endif
  136. void CLOSE_POR ( void )
  137. {
  138. TA = 0XAA;
  139. TA = 0X55;
  140. PORDIS = 0X5A;
  141. TA = 0XAA;
  142. TA = 0X55;
  143. PORDIS = 0XA5;
  144. }
  145. /******************************************************************************
  146. This code illustrates how to do APROM and CONFIG IAP from LDROM.
  147. APROM are re-programmed by the code to output P1 as 55h and P0 as aah.
  148. The CONFIG2 is also updated to disable BOD reset.
  149. User needs to configure CONFIG0 = 0x7F, CONFIG1 = 0xFE, CONFIG2 = 0xFF.
  150. ******************************************************************************/
  151. //2.关闭BOD 欠压检测 (监测运行期间VDD电平)
  152. #ifdef N76E003_IAR
  153. __sfr __no_init volatile unsigned char BODCON0 @ 0XA3;
  154. __sfr __no_init volatile unsigned char BODCON1 @ 0XAB;
  155. //__sfr __no_init volatile unsigned char CONFIG2 @ 0XFD;
  156. #else
  157. //sfr BODCON0 = 0XA3;
  158. //sfr BODCON1 = 0XAB;
  159. #endif
  160. void CLOSE_BOD ( void )
  161. {
  162. BODCON0 &= ~ ( 1 << 7 ); //0 =禁用欠压检测电路
  163. }
  164. enum
  165. {
  166. UMKNOW_RESET,
  167. POWER_ON_RESET, //上电复位
  168. BROWN_OUT_RESET, //欠压复位
  169. RST_PIN_RESET, //RST引脚复位
  170. HARD_FAULT_RESET, //硬件故障复位
  171. WATCHGOD_TIMER_RESET, //看门狗定时器复位
  172. SOFTWARE_RESET, //软件复位
  173. MAX_RESET
  174. };
  175. /*
  176. RST pin reset
  177. Brown-out reset
  178. Software reset
  179. Power-on reset
  180. Watchgod Timer reset
  181. Hard Fault reset
  182. */
  183. unsigned char Reset_Reason ( void )
  184. {
  185. unsigned char res = UMKNOW_RESET;
  186. if ( PCON & ( 1 << 4 ) )
  187. {
  188. PCON &= ~ ( 1 << 4 );
  189. res = POWER_ON_RESET;
  190. }
  191. if ( BODCON0 & ( 1 << 1 ) )
  192. {
  193. BODCON0 &= ~ ( 1 << 1 );
  194. res = BROWN_OUT_RESET;
  195. }
  196. if ( AUXR1 & ( 1 << 6 ) )
  197. {
  198. AUXR1 &= ~ ( 1 << 6 );
  199. res = RST_PIN_RESET;
  200. }
  201. if ( AUXR1 & ( 1 << 5 ) )
  202. {
  203. AUXR1 &= ~ ( 1 << 5 );
  204. res = HARD_FAULT_RESET;
  205. }
  206. if ( WDCON & ( 1 << 3 ) )
  207. {
  208. WDCON &= ~ ( 1 << 3 );
  209. res = WATCHGOD_TIMER_RESET;
  210. }
  211. if ( AUXR1 & ( 1 << 7 ) )
  212. {
  213. AUXR1 &= ~ ( 1 << 7 );
  214. res = SOFTWARE_RESET;
  215. }
  216. return res;
  217. }
  218. void ResetSys ( void )
  219. {
  220. AUXR1 &= ~ ( 1 << 7 ); //software reset flag clear
  221. EA = 0;
  222. TA = 0xAA;
  223. TA = 0x55;
  224. CHPCON |= ( 1 << 7 ); //software reset
  225. }
  226. void P12_Flash ( unsigned char num )
  227. {
  228. unsigned char i;
  229. for ( i = 0; i < num && num < 5 ; i++ )
  230. {
  231. P12 = 1;
  232. Timer3_Delay100ms ( 1 );
  233. P12 = 0;
  234. Timer3_Delay100ms ( 1 );
  235. }
  236. P12_Led_state = P12;
  237. }
  238. #if 0
  239. unsigned char flag;
  240. //NRF24L01中断处理
  241. #ifdef N76E003_IAR
  242. #pragma vector=0x3B
  243. __interrupt void NRF24L01_Handler ( void )
  244. #else
  245. void NRF24L01_Handler ( void ) interrupt 7 //interrupt address is 0x001B
  246. #endif
  247. {
  248. clr_EPI; //关闭管脚中断7
  249. flag = PIF;
  250. PIF = 0x00; //清除全部标志
  251. DBG ( "\nNRF24L01 interrupt!\n" );
  252. set_EPI; //开启管脚中断7
  253. }
  254. void NRF24L01_Interrupt_P03 ( void )
  255. {
  256. //P0M1|=SET_BIT3;P0M2&=~SET_BIT3; //input
  257. PICON &= ~ ( SET_BIT0 | SET_BIT1 );
  258. PICON |= ( 0x00 ); //[1:0]=0.端口0
  259. PICON |= SET_BIT5; //通道3=1.边沿触发
  260. PINEN |= SET_BIT3; // 1 = 低电平/ 下降沿触发中断
  261. PIPEN &= ~SET_BIT3; //0 = 关闭中断(高电平/ 上升沿触发中断)
  262. EIE |= SET_BIT1; //管脚中断,最多8个,中断号7
  263. EA = 1; //总中断
  264. }
  265. #endif
  266. //96位 UID与128位UCID
  267. unsigned char xdata UIDBuf[12]; //存储96bit的UID内容
  268. UINT8 UID_BYTE ( UINT8 Addr )
  269. {
  270. UINT8 DATATEMP;
  271. set_IAPEN;
  272. IAPAL = Addr;
  273. IAPAH = 0x00;
  274. IAPCN = READ_UID;
  275. set_IAPGO;
  276. DATATEMP = IAPFD;
  277. clr_IAPEN;
  278. return DATATEMP;
  279. }
  280. //读出芯片的96bit的UID
  281. void ReadUID_UCID ( UINT8 UID, UINT8 *out )
  282. {
  283. UINT8 i;
  284. for ( i = 0; i < 12; i++ )
  285. {
  286. out[i] = UID_BYTE ( i + UID ); //ucid=0x20
  287. }
  288. }
  289. //最长4byte
  290. #define GET_VER_POS(NUM,VER) ((VER>>(8*NUM))&0xff)
  291. //-----------------------------------------------------------------------------------------------------------
  292. void main ( void )
  293. {
  294. unsigned char reset_num, i;
  295. unsigned char datatemp[8];
  296. unsigned char TxBuf[TX_PLOAD_WIDTH] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //
  297. unsigned char RxBuf[RX_PLOAD_WIDTH] = {0};
  298. //uint8 u8MID, u8DID;
  299. reset_num = Reset_Reason();
  300. CLOSE_POR();
  301. CLOSE_BOD();
  302. Set_All_GPIO_Quasi_Mode;
  303. //NRF24L01_Interrupt_P03();
  304. //定时器
  305. Timer0_init ( TIMER_SCAN ); //1ms
  306. //ResetSys();
  307. InitialUART0_Timer1 ( 115200 ); /* 115200 Baud Rate*/
  308. Timer3_Delay10us(1000);
  309. DBG ( "\nHome Light %s, Build by Kinve in %ld\n", THIS_VER, CUSTOM_VER );
  310. ReadUID_UCID ( 0x00, UIDBuf ); //0x00=uid 0x20=ucid
  311. DBG ( "UID=%x%x%x%x\n", UIDBuf[0], UIDBuf[1], UIDBuf[2], UIDBuf[3] );
  312. //call read byte
  313. datatemp[5] = Read_APROM_BYTE ( 0x3885 );
  314. if ( datatemp[5] != 0x4A )
  315. {
  316. //call write byte
  317. for ( i = 0; i < sizeof ( datatemp ); i++ )
  318. {
  319. if ( i == 5 ) Write_DATAFLASH_BYTE ( 0x3880 + i, 0x4A );
  320. else Write_DATAFLASH_BYTE ( 0x3880 + i, GET_VER_POS ( i, CUSTOM_VER ) );
  321. datatemp[i] = Read_APROM_BYTE ( ( UINT16 code * ) ( 0x3880 + i ) );
  322. }
  323. }
  324. SPI_Initial();
  325. #ifdef USE_2401_API
  326. NRF24L01_Wait_Check(); // 等待检测到NRF24L01,程序才会向下执行
  327. NRF24L01_RT_Init ( TX_PLOAD_WIDTH, TX_ADDRESS, TX_ADR_WIDTH, RX_ADDRESS, RX_ADR_WIDTH, rate );
  328. #else
  329. NRF24L01_Init();
  330. DBG ( "NRF24L01 Check Reg: %s\n", ( NRF24L01_FLAG & NRF24L01_CHECK )?("SUCCESS"):("FAIL"));
  331. #endif
  332. DBG ( "NRF24L01 init OK!\n" );
  333. while ( 1 )
  334. {
  335. if ( System_Flag_1s )
  336. {
  337. System_Flag_1s = 0;
  338. P12_Led_state = !P12_Led_state;
  339. P12 = P12_Led_state;
  340. }
  341. if ( System_Flag_5s )
  342. {
  343. System_Flag_5s = 0;
  344. TxBuf[1] = TxBuf[1] == '1' ? '2' : '1';
  345. #ifdef USE_2401_API
  346. SEND_BUF ( TxBuf );
  347. #else
  348. NRF24L01_SendFrame ( TxBuf, 10 );
  349. DBG ( "Send: %s\n", TxBuf );
  350. #endif
  351. }
  352. #ifdef USE_2401_API
  353. if ( NRF_IRQ == 0 ) // 如果无线模块接收到数据
  354. {
  355. if ( NRF24L01_RxPacket ( RxBuf ) )
  356. {
  357. P12_Flash ( RxBuf[1] - '0' );
  358. }
  359. }
  360. #else
  361. if ( NRF24L01_FLAG & NRF24L01_TX_ACCESS )
  362. {
  363. NRF24L01_FLAG &= ~NRF24L01_TX_ACCESS; //清除发送成功标志
  364. P12_Flash ( 1 );
  365. DBG ( "Send: OK\n" );
  366. }
  367. if ( NRF24L01_FLAG & NRF24L01_RECIVE )
  368. {
  369. NRF24L01_FLAG &= ~NRF24L01_RECIVE; //清除接收成功标志
  370. NRF24L01_RecvFrame ( RxBuf );
  371. P12_Flash ( 2 );
  372. DBG ( "Recv: %s\n" );
  373. }
  374. #endif
  375. }
  376. }
  377. //-----------------------------------------------------------------------------------------------------------