[STM32] STM32常用驱动

[复制链接]
4169|23
 楼主| 一路向北lm 发表于 2018-7-31 22:40 | 显示全部楼层 |阅读模式
本帖最后由 一路向北lm 于 2018-7-31 22:54 编辑

驱动大放送

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 一路向北lm 发表于 2018-7-31 22:41 | 显示全部楼层
iic驱动(模拟iic)
  1. /**********************************************************************************/
  2. /*                                                                                */
  3. /*    Copyright (C) 2005 OLIMEX  LTD.                                             */
  4. /*                                                                                */
  5. /*    Module Name    :  i2c module                                                */
  6. /*    File   Name    :  i2c.c                                                     */
  7. /*    Revision       :  01.00                                                     */
  8. /*    Date           :  2005/07/04 initial version                                */
  9. /*                                                                                */
  10. /**********************************************************************************/
  11. #include "i2c.h"

  12. void _NOP(){
  13. volatile int i = 5;
  14. for(;i;--i);
  15. }



  16. extern GPIO_InitTypeDef GPIO_InitStructure;

  17. // SCL -------------------------------------------------------------------------
  18. void SCL_DIR(char state)  {
  19. if(state) {
  20.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
  21.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  23.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  24.   }
  25.   else {
  26.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
  27.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  29.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  30.   }
  31. }

  32. void SCL_OUT(char state) {
  33.   if(state) {
  34.     GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
  35.   }
  36.   else {
  37.     GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);
  38.   }
  39. }



  40. // SDA -------------------------------------------------------------------------
  41. void SDA_DIR(char state)  {
  42.   if(state) {
  43.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  44.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  46.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  47.   }
  48.   else {
  49.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  50.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  51.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  52.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  53.   }
  54. }

  55. void SDA_OUT(char state) {
  56.   if(state) {
  57.     GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
  58.   }
  59.   else {
  60.     GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);
  61.   }
  62. }

  63. char SDA_IN(void) {
  64.    if((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)) == Bit_SET)
  65.      return 1;
  66.    else
  67.      return 0;
  68. }

  69. #define SCL_HIGH  SCL_OUT(1);
  70. #define SCL_LOW   SCL_OUT(0);
  71. #define SDA_HIGH  SDA_DIR(0);
  72. #define SDA_LOW   SDA_DIR(1);



  73. /****************************************************************************/
  74. /*  Initialize I2C interface                                                */
  75. /*  Function : Hrd_I2C_Init                                                 */
  76. /*      Parameters                                                          */
  77. /*          Input   :   Nothing                                             */
  78. /*          Output  :   Nothing                                             */
  79. /****************************************************************************/
  80. void Hrd_I2C_Init(){
  81.   SCL_OUT(0);
  82.   SCL_DIR(1);
  83.   SDA_DIR(0);
  84.   SDA_OUT(0);
  85. }


  86. /****************************************************************************/
  87. /*  Start Conditional for I2C                                               */
  88. /*  Function : Hrd_I2C_StartCond                                            */
  89. /*      Parameters                                                          */
  90. /*          Input   :   Nothing                                             */
  91. /*          Output  :   Nothing                                             */
  92. /****************************************************************************/
  93. void Hrd_I2C_StartCond(){

  94.   SCL_LOW;
  95.   _NOP();
  96.   SDA_HIGH;
  97.   _NOP();
  98.   SCL_HIGH;
  99.   _NOP();
  100.   SDA_LOW;
  101.   _NOP();
  102.   SCL_LOW;
  103. }


  104. /****************************************************************************/
  105. /*  Stop Conditional for I2C                                                */
  106. /*  Function : Hrd_I2C_StopCond                                             */
  107. /*      Parameters                                                          */
  108. /*          Input   :   Nothing                                             */
  109. /*          Output  :   Nothing                                             */
  110. /****************************************************************************/
  111. void Hrd_I2C_StopCond(){

  112.   SCL_LOW;
  113.   SDA_LOW;
  114.   _NOP();
  115.   SCL_HIGH;
  116.   _NOP();
  117.   SDA_HIGH;
  118. }


  119. /****************************************************************************/
  120. /*  Write Byte to I2C                                                       */
  121. /*  Function : Hrd_I2C_WriteByte                                            */
  122. /*      Parameters                                                          */
  123. /*          Input   :   character to write                                  */
  124. /*          Output  :   acknowledge                                         */
  125. /****************************************************************************/
  126. I2C_AKN_DEF Hrd_I2C_WriteByte  (unsigned char ch){

  127.   unsigned int i = 0;
  128.   for(i = 8; i; --i)
  129.   {
  130.     SCL_LOW;
  131.     if(ch&0x80) {
  132.       SDA_HIGH;
  133.     }
  134.     else {
  135.       SDA_LOW;
  136.     }
  137.     _NOP();
  138.     SCL_HIGH;
  139.     ch <<= 1;
  140.     _NOP();
  141.     SCL_LOW;
  142.   }
  143.   SDA_HIGH;
  144.   _NOP();
  145.   SCL_HIGH;
  146.   _NOP();
  147.   i = SDA_IN() ? I2C_NAK: I2C_AKN;
  148.   SCL_LOW;
  149.   return((I2C_AKN_DEF)i);
  150. }


  151. /****************************************************************************/
  152. /*  Read Byte from I2C                                                      */
  153. /*  Function : Hrd_I2C_ReadByte                                             */
  154. /*      Parameters                                                          */
  155. /*          Input   :   need acknowledge                                    */
  156. /*          Output  :   read character                                      */
  157. /****************************************************************************/
  158. unsigned char Hrd_I2C_ReadByte (I2C_AKN_DEF  Akn){

  159.   unsigned char ch =0;
  160.   unsigned int i = 0;
  161.   SDA_HIGH;

  162.   for(i = 8; i; --i)
  163.   {
  164.     SCL_HIGH;
  165.     ch <<=1;
  166.     ch |= SDA_IN() ? 1:0;
  167.     SCL_LOW;
  168.   }
  169.   if (Akn) SDA_LOW;

  170.   SCL_HIGH;
  171.   _NOP();
  172.   SCL_LOW;

  173.   return ch;
  174. }



 楼主| 一路向北lm 发表于 2018-7-31 22:42 | 显示全部楼层
