打印
[STM32]

STM32常用驱动

[复制链接]
3377|23
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一路向北lm|  楼主 | 2018-7-31 22:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 一路向北lm 于 2018-7-31 22:54 编辑

驱动大放送

ADC.zip

1.67 KB

adx345.zip

3.39 KB

AT24Cxx.zip

1.67 KB

beep.zip

993 Bytes

bmp.zip

2.93 KB

can.zip

4.64 KB

DAC.zip

1.42 KB

DMA.zip

1.62 KB

DS18B20.zip

2.01 KB

相关帖子

沙发
一路向北lm|  楼主 | 2018-7-31 22:41 | 只看该作者
iic驱动(模拟iic)
/**********************************************************************************/
/*                                                                                */
/*    Copyright (C) 2005 OLIMEX  LTD.                                             */
/*                                                                                */
/*    Module Name    :  i2c module                                                */
/*    File   Name    :  i2c.c                                                     */
/*    Revision       :  01.00                                                     */
/*    Date           :  2005/07/04 initial version                                */
/*                                                                                */
/**********************************************************************************/
#include "i2c.h"

void _NOP(){
volatile int i = 5;
for(;i;--i);
}



extern GPIO_InitTypeDef GPIO_InitStructure;

// SCL -------------------------------------------------------------------------
void SCL_DIR(char state)  {
if(state) {
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  }
  else {
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  }
}

void SCL_OUT(char state) {
  if(state) {
    GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
  }
  else {
    GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);
  }
}



// SDA -------------------------------------------------------------------------
void SDA_DIR(char state)  {
  if(state) {
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  }
  else {
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
  }
}

void SDA_OUT(char state) {
  if(state) {
    GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_SET);
  }
  else {
    GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);
  }
}

char SDA_IN(void) {
   if((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)) == Bit_SET)
     return 1;
   else
     return 0;
}

#define SCL_HIGH  SCL_OUT(1);
#define SCL_LOW   SCL_OUT(0);
#define SDA_HIGH  SDA_DIR(0);
#define SDA_LOW   SDA_DIR(1);



/****************************************************************************/
/*  Initialize I2C interface                                                */
/*  Function : Hrd_I2C_Init                                                 */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void Hrd_I2C_Init(){
  SCL_OUT(0);
  SCL_DIR(1);
  SDA_DIR(0);
  SDA_OUT(0);
}


/****************************************************************************/
/*  Start Conditional for I2C                                               */
/*  Function : Hrd_I2C_StartCond                                            */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void Hrd_I2C_StartCond(){

  SCL_LOW;
  _NOP();
  SDA_HIGH;
  _NOP();
  SCL_HIGH;
  _NOP();
  SDA_LOW;
  _NOP();
  SCL_LOW;
}


/****************************************************************************/
/*  Stop Conditional for I2C                                                */
/*  Function : Hrd_I2C_StopCond                                             */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void Hrd_I2C_StopCond(){

  SCL_LOW;
  SDA_LOW;
  _NOP();
  SCL_HIGH;
  _NOP();
  SDA_HIGH;
}


/****************************************************************************/
/*  Write Byte to I2C                                                       */
/*  Function : Hrd_I2C_WriteByte                                            */
/*      Parameters                                                          */
/*          Input   :   character to write                                  */
/*          Output  :   acknowledge                                         */
/****************************************************************************/
I2C_AKN_DEF Hrd_I2C_WriteByte  (unsigned char ch){

  unsigned int i = 0;
  for(i = 8; i; --i)
  {
    SCL_LOW;
    if(ch&0x80) {
      SDA_HIGH;
    }
    else {
      SDA_LOW;
    }
    _NOP();
    SCL_HIGH;
    ch <<= 1;
    _NOP();
    SCL_LOW;
  }
  SDA_HIGH;
  _NOP();
  SCL_HIGH;
  _NOP();
  i = SDA_IN() ? I2C_NAK: I2C_AKN;
  SCL_LOW;
  return((I2C_AKN_DEF)i);
}


/****************************************************************************/
/*  Read Byte from I2C                                                      */
/*  Function : Hrd_I2C_ReadByte                                             */
/*      Parameters                                                          */
/*          Input   :   need acknowledge                                    */
/*          Output  :   read character                                      */
/****************************************************************************/
unsigned char Hrd_I2C_ReadByte (I2C_AKN_DEF  Akn){

  unsigned char ch =0;
  unsigned int i = 0;
  SDA_HIGH;

  for(i = 8; i; --i)
  {
    SCL_HIGH;
    ch <<=1;
    ch |= SDA_IN() ? 1:0;
    SCL_LOW;
  }
  if (Akn) SDA_LOW;

  SCL_HIGH;
  _NOP();
  SCL_LOW;

  return ch;
}



使用特权

评论回复
板凳
一路向北lm|  楼主 | 2018-7-31 22:42 | 只看该作者
LCD驱动(SPI屏幕)
//lcd_nokia.h
#include "lcd.h"
#include "stm32f10x_lib.h"
#include "arm_comm.h"

// LCD memory index
unsigned int  LcdMemIdx;

// represent LCD matrix
unsigned char  LcdMemory[LCD_CACHE_SIZE];

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


/****************************************************************************/
/*  Init LCD Controler                                                      */
/*  Function : LCDInit                                                      */
/*      Parameters                                                          */
/*          Input   :  Nothing                                              */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDInit(void)
{

  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

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

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

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

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

  // SPI1 configuration
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);

  // Enable SPI1
  SPI_Cmd(SPI1, ENABLE);

  // Toggle display reset pin.
  GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_RESET);
  Delay(10000);
  GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);
  Delay(10000);

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

  // Clear and Update
  LCDClear();
  LCDUpdate();

}

/****************************************************************************/
/*  Send to LCD                                                             */
/*  Function : LCDSend                                                      */
/*      Parameters                                                          */
/*          Input   :  data and  SEND_CHR or SEND_CMD                       */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDSend(unsigned char data, unsigned char cd) {

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

  // command or data - D/S low or high
  if(cd == SEND_CHR) {
    GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
  }
  else {
    GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET);
  }

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

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

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

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


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

