OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 8530|回复: 7

SPI不通,配置有问题?

[复制链接]

2

主题

6

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2012-10-18
在线时间
0 小时
发表于 2012-10-23 14:23:01 | 显示全部楼层 |阅读模式
还是原来的问题,急死了~
STM32与LTC6802用SPI传值,有几个问题问下:
1.LTC6802自带4个SPI口,是否硬件上把他们和STM32直接连接即可?
2.然后步骤是进行STM32的SPI端口配置:
   void SPIx_Init(void)
{  
RCC->APB2ENR|=1<<2;       //PORTA时钟使能  
RCC->APB2ENR|=1<<12;      //SPI1时钟使能 
  
//这里只针对SPI口初始化
GPIOA->CRL&=0X000FFFFF; 
GPIOA->CRL|=0XBBB00000;//PA5.6.7复用    
GPIOA->ODR|=0X7<<5;    //PA5.6.7上拉
SPI1->CR1|=0<<10;//全双工模式
SPI1->CR1|=1<<9; //软件nss管理
SPI1->CR1|=1<<8;  
SPI1->CR1|=1<<2; //SPI主机
SPI1->CR1|=0<<11;//8bit数据格式
SPI1->CR1|=1<<1; //空闲模式下SCK为1 CPOL=1
SPI1->CR1|=1<<0; //数据采样从第二个时间边沿开始,CPHA=1  
SPI1->CR1|=7<<3; //Fsck=Fcpu/256
SPI1->CR1|=0<<7; //MSBfirst   
SPI1->CR1|=1<<6; //SPI设备使能
SPIx_ReadWriteByte(0xff);//启动传输  
}  这样是不是就好了,对于LTC6802来说只要定义下CS,SCK,SI,SO是哪几个脚,是输入还是输出。
这样就好了吗?我试了下一只不对,连SCK的波形都是错的。
比较愚昧,希望点子哥能给正解
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

头像被屏蔽

95

主题

158

帖子

0

精华

禁止访问

积分
456
金钱
456
注册时间
2011-10-12
在线时间
7 小时
发表于 2012-10-23 15:06:21 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

2

主题

6

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2012-10-18
在线时间
0 小时
 楼主| 发表于 2012-10-23 15:47:09 | 显示全部楼层
回复【2楼】gongzaijun:
---------------------------------
#include "delay.h"
#include "LTC6802.h"
//#incluce "intrins.h"
#define CSBI   Aout(2)
#define SCK    Aout(5)
#define SDI    Aout(6)
#define SDO    Ain(7)

#define CLR_CSBI() CSBI=0
#define SET_CSBI() CSBI=1
#define CLR_SCK() SCK=0 
#define SET_SCK() SCK=1
#define CLR_SDI() SDI=0 
#define SET_SDI() SDI=1  

// define Command
#define WRCFG 0x01//写入配置寄存器组
#define RDCFG 0x02//读出配置寄存器组
#define RDCV 0x04//读出电池电压
#define RDFLG 0x06//读出标致寄存器组
#define RDTMP 0x08//读出温度寄存器组
//* Start Cell Voltage A/D Conversions and oll Status => STCVAD */
//起动电池电压ADC转换和轮询状态
#define STCVADALL 0x10 // (all cell voltage inputs) 
#define STCVAD1 0x11 // (cell 1 only) 
#define STCVAD2 0x12 // (cell 2 only) 
#define STCVAD3 0x13 // (cell 2 only)
#define STCVAD4 0x14 // (cell 4 only) 
#define STCVAD5 0x15 // (cell 5 only) 
#define STCVAD6 0x16 // (cell 6 only)
#define STCVAD7 0x17 // (cell 7 only) 
#define STCVAD8 0x18 // (cell 8 only) 
#define STCVAD9 0x19 // (cell 9 only)
#define STCVAD10 0x1A // (cell 10 only) 
#define STCVAD11 0x1B // (cell 11 only) 
#define STCVAD12 0x1C // (cell 12 only)
#define STCVADCST1 0x1E // (cell self test 1; all CV=0x555) 
#define STCVADCST2 0x1F // (cell self test 2; all CV=0xAAA)
/* Start Open-Wire A/D Conversions and oll Status => STOWAD */
//起动导线开路ADC转8换和轮询状态
#define STOWADALL 0x20 // (all cell voltage inputs) 
#define STOWAD1 0x21 // (cell 1 only) 
#define STOWAD2 0x22 // (cell 2 only) 
#define STOWAD3 0x23 // (cell 2 only)
#define STOWAD4 0x24 // (cell 4 only) 
#define STOWAD5 0x25 // (cell 5 only) 
#define STOWAD6 0x26 // (cell 6 only)
#define STOWAD7 0x27 // (cell 7 only) 
#define STOWAD8 0x28 // (cell 8 only) 
#define STOWAD9 0x29 // (cell 9 only)
#define STOWAD10 0x2A // (cell 10 only) 
#define STOWAD11 0x2B // (cell 11 only) 
#define STOWAD12 0x2C // (cell 12 only)
#define STOWADCST1 0x2E // (cell self test 1; all CV=0x555) 
#define STOWADCST2 0x2F // (cell self test 2; all CV=0xAAA)
//* Start Temperature A/D Conversions and oll Status => STTMPAD */
//起动温度ADC转换和轮询状态
#define STTMPADALL 0x30 // (all temperature inputs) 
#define STTMPAD1 0x31 // (external temp 1 only) 
#define STTMPAD2 0x32 // (external temp 2 only) 
#define STTMPAD3 0x33 // (internal temp only)
#define STTMPADCST1 0x3E // (temp self test 1; all TMP=0x555) 
#define STTMPADCST2 0x3F // (temp self test 2; all TMP=0xAAA) 