LCD驱动(SPI屏幕)
  1. //lcd_nokia.h
  2. #include "lcd.h"
  3. #include "stm32f10x_lib.h"
  4. #include "arm_comm.h"

  5. // LCD memory index
  6. unsigned int  LcdMemIdx;

  7. // represent LCD matrix
  8. unsigned char  LcdMemory[LCD_CACHE_SIZE];

  9. // simple delay
  10. void Delay(unsigned long a) { while (--a!=0); }


  11. /****************************************************************************/
  12. /*  Init LCD Controler                                                      */
  13. /*  Function : LCDInit                                                      */
  14. /*      Parameters                                                          */
  15. /*          Input   :  Nothing                                              */
  16. /*          Output  :  Nothing                                              */
  17. /****************************************************************************/
  18. void LCDInit(void)
  19. {

  20.   SPI_InitTypeDef  SPI_InitStructure;
  21.   GPIO_InitTypeDef GPIO_InitStructure;

  22.   // Enable SPI1 and GPIOA clocks
  23.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

  24.   // Configure SPI1 pins: NSS, SCK, MISO and MOSI
  25.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  26.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  28.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  29.   // Configure PB2 as Output push-pull, used as D/C
  30.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  31.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  32.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  34.   // D/C high
  35.   GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);

  36.   // Configure PC7 and PC10 as Output push-pull, used as LCD_RES and LCD_E
  37.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_10;
  38.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  40.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  41.   // LCD_E - disable
  42.   GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_SET);
  43.   // Set Reset pin (active low)
  44.   GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);

  45.   // SPI1 configuration
  46.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  47.   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  48.   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  49.   SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  50.   SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  51.   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  52.   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  53.   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  54.   SPI_InitStructure.SPI_CRCPolynomial = 7;
  55.   SPI_Init(SPI1, &SPI_InitStructure);

  56.   // Enable SPI1
  57.   SPI_Cmd(SPI1, ENABLE);

  58.   // Toggle display reset pin.
  59.   GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_RESET);
  60.   Delay(10000);
  61.   GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);
  62.   Delay(10000);

  63.   // Send sequence of command
  64.   LCDSend( 0x21, SEND_CMD );  // LCD Extended Commands.
  65.   LCDSend( 0xC8, SEND_CMD );  // Set LCD Vop (Contrast).
  66.   LCDSend( 0x06, SEND_CMD );  // Set Temp coefficent.
  67.   LCDSend( 0x13, SEND_CMD );  // LCD bias mode 1:48.
  68.   LCDSend( 0x20, SEND_CMD );  // LCD Standard Commands, Horizontal addressing mode.
  69.   LCDSend( 0x08, SEND_CMD );  // LCD blank
  70.   LCDSend( 0x0C, SEND_CMD );  // LCD in normal mode.

  71.   // Clear and Update
  72.   LCDClear();
  73.   LCDUpdate();

  74. }

  75. /****************************************************************************/
  76. /*  Send to LCD                                                             */
  77. /*  Function : LCDSend                                                      */
  78. /*      Parameters                                                          */
  79. /*          Input   :  data and  SEND_CHR or SEND_CMD                       */
  80. /*          Output  :  Nothing                                              */
  81. /****************************************************************************/
  82. void LCDSend(unsigned char data, unsigned char cd) {

  83.   // Enable display controller (active low) -> LCD_E low
  84.   GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_RESET);

  85.   // command or data - D/S low or high
  86.   if(cd == SEND_CHR) {
  87.     GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
  88.   }
  89.   else {
  90.     GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET);
  91.   }

  92.   ///// SEND SPI /////
  93.   // Loop while DR register in not emplty
  94.   while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);

  95.   // Send byte through the SPI1 peripheral
  96.   SPI_SendData(SPI1, data);       

  97.   // Wait to receive a byte
  98.   while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

  99.   // Return the byte read from the SPI bus
  100.   // return SPI_ReceiveData(SPI1);
  101.   ///// SEND SPI END /////


  102.   // Disable display controller -> LCD_E high
  103.   GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_SET);
  104. }

  105. /****************************************************************************/
  106. /*  Update LCD memory                                                       */
  107. /*  Function : LCDUpdate                                                    */
  108. /*      Parameters                                                          */
  109. /*          Input   :  Nothing                                              */
  110. /*          Output  :  Nothing                                              */
  111. /****************************************************************************/
  112. void LCDUpdate ( void )
  113. {

  114.   int i;

  115.   //  Set base address X=0 Y=0
  116.   LCDSend(0x80, SEND_CMD );
  117.   LCDSend(0x40, SEND_CMD );

  118.   //  Serialize the video buffer.
  119.   for (i=0; i<LCD_CACHE_SIZE; i++) {
  120.     LCDSend( LcdMemory[i], SEND_CHR );
  121.   }
  122. }

  123. /****************************************************************************/
  124. /*  Clear LCD                                                               */
  125. /*  Function : LCDClear                                                     */
  126. /*      Parameters                                                          */
  127. /*          Input   :  Nothing                                              */
  128. /*          Output  :  Nothing                                              */
  129. /****************************************************************************/
  130. void LCDClear(void) {

  131.   int i;

  132.   // loop all cashe array
  133.   for (i=0; i<LCD_CACHE_SIZE; i++)
  134.   {
  135.      LcdMemory[i] = 0x0;
  136.   }

  137. }




  138. /****************************************************************************/
  139. /*  Change LCD Pixel mode                                                   */
  140. /*  Function : LcdContrast                                                  */
  141. /*      Parameters                                                          */
  142. /*          Input   :  contrast                                             */
  143. /*          Output  :  Nothing                                              */
  144. /****************************************************************************/
  145. void LCDPixel (unsigned char x, unsigned char y, unsigned char mode )
  146. {
  147.     unsigned int    index   = 0;
  148.     unsigned char   offset  = 0;
  149.     unsigned char   data    = 0;

  150.     // check for out off range
  151.     if ( x > LCD_X_RES ) return;
  152.     if ( y > LCD_Y_RES ) return;

  153.     index = ((y / 8) * 84) + x;
  154.     offset  = y - ((y / 8) * 8);

  155.     data = LcdMemory[index];

  156.     if ( mode == PIXEL_OFF )
  157.     {
  158.         data &= (~(0x01 << offset));
  159.     }
  160.     else if ( mode == PIXEL_ON )
  161.     {
  162.         data |= (0x01 << offset);
  163.     }
  164.     else if ( mode  == PIXEL_XOR )
  165.     {
  166.         data ^= (0x01 << offset);
  167.     }

  168.     LcdMemory[index] = data;

  169. }

  170. /****************************************************************************/
  171. /*  Write char at x position on y row                                       */
  172. /*  Function : LCDChrXY                                                     */
  173. /*      Parameters                                                          */
  174. /*          Input   :  pos, row, char                                       */
  175. /*          Output  :  Nothing                                              */
  176. /****************************************************************************/
  177. void LCDChrXY (unsigned char x, unsigned char y, unsigned char ch )
  178. {
  179.     unsigned int    index   = 0;
  180.     unsigned int    i       = 0;

  181.     // check for out off range
  182.     if ( x > LCD_X_RES ) return;
  183.     if ( y > LCD_Y_RES ) return;

  184.     index = (x*48 + y*48*14)/8 ;

  185.     for ( i = 0; i < 5; i++ )
  186.     {
  187.       LcdMemory[index] = FontLookup[ch - 32][i] << 1;
  188.       index++;
  189.     }

  190. }


  191. /****************************************************************************/
  192. /*  Write char at x position on y row - inverse                             */
  193. /*  Function : LCDChrXY                                                     */
  194. /*      Parameters                                                          */
  195. /*          Input   :  pos, row, char                                       */
  196. /*          Output  :  Nothing                                              */
  197. /****************************************************************************/
  198. void LCDChrXYInverse (unsigned char x, unsigned char y, unsigned char ch )
  199. {
  200.     unsigned int    index   = 0;
  201.     unsigned int    i       = 0;

  202.     // check for out off range
  203.     if ( x > LCD_X_RES ) return;
  204.     if ( y > LCD_Y_RES ) return;

  205.     index = (x*48 + y*48*14)/8 ;

  206.     for ( i = 0; i < 5; i++ )
  207.     {
  208.       LcdMemory[index] = ~(FontLookup[ch - 32][i]);
  209.       index++;

  210.       if(i==4)
  211.         LcdMemory[index] = 0xFF;
  212.     }

  213. }


  214. /****************************************************************************/
  215. /*  Set LCD Contrast                                                        */
  216. /*  Function : LcdContrast                                                  */
  217. /*      Parameters                                                          */
  218. /*          Input   :  contrast                                             */
  219. /*          Output  :  Nothing                                              */
  220. /****************************************************************************/
  221. void LCDContrast(unsigned char contrast) {

  222.     //  LCD Extended Commands.
  223.     LCDSend( 0x21, SEND_CMD );

  224.     // Set LCD Vop (Contrast).
  225.     LCDSend( 0x80 | contrast, SEND_CMD );

  226.     //  LCD Standard Commands, horizontal addressing mode.
  227.     LCDSend( 0x20, SEND_CMD );
  228. }


  229. /****************************************************************************/
  230. /*  Send string to LCD                                                      */
  231. /*  Function : LCDStr                                                       */
  232. /*      Parameters                                                          */
  233. /*          Input   :  row, text, inversion                                 */
  234. /*          Output  :  Nothing                                              */
  235. /****************************************************************************/
  236. void LCDStr(unsigned char row, unsigned char *dataPtr, unsigned char inv ) {

  237.   // variable for X coordinate
  238.   unsigned char x = 0;

  239.   // loop to the and of string
  240.   while ( *dataPtr ) {

  241.     if(inv) {
  242.       LCDChrXYInverse(x, row, (*dataPtr));
  243.     }
  244.     else {
  245.       LCDChrXY( x, row, (*dataPtr));
  246.     }
  247.     x++;
  248.     dataPtr++;
  249.   }
  250. }





 楼主| 一路向北lm 发表于 2018-7-31 22:43 | 显示全部楼层