/****************************************************************************/
/*  Update LCD memory                                                       */
/*  Function : LCDUpdate                                                    */
/*      Parameters                                                          */
/*          Input   :  Nothing                                              */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDUpdate ( void )
{

  int i;

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

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

/****************************************************************************/
/*  Clear LCD                                                               */
/*  Function : LCDClear                                                     */
/*      Parameters                                                          */
/*          Input   :  Nothing                                              */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDClear(void) {

  int i;

  // loop all cashe array
  for (i=0; i<LCD_CACHE_SIZE; i++)
  {
     LcdMemory[i] = 0x0;
  }

}




/****************************************************************************/
/*  Change LCD Pixel mode                                                   */
/*  Function : LcdContrast                                                  */
/*      Parameters                                                          */
/*          Input   :  contrast                                             */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDPixel (unsigned char x, unsigned char y, unsigned char mode )
{
    unsigned int    index   = 0;
    unsigned char   offset  = 0;
    unsigned char   data    = 0;

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

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

    data = LcdMemory[index];

    if ( mode == PIXEL_OFF )
    {
        data &= (~(0x01 << offset));
    }
    else if ( mode == PIXEL_ON )
    {
        data |= (0x01 << offset);
    }
    else if ( mode  == PIXEL_XOR )
    {
        data ^= (0x01 << offset);
    }

    LcdMemory[index] = data;

}

/****************************************************************************/
/*  Write char at x position on y row                                       */
/*  Function : LCDChrXY                                                     */
/*      Parameters                                                          */
/*          Input   :  pos, row, char                                       */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDChrXY (unsigned char x, unsigned char y, unsigned char ch )
{
    unsigned int    index   = 0;
    unsigned int    i       = 0;

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

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

    for ( i = 0; i < 5; i++ )
    {
      LcdMemory[index] = FontLookup[ch - 32][i] << 1;
      index++;
    }

}


/****************************************************************************/
/*  Write char at x position on y row - inverse                             */
/*  Function : LCDChrXY                                                     */
/*      Parameters                                                          */
/*          Input   :  pos, row, char                                       */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDChrXYInverse (unsigned char x, unsigned char y, unsigned char ch )
{
    unsigned int    index   = 0;
    unsigned int    i       = 0;

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

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

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

      if(i==4)
        LcdMemory[index] = 0xFF;
    }

}


/****************************************************************************/
/*  Set LCD Contrast                                                        */
/*  Function : LcdContrast                                                  */
/*      Parameters                                                          */
/*          Input   :  contrast                                             */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDContrast(unsigned char contrast) {

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

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

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


/****************************************************************************/
/*  Send string to LCD                                                      */
/*  Function : LCDStr                                                       */
/*      Parameters                                                          */
/*          Input   :  row, text, inversion                                 */
/*          Output  :  Nothing                                              */
/****************************************************************************/
void LCDStr(unsigned char row, unsigned char *dataPtr, unsigned char inv ) {

  // variable for X coordinate
  unsigned char x = 0;

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

    if(inv) {
      LCDChrXYInverse(x, row, (*dataPtr));
    }
    else {
      LCDChrXY( x, row, (*dataPtr));
    }
    x++;
    dataPtr++;
  }
}





使用特权

评论回复
地板
一路向北lm|  楼主 | 2018-7-31 22:43 | 只看该作者
2.4G无心模块 (NRF24L01)

#include "stm32f10x_lib.h"
#include "arm_comm.h"
#include "nRF24L01.h"
#include "const.h"

#define CSN_TIME      2
#define CE_HIGH_TIME  10000

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

unsigned char status;

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


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

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


// Init SPI0 Interface
void SPI1Init (void) {

  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

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

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

  // Reset SPI Interface
  SPI_DeInit(SPI1);

  // SPI1 configuration
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);

  // Enable SPI1
  SPI_Cmd(SPI1, ENABLE);

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

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

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

  // Button B1
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

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

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

  // Set chip select and chip enable
  CSN_HIGH();
  CE_LOW();

}

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

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

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

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

  // Return the byte read from the SPI bus
  return SPI_ReceiveData(SPI1);
}

unsigned char SPI_Send_command_with_ADDR (unsigned char cmd, unsigned char addr, unsigned char data_byte)
{
  unsigned char temp,command = 0;
  command = (cmd << 5) | addr;
  CSN_LOW();
  if (cmd == R_REGISTER)
  {
    if (addr == RX_ADDR_P0 || addr == RX_ADDR_P1 || addr == TX_ADDR)
    {

      status=SPI_SendByte(command);
      for (int k=0;k!=5;k++)
      {
        ADDRESS[k]=SPI_SendByte(NOP);
      }
      CSN_HIGH();
      return status;
    }
    else
    {
      status=SPI_SendByte(command);
      temp=SPI_SendByte(NOP);
      CSN_HIGH();
      return temp;
    }
  }
  if (cmd == W_REGISTER)
  {
    if (addr == RX_ADDR_P0)
    {
      status=SPI_SendByte(command);
      for (int j=0;j!=5;j++)
        {
          temp=RX_ADDRESS_P0[j];
          SPI_SendByte(temp);
        }
      CSN_HIGH();
      return status;
    }

    if (addr == RX_ADDR_P1)
    {
      status=SPI_SendByte(command);
      for (int j=0;j!=5;j++)
        {
          temp=RX_ADDRESS_P1[j];
          SPI_SendByte(temp);
        }
      CSN_HIGH();
      return status;
    }

    if (addr == TX_ADDR)
    {
      status=SPI_SendByte(command);
      for (int j=0;j!=5;j++)
        {
          temp=TX_ADDRESS[j];
          SPI_SendByte(temp);
        }
      CSN_HIGH();
      return status;
    }


    else
    {
      temp=SPI_SendByte(command);
      SPI_SendByte(data_byte);
      CSN_HIGH();
      return temp;
    }
  }

  return 1;
}

