3
|
|
|
|
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;
- }
复制代码
|
|
|
|
|
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
- 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
- {
- 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++;
- }
- }
复制代码
|
|
|
|
|
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);
- }
复制代码
|
|
|
|
|
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);
- }
复制代码
|
|
|
|
|
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();
- }
复制代码
|
|
|
|
|
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
- }
复制代码
|
|
|
|
|