2.4G无心模块 (NRF24L01)

  1. #include "stm32f10x_lib.h"
  2. #include "arm_comm.h"
  3. #include "nRF24L01.h"
  4. #include "const.h"

  5. #define CSN_TIME      2
  6. #define CE_HIGH_TIME  10000

  7. unsigned char RX_ADDRESS_P0[5]  = {5,6,7,8,9};
  8. unsigned char RX_ADDRESS_P1[5]  = {0,1,2,3,4};
  9. unsigned char TX_ADDRESS[5]     = {5,6,7,8,9};
  10. unsigned char ADDRESS[5];

  11. unsigned char status;

  12. // just simple delay
  13. void sDelay (unsigned long a) { while (--a!=0); }


  14. // Chip Select -> high
  15. void CSN_HIGH (void)  { GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); }
  16. // Chip Select -> low
  17. void CSN_LOW (void)   { sDelay(CSN_TIME); GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); }

  18. // Chip enable High
  19. void CE_HIGH(void)    { GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET);  sDelay(CE_HIGH_TIME); }
  20. // Chip enable low
  21. void CE_LOW(void)     { GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET); }


  22. // Init SPI0 Interface
  23. void SPI1Init (void) {

  24.   SPI_InitTypeDef  SPI_InitStructure;
  25.   GPIO_InitTypeDef GPIO_InitStructure;

  26.   // Enable SPI1 and GPIOA clocks
  27.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

  28.   // Configure SPI1 pins: NSS, SCK, MISO and MOSI
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  30.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  32.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  33.   // Reset SPI Interface
  34.   SPI_DeInit(SPI1);

  35.   // SPI1 configuration
  36.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  37.   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  38.   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  39.   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  40.   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  41.   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  42.   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  43.   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  44.   SPI_InitStructure.SPI_CRCPolynomial = 7;
  45.   SPI_Init(SPI1, &SPI_InitStructure);

  46.   // Enable SPI1
  47.   SPI_Cmd(SPI1, ENABLE);

  48.   // Chip select - output
  49.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  50.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  51.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  52.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  53.   // Chip enable - output
  54.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  55.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  56.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  57.   GPIO_Init(GPIOC, &GPIO_InitStructure);

  58.   // IRQ pin - input
  59.   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
  60.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  61.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  62.   GPIO_Init(GPIOC, &GPIO_InitStructure);

  63.   // Button B1
  64.   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13;
  65.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  66.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67.   GPIO_Init(GPIOC, &GPIO_InitStructure);

  68.   // Button WAKE-UP
  69.   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
  70.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  71.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  73.    // Led PC12 as output
  74.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  75.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  77.   GPIO_Init (GPIOC, &GPIO_InitStructure);

  78.   // Set chip select and chip enable
  79.   CSN_HIGH();
  80.   CE_LOW();

  81. }

  82. // Transmit byte via SPI0 chanel
  83. unsigned char  SPI_SendByte(unsigned char data) {

  84.   // Loop while DR register in not emplty
  85.   while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);

  86.   // Send byte through the SPI1 peripheral
  87.   SPI_SendData(SPI1, data);       

  88.   // Wait to receive a byte
  89.   while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

  90.   // Return the byte read from the SPI bus
  91.   return SPI_ReceiveData(SPI1);
  92. }

  93. unsigned char SPI_Send_command_with_ADDR (unsigned char cmd, unsigned char addr, unsigned char data_byte)
  94. {
  95.   unsigned char temp,command = 0;
  96.   command = (cmd << 5) | addr;
  97.   CSN_LOW();
  98.   if (cmd == R_REGISTER)
  99.   {
  100.     if (addr == RX_ADDR_P0 || addr == RX_ADDR_P1 || addr == TX_ADDR)
  101.     {

  102.       status=SPI_SendByte(command);
  103.       for (int k=0;k!=5;k++)
  104.       {
  105.         ADDRESS[k]=SPI_SendByte(NOP);
  106.       }
  107.       CSN_HIGH();
  108.       return status;
  109.     }
  110.     else
  111.     {
  112.       status=SPI_SendByte(command);
  113.       temp=SPI_SendByte(NOP);
  114.       CSN_HIGH();
  115.       return temp;
  116.     }
  117.   }
  118.   if (cmd == W_REGISTER)
  119.   {
  120.     if (addr == RX_ADDR_P0)
  121.     {
  122.       status=SPI_SendByte(command);
  123.       for (int j=0;j!=5;j++)
  124.         {
  125.           temp=RX_ADDRESS_P0[j];
  126.           SPI_SendByte(temp);
  127.         }
  128.       CSN_HIGH();
  129.       return status;
  130.     }

  131.     if (addr == RX_ADDR_P1)
  132.     {
  133.       status=SPI_SendByte(command);
  134.       for (int j=0;j!=5;j++)
  135.         {
  136.           temp=RX_ADDRESS_P1[j];
  137.           SPI_SendByte(temp);
  138.         }
  139.       CSN_HIGH();
  140.       return status;
  141.     }

  142.     if (addr == TX_ADDR)
  143.     {
  144.       status=SPI_SendByte(command);
  145.       for (int j=0;j!=5;j++)
  146.         {
  147.           temp=TX_ADDRESS[j];
  148.           SPI_SendByte(temp);
  149.         }
  150.       CSN_HIGH();
  151.       return status;
  152.     }


  153.     else
  154.     {
  155.       temp=SPI_SendByte(command);
  156.       SPI_SendByte(data_byte);
  157.       CSN_HIGH();
  158.       return temp;
  159.     }
  160.   }

  161.   return 1;
  162. }

  163. unsigned char SPI_Send_command_without_ADDR (unsigned char cmd, unsigned char data_byte)
  164. {
  165.   unsigned char temp = 0;
  166.   CSN_LOW();
  167.   if (cmd == R_RX_PAYLOAD)
  168.   {
  169.     status=SPI_SendByte(cmd);
  170.     temp=SPI_SendByte(NOP);
  171.     CSN_HIGH();
  172.     return temp;
  173.   }
  174.   if (cmd == W_TX_PAYLOAD)
  175.   {
  176.     status=SPI_SendByte(cmd);
  177.     SPI_SendByte(data_byte);
  178.     CSN_HIGH();
  179.     return status;
  180.   }
  181.   status = SPI_SendByte(cmd);
  182.   CSN_HIGH();
  183.   return status;

  184. }

  185. // Setting for nRF chip
  186. void nRFSetting(void) {

  187.   // Init SPI
  188.   SPI1Init();

  189.   // Discard transmision
  190.   CE_LOW();

  191.   // Write CONFIG register (addres - 0x00)
  192.   //00001010 - CRC enable, power-up, RX
  193.   status = SPI_Send_command_with_ADDR(W_REGISTER, CONFIG_REG_ADDR, 0x0B);
  194.   // read
  195.   status = SPI_Send_command_with_ADDR(R_REGISTER, CONFIG_REG_ADDR, NOP);

  196.   // Write RX_ADDR_P0 register -> Set receive address data Pipe0 -> address in RX_ADDRESS_P0 array
  197.   status = SPI_Send_command_with_ADDR(W_REGISTER, RX_ADDR_P0, NOP);
  198.   // read
  199.   status = SPI_Send_command_with_ADDR(R_REGISTER, RX_ADDR_P0, NOP);

  200.   // Write RX_ADDR_P1 register -> Set receive address data Pipe1 -> address in RX_ADDRESS_P1 array
  201.   status = SPI_Send_command_with_ADDR(W_REGISTER, RX_ADDR_P1, NOP);
  202.   // read
  203.   status = SPI_Send_command_with_ADDR(R_REGISTER,RX_ADDR_P1, NOP);

  204.   // Write TX_ADDR register -> Transmit address. Used for a PTX device only. Address in TX_ADDRESS array
  205.   status = SPI_Send_command_with_ADDR(W_REGISTER, TX_ADDR, NOP);
  206.   // read
  207.   status = SPI_Send_command_with_ADDR(R_REGISTER, TX_ADDR, NOP);

  208.   // Write RX_PW_P0 register -> Set number of bytes in RX payload in data pipe0 -> 1 byte
  209.   status = SPI_Send_command_with_ADDR(W_REGISTER, RX_PW_P0, 1);
  210.   // read
  211.   status = SPI_Send_command_with_ADDR(R_REGISTER, RX_PW_P0, NOP);

  212.   // Write RX_PW_P1 register -> Set number of bytes in RX payload in data pipe1 -> 1 byte
  213.   status = SPI_Send_command_with_ADDR(W_REGISTER, RX_PW_P1, 1);
  214.   // read
  215.   status = SPI_Send_command_with_ADDR(R_REGISTER, RX_PW_P1, NOP);

  216. }



 楼主| 一路向北lm 发表于 2018-7-31 22:44 | 显示全部楼层
PWM驱动
  1. #include "stm32f10x_lib.h"
  2. #include "pwm.h"

  3. TIM1_TimeBaseInitTypeDef  TIM1_TimeBaseStructure;
  4. TIM1_OCInitTypeDef  TIM1_OCInitStructure;
  5. TIM1_BDTRInitTypeDef TIM1_BDTRInitStructure;
  6. extern GPIO_InitTypeDef GPIO_InitStructure;

  7. void InitPWM(void) {

  8.   // Enable GPIOA and GPIOB clock
  9.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  10.   // GPIOA Configuration: Channel 1
  11.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  12.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  13.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  15.   // Enable TIM1 clock
  16.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  17.   // TIM1 Peripheral Configuration
  18.   TIM1_DeInit();

  19.   // Time Base configuration
  20.   TIM1_TimeBaseStructure.TIM1_Prescaler = 0x0;
  21.   TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;
  22.   // TIM1_TimeBaseStructure.TIM1_Period = 0xFFF; // 12 bit
  23.   TIM1_TimeBaseStructure.TIM1_Period = 0x3FF; // 10 bit
  24.   TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0;
  25.   TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0x0;
  26.   TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);


  27.   // Channel 1 Configuration in PWM mode
  28.   TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_PWM2;
  29.   TIM1_OCInitStructure.TIM1_OutputState = TIM1_OutputState_Enable;
  30.   TIM1_OCInitStructure.TIM1_OutputNState = TIM1_OutputNState_Enable;
  31.   TIM1_OCInitStructure.TIM1_Pulse = 0x3FF;
  32.   TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_Low;
  33.   TIM1_OCInitStructure.TIM1_OCNPolarity = TIM1_OCNPolarity_Low;
  34.   TIM1_OCInitStructure.TIM1_OCIdleState = TIM1_OCIdleState_Set;
  35.   TIM1_OCInitStructure.TIM1_OCNIdleState = TIM1_OCIdleState_Reset;
  36.   TIM1_OC1Init(&TIM1_OCInitStructure);

  37.   // Automatic Output enable, Break, dead time and lock configuration
  38.   TIM1_BDTRInitStructure.TIM1_OSSRState = TIM1_OSSRState_Enable;
  39.   TIM1_BDTRInitStructure.TIM1_OSSIState = TIM1_OSSIState_Enable;
  40.   TIM1_BDTRInitStructure.TIM1_LOCKLevel = TIM1_LOCKLevel_1;
  41.   TIM1_BDTRInitStructure.TIM1_DeadTime = 0x05;
  42.   TIM1_BDTRInitStructure.TIM1_Break = TIM1_Break_Disable;
  43.   TIM1_BDTRInitStructure.TIM1_BreakPolarity = TIM1_BreakPolarity_High;
  44.   TIM1_BDTRInitStructure.TIM1_AutomaticOutput = TIM1_AutomaticOutput_Enable;
  45.   TIM1_BDTRConfig(&TIM1_BDTRInitStructure);

  46.   // TIM1 counter enable
  47.   TIM1_Cmd(ENABLE);

  48.   // Main Output Enable
  49.   TIM1_CtrlPWMOutputs(ENABLE);

  50. }

  51. void SetDutyPeriod(Int16U period) {

  52.   TIM1_OCInitStructure.TIM1_Pulse = period;
  53.   TIM1_OC1Init(&TIM1_OCInitStructure);

  54. }






 楼主| 一路向北lm 发表于 2018-7-31 22:45 | 显示全部楼层
RTC驱动
  1. // ADC.c
  2. #include "stm32f10x_lib.h"
  3. #include "rtc.h"
  4. #include "arm_comm.h"
  5. #include "includes.h"

  6. extern GPIO_InitTypeDef GPIO_InitStructure;
  7. NVIC_InitTypeDef NVIC_InitStructure;

  8. void RTCInit(void) {

  9.   // Led PC12 as output
  10.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  11.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  12.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  13.   GPIO_Init (GPIOC, &GPIO_InitStructure);

  14.   // Clock for pheriphery
  15.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);

  16.   // CK_RTC clock selection
  17.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  18.   // Allow access to BKP Domain
  19.   PWR_BackupAccessCmd(ENABLE);

  20.   // Reset Backup Domain
  21.   BKP_DeInit();

  22.   // Enable the LSE OSC
  23.   RCC_LSEConfig(RCC_LSE_ON);

  24.   // Disable the LSI OSC
  25.   RCC_LSICmd(DISABLE);

  26.   // Select the RTC Clock Source
  27.   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  28.   // Enable the RTC Clock
  29.   RCC_RTCCLKCmd(ENABLE);

  30.   // Wait for RTC registers synchronization
  31.   RTC_WaitForSynchro();

  32.   // Wait until last write operation on RTC registers has finished
  33.   RTC_WaitForLastTask();

  34.   // Enable the RTC overflow interrupt
  35.   RTC_ITConfig(RTC_IT_SEC, ENABLE);

  36.   //Set 32768 prescaler - for one second interupt
  37.   RTC_SetPrescaler(0x7FFF);

  38.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

  39.   // Configure one bit for preemption priority
  40.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  41.   // Enable the RTC Interrupt
  42.   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
  43.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  44.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  45.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  46.   NVIC_Init(&NVIC_InitStructure);

  47.   // Enable interrupts
  48.   __enable_interrupt();

  49. }





 楼主| 一路向北lm 发表于 2018-7-31 22:46 | 显示全部楼层