unsigned char SPI_Send_command_without_ADDR (unsigned char cmd, unsigned char data_byte)
{
  unsigned char temp = 0;
  CSN_LOW();
  if (cmd == R_RX_PAYLOAD)
  {
    status=SPI_SendByte(cmd);
    temp=SPI_SendByte(NOP);
    CSN_HIGH();
    return temp;
  }
  if (cmd == W_TX_PAYLOAD)
  {
    status=SPI_SendByte(cmd);
    SPI_SendByte(data_byte);
    CSN_HIGH();
    return status;
  }
  status = SPI_SendByte(cmd);
  CSN_HIGH();
  return status;

}

// Setting for nRF chip
void nRFSetting(void) {

  // Init SPI
  SPI1Init();

  // Discard transmision
  CE_LOW();

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

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

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

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

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

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

}



使用特权

评论回复
5
一路向北lm|  楼主 | 2018-7-31 22:44 | 只看该作者
PWM驱动
#include "stm32f10x_lib.h"
#include "pwm.h"

TIM1_TimeBaseInitTypeDef  TIM1_TimeBaseStructure;
TIM1_OCInitTypeDef  TIM1_OCInitStructure;
TIM1_BDTRInitTypeDef TIM1_BDTRInitStructure;
extern GPIO_InitTypeDef GPIO_InitStructure;

void InitPWM(void) {

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

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

  // Enable TIM1 clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  // TIM1 Peripheral Configuration
  TIM1_DeInit();

  // Time Base configuration
  TIM1_TimeBaseStructure.TIM1_Prescaler = 0x0;
  TIM1_TimeBaseStructure.TIM1_CounterMode = TIM1_CounterMode_Up;
  // TIM1_TimeBaseStructure.TIM1_Period = 0xFFF; // 12 bit
  TIM1_TimeBaseStructure.TIM1_Period = 0x3FF; // 10 bit
  TIM1_TimeBaseStructure.TIM1_ClockDivision = 0x0;
  TIM1_TimeBaseStructure.TIM1_RepetitionCounter = 0x0;
  TIM1_TimeBaseInit(&TIM1_TimeBaseStructure);


  // Channel 1 Configuration in PWM mode
  TIM1_OCInitStructure.TIM1_OCMode = TIM1_OCMode_PWM2;
  TIM1_OCInitStructure.TIM1_OutputState = TIM1_OutputState_Enable;
  TIM1_OCInitStructure.TIM1_OutputNState = TIM1_OutputNState_Enable;
  TIM1_OCInitStructure.TIM1_Pulse = 0x3FF;
  TIM1_OCInitStructure.TIM1_OCPolarity = TIM1_OCPolarity_Low;
  TIM1_OCInitStructure.TIM1_OCNPolarity = TIM1_OCNPolarity_Low;
  TIM1_OCInitStructure.TIM1_OCIdleState = TIM1_OCIdleState_Set;
  TIM1_OCInitStructure.TIM1_OCNIdleState = TIM1_OCIdleState_Reset;
  TIM1_OC1Init(&TIM1_OCInitStructure);

  // Automatic Output enable, Break, dead time and lock configuration
  TIM1_BDTRInitStructure.TIM1_OSSRState = TIM1_OSSRState_Enable;
  TIM1_BDTRInitStructure.TIM1_OSSIState = TIM1_OSSIState_Enable;
  TIM1_BDTRInitStructure.TIM1_LOCKLevel = TIM1_LOCKLevel_1;
  TIM1_BDTRInitStructure.TIM1_DeadTime = 0x05;
  TIM1_BDTRInitStructure.TIM1_Break = TIM1_Break_Disable;
  TIM1_BDTRInitStructure.TIM1_BreakPolarity = TIM1_BreakPolarity_High;
  TIM1_BDTRInitStructure.TIM1_AutomaticOutput = TIM1_AutomaticOutput_Enable;
  TIM1_BDTRConfig(&TIM1_BDTRInitStructure);

  // TIM1 counter enable
  TIM1_Cmd(ENABLE);

  // Main Output Enable
  TIM1_CtrlPWMOutputs(ENABLE);

}

void SetDutyPeriod(Int16U period) {

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

}






使用特权

评论回复
6
一路向北lm|  楼主 | 2018-7-31 22:45 | 只看该作者
RTC驱动
// ADC.c
#include "stm32f10x_lib.h"
#include "rtc.h"
#include "arm_comm.h"
#include "includes.h"

extern GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

void RTCInit(void) {

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

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

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

  // Allow access to BKP Domain
  PWR_BackupAccessCmd(ENABLE);

  // Reset Backup Domain
  BKP_DeInit();

  // Enable the LSE OSC
  RCC_LSEConfig(RCC_LSE_ON);

  // Disable the LSI OSC
  RCC_LSICmd(DISABLE);

  // Select the RTC Clock Source
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  // Enable the RTC Clock
  RCC_RTCCLKCmd(ENABLE);

  // Wait for RTC registers synchronization
  RTC_WaitForSynchro();

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

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

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

  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

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

  // Enable the RTC Interrupt
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  // Enable interrupts
  __enable_interrupt();

}





使用特权

