baycoder

ADE7880 with STM32F429 Example Code

Discussion created by baycoder on Sep 9, 2017
Latest reply on Oct 12, 2017 by baycoder

Hi all, I designed a pcb with (stm32f429 MCU + ADE7880) (1 MCU and 2 ADE7880. Connected common SPI bus line but different Chip Select. İf I Cs1=0, Cs2=1, I will use ADE1 it is not problem I think).

 

I am working on Keil. I write spi code but I read 255 everytime on SPI. Has anyone any Keil or MicroC code with (stm32f4+ade7880) ?

 

Keil Code:


#include "stm32f4xx.h"
#include <stdio.h>
#include <defines.h>
int x,y,z,w;
int k;
SPI_InitTypeDef SPI_InitTypeStructure;
GPIO_InitTypeDef GPIO_InitTypeStructure;
USART_InitTypeDef USART_InitStructure;


char str[50];
uint32_t i = 0;


void USART_Puts(USART_TypeDef* USARTx, volatile char *s)
{
while(*s)
{
while(!(USARTx ->SR & 0x00000040));
USART_SendData(USARTx, *s);
*s++;
}
}


uint16_t SPI_Rx(uint32_t adress, uint32_t d)
{



adress=0x80|adress; // okuma islemi yapilacak
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,adress);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,d);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
GPIO_SetBits(GPIOG,GPIO_Pin_2); //CS pini lojik 1 e çekildi
return SPI_I2S_ReceiveData(SPI1);
}


void usart_ayar(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitTypeStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitTypeStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitTypeStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, & GPIO_InitTypeStructure);
GPIO_PinAFConfig(GPIOA , GPIO_PinSource2, GPIO_AF_USART2);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART2,& USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}


void ayarlar(void)
{
/* Structure ve Clock Ayarlari */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOG| RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOD, ENABLE);


/* Chip Select VE E portu Led ayarlari */
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitTypeStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOE, &GPIO_InitTypeStructure);

GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitTypeStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOG, &GPIO_InitTypeStructure);


/* SPI SCK-MOSI-MISO pin Ayarlari */
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitTypeStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitTypeStructure);
/* GPIO AF pin Ayarlari */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); // sck pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); // mosi pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // miso pin
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);

GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_7;
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitTypeStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOD, &GPIO_InitTypeStructure);


/* SPI Ayarlari */
SPI_InitTypeStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;
SPI_InitTypeStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitTypeStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitTypeStructure.SPI_DataSize=SPI_DataSize_16b;
SPI_InitTypeStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitTypeStructure.SPI_CPOL=SPI_CPOL_High;
SPI_InitTypeStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitTypeStructure.SPI_NSS=SPI_NSS_Soft|SPI_NSSInternalSoft_Set;
SPI_Init(SPI1,&SPI_InitTypeStructure);
SPI_Cmd(SPI1, ENABLE);
GPIO_SetBits(GPIOG,GPIO_Pin_2);  //ADE1 CS ı use this
GPIO_SetBits(GPIOG,GPIO_Pin_3);  //ADE2 CS
}
int main()
{
ayarlar();

usart_ayar();
while(1)
{
for(int i=0;i<10;i++)
{
SPI_Rx(0x80,0xFF);
SPI_Rx(0x00,0xFF);
}
SPI_Rx(0x00,0x40);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x95);


GPIO_ResetBits(GPIOG,GPIO_Pin_2);
GPIO_SetBits(GPIOG,GPIO_Pin_2);
GPIO_ResetBits(GPIOG,GPIO_Pin_2);
GPIO_SetBits(GPIOG,GPIO_Pin_2);
GPIO_ResetBits(GPIOG,GPIO_Pin_2); //ADE1 CS 1 TO 0 THREE TİMES

SPI_Rx(0x00,0x40);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x00);
SPI_Rx(0x00,0x95);

x=SPI_Rx(STATUS0,0x01);
y=SPI_Rx(STATUS1,0x01);
z=SPI_Rx(CONFIG2,0x01);
w=SPI_Rx(LAST_RWDATA_8bit,0x01);

sprintf(str,"x=%d ,y=%d ,z=%d , w=%X\n", x, y,z,w);  // I read datas on USART screen. 

USART_Puts(USART2,str);
}
}

Output:

 

 

Outcomes