DMA驱动
  1. #include "dma.h"
  2. u32 adc_data[10];
  3. void dma_init()
  4. {
  5.         GPIO_InitTypeDef GPIO_InitStructure;
  6.         ADC_InitTypeDef ADC_InitStructure;
  7.         DMA_InitTypeDef DMA_InitStructure;

  8.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_ADC1,ENABLE);
  9.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  10.         RCC_ADCCLKConfig(RCC_PCLK2_Div6);//12M  最大14M 设置ADC时钟(ADCCLK)
  11.         ADC_DeInit(ADC1);

  12.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC  //1
  13.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;        //模拟输入
  14.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15.         GPIO_Init(GPIOA,&GPIO_InitStructure);          //A


  16.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  17.         ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  18.         ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  19.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  20.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  21.         ADC_InitStructure.ADC_NbrOfChannel = 1;
  22.         ADC_Init(ADC1, &ADC_InitStructure);
  23.        
  24.         //设置指定ADC的规则组通道,设置它们的转化顺序和采样时间
  25.         ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5); //1
  26.        
  27.         //内部温度传感器是在ADC1通道16的。
  28. //        ADC_RegularChannelConfig(ADC1,ADC_Channel_16,1,ADC_SampleTime_239Cycles5);
  29. //        ADC_TempSensorVrefintCmd(ENABLE);//打开内部温度传感器使能

  30.         ADC_DMACmd(ADC1,ENABLE);//将ADC与DMA链接在一起
  31.         ADC_Cmd(ADC1,ENABLE);       

  32.         ADC_ResetCalibration(ADC1);//重置指定的ADC的校准寄存器
  33.         while(ADC_GetResetCalibrationStatus(ADC1));//获取ADC重置校准寄存器的状态       
  34.         ADC_StartCalibration(ADC1);//开始指定ADC的校准状态
  35.         while(ADC_GetCalibrationStatus(ADC1));//获取指定ADC的校准程序
  36.         ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能或者失能指定的ADC的软件转换启动功能

  37.         DMA_DeInit(DMA1_Channel1);
  38.         DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&ADC1->DR;//DMA外设地址
  39.         DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&adc_data;//DMA内存地址
  40.         DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//外设作为数据传输的来源
  41.         DMA_InitStructure.DMA_BufferSize=1;//指定DMA通道的DMA缓存的大小
  42.         DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//外设地址寄存器递增
  43.         DMA_InitStructure.DMA_MemoryInc=DMA_PeripheralInc_Enable;//内存地址寄存器递增
  44.         DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;//外设数据宽度16
  45.         DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;//存储数据宽度16
  46.         DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//工作在循环缓存模式
  47.         DMA_InitStructure.DMA_Priority=DMA_Priority_High;//DMA通道x拥有高优先级
  48.         DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMA通道x没有设置为内存到内存传输
  49.         DMA_Init(DMA1_Channel1,&DMA_InitStructure);        //ADC1在DMA1通道1内
  50.         DMA_Cmd(DMA1_Channel1,ENABLE);//使能DMA1
  51. }


 楼主| 一路向北lm 发表于 2018-7-31 22:47 | 显示全部楼层
ADX345驱动
  1. #include "adx345.h"
  2. #include "iic.h"
  3. #include "math.h"

  4. #define ADX345_DelayMs(x)     {delay_ms(x);}  //延时函数

  5. /****************************************************************************
  6. * Function Name  : ADX345_WriteReg
  7. * Description    : 设置ADX345寄存器
  8. * Input          : addr:寄存器地址
  9. *                * dat:吸入数据
  10. * Output         : None
  11. * Return         : None
  12. ****************************************************************************/

  13. static int8_t ADX345_WriteReg(uint8_t addr, uint8_t dat)
  14. {
  15.         I2C_Start();
  16.         I2C_Send_Byte(ADX345_ADDR); //24C02写地址
  17.     if(I2C_Wait_Ack())          //如果无应答,表示发送失败
  18.     {
  19.         return 0xFF;
  20.     }
  21.     I2C_Send_Byte(addr);
  22.     if(I2C_Wait_Ack())         //如果无应答,表示发送失败
  23.     {
  24.         return 0xFF;
  25.     }
  26.     I2C_Send_Byte(dat);
  27.     if(I2C_Wait_Ack())
  28.     {
  29.         return 0xFF;
  30.     }
  31.    
  32.     I2C_Stop();
  33.     return 0;   
  34. }

  35. /****************************************************************************
  36. * Function Name  : ADX345_ReadReg
  37. * Description    : 读取ADX345寄存器
  38. * Input          : addr:读取地址
  39. * Output         : None
  40. * Return         : 读取到的8位数据
  41. ****************************************************************************/

  42. static uint8_t ADX345_ReadReg(uint8_t addr)
  43. {
  44.     uint8_t readValue = 0xFF;

  45.     I2C_Start();
  46.         I2C_Send_Byte(ADX345_ADDR);    //24C02写地址
  47.     if(I2C_Wait_Ack())
  48.     {
  49.         return readValue;
  50.     }
  51.     I2C_Send_Byte(addr);
  52.     if(I2C_Wait_Ack())
  53.     {
  54.         return readValue;
  55.     }

  56.     I2C_Start();
  57.         I2C_Send_Byte(ADX345_ADDR + 1); //24C02读地址
  58.     if(I2C_Wait_Ack())
  59.     {
  60.         return readValue;      
  61.     }
  62.     readValue = I2C_Read_Byte(0);
  63.     I2C_Stop();

  64.     return readValue;
  65. }

  66. /****************************************************************************
  67. * Function Name  : ADX345_ReadXYZ
  68. * Description    : 读取X,Y,Z的AD值
  69. * Input          : xValue:X轴的保存地址
  70. *                * yValue:Y轴的保存地址
  71. *                * zValue:Z轴的保存地址
  72. * Output         : None
  73. * Return         : 0:读取成功。0xFF:读取失败
  74. ****************************************************************************/

  75. static int8_t ADX345_ReadXYZ(int16_t *xValue, int16_t *yValue, int16_t *zValue)
  76. {
  77.     uint8_t readValue[6], i;
  78.     I2C_Start();
  79.         I2C_Send_Byte(ADX345_ADDR);    //24C02写地址
  80.     if(I2C_Wait_Ack())
  81.     {
  82.         return 0xFF;
  83.     }
  84.     I2C_Send_Byte(0x32);           //发送读地址(X轴首地址)
  85.     if(I2C_Wait_Ack())
  86.     {
  87.         return 0xFF;
  88.     }

  89.     I2C_Start();
  90.         I2C_Send_Byte(ADX345_ADDR + 1); //24C02读地址
  91.     if(I2C_Wait_Ack())
  92.     {
  93.         return 0xFF;      
  94.     }

  95.     /* 读取六个字节数据 */
  96.     for(i=0; i<6; i++)
  97.     {
  98.         
  99.         if(i == 5)        //接收最后一个字节时,发送NACK
  100.         {
  101.             readValue[i] = I2C_Read_Byte(0);
  102.         }
  103.         else             //发送接收后应答
  104.         {
  105.             readValue[i] = I2C_Read_Byte(1);
  106.         }
  107.     }
  108.     I2C_Stop();
  109.     /* 处理读取到的数据 */
  110.         *xValue = (uint16_t)(readValue[1] << 8) + readValue[0];             
  111.         *yValue = (uint16_t)(readValue[3] << 8) + readValue[2];             
  112.         *zValue = (uint16_t)(readValue[5] << 8) + readValue[4];           

  113.    return 0;
  114. }

  115. /****************************************************************************
  116. * Function Name  : ADX345_Init
  117. * Description    : 初始化ADX345,并核对ADX的ID
  118. * Input          : None
  119. * Output         : None
  120. * Return         : 0:成功。0xFF:失败
  121. ****************************************************************************/

  122. int ADX345_Init(void)
  123. {
  124.     I2C_INIT();
  125.     if(ADX345_ReadReg(ADX_DEVID) == 0xE5)
  126.     {
  127.         ADX345_WriteReg(ADX_DATA_FORMAT,0x2B);//13位全分辨率,输出数据右对齐,16g量程
  128.                 ADX345_WriteReg(ADX_BW_RATE,0x0A);          //数据输出速度为100Hz
  129.                 ADX345_WriteReg(ADX_POWER_CTL,0x28);  //链接使能,测量模式
  130.                 ADX345_WriteReg(ADX_INT_ENABLE,0x00); //不使用中断                 
  131.                  ADX345_WriteReg(ADX_OFSX,0x00);       //敲击阀值
  132.                 ADX345_WriteReg(ADX_OFSY,0x00);       //X轴偏移
  133.                 ADX345_WriteReg(ADX_OFSZ,0x00);       //Y轴偏移
  134.         return 0;
  135.     }
  136.    
  137.     return 0xFF; //返回初始化失败
  138. }

  139. /****************************************************************************
  140. * Function Name  : ADX345_Adjust
  141. * Description    : ADX345进行校正。
  142. * Input          : None
  143. * Output         : None
  144. * Return         : None
  145. ****************************************************************************/

  146. void ADX345_Adjust(void)
  147. {
  148.     int32_t offx = 0, offy = 0, offz = 0;
  149.     int16_t xValue, yValue, zValue;
  150.     uint8_t i;

  151.         ADX345_WriteReg(ADX_POWER_CTL, 0x00);         //先进入休眠模式.
  152.         ADX345_DelayMs(100);
  153.         ADX345_Init();
  154.     ADX345_DelayMs(20);
  155.    
  156.     /* 读取十次数值 */
  157.     for(i=0; i<10; i++)
  158.     {
  159.         ADX345_ReadXYZ(&xValue, &yValue, &zValue);
  160.         offx += xValue;
  161.         offy += yValue;
  162.         offz += zValue;
  163.         ADX345_DelayMs(10);   //才样频率在100HZ,10ms采样一次最好         
  164.     }
  165.    
  166.     /* 求出平均值 */
  167.     offx /= 10;
  168.     offy /= 10;
  169.     offz /= 10;
  170.    
  171.     /* 全分辨率下,每个输出LSB为3.9 mg或偏移寄存器LSB的四分之一,所以除以4 */
  172.     xValue = -(offx / 4);
  173.         yValue = -(offy / 4);
  174.         zValue = -((offz - 256) / 4);
  175.    
  176.     /* 设置偏移量 */
  177.         ADX345_WriteReg(ADX_OFSX, xValue);
  178.         ADX345_WriteReg(ADX_OFSY, yValue);
  179.         ADX345_WriteReg(ADX_OFSZ, zValue);
  180.             
  181. }

  182. /****************************************************************************
  183. * Function Name  : ADX_GetXYZData
  184. * Description    : 读取ADX的XYZ轴的值(进行过数据处理)
  185. * Input          : xValue:X轴的保存地址
  186. *                * yValue:Y轴的保存地址
  187. *                * zValue:Z轴的保存地址
  188. * Output         : None
  189. * Return         : None
  190. ****************************************************************************/

  191. void ADX_GetXYZData(int16_t *xValue, int16_t *yValue, int16_t *zValue)
  192. {
  193.     int32_t xTotal = 0, yTotal = 0, zTotal = 0;
  194.     uint8_t i;
  195.    
  196.     *xValue = 0;
  197.     *yValue = 0;
  198.     *zValue = 0;

  199.     /* 读取十次采样值 */
  200.     for(i=0; i<10; i++)
  201.     {
  202.         ADX345_ReadXYZ(xValue, yValue, zValue);
  203.         xTotal += *xValue;
  204.         yTotal += *yValue;
  205.         zTotal += *zValue;
  206.         ADX345_DelayMs(10);  //才样频率在100HZ,10ms采样一次最好
  207.     }
  208.    
  209.     /* 求出平均值 */
  210.     *xValue = xTotal / 10;
  211.     *yValue = yTotal / 10;
  212.     *zValue = zTotal / 10;      
  213. }

  214. /****************************************************************************
  215. * Function Name  : ADX_GetAngle
  216. * Description    : 将AD值转换成角度值
  217. * Input          : xValue:x的值
  218. *                * yValue:y的值
  219. *                * zValue:z的值
  220. *                * dir:0:与Z轴的角度;1:与X轴的角度;2:与Y轴的角度.
  221. * Output         : None
  222. * Return         : None
  223. ****************************************************************************/

  224. int16_t ADX_GetAngle(float xValue, float yValue, float zValue, uint8_t dir)
  225. {
  226.         float temp;
  227.         float res = 0;

  228.         switch(dir)
  229.         {   
  230.         /* 与自然Z轴的角度 */
  231.                 case 0:
  232.                         temp = sqrt((xValue * xValue + yValue * yValue)) / zValue;
  233.                         res = atan(temp);
  234.                         break;
  235.         
  236.         /* 与自然X轴的角度 */
  237.                 case 1:
  238.                         temp = xValue / sqrt((yValue * yValue + zValue * zValue));
  239.                         res = atan(temp);
  240.                         break;

  241.         /* 与自然Y轴的角度 */
  242.                 case 2:
  243.                         temp = yValue / sqrt((xValue * xValue + zValue * zValue));
  244.                         res = atan(temp);
  245.                         break;

  246.         default:
  247.             break;
  248.         }

  249.     res = res * 1800 / 3.14;

  250.         return res;
  251. }
  252. void ADX345_pros()
  253. {
  254.         int16_t value;
  255.     uint8_t num = 6;
  256.         int16_t Xval, Yval, Zval, Xang, Yang, Zang;
  257.         uint8_t ShowData[6] = {0, 0, 0, 0, 0, 0};
  258.         TFT_ClearScreen(BLACK);
  259.         delay_ms(10);
  260.         while(ADX345_Init())
  261.     {
  262.         GUI_Show12Char(60, 42, "ADX345 ERROR!", RED, BLACK);
  263.         delay_ms(100);        
  264.     }
  265.         while(1)
  266.         {
  267.                 if(TouchData.lcdx>16&&TouchData.lcdx<56&&TouchData.lcdy>200&&TouchData.lcdy<238)
  268.                 {               

  269.                     GUI_Show12Char(10, 10, "ADX345加速度传感器实验", RED, BLACK);
  270.                     GUI_Show12Char(0, 84, "X VAL:", RED, BLACK);
  271.                     GUI_Show12Char(0, 105, "Y VAL:", RED, BLACK);
  272.                     GUI_Show12Char(0, 126, "Z VAL:", RED, BLACK);
  273.                     GUI_Show12Char(0, 147, "X ANG:", RED, BLACK);
  274.                     GUI_Show12Char(0, 168, "Y ANG:", RED, BLACK);
  275.                     GUI_Show12Char(0, 189, "Z ANG:", RED, BLACK);
  276.                         GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);               
  277.                 }
  278.                        
  279.                 ADX345_Adjust();

  280.                 /* 读取X,Y,Z的加速度值 */
  281.         ADX_GetXYZData(&Xval, &Yval, &Zval);

  282.         /* 将读取到的加速度值转换为角度值 */
  283.         Xang=ADX_GetAngle(Xval, Yval, Zval,1);   
  284.                 Yang=ADX_GetAngle(Xval, Yval, Zval,2);   
  285.                 Zang=ADX_GetAngle(Xval, Yval, Zval,0);

  286.                 while(num)
  287.             {
  288.                 switch(num)
  289.                 {
  290.                     case(1):
  291.                         value = Zang;
  292.                         break;
  293.                     case(2):
  294.                         value = Yang;
  295.                         break;
  296.                     case(3):
  297.                         value = Xang;
  298.                         break;
  299.                     case(4):
  300.                         value = Zval;
  301.                         break;
  302.                     case(5):
  303.                         value = Yval;
  304.                         break;
  305.                     case(6):
  306.                         value = Xval;
  307.                         break;
  308.                     default:
  309.                        break;
  310.                 }
  311.                         TOUCH_Scan();
  312.                         if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
  313.                         {
  314.                                 GUI_DisplayInit();
  315.                                 break;                                       
  316.                         }
  317.                 if(value > 0)
  318.                 {
  319.                     ShowData[0] = '+';
  320.                 }
  321.                 else
  322.                 {
  323.                     ShowData[0] = '-';
  324.                     value = abs(value);//求出绝对值
  325.                 }
  326.                 ShowData[1] = (value % 10000 /1000) + '0';
  327.                 ShowData[2] = (value % 1000 /100) + '0';
  328.                 ShowData[3] = (value % 100 /10) + '0';
  329.                 ShowData[4] = (value % 10) + '0';
  330.                 
  331.                 GUI_Show12Char(48, (210 - num*21), ShowData, RED, BLACK);
  332.                 num--;
  333.             }
  334.                 num=6;
  335.                 if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
  336.                 {
  337.                         GUI_DisplayInit();
  338.                         break;                                       
  339.                 }
  340.                 TOUCH_Scan();
  341.         }       
  342. }



 楼主| 一路向北lm 发表于 2018-7-31 22:48 | 显示全部楼层