评论回复
7
一路向北lm|  楼主 | 2018-7-31 22:46 | 只看该作者
DMA驱动
#include "dma.h"
u32 adc_data[10];
void dma_init()
{
        GPIO_InitTypeDef GPIO_InitStructure;
        ADC_InitTypeDef ADC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;

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

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


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

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

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

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


使用特权

评论回复
8
一路向北lm|  楼主 | 2018-7-31 22:47 | 只看该作者
ADX345驱动
#include "adx345.h"
#include "iic.h"
#include "math.h"

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

/****************************************************************************
* Function Name  : ADX345_WriteReg
* Description    : 设置ADX345寄存器
* Input          : addr:寄存器地址
*                * dat:吸入数据
* Output         : None
* Return         : None
****************************************************************************/

static int8_t ADX345_WriteReg(uint8_t addr, uint8_t dat)
{
        I2C_Start();
        I2C_Send_Byte(ADX345_ADDR); //24C02写地址
    if(I2C_Wait_Ack())          //如果无应答,表示发送失败
    {
        return 0xFF;
    }
    I2C_Send_Byte(addr);
    if(I2C_Wait_Ack())         //如果无应答,表示发送失败
    {
        return 0xFF;
    }
    I2C_Send_Byte(dat);
    if(I2C_Wait_Ack())
    {
        return 0xFF;
    }
   
    I2C_Stop();
    return 0;   
}

/****************************************************************************
* Function Name  : ADX345_ReadReg
* Description    : 读取ADX345寄存器
* Input          : addr:读取地址
* Output         : None
* Return         : 读取到的8位数据
****************************************************************************/

static uint8_t ADX345_ReadReg(uint8_t addr)
{
    uint8_t readValue = 0xFF;

    I2C_Start();
        I2C_Send_Byte(ADX345_ADDR);    //24C02写地址
    if(I2C_Wait_Ack())
    {
        return readValue;
    }
    I2C_Send_Byte(addr);
    if(I2C_Wait_Ack())
    {
        return readValue;
    }

    I2C_Start();
        I2C_Send_Byte(ADX345_ADDR + 1); //24C02读地址
    if(I2C_Wait_Ack())
    {
        return readValue;      
    }
    readValue = I2C_Read_Byte(0);
    I2C_Stop();

    return readValue;
}

/****************************************************************************
* Function Name  : ADX345_ReadXYZ
* Description    : 读取X,Y,Z的AD值
* Input          : xValue:X轴的保存地址
*                * yValue:Y轴的保存地址
*                * zValue:Z轴的保存地址
* Output         : None
* Return         : 0:读取成功。0xFF:读取失败
****************************************************************************/

static int8_t ADX345_ReadXYZ(int16_t *xValue, int16_t *yValue, int16_t *zValue)
{
    uint8_t readValue[6], i;
    I2C_Start();
        I2C_Send_Byte(ADX345_ADDR);    //24C02写地址
    if(I2C_Wait_Ack())
    {
        return 0xFF;
    }
    I2C_Send_Byte(0x32);           //发送读地址(X轴首地址)
    if(I2C_Wait_Ack())
    {
        return 0xFF;
    }

    I2C_Start();
        I2C_Send_Byte(ADX345_ADDR + 1); //24C02读地址
    if(I2C_Wait_Ack())
    {
        return 0xFF;      
    }

    /* 读取六个字节数据 */
    for(i=0; i<6; i++)
    {
        
        if(i == 5)        //接收最后一个字节时,发送NACK
        {
            readValue[i] = I2C_Read_Byte(0);
        }
        else             //发送接收后应答
        {
            readValue[i] = I2C_Read_Byte(1);
        }
    }
    I2C_Stop();
    /* 处理读取到的数据 */
        *xValue = (uint16_t)(readValue[1] << 8) + readValue[0];             
        *yValue = (uint16_t)(readValue[3] << 8) + readValue[2];             
        *zValue = (uint16_t)(readValue[5] << 8) + readValue[4];           

   return 0;
}

/****************************************************************************
* Function Name  : ADX345_Init
* Description    : 初始化ADX345,并核对ADX的ID
* Input          : None
* Output         : None
* Return         : 0:成功。0xFF:失败
****************************************************************************/

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

/****************************************************************************
* Function Name  : ADX345_Adjust
* Description    : ADX345进行校正。
* Input          : None
* Output         : None
* Return         : None
****************************************************************************/

void ADX345_Adjust(void)
{
    int32_t offx = 0, offy = 0, offz = 0;
    int16_t xValue, yValue, zValue;
    uint8_t i;

        ADX345_WriteReg(ADX_POWER_CTL, 0x00);         //先进入休眠模式.
        ADX345_DelayMs(100);
        ADX345_Init();
    ADX345_DelayMs(20);
   
    /* 读取十次数值 */
    for(i=0; i<10; i++)
    {
        ADX345_ReadXYZ(&xValue, &yValue, &zValue);
        offx += xValue;
        offy += yValue;
        offz += zValue;
        ADX345_DelayMs(10);   //才样频率在100HZ,10ms采样一次最好         
    }
   
    /* 求出平均值 */
    offx /= 10;
    offy /= 10;
    offz /= 10;
   
    /* 全分辨率下,每个输出LSB为3.9 mg或偏移寄存器LSB的四分之一,所以除以4 */
    xValue = -(offx / 4);
        yValue = -(offy / 4);
        zValue = -((offz - 256) / 4);
   
    /* 设置偏移量 */
        ADX345_WriteReg(ADX_OFSX, xValue);
        ADX345_WriteReg(ADX_OFSY, yValue);
        ADX345_WriteReg(ADX_OFSZ, zValue);
            
}

/****************************************************************************
* Function Name  : ADX_GetXYZData
* Description    : 读取ADX的XYZ轴的值(进行过数据处理)
* Input          : xValue:X轴的保存地址
*                * yValue:Y轴的保存地址
*                * zValue:Z轴的保存地址
* Output         : None
* Return         : None
****************************************************************************/

void ADX_GetXYZData(int16_t *xValue, int16_t *yValue, int16_t *zValue)
{
    int32_t xTotal = 0, yTotal = 0, zTotal = 0;
    uint8_t i;
   
    *xValue = 0;
    *yValue = 0;
    *zValue = 0;

    /* 读取十次采样值 */
    for(i=0; i<10; i++)
    {
        ADX345_ReadXYZ(xValue, yValue, zValue);
        xTotal += *xValue;
        yTotal += *yValue;
        zTotal += *zValue;
        ADX345_DelayMs(10);  //才样频率在100HZ,10ms采样一次最好
    }
   
    /* 求出平均值 */
    *xValue = xTotal / 10;
    *yValue = yTotal / 10;
    *zValue = zTotal / 10;      
}

/****************************************************************************
* Function Name  : ADX_GetAngle
* Description    : 将AD值转换成角度值
* Input          : xValue:x的值
*                * yValue:y的值
*                * zValue:z的值
*                * dir:0:与Z轴的角度;1:与X轴的角度;2:与Y轴的角度.
* Output         : None
* Return         : None
****************************************************************************/

int16_t ADX_GetAngle(float xValue, float yValue, float zValue, uint8_t dir)
{
        float temp;
        float res = 0;

        switch(dir)
        {   
        /* 与自然Z轴的角度 */
                case 0:
                        temp = sqrt((xValue * xValue + yValue * yValue)) / zValue;
                        res = atan(temp);
                        break;
        
        /* 与自然X轴的角度 */
                case 1:
                        temp = xValue / sqrt((yValue * yValue + zValue * zValue));
                        res = atan(temp);
                        break;

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

        default:
            break;
        }

    res = res * 1800 / 3.14;

        return res;
}
void ADX345_pros()
{
        int16_t value;
    uint8_t num = 6;
        int16_t Xval, Yval, Zval, Xang, Yang, Zang;
        uint8_t ShowData[6] = {0, 0, 0, 0, 0, 0};
        TFT_ClearScreen(BLACK);
        delay_ms(10);
        while(ADX345_Init())
    {
        GUI_Show12Char(60, 42, "ADX345 ERROR!", RED, BLACK);
        delay_ms(100);        
    }
        while(1)
        {
                if(TouchData.lcdx>16&&TouchData.lcdx<56&&TouchData.lcdy>200&&TouchData.lcdy<238)
                {               

                    GUI_Show12Char(10, 10, "ADX345加速度传感器实验", RED, BLACK);
                    GUI_Show12Char(0, 84, "X VAL:", RED, BLACK);
                    GUI_Show12Char(0, 105, "Y VAL:", RED, BLACK);
                    GUI_Show12Char(0, 126, "Z VAL:", RED, BLACK);
                    GUI_Show12Char(0, 147, "X ANG:", RED, BLACK);
                    GUI_Show12Char(0, 168, "Y ANG:", RED, BLACK);
                    GUI_Show12Char(0, 189, "Z ANG:", RED, BLACK);
                        GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);               
                }
                       
                ADX345_Adjust();

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

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

                while(num)
            {
                switch(num)
                {
                    case(1):
                        value = Zang;
                        break;
                    case(2):
                        value = Yang;
                        break;
                    case(3):
                        value = Xang;
                        break;
                    case(4):
                        value = Zval;
                        break;
                    case(5):
                        value = Yval;
                        break;
                    case(6):
                        value = Xval;
                        break;
                    default:
                       break;
                }
                        TOUCH_Scan();
                        if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
                        {
                                GUI_DisplayInit();
                                break;                                       
                        }
                if(value > 0)
                {
                    ShowData[0] = '+';
                }
                else
                {
                    ShowData[0] = '-';
                    value = abs(value);//求出绝对值
                }
                ShowData[1] = (value % 10000 /1000) + '0';
                ShowData[2] = (value % 1000 /100) + '0';
                ShowData[3] = (value % 100 /10) + '0';
                ShowData[4] = (value % 10) + '0';
                
                GUI_Show12Char(48, (210 - num*21), ShowData, RED, BLACK);
                num--;
            }
                num=6;
                if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
                {
                        GUI_DisplayInit();
                        break;                                       
                }
                TOUCH_Scan();
        }       
}



