从业三个月以来的一些总结

ADC:
模式:独立工作模式,每个ADC互不干扰,
 扫描模式,根据SQR寄存器的设定,按顺序扫描AD端口
通道数:15
通道使用情况:
0  -- 右后防跌落
1  -- 电池温度
2  -- 右前防跌落
3  -- 前防碰撞
4  -- 左后防跌落

6  -- 前防跌落
7  -- 电池电压
8  -- 左防跌落
9  -- 充电电流
10 -- 左防碰撞
11 -- 风扇检测
12 -- 充电电压
13 -- 右前防碰撞
14 -- 右防碰撞
15 -- 左防碰撞
工作描述:将ADC相关参数配置完成以后,由定时器4的CC4(捕获/比较4通道)事件触发转换,将15个通道转换完成以后,等待TIM4的CC4事件再次触发
时间计算:TIM4

睡眠唤醒事件:休眠过程中,接收到面板clean按键的指令或者是遥控的指令
             休眠过程中,预约的时间到了(RTC_Alarm)
 休眠过程中,接上电源      






关于assert_param函数的使用:
在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用。如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义;
在固件库中,它的作用就是检测传递给函数的参数是否是有效的参数。
所谓有效的参数是指满足规定范围的参数,比如某个参数的取值范围只能是小于3的正整数,如果给出的参数大于3,
则这个assert_param()可以在运行的程序调用到这个函数时报告错误,使程序员可以及时发现错误,而不必等到程序运行结果的错误而大费周折。                                                                                                                                                                                          










  关于SPI通讯,
利用SPI通讯,读和写一个压力传感器的例子。


The code below starts out by setting the SCP1000's configuration registers in the setup(). In the main loop, it sets the sensor to read in high resolution mode, meaning that it will return a 19-bit value, for the pressure reading, and 16 bits for the temperature. The actual reading in degrees Celsius is the 16-bit result divided by 20.


Then it reads the temperature's two bytes. Once it's got the temperature, it reads the pressure in two parts. First it reads the highest three bits, then the lower 16 bits. It combines these two into one single long integer by bit shifting the high bits then using a bitwise OR to combine them with the lower 16 bits. The actual pressure in Pascal is the 19-bit result divide by 4.


   /*
 SCP1000 Barometric Pressure Sensor Display


 Shows the output of a Barometric Pressure Sensor on a
 Uses the SPI library. For details on the sensor, see:
 http://www.sparkfun.com/commerce/product_info.php?products_id=8161
 http://www.vti.fi/en/support/obsolete_products/pressure_sensors/


 This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
 http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip


 Circuit:
 SCP1000 sensor attached to pins 6, 7, 10 - 13:
 DRDY: pin 6
 CSB: pin 7
 MOSI: pin 11
 MISO: pin 12
 SCK: pin 13


 created 31 July 2010
 modified 14 August 2010
 by Tom Igoe
 */


// the sensor communicates using SPI, so include the library:
#include <SPI.h>


//Sensor's memory register addresses:
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
const int TEMPERATURE = 0x21;   //16 bit temperature reading
const byte READ = 0b11111100;     // SCP1000's read command
const byte WRITE = 0b00000010;   // SCP1000's write command


// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int dataReadyPin = 6;
const int chipSelectPin = 7;


void setup() {
  Serial.begin(9600);


  // start the SPI library:
  SPI.begin();


  // initalize the  data ready and chip select pins:
  pinMode(dataReadyPin, INPUT);
  pinMode(chipSelectPin, OUTPUT);


  //Configure SCP1000 for low noise configuration:
  writeRegister(0x02, 0x2D);
  writeRegister(0x01, 0x03);
  writeRegister(0x03, 0x02);
  // give the sensor time to set up:
  delay(100);
}


void loop() {
  //Select High Resolution Mode
  writeRegister(0x03, 0x0A);


  // don't do anything until the data ready pin is high:
  if (digitalRead(dataReadyPin) == HIGH) {
    //Read the temperature data
    int tempData = readRegister(0x21, 2);


    // convert the temperature to celsius and display it:
    float realTemp = (float)tempData / 20.0;
    Serial.print("Temp[C]=");
    Serial.print(realTemp);




    //Read the pressure data highest 3 bits:
    byte  pressure_data_high = readRegister(0x1F, 1);
    pressure_data_high &= 0b00000111; //you only needs bits 2 to 0


    //Read the pressure data lower 16 bits:
    unsigned int pressure_data_low = readRegister(0x20, 2);
    //combine the two parts into one 19-bit number:
    long pressure = ((pressure_data_high << 16) | pressure_data_low) / 4;


    // display the temperature:
    Serial.println("\tPressure [Pa]=" + String(pressure));
  }
}


//Read from or write to register from the SCP1000:
unsigned int readRegister(byte thisRegister, int bytesToRead) {
  byte inByte = 0;           // incoming byte from the SPI
  unsigned int result = 0;   // result to return
  Serial.print(thisRegister, BIN);
  Serial.print("\t");
  // SCP1000 expects the register name in the upper 6 bits
  // of the byte. So shift the bits left by two bits:
  thisRegister = thisRegister << 2;
  // now combine the address and the command into one byte
  byte dataToSend = thisRegister & READ;
  Serial.println(thisRegister, BIN);
  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);
  // send the device the register you want to read:
  SPI.transfer(dataToSend);
  // send a value of 0 to read the first byte returned:
  result = SPI.transfer(0x00);
  // decrement the number of bytes left to read:
  bytesToRead--;
  // if you still have another byte to read:
  if (bytesToRead > 0) {
    // shift the first byte left, then get the second byte:
    result = result << 8;
    inByte = SPI.transfer(0x00);
    // combine the byte you just got with the previous one:
    result = result | inByte;
    // decrement the number of bytes left to read:
    bytesToRead--;
  }
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return (result);
}