CAN总线驱动

  1. #include "can.h"
  2. #include "key.h"

  3. #ifdef CAN_RX0_INT_ENABLE

  4. uint8_t CAN_RX_BUFF[8];

  5. #endif

  6. uint8_t ShowData[5] = {'0', '0', 0};
  7. uint8_t CAN_SendData[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}; //最后两位用来存放发送ID和接收ID
  8. uint8_t *Mode;
  9. const uint8_t CAN_ModeNormal[9] = {"正常模式"};
  10. const uint8_t CAN_ModeLoopBack[9] = {"环回模式"};

  11. /****************************************************************************
  12. * Function Name  : CAN1_NVIC_Config
  13. * Description    : Main program.
  14. * Input          : None
  15. * Output         : None
  16. * Return         : None
  17. ****************************************************************************/

  18. static void CAN1_NVIC_Config(void)
  19. {
  20.         NVIC_InitTypeDef NVIC_InitStructure;

  21.         /* 使能接收的中断和中断优先级 */
  22.         NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;  
  23.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  24.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  25.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  26.         NVIC_Init(&NVIC_InitStructure);
  27.        
  28.         CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);   //FIFO0消息挂号中断允许.       
  29. }

  30. /****************************************************************************
  31. * Function Name  : CAN1_Config
  32. * Description    : 初始化CAN,波特率设置为450K
  33. * Input          : mode:用来选择要使用的工作模式:主要有四种工作模式:1、正常
  34. *                * 模式:CAN_Mode_Normal;2、CAN_Mode_Silent :静默模式;3、环
  35. *                * 回模式:CAN_Mode_LoopBack;4、静默环回模式:CAN_Mode_Silent
  36. *                * _LoopBack。
  37. * Output         : None
  38. * Return         : None
  39. ****************************************************************************/

  40. void CAN1_Config(uint8_t mode)
  41. {
  42.         GPIO_InitTypeDef GPIO_InitStructure;
  43.         CAN_InitTypeDef CAN_InitStructure;

  44.         /* 初始化IO口 */
  45.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  46.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   //复用推挽输出
  47.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;        //PA12
  48.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  49.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  50.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;     //上拉输入
  51.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;        //PA11

  52.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  53. /***************************************************************************/
  54. /********************* CAN设置和初始化 *************************************/
  55. /***************************************************************************/

  56.         /* 打开时钟使能 */
  57.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

  58.     /* 初始化CAN的参数 */

  59.         CAN_DeInit(CAN1);
  60.         CAN_StructInit(&CAN_InitStructure);

  61.         /* CAN 参数初始化 */
  62.         CAN_InitStructure.CAN_TTCM = DISABLE;    //失能时间触发模式
  63.         CAN_InitStructure.CAN_ABOM = DISABLE;    //失能自动离线管理
  64.         CAN_InitStructure.CAN_AWUM = DISABLE;    //失能睡眠模式通过软件唤醒
  65.         CAN_InitStructure.CAN_NART = DISABLE;    //失能非自动重传输模式(也就是会自动重传输)
  66.         CAN_InitStructure.CAN_RFLM = DISABLE;    //失能接收FIFO锁定模式,新数据会覆盖旧数据
  67.         CAN_InitStructure.CAN_TXFP = DISABLE;    //优先级由报文标识符决定
  68.         CAN_InitStructure.CAN_Mode = mode;       //有普通模式和拓展模式
  69.         CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //重新同步跳跃宽度 1 个时间单位

  70.     /* 波特率设置, 当APB1的时钟频率是36MHZ的时候。 波特率的公式为: */
  71.     /* 波特率(Kpbs) = 36M / ((CAN_BS1 + CAN_BS2 + 1) *  CAN_Prescaler) */
  72.         CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq; //时间段 1 为8 个时间单位
  73.         CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq; //时间段 2 为7 个时间单位
  74.         CAN_InitStructure.CAN_Prescaler = 5;         

  75.         CAN_Init(CAN1, &CAN_InitStructure);

  76. #ifdef CAN_RX0_INT_ENABLE
  77.         CAN1_NVIC_Config();
  78. #endif   

  79. }

  80. /****************************************************************************
  81. * Function Name  : CAN1_SendMesg
  82. * Description    : 发送一个报文
  83. * Input          : id:发送的ID。
  84. *                * len:发送的数据长度(注意发送数据长度不能超过8个字节)
  85. *                * dat:存放数据的指针
  86. * Output         : None
  87. * Return         : None
  88. ****************************************************************************/

  89. void CAN1_SendMesg(uint32_t id, uint8_t len, uint8_t *dat)
  90. {
  91.         uint16_t i = 0;
  92.         CanTxMsg TxMessage;

  93.     /* 一次发送只能发送8个字节 */
  94.     if(len > 8)
  95.     {
  96.         return ;
  97.     }
  98.         /* 配置邮箱:设置标识符,数据长度和待发送数据 */
  99.         TxMessage.StdId = (id & 0x7FF); //标准帧ID11位
  100.         TxMessage.ExtId = (id >> 11);   //设置扩展标示符(拓展标示符有29位)
  101.         TxMessage.RTR = CAN_RTR_DATA;   //设置为数据帧(或远程帧为CAN_RTR_Remote)
  102.     if((id & 0x7FF) == 0x7FF)       //检测是标准帧还是拓展帧(拓展帧大于11位)
  103.     {
  104.             TxMessage.IDE = CAN_ID_STD;        //拓展ID   
  105.     }
  106.     else
  107.     {
  108.             TxMessage.IDE = CAN_ID_EXT;        //标准ID
  109.     }
  110.         TxMessage.DLC = len;                //发送的数据长度

  111.         /* 将数据放入到邮箱中 */
  112.         for(i=0; i<len; i++)                
  113.         {
  114.                 TxMessage.Data[i] = *dat;
  115.                 dat++;       
  116.         }
  117.    
  118.     /* 开始传送数据 */
  119.         CAN_Transmit(CAN1, &TxMessage);
  120. }

  121. /****************************************************************************
  122. * Function Name  : CAN1_Config16BitFilter
  123. * Description    : 设置CAN接收16两个标准ID(设置ID位数全部相同才能够通过)
  124. * Input          : id1:要接收的一个ID
  125. *                * id2:要接收的一个ID
  126. * Output         : None
  127. * Return         : None
  128. ****************************************************************************/

  129. void CAN1_Config16BitFilter(uint16_t id1, uint16_t id2)
  130. {
  131.     CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  132.     uint16_t mask = 0xFFFF;

  133.         /* CAN filter init 屏蔽寄存器初始化 */
  134.         CAN_FilterInitStructure.CAN_FilterNumber = 1;                       //过滤器1
  135.         CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;//ID模式

  136.         /* 寄存器组设置为16位 */
  137.         CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
  138.     CAN_FilterInitStructure.CAN_FilterIdHigh = (id1 << 5);    //要接收的ID标示符1               
  139.     CAN_FilterInitStructure.CAN_FilterIdLow =  (id2 << 5);          //要接收的ID标示符2

  140.         /* 设置为所有ID位都要相同才接收 */       
  141.         CAN_FilterInitStructure.CAN_FilterMaskIdHigh = (mask << 5); //MASK
  142.         CAN_FilterInitStructure.CAN_FilterMaskIdLow  = (mask << 5);
  143.         CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //FIFO0
  144.         CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //使能过滤器1

  145.         CAN_FilterInit(&CAN_FilterInitStructure);
  146. }

  147. /****************************************************************************
  148. * Function Name  : CAN1_Config32BitFilter
  149. * Description    : 设置一个拓展ID的接收
  150. * Input          : id:要接收的ID
  151. * Output         : None
  152. * Return         : None
  153. ****************************************************************************/

  154. void CAN1_Config32BitFilter(uint32_t id)
  155. {
  156.     uint32_t mask = 0xFFFFFFFF;
  157.     CAN_FilterInitTypeDef  CAN_FilterInitStructure;

  158.         /* CAN filter init 屏蔽寄存器初始化 */
  159.         CAN_FilterInitStructure.CAN_FilterNumber = 1;                       //过滤器1
  160.         CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;//ID模式

  161.         /* 寄存器组设置为32位 */
  162.         CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  163.     CAN_FilterInitStructure.CAN_FilterIdHigh = (id >> 13);    //要接收的ID标示符1               
  164.     CAN_FilterInitStructure.CAN_FilterIdLow =  (id << 3 ) | 4;//要接收的ID标示符2

  165.         /* 设置为所有ID位都要相同才接收 */       
  166.         CAN_FilterInitStructure.CAN_FilterMaskIdHigh = mask >> 13;     //MASK
  167.         CAN_FilterInitStructure.CAN_FilterMaskIdLow  = (mask << 3) | 4;
  168.         CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; //FIFO0
  169.         CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //使能过滤器1

  170.         CAN_FilterInit(&CAN_FilterInitStructure);
  171. }

  172. /****************************************************************************
  173. * Function Name  : CAN1_ReceiveMesg
  174. * Description    : 接收一个报文
  175. * Input          : receiveBuff:接收数据的数组指针
  176. * Output         : None
  177. * Return         : None
  178. ****************************************************************************/

  179. void CAN1_ReceiveMesg(uint8_t *receiveBuff)
  180. {
  181.         uint8_t i = 0;

  182.         CanRxMsg RxMessage;        //设置接收邮箱

  183.         if((CAN_MessagePending(CAN1, CAN_FIFO0) != 0)) //检查FIFO0里面是否有数据
  184.         {
  185.             CAN_Receive(CAN1,CAN_FIFO0,&RxMessage); //读取FIFO0里面的数据
  186.             for(i=0; i<RxMessage.DLC; i++)          //将读取到的数据位赋给CAN_RXSBUF
  187.             {
  188.                     *receiveBuff = RxMessage.Data[i];
  189.                     receiveBuff++;
  190.             }
  191.     }                       
  192. }

  193. /****************************************************************************
  194. * Function Name  : USB_LP_CAN1_RX0_IRQHandler
  195. * Description    : FIFO0接收一个报文数据数据(最大8个字节)
  196. * Input          : None
  197. * Output         : None
  198. * Return         : None
  199. ****************************************************************************/

  200. #ifdef CAN_RX0_INT_ENABLE

  201. void USB_LP_CAN1_RX0_IRQHandler(void)
  202. {
  203.         CAN1_ReceiveMesg(CAN_RX_BUFF);       
  204. }

  205. #endif

  206. void GUI_DisplayData(uint8_t num)
  207. {
  208.     uint8_t addr;
  209.    
  210.     for(addr=0; addr<10; addr++)
  211.     {   
  212.         if(addr < 8)
  213.         {
  214.             /* 如果接收到数据,显示数据 */
  215.             /* 将读取到的16位数据转换成可以显示的ASCII码 */
  216.             ShowData[0] = CAN_RX_BUFF[addr] >> 4;
  217.             if(ShowData[0] > 9)
  218.             {
  219.                 ShowData[0] += '7';  //当大于9时,用ABCDEF来表示
  220.             }
  221.             else
  222.             {
  223.                 ShowData[0] += '0';   
  224.             }
  225.             ShowData[1] = CAN_RX_BUFF[addr] & 0x0F;
  226.             if(ShowData[1] > 9)
  227.             {
  228.                 ShowData[1] += '7';  //当大于9时,用ABCDEF来表示
  229.             }
  230.             else
  231.             {
  232.                 ShowData[1] += '0';   
  233.             }
  234.             /* 显示接收到的数据 */
  235.             GUI_Show12Char(208, (60 + addr * 20), ShowData, RED, BLACK);
  236.         }
  237.         
  238.         /* 将要发送的16位数据转换成可以显示的ASCII码 */
  239.         ShowData[0] = CAN_SendData[addr] >> 4;
  240.         if(ShowData[0] > 9)
  241.         {
  242.             ShowData[0] += '7';    //当大于9时,用ABCDEF来表示
  243.         }
  244.         else
  245.         {
  246.             ShowData[0] += '0';   
  247.         }
  248.         ShowData[1] = CAN_SendData[addr] & 0x0F;
  249.         if(ShowData[1] > 9)
  250.         {
  251.             ShowData[1] += '7';    //当大于9时,用ABCDEF来表示
  252.         }
  253.         else
  254.         {
  255.             ShowData[1] += '0';   
  256.         }

  257.         if(addr < 8)
  258.         {
  259.             /* 显示发送数据,选择高亮位置 */
  260.             if(num == addr)
  261.             {
  262.                 GUI_Show12Char(88, (60 + addr * 20), ShowData, GREEN, BLACK);
  263.             }
  264.             else
  265.             {
  266.                 GUI_Show12Char(88, (60 + addr * 20), ShowData, RED, BLACK);
  267.             }
  268.         }
  269.         else //显示ID部分
  270.         {
  271.             if(addr == 8)
  272.             {
  273.                 if(num == 8)
  274.                 {
  275.                     GUI_Show12Char(52, 40, ShowData, GREEN, BLACK);
  276.                 }
  277.                 else
  278.                 {
  279.                     GUI_Show12Char(52, 40, ShowData, RED, BLACK);
  280.                 }
  281.             }
  282.             if(addr == 9)
  283.             {
  284.                 if(num == 9)
  285.                 {
  286.                     GUI_Show12Char(176, 40, ShowData, GREEN, BLACK);
  287.                 }
  288.                 else
  289.                 {
  290.                     GUI_Show12Char(176, 40, ShowData, RED, BLACK);
  291.                 }
  292.             }
  293.         }        
  294.     }
  295.     if(num > 9)
  296.     {
  297.         GUI_Show12Char(104, 20, Mode, GREEN, BLACK);
  298.     }
  299.     else
  300.     {
  301.         GUI_Show12Char(104, 20, Mode, RED, BLACK);        
  302.     }
  303. }

  304. void CAN_pros()
  305. {
  306.         uint8_t m, receiveId,canMode;
  307.         m = 10;
  308.     canMode = 1; //要设置canMode不等于CAN_SendData[10],以便进入循环一开始就初始化
  309.         TFT_ClearScreen(BLACK);
  310.         delay_ms(10);
  311.         while(1)
  312.         {
  313.                 if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>268&&TouchData.lcdy<306)
  314.                 {               
  315.                
  316.                         GUI_Show12Char(76, 0, "CAN通信实验", RED, BLACK);
  317.                     /* 显示工作模式 */
  318.                     GUI_Show12Char(0, 20, "CAN工作模式:", RED, BLACK);
  319.                
  320.                     /* 显示发送接收ID */
  321.                     GUI_Show12Char(0, 40, "发送ID:", RED, BLACK);
  322.                     GUI_Show12Char(120, 40, "接收ID:", RED, BLACK);
  323.                     
  324.                     /* 显示发送和接收数据的8位数 */   
  325.                     GUI_Show12Char(0, 60, "发送第一位:", RED, BLACK);
  326.                     GUI_Show12Char(120, 60, "接收第一位:", RED, BLACK);
  327.                     GUI_Show12Char(0, 80, "发送第二位:", RED, BLACK);
  328.                     GUI_Show12Char(120, 80, "接收第二位:", RED, BLACK);
  329.                     GUI_Show12Char(0, 100, "发送第三位:", RED, BLACK);
  330.                     GUI_Show12Char(120, 100, "接收第三位:", RED, BLACK);
  331.                     GUI_Show12Char(0, 120, "发送第四位:", RED, BLACK);
  332.                     GUI_Show12Char(120, 120, "接收第四位:", RED, BLACK);
  333.                     GUI_Show12Char(0, 140, "发送第五位:", RED, BLACK);
  334.                     GUI_Show12Char(120, 140, "接收第五位:", RED, BLACK);
  335.                     GUI_Show12Char(0, 160, "发送第六位:", RED, BLACK);
  336.                     GUI_Show12Char(120, 160, "接收第六位:", RED, BLACK);
  337.                     GUI_Show12Char(0, 180, "发送第七位:", RED, BLACK);
  338.                     GUI_Show12Char(120, 180, "接收第七位:", RED, BLACK);
  339.                     GUI_Show12Char(0, 200, "发送第八位:", RED, BLACK);
  340.                     GUI_Show12Char(120, 200, "接收第八位:", RED, BLACK);
  341.                     GUI_Show12Char(0, 220, "上键:高亮部分加一", BLUE, BLACK);
  342.                     GUI_Show12Char(0, 240, "下键:高亮部分减一", BLUE, BLACK);
  343.                     GUI_Show12Char(0, 260, "左键:高亮部分向下移动一位", BLUE, BLACK);
  344.                     GUI_Show12Char(0, 280, "右键:发送数据", BLUE, BLACK);
  345.                     GUI_Show12Char(0, 300, "注:波特率为450K", BLUE, BLACK);
  346.                         GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);
  347.                         TouchData.lcdx=0;
  348.                         TouchData.lcdy=0;               
  349.                 }
  350.                 TOUCH_Scan();
  351.                 /* 修改模式 */
  352.         if(canMode != CAN_SendData[10] )
  353.         {
  354.             canMode = CAN_SendData[10];
  355.             if(CAN_SendData[10])
  356.             {
  357.                CAN1_Config(CAN_Mode_Normal);
  358.                Mode = (uint8_t *)CAN_ModeNormal;   
  359.             }
  360.             else
  361.             {
  362.                 CAN1_Config(CAN_Mode_LoopBack);
  363.                 Mode = (uint8_t *)CAN_ModeLoopBack;
  364.             }
  365.             CAN1_Config16BitFilter(CAN_SendData[9], 0x00);   
  366.         }
  367.         /* 如果接收ID改变,就重新设置接收ID */
  368.         if(receiveId != CAN_SendData[9])
  369.         {
  370.             receiveId = CAN_SendData[9];
  371.             if(receiveId != 0x00)
  372.             {
  373.                 CAN1_Config16BitFilter(CAN_SendData[9], 0x00);
  374.             }               
  375.         }
  376.                 /* 显示数据 */
  377.         GUI_DisplayData(m);

  378.                 if(k_up==1)          //判断按键k_up是否按下
  379.                 {
  380.                         delay_ms(10); //消抖处理
  381.                         if(k_up==1)         //再次判断按键k_up是否按下
  382.                         {
  383.                                 if(m == 10)
  384.                 {
  385.                     CAN_SendData[10] = ~CAN_SendData[10];
  386.                 }
  387.                 else
  388.                 {
  389.                     CAN_SendData[m]++;   
  390.                 }
  391.                 GUI_Show12Char(175, 20, "等待发送", RED, BLACK);                                       
  392.                         }
  393.                         while(k_up); //等待按键松开
  394.                 }
  395.                 if(k_down==0)
  396.                 {
  397.                         delay_ms(10);
  398.                         if(k_down==0)
  399.                         {
  400.                                 if(m == 10)
  401.                 {
  402.                     CAN_SendData[10] = ~CAN_SendData[10];
  403.                 }
  404.                 else
  405.                 {
  406.                     CAN_SendData[m]--;   
  407.                 }
  408.                 GUI_Show12Char(175, 20, "等待发送", RED, BLACK);                       
  409.                         }
  410.                         while(!k_down);
  411.                 }
  412.                 if(k_left==0)
  413.                 {
  414.                         delay_ms(10);
  415.                         if(k_left==0)
  416.                         {
  417.                                 if(m == 10)
  418.                 {
  419.                     m = 0;
  420.                 }
  421.                 else
  422.                 {
  423.                     m++;
  424.                 }                       
  425.                         }
  426.                         while(!k_left);
  427.                 }
  428.                 if(k_right==0)
  429.                 {
  430.                         delay_ms(10);
  431.                         if(k_right==0)
  432.                         {
  433.                                 CAN1_SendMesg(CAN_SendData[8], 8, CAN_SendData);//发送数据
  434.                 GUI_Show12Char(175, 20, "发送成功", BLUE, BLACK);                       
  435.                         }
  436.                         while(!k_right);
  437.                 }               


  438.                 if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
  439.                 {
  440.                         GUI_DisplayInit();
  441.                         break;                                       
  442.                 }
  443.                 TOUCH_Scan();       
  444.         }       
  445. }



 楼主| 一路向北lm 发表于 2018-7-31 22:49 | 显示全部楼层