使用特权

评论回复
9
一路向北lm|  楼主 | 2018-7-31 22:48 | 只看该作者
CAN总线驱动

#include "can.h"
#include "key.h"

#ifdef CAN_RX0_INT_ENABLE

uint8_t CAN_RX_BUFF[8];

#endif

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

/****************************************************************************
* Function Name  : CAN1_NVIC_Config
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
****************************************************************************/

static void CAN1_NVIC_Config(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;

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

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

void CAN1_Config(uint8_t mode)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        CAN_InitTypeDef CAN_InitStructure;

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

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

        GPIO_Init(GPIOA, &GPIO_InitStructure);

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

        GPIO_Init(GPIOA, &GPIO_InitStructure);

/***************************************************************************/
/********************* CAN设置和初始化 *************************************/
/***************************************************************************/

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

    /* 初始化CAN的参数 */

        CAN_DeInit(CAN1);
        CAN_StructInit(&CAN_InitStructure);

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

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

        CAN_Init(CAN1, &CAN_InitStructure);

#ifdef CAN_RX0_INT_ENABLE
        CAN1_NVIC_Config();
#endif   

}

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

void CAN1_SendMesg(uint32_t id, uint8_t len, uint8_t *dat)
{
        uint16_t i = 0;
        CanTxMsg TxMessage;

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

        /* 将数据放入到邮箱中 */
        for(i=0; i<len; i++)                
        {
                TxMessage.Data[i] = *dat;
                dat++;       
        }
   
    /* 开始传送数据 */
        CAN_Transmit(CAN1, &TxMessage);
}

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

void CAN1_Config16BitFilter(uint16_t id1, uint16_t id2)
{
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;
    uint16_t mask = 0xFFFF;

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

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

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

        CAN_FilterInit(&CAN_FilterInitStructure);
}

/****************************************************************************
* Function Name  : CAN1_Config32BitFilter
* Description    : 设置一个拓展ID的接收
* Input          : id:要接收的ID
* Output         : None
* Return         : None
****************************************************************************/

void CAN1_Config32BitFilter(uint32_t id)
{
    uint32_t mask = 0xFFFFFFFF;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;

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

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

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

        CAN_FilterInit(&CAN_FilterInitStructure);
}

/****************************************************************************
* Function Name  : CAN1_ReceiveMesg
* Description    : 接收一个报文
* Input          : receiveBuff:接收数据的数组指针
* Output         : None
* Return         : None
****************************************************************************/

