|
有人用弄过STM32弄过SPI控制数字电位器吗?配置好了之后一直不对
[复制链接]
10芯积分
用单片机stm32f103的SPI1控制数字电位器的电压,参考的是正点原子的例程,但是配置完之后输出一直不对,有哪位大神可以帮忙看看吗?
spi.c
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );//PORTBê±Öóê1Äü
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );//SPI2ê±Öóê1Äü
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA4/5/7¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA
// GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7); //PB13/14/15éÏà
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎaÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //′®DDí¬2½ê±ÖóμÄ¿ÕÏD×′ì¬Îa¸ßμçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //′®DDí¬2½ê±ÖóμÄμú¶t¸öìø±äÑØ£¨éÏéy»òϽ죩êy¾Y±»2éÑù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPIx¼Ä′æÆ÷
SPI_Cmd(SPI1, ENABLE); //ê1ÄüSPIíaéè
}
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI1->CR1&=0XFFC7;
SPI1->CR1|=SPI_BaudRatePrescaler; //éèÖÃSPI2Ëù¶è
SPI_Cmd(SPI1,ENABLE);
}
u8 SPI1_WriteByte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){} // ÅD¶ÏSPI1 ·¢Ëí»o3åÇøêÇ·ñ¿Õ
SPI_I2S_SendData(SPI1, byte); // ·¢Ëí8λêy¾Y
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} // ÅD¶ÏêÇ·ñ½óêÕ»o3åÇø·Ç¿Õ
return SPI_I2S_ReceiveData(SPI1);
}
void TPL0501_Write_SR(u8 sr)
{
TPL0501=0; //ê1ÄüÆ÷¼t
SPI1_WriteByte(sr); //D′èëò»¸ö×Ö½ú
TPL0501=1; //è¡ÏûƬѡ
}
spi.h
#ifndef __SYS_H
#define __SYS_H
#include "stm32f10x.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾ ×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//ALIENTEK STM32¿a·¢°å
//ÕyμãÔ ×ó@ALIENTEK
//¼¼êõÂÛì3:www.openedv.com
//DT¸ÄèÕÆú:2012/8/18
//°æ±¾£oV1.7
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) 1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾ 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//0,2»Ö§3Öucos
//1,Ö§3Öucos
#define SYSTEM_SUPPORT_OS 0 //¶¨òåÏμí3Îļt¼DêÇ·ñÖ§3ÖUCOS
//λ′ø2ù×÷,êμÏÖ51ààËÆμÄGPIO¿ØÖÆ1|Äü
//¾ßìåêμÏÖ˼Ïë,2ο¼<>μúÎåÕÂ(87ò3~92ò3).
//IO¿ú2ù×÷o궨òå
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO¿úμØÖ·ó3éä
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
//IO¿ú2ù×÷,Ö»¶Ôμ¥ò»μÄIO¿ú!
//è·±£nμÄÖμD¡óú16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //êä3ö
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //êäèë
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //êä3ö
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //êäèë
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //êä3ö
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //êäèë
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //êä3ö
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //êäèë
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //êä3ö
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //êäèë
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //êä3ö
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //êäèë
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //êä3ö
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //êäèë
//òÔÏÂÎa»ã±àoˉêy
void WFI_SET(void); //Ö′DDWFIÖ¸áî
void INTX_DISABLE(void);//1رÕËùóDÖD¶Ï
void INTX_ENABLE(void); //¿aÆôËùóDÖD¶Ï
void MSR_MSP(u32 addr); //éèÖöÑÕ»μØÖ·
#endif
main.c
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "spi.h"
/************************************************
ALIENTEK¾«ó¢STM32¿a·¢°åêμÑé23
SPI êμÑé
¼¼êõÖ§3Ö£owww.openedv.com
ìÔ±|μêÆì£ohttp://eboard.taobao.com
1Øע΢DÅ1«Öúƽì¨Î¢DÅoÅ£o"ÕyμãÔ ×ó"£¬Ãa·Ñ»ñè¡STM32×êáÏ¡£
1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾
×÷ÕߣoÕyμãÔ ×ó @ALIENTEK
************************************************/
//òaD′èëμ½W25Q64μÄ×Ö·û′®êy×é
//const u8 TEXT_Buffer[]={"ELITE STM32 SPI TEST"};
//#define SIZE sizeof(TEXT_Buffer)
int main(void)
{
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÖD¶ÏóÅÏ輶·Ö×éÎa×é2£o2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
KEY_Init(); //°′¼ü3õê¼»ˉ
SPI1_Init(); //W25QXX3õê¼»ˉ
SPI1_SetSpeed(SPI_BaudRatePrescaler_2);
while(1)
{
TPL0501_Write_SR(128);
}
}
|
|