DS18B20温度传感器驱动
  1. #include "ds18b20.h"

  2. /*******************************************************************************
  3. * 函 数 名         : ds18b20_init
  4. * 函数功能                   : IO端口时钟初始化函数          
  5. * 输    入         : 无
  6. * 输    出         : 无
  7. *******************************************************************************/
  8. void ds18b20_init()
  9. {
  10.         GPIO_InitTypeDef GPIO_InitStructure;

  11.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);

  12.         GPIO_InitStructure.GPIO_Pin=dq;
  13.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  14.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);
  16. }

  17. /*******************************************************************************
  18. * 函 数 名         : DQININT
  19. * 函数功能                   : 输入配置          
  20. * 输    入         : 无
  21. * 输    出         : 无
  22. *******************************************************************************/
  23. void DQININT()         //输入配置
  24. {
  25.         GPIO_InitTypeDef GPIO_InitStructure;
  26.         GPIO_InitStructure.GPIO_Pin=dq;
  27.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  28.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  29.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
  30. }

  31. /*******************************************************************************
  32. * 函 数 名         : DQOUTINT
  33. * 函数功能                   : 输出配置          
  34. * 输    入         : 无
  35. * 输    出         : 无
  36. *******************************************************************************/
  37. void DQOUTINT()         //输出配置
  38. {
  39.         GPIO_InitTypeDef GPIO_InitStructure;
  40.         GPIO_InitStructure.GPIO_Pin=dq;
  41.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  42.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  43.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
  44. }

  45. /*******************************************************************************
  46. * 函 数 名         : ds18b20init
  47. * 函数功能                   : DS18B20初始化时序          
  48. * 输    入         : 无
  49. * 输    出         : 无
  50. *******************************************************************************/
  51. void ds18b20init()
  52. {
  53.         DQOUTINT();//输出
  54.         ds18b20_dq_L;
  55.         delay_us(480);//延时480微妙       
  56.         ds18b20_dq_H;
  57.         delay_us(480);//延时480微妙
  58. }

  59. /*******************************************************************************
  60. * 函 数 名         : ds18b20wr
  61. * 函数功能                   : DS18B20写数据时序          
  62. * 输    入         : dat
  63. * 输    出         : 无
  64. *******************************************************************************/
  65. void ds18b20wr(u8 dat)
  66. {
  67.         u8 i=0;
  68.         DQOUTINT();//输出

  69.         for(i=0;i<8;i++)
  70.         {
  71.                 ds18b20_dq_L;         //拉低
  72.                 delay_us(15);//延时15微妙
  73.                
  74.                 if((dat&0x01)==1)
  75.                 {
  76.                         ds18b20_dq_H;
  77.                 }
  78.                 else
  79.                 {
  80.                         ds18b20_dq_L;
  81.                 }
  82.                 delay_us(60);//延时60微妙
  83.                 ds18b20_dq_H;
  84.                
  85.                 dat>>=1;//准备下一位数据的发送       
  86.         }
  87. }

  88. /*******************************************************************************
  89. * 函 数 名         : DS18b20rd
  90. * 函数功能                   : DS18B20读数据时序          
  91. * 输    入         : 无
  92. * 输    出         : value
  93. *******************************************************************************/
  94. u8 DS18b20rd()
  95. {
  96.         u8 i=0,value=0;

  97.         for(i=0;i<8;i++)
  98.         {
  99.                 value>>=1;
  100.                 DQOUTINT();//输出
  101.                 ds18b20_dq_L;         //拉低
  102.                 delay_us(4);//延时4微妙
  103.                 ds18b20_dq_H;
  104.                 delay_us(10);//延时10微妙
  105.                 DQININT();         //输入配置

  106.                 if(GPIO_ReadInputDataBit(GPIO_ds18b20,dq)==1)
  107.                 {
  108.                    value|=0x80;//读数据 从低位开始
  109.                 }

  110.                 delay_us(45);//延时45微妙
  111.         }

  112.         return value;       
  113. }

  114. /*******************************************************************************
  115. * 函 数 名         : readtemp
  116. * 函数功能                   : DS18B2温度寄存器配置,温度读取          
  117. * 输    入         : 无
  118. * 输    出         : value
  119. *******************************************************************************/
  120. double readtemp()                          //读取温度内需要复位的
  121. {
  122.         u16 temp;
  123.         u8 a,b;
  124.         double value;
  125.         ds18b20init();                //初始化
  126.         ds18b20wr(0xcc);   //发送忽略ROM指令
  127.         ds18b20wr(0x44);   //发送温度转换指令
  128.         delay_ms(10);         
  129.         ds18b20init();           //初始化
  130.         ds18b20wr(0xcc);   //发送忽略ROM指令
  131.         ds18b20wr(0xbe);   //发读暂存器指令
  132.         a=DS18b20rd();         //温度的低八位
  133.         b=DS18b20rd();         //温度的高八位
  134.         temp=b;
  135.         temp=(temp<<8)+a;
  136.         if((temp&0xf800)==0xf800)
  137.         {
  138.                 temp=(~temp)+1;
  139.                 value=temp*(-0.0625);
  140.         }
  141.         else
  142.         {
  143.                 value=temp*0.0625;       
  144.         }
  145.         return value;
  146. }
  147. void DS18B20_pros()
  148. {
  149.         double temp;
  150.         u16 tem_dat;
  151.         u8 dat[8];
  152.         TFT_ClearScreen(BLACK);
  153.         delay_ms(10);
  154.         ds18b20_init();
  155.         while(1)
  156.         {

  157.                 if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>82&&TouchData.lcdy<118)
  158.                 {               
  159.                         GUI_Show12Char(10,10,"DS18B20温度检测:",YELLOW,BLACK);
  160.                         GUI_Show12Char(10,40,"将温度传感器插上主板接口上,注意温度传感器的方向,板子上有对应的丝印指示",YELLOW,BLACK);
  161.                         GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);       
  162.                 }
  163.                        
  164.                 temp=readtemp();
  165.                 tem_dat=temp*100;
  166.                 dat[0]=tem_dat/1000+0x30;
  167.                 dat[1]=tem_dat%1000/100+0x30;
  168.                 dat[2]='.';
  169.                 dat[3]=tem_dat%1000%100/10+0x30;
  170.                 dat[4]=tem_dat%1000%100%10+0x30;
  171.                 dat[5]='C';
  172.                 dat[6]='\0';
  173.                 GUI_Show12Char(150,10,dat,YELLOW,BLACK);                               
  174.                 if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
  175.                 {
  176.                         GUI_DisplayInit();
  177.                         break;                                       
  178.                 }
  179.                 TOUCH_Scan();       
  180.         }
  181.                        
  182. }



 楼主| 一路向北lm 发表于 2018-7-31 22:50 | 显示全部楼层