#define LADC 0x40  //轮询ADC转换器状态
#define LINT 0x50  //轮询中断状态
/* Start Cell Voltage A/D Conversions and oll Status, with Discharge Permitted => STCVDC */
//起动电池电压ADC转换和轮询状态,允许放电
#define STCVDCALL 0x60 // (all cell voltage inputs) 
#define STCVDC1 0x61 // (cell 1 only) 
#define STCVDC2 0x62 // (cell 2 only) 
#define STCVDC3 0x63 // (cell 2 only)
#define STCVDC4 0x64 // (cell 4 only) 
#define STCVDC5 0x65 // (cell 5 only) 
#define STCVDC6 0x66 // (cell 6 only)
#define STCVDC7 0x67 // (cell 7 only) 
#define STCVDC8 0x68 // (cell 8 only) 
#define STCVDC9 0x69 // (cell 9 only)
#define STCVDC10 0x6A // (cell 10 only) 
#define STCVDC11 0x6B // (cell 11 only) 
#define STCVDC12 0x6C // (cell 12 only)
#define STCVDCCST1 0x6E // (cell self test 1; all CV=0x555) 
#define STCVDCCST2 0x6F // (cell self test 2; all CV=0xAAA)
/* Start Open-Wire A/D Conversions and Poll Status, with Discharge Permitted => STCVDC */
//起动导线开路ADC转换和轮询状态,允许放电
#define STOWDCALL 0x70 // (all cell voltage inputs) 
#define STOWDC1 0x71 // (cell 1 only) 
#define STOWDC2 0x72 // (cell 2 only) 
#define STOWDC3 0x73 // (cell 2 only)
#define STOWDC4 0x74 // (cell 4 only) 
#define STOWDC5 0x75 // (cell 5 only) 
#define STOWDC6 0x76 // (cell 6 only)
#define STOWDC7 0x77 // (cell 7 only) 
#define STOWDC8 0x78 // (cell 8 only) 
#define STOWDC9 0x79 // (cell 9 only)
#define STOWDC10 0x7A // (cell 10 only) 
#define STOWDC11 0x7B // (cell 11 only) 
#define STOWDC12 0x7C // (cell 12 only)
#define STOWDCCST1 0x7E // (cell self test 1; all CV=0x555) 
#define STOWDCCST2 0x7F // (cell self test 2; all CV=0xAAA)/* 定义全局变量 */

u8 pp,flag;

//初始化SPI 6802的IO口

////////////////////////////
void SPI_6802_Init(void)
{  
RCC->APB2ENR|=1<<2;       //PORTA时钟使能   
RCC->APB2ENR|=1<<12;      //SPI1时钟使能 
   
//这里只针对SPI口初始化
GPIOA->CRL&=0X000FFFFF; 
GPIOA->CRL|=0XBBB00000;//PA5.6.7复用      
//GPIOA->ODR|=0X7<<5;    //PA5.6.7上拉

SPI1->CR1|=0<<10;//全双工模式
SPI1->CR1|=1<<9; //软件nss管理
SPI1->CR1|=1<<8;  

SPI1->CR1|=1<<2; //SPI主机
SPI1->CR1|=0<<11;//8bit数据格式
SPI1->CR1|=1<<1; //空闲模式下SCK为1 CPOL=1
SPI1->CR1|=1<<0; //数据采样从第二个时间边沿开始,CPHA=1  
SPI1->CR1|=7<<3; //Fsck=Fcpu/256
SPI1->CR1|=0<<7; //MSBfirst   
SPI1->CR1|=1<<6; //SPI设备使能
SPIx_ReadWriteByte(0xff);//启动传输 
  }