//Sends a write command to SCP1000


void writeRegister(byte thisRegister, byte thisValue) {


  // SCP1000 expects the register address in the upper 6 bits
  // of the byte. So shift the bits left by two bits:
  thisRegister = thisRegister << 2;
  // now combine the register address and the command into one byte:
  byte dataToSend = thisRegister | WRITE;


  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);


  SPI.transfer(dataToSend); //Send register location
  SPI.transfer(thisValue);  //Send value to record into register


  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);                                                                                                                                                                      


感谢Arduino。                                                                                                                                                         






SPI通讯




 以下是通过SPI如何发送数据的详细解析。
    SPI读写:(非中断模式)




    a)写一个字节:




 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
      //确保发生前Buffer为空,也就是说上一次已经发生完成
      SPI_I2S_SendData(SPI1, Data);        //往寄存器中写入一个字节




       while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
       //等待接受到一个字节数据,为什么要这么做?加这一句的原因是为了确保这个字节已经发送出去,因为发生和接受是并行同步进行,那就是说你发生出去一个字节意味着你收到一个字节。所以这样判断完全没有问题,再说必要性,如果你不加这句你就会容易犯过早拉高CS信号的错误,你想想如果在SPI_I2S_SendData(SPI1, Data)后面立即拉高CS是什么后果。
     SPI_I2S_ReceiveData(SPI1);  //都会接收到的数据,看起来没什么必要,但以用stm32的经验推荐这样做,也许会有意想不到的收获。  
  
  例子:
         SPI_Writebyte(u8 data)
           
            {




          while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
          SPI_I2S_SendData(SPI1, Data);      




          while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);  




          SPI_I2S_ReceiveData(SPI1);  




             }




  b)读一个字节:




以下是通过SPI如何读取数据的详细解析。


         读的时候要注意一个问题,因为从模式是没法提供时钟的,所以主模式下必须要在接收的同时提供时钟。办法就是发送一个字节来实现,因为还是上面说的,发送一个字节就意味着收到一个字节,代码和写完全一样,只要把读出来的字节保存即可。
       
        u8   SPI_Readbyte(u8 data)


        {     








          while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
          SPI_I2S_SendData(SPI1, Data);      




          while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);  




          return SPI_I2S_ReceiveData(SPI1);  




         }




C语言中问号?的用法:                                                                                                                                                                                                                                                                                                          int a=1,b=2,c=3,d=4;


printf("%d",a<b?a:c<d?c:d);


正确结果是:1


因为先执行的是c<d?c:d,会返回c,然后执行a<b?a:c


因此得到的结果就是1。                                                                                                                                                                                                                                                                                                                                                                                                                                                                1.在IIC通讯中,地址发送完成标志位ADDR,需要读取SR1后,再读取SR3才能复位 。                                                                                                                                                                                                                                               2. 在每传送完一个地址字节或一个数据字节之后的第九个时钟脉冲期间,接收方在SDA线上产生一个低电平的应答信号ACK,以通知发送方数据已接收到,可以继续发送。所有数据传送完毕后,单片机向SDA线发送一个停止信号,结束该次数据传送。                                                                                                                                                                                                                                                                                                                                                                                                                   hc110芯片中的adt初始化程序
// DDL_ZERO_STRUCT(stcAdcCfg);   //把定义的stcAdcCfg结构体内的成员归零。结构体初始化
// DDL_ZERO_STRUCT(stcAdcScanCfg);
//
// Clk_SetPeripheralGate(ClkPeripheralAdcBgr, TRUE);


//  Gpio_SetAnalog(2, 4, TRUE);  //电池电流
//  Gpio_SetAnalog(3, 6, TRUE);  //电池电压
//  Gpio_SetAnalog(3, 3, TRUE);  //电池温度
//    
// Adc_Enable();
// M0P_BGR->CR_f.BGR_EN = 0x1u;   //
// M0P_BGR->CR_f.TS_EN = 0x1u;
// delay100us(1);
//  
// stcAdcCfg.enAdcOpMode = AdcScanMode;     //多通道扫描模式
// stcAdcCfg.enAdcClkSel = AdcClkSysTDiv8;  //8分频
// stcAdcCfg.enAdcSampTimeSel = AdcSampTime3Clk;
//  stcAdcCfg.enAdcRefVolSel = RefVolSelInBgr2p5;  //采用2.5v的电源
// stcAdcCfg.enAdcPreBias = AdcPreBias5u;
// stcAdcCfg.enAdcVcmBias = AdcVcmBias5u;
// stcAdcCfg.enAdcClockDelay = AdcClockDelay4Div8;
// stcAdcCfg.bAdcInBufEn = FALSE; 
// stcAdcCfg.enAdcTrig0Sel = AdcTrigDisable;
// //stcAdcCfg.enAdcTrig1Sel = AdcTrigDisable;
// Adc_Init(&stcAdcCfg);
// stcAdcScanCfg.u8AdcScanModeCh = ADC_SCAN_CH0_EN 
//    | ADC_SCAN_CH3_EN
// | ADC_SCAN_CH6_EN;
// stcAdcScanCfg.u8AdcSampCnt = 0x6;
// Adc_ConfigScanMode(&stcAdcCfg, &stcAdcScanCfg);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值