cancel
Showing results for 
Search instead for 
Did you mean: 

SPI potentiometer don't work (CubeMX & Nucleo103)

ioannis
Associate II
Posted on March 06, 2018 at 00:35

Hi,

Maybe i have a stupid question or mistake but i am searching and i can't make it work, so if you can help please.

First i created a new project at CubeMX integrated in Atollic TrueStudio 8.0.

I enable the SPI2 as Full Duplex Master and the Hardware NSS disabled, and the PB1 as output for the Chip Select (CS).

I generated the code without change the configuration at SPI and i add the follow lines (my last of many tries) at the main.c

&sharpdefine CS GPIOB, GPIO_PIN_1

main(void){

       HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

      while(1){

            

for(int i=0;i<128; i++){

HAL_GPIO_WritePin(CS, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi2, 0x00, size, 0xFF);

HAL_SPI_Transmit(&hspi2, 0x4, size, 0xFF);

HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

HAL_Delay(10);

}

for(int j=128;j>0; j++){

HAL_GPIO_WritePin(CS, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi2, 0x00, size, 0xFF);

HAL_SPI_Transmit(&hspi2, 0x8, size, 0xFF);

HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

HAL_Delay(10);

}

for(int o=0;o<128; o++){

HAL_GPIO_WritePin(CS, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi2, 0x01, size, 0xFF);

HAL_SPI_Transmit(&hspi2, 0x20, size, 0xFF);

HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

HAL_Delay(10);

}

for(int k=128;k>0; k++){

HAL_GPIO_WritePin(CS, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi2, 0x01, size, 0xFF);

HAL_SPI_Transmit(&hspi2, 0x24, size, 0xFF);

HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

HAL_Delay(10);

}

}

}

Of course, i write here only the code i add. The pre-generated code remains as is (

http://prntscr.com/incfqx

and more).

At the hardware part now, i used a common MCP4231 with the following connection:

1 -> PB1, 2 ->PB13, 3-> PB15, 4,5,10 -> GND,  6,9-> LED with series resistor 220 Ohm, 7,8,14 -> 5V

I left the MISO float, because there is nothing to receive.

I check the connections many times but nothing happens.

Maybe i missed information, but anything needed i can add after.

Any ideas how it works? Thank you very much!!!

#spi #potentiometer
8 REPLIES 8
T J
Lead
Posted on March 06, 2018 at 02:32

You forgot the 'Port'

    HAL_GPIO_WritePin(E144_M0_GPIO_Port, E144_M0_Pin, GPIO_PIN_SET);

do you have a scope ?  you can see the pin is not working.

it shouldn't compile, i thought ??

(never liked Atollic)

Posted on March 06, 2018 at 10:57

Hi,

thanks for answer.

Unfortunately i dont have scope or logic analyser...

About the port., i guess the CubeMX set up the

https://prnt.sc/inifbu

, and i only set the slave select pin.

The program compiles normally.

Posted on March 06, 2018 at 13:01

Tough to debug without a scope or logic analyzer but not impossible.

If you have a debugger, try to get a shot of the set up of the registers, compare them to the data sheet to see if they are as expected.

If you have UART, send the data out so you can check on the computer.

If you have a frequency meter - many multimeters do, out it on the sclk ot mosi line to make sure that there is some activity.

Anything fails, put an led on the sclk or mosi and see if the led blinks - you will need to slow down the transmission. 

....

So if you look around there must be things around you that can help.

Posted on March 06, 2018 at 17:06

I slow down the SPI Baud Rate at 3 bits/sec and i attach the led... nothing flashed.

the registers (as i can see) are all empty

https://i.gyazo.com/fd1e0309f221d0e372b051d1afb1419c.png

so, i guess i have made wrong the setup??

Posted on March 06, 2018 at 18:47

Compare those 0s with the values you would have expected and see if they match.

Your code showed no initialization so you may want to look into that first.

T J
Lead
Posted on March 07, 2018 at 18:20

did you fix these faulty lines ?

HAL_GPIO_WritePin(CS, GPIO_PIN_RESET);

HAL_GPIO_WritePin(CS, GPIO_PIN_SET);

Posted on March 07, 2018 at 17:38

for the setup i use the CubeMX... i don't know if there is a mistake

https://i.gyazo.com/b2c6ac4081c543e0dfcd02f85e891apng

Posted on March 07, 2018 at 18:09

'

i don't know if there is a mistake'

easy: take out the datasheet and ask yourself what values the registers should have. and then figure out why they don't have those values you were expecting.