PIC24F16KL402开发板使用笔记五: SPI与ARM RK3188例程

 

SPI数据发送例程:

  1. /*
  2. * File: main_xc16.c
  3. * Author: Aric Wang
  4. *
  5. * Created on 2015 3.24
  6. * DESC: Demo code for PIC24F16KL402 SPI communication.
  7. */
  8. #include "xc.h"
  9. _FBS
  10. (
  11. BWRP_OFF & // Boot Segment Write Protect (Disabled)
  12. BSS_OFF // Boot segment Protect (No boot flash segment)
  13. )
  14. //_FGS
  15. //(
  16. // GWRP_OFF & // General Segment Flash Write Protect (General segment may be written)
  17. // GSS0_OFF // General Segment Code Protect (No Protection)
  18. //)
  19. _FOSCSEL
  20. (
  21. FNOSC_LPRC & // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
  22. // SOSCSRC_ANA & // SOSC Source Type (Analog Mode for use with crystal)
  23. // LPRCSEL_HP & // LPRC Power and Accuracy (High Power/High Accuracy)
  24. IESO_ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
  25. )
  26. _FOSC
  27. (
  28. // POSCMD_HS & // Primary Oscillator Mode (Primary oscillator disabled)
  29. OSCIOFNC_ON & // CLKO Enable Configuration bit (CLKO output signal enabled)
  30. POSCFREQ_MS & // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
  31. SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
  32. FCKSM_CSECME // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
  33. )
  34. _FWDT
  35. (
  36. // WDTPS_PS32768 & // Watchdog Timer Postscale Select bits (1:32768)
  37. FWPSA_PR128 & // WDT Prescaler bit (WDT prescaler ratio of 1:128)
  38. FWDTEN_OFF & // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
  39. WINDIS_OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
  40. )
  41. // Warning:
  42. // Always enable MCLRE_ON config bit setting so that the MCLR pin function will
  43. // work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
  44. // programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
  45. // functionality, a high-voltage ICSP tool will be required to re-program the
  46. // part in the future.
  47. _FPOR
  48. (
  49. BOREN_BOR3 & // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
  50. PWRTEN_ON & // Power-up Timer Enable (PWRT enabled)
  51. I2C1SEL_PRI & // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
  52. BORV_V18 & // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
  53. MCLRE_ON // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
  54. )
  55. _FICD
  56. (
  57. ICS_PGx3 // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
  58. )
  59. unsigned char data_temp;
  60. void init_channel()
  61. {
  62. //Init SPI pins.
  63. //As master SDI1 is input, SDO1 is ouput, SCK1 is output, SS is output.
  64. ;
  65. }
  66. void init_SPI1_IO()
  67. {
  68. TRISBbits.TRISB10 = 1; //Set SDI1 as an input.
  69. TRISBbits.TRISB13 = 0; //Set SDO1 as an output.
  70. TRISBbits.TRISB11 = 0; //Set SCK1 as an output.
  71. TRISBbits.TRISB15 = 0; //Set SS1 as an output.
  72. TRISAbits.TRISA0 = 0;
  73. TRISBbits.TRISB3 = 0;
  74. }
  75. void init_SPI1(void)
  76. {
  77. // SPI1CON1 setting 
  78. SSP1STAT =0b0000000010000000; //0b 0000 0000 1000 0000 0x0000; 
  79. SSP1CON1 = 0b0000000010100001; //0x0021;//
  80. //SSP1CON3bits.BOEN = 0;
  81. SSP1ADD = 0x000f;
  82. PADCFG1 = 0x0c00; //0b0000 1100 0000 0000
  83. // IFS1bits.SSP1IF = 0;
  84. // IEC1bits.SSP1IE = 1;
  85. // IPC4bits.SSP1IP = 4;
  86. }
  87. void tx_spi_data(unsigned char dat)
  88. {
  89. SSP1BUF = dat;
  90. //while(!IFS1bits.SSP1IF);
  91. while(SSP1STATbits.BF)
  92. // IFS1bits.SSP1IF = 0;
  93. data_temp = SSP1BUF;
  94. }
  95. void delay(unsigned int uiCnt)
  96. {
  97. int i,j;
  98. for(i=0;i<uiCnt;i++)
  99. for(j=0;j<10;j++);
  100. }
  101. int main(void) {
  102. //Data to be sent.
  103. unsigned int tx[10]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  104. int i;
  105. delay(100);
  106. //SSP1BUF=0;
  107. init_channel();
  108. init_SPI1_IO();
  109. init_SPI1();
  110. //SSP1BUF=0;
  111. LATBbits.LATB3 = 1;
  112. // SSP1BUF = 0;
  113. LATBbits.LATB15 = 1;
  114. while(1)
  115. {
  116. for(i=0;i<9;i++)
  117. {
  118. tx_spi_data(tx[i]);
  119. // LATBbits.LATB3 ^= 1;
  120. }
  121. // delay(2);
  122. LATAbits.LATA0 ^= 1;
  123. }
  124. return 0;
  125. }
  126. void __attribute__((interrupt,no_auto_psv)) _SPI1Interrupt(void)
  127. {
  128. //IFS1bits.SSP1IF = 0; //Clear Interrupt status of SPI1
  129. // IEC1bits.SSP1IE = 1;
  130. }
粽子糖果 发表于10-26 10:53 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

粽子糖果
粽子糖果(总统)
金币:41631个|学分:51991个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号