flash驱动
  1. #include "ds18b20.h"

  2. /*******************************************************************************
  3. * 函 数 名         : ds18b20_init
  4. * 函数功能                   : IO端口时钟初始化函数          
  5. * 输    入         : 无
  6. * 输    出         : 无
  7. *******************************************************************************/
  8. void ds18b20_init()
  9. {
  10.         GPIO_InitTypeDef GPIO_InitStructure;

  11.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);

  12.         GPIO_InitStructure.GPIO_Pin=dq;
  13.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  14.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);
  16. }

  17. /*******************************************************************************
  18. * 函 数 名         : DQININT
  19. * 函数功能                   : 输入配置          
  20. * 输    入         : 无
  21. * 输    出         : 无
  22. *******************************************************************************/
  23. void DQININT()         //输入配置
  24. {
  25.         GPIO_InitTypeDef GPIO_InitStructure;
  26.         GPIO_InitStructure.GPIO_Pin=dq;
  27.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  28.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  29.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
  30. }

  31. /*******************************************************************************
  32. * 函 数 名         : DQOUTINT
  33. * 函数功能                   : 输出配置          
  34. * 输    入         : 无
  35. * 输    出         : 无
  36. *******************************************************************************/
  37. void DQOUTINT()         //输出配置
  38. {
  39.         GPIO_InitTypeDef GPIO_InitStructure;
  40.         GPIO_InitStructure.GPIO_Pin=dq;
  41.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  42.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  43.         GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
  44. }

  45. /*******************************************************************************
  46. * 函 数 名         : ds18b20init
  47. * 函数功能                   : DS18B20初始化时序          
  48. * 输    入         : 无
  49. * 输    出         : 无
  50. *******************************************************************************/
  51. void ds18b20init()
  52. {
  53.         DQOUTINT();//输出
  54.         ds18b20_dq_L;
  55.         delay_us(480);//延时480微妙       
  56.         ds18b20_dq_H;
  57.         delay_us(480);//延时480微妙
  58. }

  59. /*******************************************************************************
  60. * 函 数 名         : ds18b20wr
  61. * 函数功能                   : DS18B20写数据时序          
  62. * 输    入         : dat
  63. * 输    出         : 无
  64. *******************************************************************************/
  65. void ds18b20wr(u8 dat)
  66. {
  67.         u8 i=0;
  68.         DQOUTINT();//输出

  69.         for(i=0;i<8;i++)
  70.         {
  71.                 ds18b20_dq_L;         //拉低
  72.                 delay_us(15);//延时15微妙
  73.                
  74.                 if((dat&0x01)==1)
  75.                 {
  76.                         ds18b20_dq_H;
  77.                 }
  78.                 else
  79.                 {
  80.                         ds18b20_dq_L;
  81.                 }
  82.                 delay_us(60);//延时60微妙
  83.                 ds18b20_dq_H;
  84.                
  85.                 dat>>=1;//准备下一位数据的发送       
  86.         }
  87. }

  88. /*******************************************************************************
  89. * 函 数 名         : DS18b20rd
  90. * 函数功能                   : DS18B20读数据时序          
  91. * 输    入         : 无
  92. * 输    出         : value
  93. *******************************************************************************/
  94. u8 DS18b20rd()
  95. {
  96.         u8 i=0,value=0;

  97.         for(i=0;i<8;i++)
  98.         {
  99.                 value>>=1;
  100.                 DQOUTINT();//输出
  101.                 ds18b20_dq_L;         //拉低
  102.                 delay_us(4);//延时4微妙
  103.                 ds18b20_dq_H;
  104.                 delay_us(10);//延时10微妙
  105.                 DQININT();         //输入配置

  106.                 if(GPIO_ReadInputDataBit(GPIO_ds18b20,dq)==1)
  107.                 {
  108.                    value|=0x80;//读数据 从低位开始
  109.                 }

  110.                 delay_us(45);//延时45微妙
  111.         }

  112.         return value;       
  113. }

  114. /*******************************************************************************
  115. * 函 数 名         : readtemp
  116. * 函数功能                   : DS18B2温度寄存器配置,温度读取          
  117. * 输    入         : 无
  118. * 输    出         : value
  119. *******************************************************************************/
  120. double readtemp()                          //读取温度内需要复位的
  121. {
  122.         u16 temp;
  123.         u8 a,b;
  124.         double value;
  125.         ds18b20init();                //初始化
  126.         ds18b20wr(0xcc);   //发送忽略ROM指令
  127.         ds18b20wr(0x44);   //发送温度转换指令
  128.         delay_ms(10);         
  129.         ds18b20init();           //初始化
  130.         ds18b20wr(0xcc);   //发送忽略ROM指令
  131.         ds18b20wr(0xbe);   //发读暂存器指令
  132.         a=DS18b20rd();         //温度的低八位
  133.         b=DS18b20rd();         //温度的高八位
  134.         temp=b;
  135.         temp=(temp<<8)+a;
  136.         if((temp&0xf800)==0xf800)
  137.         {
  138.                 temp=(~temp)+1;
  139.                 value=temp*(-0.0625);
  140.         }
  141.         else
  142.         {
  143.                 value=temp*0.0625;       
  144.         }
  145.         return value;
  146. }
  147. void DS18B20_pros()
  148. {
  149.         double temp;
  150.         u16 tem_dat;
  151.         u8 dat[8];
  152.         TFT_ClearScreen(BLACK);
  153.         delay_ms(10);
  154.         ds18b20_init();
  155.         while(1)
  156.         {

  157.                 if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>82&&TouchData.lcdy<118)
  158.                 {               
  159.                         GUI_Show12Char(10,10,"DS18B20温度检测:",YELLOW,BLACK);
  160.                         GUI_Show12Char(10,40,"将温度传感器插上主板接口上,注意温度传感器的方向,板子上有对应的丝印指示",YELLOW,BLACK);
  161.                         GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);       
  162.                 }
  163.                        
  164.                 temp=readtemp();
  165.                 tem_dat=temp*100;
  166.                 dat[0]=tem_dat/1000+0x30;
  167.                 dat[1]=tem_dat%1000/100+0x30;
  168.                 dat[2]='.';
  169.                 dat[3]=tem_dat%1000%100/10+0x30;
  170.                 dat[4]=tem_dat%1000%100%10+0x30;
  171.                 dat[5]='C';
  172.                 dat[6]='\0';
  173.                 GUI_Show12Char(150,10,dat,YELLOW,BLACK);                               
  174.                 if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
  175.                 {
  176.                         GUI_DisplayInit();
  177.                         break;                                       
  178.                 }
  179.                 TOUCH_Scan();       
  180.         }
  181.                        
  182. }



strang 发表于 2018-8-1 10:19 | 显示全部楼层
楼主辛苦 感谢分享
wsnsyy 发表于 2018-8-1 10:22 | 显示全部楼层
mark,多谢分享
pq113_6 发表于 2018-8-1 11:28 | 显示全部楼层
研发工程师 发表于 2018-8-1 11:41 | 显示全部楼层
感谢分享
zheng2013 发表于 2018-8-1 12:36 | 显示全部楼层
Thank you!
 楼主| 一路向北lm 发表于 2018-8-1 12:51 | 显示全部楼层
strang 发表于 2018-8-1 10:19
楼主辛苦 感谢分享

不辛苦了
efen 发表于 2018-8-1 14:09 | 显示全部楼层
caijie001 发表于 2018-8-1 22:40 | 显示全部楼层
muxb 发表于 2018-8-2 09:02 | 显示全部楼层
楼主V5
您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部