void CAN1_ReceiveMesg(uint8_t *receiveBuff)
{
        uint8_t i = 0;

        CanRxMsg RxMessage;        //设置接收邮箱

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

/****************************************************************************
* Function Name  : USB_LP_CAN1_RX0_IRQHandler
* Description    : FIFO0接收一个报文数据数据(最大8个字节)
* Input          : None
* Output         : None
* Return         : None
****************************************************************************/

#ifdef CAN_RX0_INT_ENABLE

void USB_LP_CAN1_RX0_IRQHandler(void)
{
        CAN1_ReceiveMesg(CAN_RX_BUFF);       
}

#endif

void GUI_DisplayData(uint8_t num)
{
    uint8_t addr;
   
    for(addr=0; addr<10; addr++)
    {   
        if(addr < 8)
        {
            /* 如果接收到数据,显示数据 */
            /* 将读取到的16位数据转换成可以显示的ASCII码 */
            ShowData[0] = CAN_RX_BUFF[addr] >> 4;
            if(ShowData[0] > 9)
            {
                ShowData[0] += '7';  //当大于9时,用ABCDEF来表示
            }
            else
            {
                ShowData[0] += '0';   
            }
            ShowData[1] = CAN_RX_BUFF[addr] & 0x0F;
            if(ShowData[1] > 9)
            {
                ShowData[1] += '7';  //当大于9时,用ABCDEF来表示
            }
            else
            {
                ShowData[1] += '0';   
            }
            /* 显示接收到的数据 */
            GUI_Show12Char(208, (60 + addr * 20), ShowData, RED, BLACK);
        }
        
        /* 将要发送的16位数据转换成可以显示的ASCII码 */
        ShowData[0] = CAN_SendData[addr] >> 4;
        if(ShowData[0] > 9)
        {
            ShowData[0] += '7';    //当大于9时,用ABCDEF来表示
        }
        else
        {
            ShowData[0] += '0';   
        }
        ShowData[1] = CAN_SendData[addr] & 0x0F;
        if(ShowData[1] > 9)
        {
            ShowData[1] += '7';    //当大于9时,用ABCDEF来表示
        }
        else
        {
            ShowData[1] += '0';   
        }

        if(addr < 8)
        {
            /* 显示发送数据,选择高亮位置 */
            if(num == addr)
            {
                GUI_Show12Char(88, (60 + addr * 20), ShowData, GREEN, BLACK);
            }
            else
            {
                GUI_Show12Char(88, (60 + addr * 20), ShowData, RED, BLACK);
            }
        }
        else //显示ID部分
        {
            if(addr == 8)
            {
                if(num == 8)
                {
                    GUI_Show12Char(52, 40, ShowData, GREEN, BLACK);
                }
                else
                {
                    GUI_Show12Char(52, 40, ShowData, RED, BLACK);
                }
            }
            if(addr == 9)
            {
                if(num == 9)
                {
                    GUI_Show12Char(176, 40, ShowData, GREEN, BLACK);
                }
                else
                {
                    GUI_Show12Char(176, 40, ShowData, RED, BLACK);
                }
            }
        }        
    }
    if(num > 9)
    {
        GUI_Show12Char(104, 20, Mode, GREEN, BLACK);
    }
    else
    {
        GUI_Show12Char(104, 20, Mode, RED, BLACK);        
    }
}

void CAN_pros()
{
        uint8_t m, receiveId,canMode;
        m = 10;
    canMode = 1; //要设置canMode不等于CAN_SendData[10],以便进入循环一开始就初始化
        TFT_ClearScreen(BLACK);
        delay_ms(10);
        while(1)
        {
                if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>268&&TouchData.lcdy<306)
                {               
               
                        GUI_Show12Char(76, 0, "CAN通信实验", RED, BLACK);
                    /* 显示工作模式 */
                    GUI_Show12Char(0, 20, "CAN工作模式:", RED, BLACK);
               
                    /* 显示发送接收ID */
                    GUI_Show12Char(0, 40, "发送ID:", RED, BLACK);
                    GUI_Show12Char(120, 40, "接收ID:", RED, BLACK);
                    
                    /* 显示发送和接收数据的8位数 */   
                    GUI_Show12Char(0, 60, "发送第一位:", RED, BLACK);
                    GUI_Show12Char(120, 60, "接收第一位:", RED, BLACK);
                    GUI_Show12Char(0, 80, "发送第二位:", RED, BLACK);
                    GUI_Show12Char(120, 80, "接收第二位:", RED, BLACK);
                    GUI_Show12Char(0, 100, "发送第三位:", RED, BLACK);
                    GUI_Show12Char(120, 100, "接收第三位:", RED, BLACK);
                    GUI_Show12Char(0, 120, "发送第四位:", RED, BLACK);
                    GUI_Show12Char(120, 120, "接收第四位:", RED, BLACK);
                    GUI_Show12Char(0, 140, "发送第五位:", RED, BLACK);
                    GUI_Show12Char(120, 140, "接收第五位:", RED, BLACK);
                    GUI_Show12Char(0, 160, "发送第六位:", RED, BLACK);
                    GUI_Show12Char(120, 160, "接收第六位:", RED, BLACK);
                    GUI_Show12Char(0, 180, "发送第七位:", RED, BLACK);
                    GUI_Show12Char(120, 180, "接收第七位:", RED, BLACK);
                    GUI_Show12Char(0, 200, "发送第八位:", RED, BLACK);
                    GUI_Show12Char(120, 200, "接收第八位:", RED, BLACK);
                    GUI_Show12Char(0, 220, "上键:高亮部分加一", BLUE, BLACK);
                    GUI_Show12Char(0, 240, "下键:高亮部分减一", BLUE, BLACK);
                    GUI_Show12Char(0, 260, "左键:高亮部分向下移动一位", BLUE, BLACK);
                    GUI_Show12Char(0, 280, "右键:发送数据", BLUE, BLACK);
                    GUI_Show12Char(0, 300, "注:波特率为450K", BLUE, BLACK);
                        GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);
                        TouchData.lcdx=0;
                        TouchData.lcdy=0;               
                }
                TOUCH_Scan();
                /* 修改模式 */
        if(canMode != CAN_SendData[10] )
        {
            canMode = CAN_SendData[10];
            if(CAN_SendData[10])
            {
               CAN1_Config(CAN_Mode_Normal);
               Mode = (uint8_t *)CAN_ModeNormal;   
            }
            else
            {
                CAN1_Config(CAN_Mode_LoopBack);
                Mode = (uint8_t *)CAN_ModeLoopBack;
            }
            CAN1_Config16BitFilter(CAN_SendData[9], 0x00);   
        }
        /* 如果接收ID改变,就重新设置接收ID */
        if(receiveId != CAN_SendData[9])
        {
            receiveId = CAN_SendData[9];
            if(receiveId != 0x00)
            {
                CAN1_Config16BitFilter(CAN_SendData[9], 0x00);
            }               
        }
                /* 显示数据 */
        GUI_DisplayData(m);

                if(k_up==1)          //判断按键k_up是否按下
                {
                        delay_ms(10); //消抖处理
                        if(k_up==1)         //再次判断按键k_up是否按下
                        {
                                if(m == 10)
                {
                    CAN_SendData[10] = ~CAN_SendData[10];
                }
                else
                {
                    CAN_SendData[m]++;   
                }
                GUI_Show12Char(175, 20, "等待发送", RED, BLACK);                                       
                        }
                        while(k_up); //等待按键松开
                }
                if(k_down==0)
                {
                        delay_ms(10);
                        if(k_down==0)
                        {
                                if(m == 10)
                {
                    CAN_SendData[10] = ~CAN_SendData[10];
                }
                else
                {
                    CAN_SendData[m]--;   
                }
                GUI_Show12Char(175, 20, "等待发送", RED, BLACK);                       
                        }
                        while(!k_down);
                }
                if(k_left==0)
                {
                        delay_ms(10);
                        if(k_left==0)
                        {
                                if(m == 10)
                {
                    m = 0;
                }
                else
                {
                    m++;
                }                       
                        }
                        while(!k_left);
                }
                if(k_right==0)
                {
                        delay_ms(10);
                        if(k_right==0)
                        {
                                CAN1_SendMesg(CAN_SendData[8], 8, CAN_SendData);//发送数据
                GUI_Show12Char(175, 20, "发送成功", BLUE, BLACK);                       
                        }
                        while(!k_right);
                }               


                if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
                {
                        GUI_DisplayInit();
                        break;                                       
                }
                TOUCH_Scan();       
        }       
}