/////////////////////////
以上是6802的C文件,下面是主程序
/////////////////////////

#include <stm32f10x_lib.h>
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h" 
#include "key.h"
#include "exti.h"
#include "wdg.h"
#include "timer.h"
#include "lcd.h"    
#include "rtc.h"
#include "wkup.h"
#include "adc.h"
#include "dma.h"
#include "24cxx.h"
#include "LTC6802.h" 
#include "spi.h"
 
//Mini STM32开发板范例代码17
//SPI 实验
//正点原子@ALIENTEK
//技术支持论坛:www.openedv.com


//要写入到W25X16的字符串数组
//const u8 TEXT_Buffer[]={"MiniSTM32 SPI TEST"};
//#define SIZE sizeof(TEXT_Buffer)  
int main(void)
{
//u8 key;
//u16 i=0;
//u8 datatemp[SIZE];
 
 u8 i;
 u8 CFGR[6]={0x03,0x00,0x00,0x00,0x5E,0x98};
// u8 Address=0x80;
 u8 CVR[18];
 u8 PEC;
 u8 irp;  
 Stm32_Clock_Init(9); //系统时钟设置
     uart_init(72,9600);
 delay_init(72); //延时初始化
        SPI_6802_Init();
// SPIx_Init(); 
 
/* Write Configuration Registers (Broadcast Command) */  
        
CSBI = 0;
Write(WRCFG);//写入及配置寄存器
for (i=0;i<6;i++)
{
Write(CFGR);//写CFGR
}
CSBI = 1;
delay_ms(100);  

/* Read Cell Voltage Registers (Address Command)  */
CSBI = 0;
//Write(Address);
Write(RDCV);
for (i=0;i<18;i++)
{
CVR = Read();
}   
//PEC = Read();
CSBI = 1;
delay_ms(1);


/* Start Cell Voltage A/D Conversions and Poll Status (Broadcast Command with Toggle Polling) 
*/
CSBI = 0;
Write(STCVADALL);
delay_ms(100);
while(!SDO);
delay_ms(1);
while(!SDO);
CSBI = 1;
delay_ms(100);
  
/* Poll Interrupt Status (Level Polling) */
CSBI = 0;
//Write(Address);
Write(PLINT);
SCK = 1;
delay_ms(1);
irp = 0x01 & (~SDO);
SCK = 0;
CSBI = 1;
delay_ms(1);  

/* 通过串口发送数据 */
while(1)
{
printf("0x555");
printf("0xAAA");
for (i=0;i<18;i++)
{
printf("%d  ",CVR);
delay_ms(100);
}

}
}
   
  
 














回复 支持 反对

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165475
金钱
165475
注册时间
2010-12-1
在线时间
2115 小时
发表于 2012-10-23 22:03:34 | 显示全部楼层
回复【楼主位】hlalice:
---------------------------------
先弄懂我们的SPI例程(如何操作W25X16的),再去看你的6802。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165475
金钱
165475
注册时间
2010-12-1
在线时间
2115 小时
发表于 2012-10-23 22:03:52 | 显示全部楼层
你现在都不知道SPI怎么用的...
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

2

主题

6

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2012-10-18
在线时间
0 小时
 楼主| 发表于 2012-10-24 08:29:45 | 显示全部楼层
回复【5楼】正点原子:
---------------------------------
没有接触过5555,好难受~我好好看去~
回复 支持 反对

使用道具 举报

头像被屏蔽

95

主题

158

帖子

0

精华

禁止访问

积分
456
金钱
456
注册时间
2011-10-12
在线时间
7 小时
发表于 2012-10-24 09:08:09 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

53

主题

324

帖子

0

精华

高级会员

Rank: 4

积分
598
金钱
598
注册时间
2012-6-18
在线时间
7 小时
发表于 2014-4-23 13:43:22 | 显示全部楼层
回复【楼主位】hlalice:
---------------------------------
楼主的6802调通了么,可否把硬件电路贴出来
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-10 04:06

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表