使用特权

评论回复
10
一路向北lm|  楼主 | 2018-7-31 22:49 | 只看该作者
DS18B20温度传感器驱动
#include "ds18b20.h"

/*******************************************************************************
* 函 数 名         : ds18b20_init
* 函数功能                   : IO端口时钟初始化函数          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void ds18b20_init()
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);

        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);
}

/*******************************************************************************
* 函 数 名         : DQININT
* 函数功能                   : 输入配置          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void DQININT()         //输入配置
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
}

/*******************************************************************************
* 函 数 名         : DQOUTINT
* 函数功能                   : 输出配置          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void DQOUTINT()         //输出配置
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
}

/*******************************************************************************
* 函 数 名         : ds18b20init
* 函数功能                   : DS18B20初始化时序          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void ds18b20init()
{
        DQOUTINT();//输出
        ds18b20_dq_L;
        delay_us(480);//延时480微妙       
        ds18b20_dq_H;
        delay_us(480);//延时480微妙
}

/*******************************************************************************
* 函 数 名         : ds18b20wr
* 函数功能                   : DS18B20写数据时序          
* 输    入         : dat
* 输    出         : 无
*******************************************************************************/
void ds18b20wr(u8 dat)
{
        u8 i=0;
        DQOUTINT();//输出

        for(i=0;i<8;i++)
        {
                ds18b20_dq_L;         //拉低
                delay_us(15);//延时15微妙
               
                if((dat&0x01)==1)
                {
                        ds18b20_dq_H;
                }
                else
                {
                        ds18b20_dq_L;
                }
                delay_us(60);//延时60微妙
                ds18b20_dq_H;
               
                dat>>=1;//准备下一位数据的发送       
        }
}

/*******************************************************************************
* 函 数 名         : DS18b20rd
* 函数功能                   : DS18B20读数据时序          
* 输    入         : 无
* 输    出         : value
*******************************************************************************/
u8 DS18b20rd()
{
        u8 i=0,value=0;

        for(i=0;i<8;i++)
        {
                value>>=1;
                DQOUTINT();//输出
                ds18b20_dq_L;         //拉低
                delay_us(4);//延时4微妙
                ds18b20_dq_H;
                delay_us(10);//延时10微妙
                DQININT();         //输入配置

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

                delay_us(45);//延时45微妙
        }

        return value;       
}

/*******************************************************************************
* 函 数 名         : readtemp
* 函数功能                   : DS18B2温度寄存器配置,温度读取          
* 输    入         : 无
* 输    出         : value
*******************************************************************************/
double readtemp()                          //读取温度内需要复位的
{
        u16 temp;
        u8 a,b;
        double value;
        ds18b20init();                //初始化
        ds18b20wr(0xcc);   //发送忽略ROM指令
        ds18b20wr(0x44);   //发送温度转换指令
        delay_ms(10);         
        ds18b20init();           //初始化
        ds18b20wr(0xcc);   //发送忽略ROM指令
        ds18b20wr(0xbe);   //发读暂存器指令
        a=DS18b20rd();         //温度的低八位
        b=DS18b20rd();         //温度的高八位
        temp=b;
        temp=(temp<<8)+a;
        if((temp&0xf800)==0xf800)
        {
                temp=(~temp)+1;
                value=temp*(-0.0625);
        }
        else
        {
                value=temp*0.0625;       
        }
        return value;
}
void DS18B20_pros()
{
        double temp;
        u16 tem_dat;
        u8 dat[8];
        TFT_ClearScreen(BLACK);
        delay_ms(10);
        ds18b20_init();
        while(1)
        {

                if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>82&&TouchData.lcdy<118)
                {               
                        GUI_Show12Char(10,10,"DS18B20温度检测:",YELLOW,BLACK);
                        GUI_Show12Char(10,40,"将温度传感器插上主板接口上,注意温度传感器的方向,板子上有对应的丝印指示",YELLOW,BLACK);
                        GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);       
                }
                       
                temp=readtemp();
                tem_dat=temp*100;
                dat[0]=tem_dat/1000+0x30;
                dat[1]=tem_dat%1000/100+0x30;
                dat[2]='.';
                dat[3]=tem_dat%1000%100/10+0x30;
                dat[4]=tem_dat%1000%100%10+0x30;
                dat[5]='C';
                dat[6]='\0';
                GUI_Show12Char(150,10,dat,YELLOW,BLACK);                               
                if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
                {
                        GUI_DisplayInit();
                        break;                                       
                }
                TOUCH_Scan();       
        }
                       
}



使用特权

评论回复
11
一路向北lm|  楼主 | 2018-7-31 22:50 | 只看该作者
flash驱动
#include "ds18b20.h"

/*******************************************************************************
* 函 数 名         : ds18b20_init
* 函数功能                   : IO端口时钟初始化函数          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void ds18b20_init()
{
        GPIO_InitTypeDef GPIO_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);

        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);
}

/*******************************************************************************
* 函 数 名         : DQININT
* 函数功能                   : 输入配置          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void DQININT()         //输入配置
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
}

/*******************************************************************************
* 函 数 名         : DQOUTINT
* 函数功能                   : 输出配置          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void DQOUTINT()         //输出配置
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=dq;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIO_ds18b20,&GPIO_InitStructure);       
}

/*******************************************************************************
* 函 数 名         : ds18b20init
* 函数功能                   : DS18B20初始化时序          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void ds18b20init()
{
        DQOUTINT();//输出
        ds18b20_dq_L;
        delay_us(480);//延时480微妙       
        ds18b20_dq_H;
        delay_us(480);//延时480微妙
}

/*******************************************************************************
* 函 数 名         : ds18b20wr
* 函数功能                   : DS18B20写数据时序          
* 输    入         : dat
* 输    出         : 无
*******************************************************************************/
void ds18b20wr(u8 dat)
{
        u8 i=0;
        DQOUTINT();//输出

        for(i=0;i<8;i++)
        {
                ds18b20_dq_L;         //拉低
                delay_us(15);//延时15微妙
               
                if((dat&0x01)==1)
                {
                        ds18b20_dq_H;
                }
                else
                {
                        ds18b20_dq_L;
                }
                delay_us(60);//延时60微妙
                ds18b20_dq_H;
               
                dat>>=1;//准备下一位数据的发送       
        }
}

/*******************************************************************************
* 函 数 名         : DS18b20rd
* 函数功能                   : DS18B20读数据时序          
* 输    入         : 无
* 输    出         : value
*******************************************************************************/
u8 DS18b20rd()
{
        u8 i=0,value=0;

        for(i=0;i<8;i++)
        {
                value>>=1;
                DQOUTINT();//输出
                ds18b20_dq_L;         //拉低
                delay_us(4);//延时4微妙
                ds18b20_dq_H;
                delay_us(10);//延时10微妙
                DQININT();         //输入配置

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

                delay_us(45);//延时45微妙
        }

        return value;       
}

/*******************************************************************************
* 函 数 名         : readtemp
* 函数功能                   : DS18B2温度寄存器配置,温度读取          
* 输    入         : 无
* 输    出         : value
*******************************************************************************/
double readtemp()                          //读取温度内需要复位的
{
        u16 temp;
        u8 a,b;
        double value;
        ds18b20init();                //初始化
        ds18b20wr(0xcc);   //发送忽略ROM指令
        ds18b20wr(0x44);   //发送温度转换指令
        delay_ms(10);         
        ds18b20init();           //初始化
        ds18b20wr(0xcc);   //发送忽略ROM指令
        ds18b20wr(0xbe);   //发读暂存器指令
        a=DS18b20rd();         //温度的低八位
        b=DS18b20rd();         //温度的高八位
        temp=b;
        temp=(temp<<8)+a;
        if((temp&0xf800)==0xf800)
        {
                temp=(~temp)+1;
                value=temp*(-0.0625);
        }
        else
        {
                value=temp*0.0625;       
        }
        return value;
}
void DS18B20_pros()
{
        double temp;
        u16 tem_dat;
        u8 dat[8];
        TFT_ClearScreen(BLACK);
        delay_ms(10);
        ds18b20_init();
        while(1)
        {

                if(TouchData.lcdx>72&&TouchData.lcdx<112&&TouchData.lcdy>82&&TouchData.lcdy<118)
                {               
                        GUI_Show12Char(10,10,"DS18B20温度检测:",YELLOW,BLACK);
                        GUI_Show12Char(10,40,"将温度传感器插上主板接口上,注意温度传感器的方向,板子上有对应的丝印指示",YELLOW,BLACK);
                        GUI_Show12Char(TFT_XMAX-16*2,TFT_YMAX-16,"返回",YELLOW,BLACK);       
                }
                       
                temp=readtemp();
                tem_dat=temp*100;
                dat[0]=tem_dat/1000+0x30;
                dat[1]=tem_dat%1000/100+0x30;
                dat[2]='.';
                dat[3]=tem_dat%1000%100/10+0x30;
                dat[4]=tem_dat%1000%100%10+0x30;
                dat[5]='C';
                dat[6]='\0';
                GUI_Show12Char(150,10,dat,YELLOW,BLACK);                               
                if(TouchData.lcdx>TFT_XMAX-16*2&&TouchData.lcdx<TFT_XMAX&&TouchData.lcdy>TFT_YMAX-16&&TouchData.lcdy<TFT_YMAX)       
                {
                        GUI_DisplayInit();
                        break;                                       
                }
                TOUCH_Scan();       
        }
                       
}



使用特权

评论回复
12
strang| | 2018-8-1 10:19 | 只看该作者
楼主辛苦 感谢分享

使用特权

评论回复
13
wsnsyy| | 2018-8-1 10:22 | 只看该作者
mark,多谢分享

使用特权

评论回复
14
pq113_6| | 2018-8-1 11:28 | 只看该作者
赞!

使用特权

评论回复
15
研发工程师| | 2018-8-1 11:41 | 只看该作者
感谢分享

使用特权

评论回复
16
zheng2013| | 2018-8-1 12:36 | 只看该作者
Thank you!

使用特权

评论回复
17
一路向北lm|  楼主 | 2018-8-1 12:51 | 只看该作者
strang 发表于 2018-8-1 10:19
楼主辛苦 感谢分享

不辛苦了

使用特权

评论回复
18
efen| | 2018-8-1 14:09 | 只看该作者
mark

使用特权

评论回复
19
caijie001| | 2018-8-1 22:40 | 只看该作者
mark

使用特权

评论回复
20
muxb| | 2018-8-2 09:02 | 只看该作者
楼主V5

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

257

主题

3640

帖子

73

粉丝