8 years, 9 months ago.

SPI not working on FRDM K22F

#include "mbed.h"

enum {off,on};

void Sample();
SPI COM(PTC6, PTC7,PTC5);
Ticker Sample_func; 
AnalogIn ADC_PIN(PTB0);  
PwmOut PWM_Brightness(PTC3); // LED pin
DigitalOut Flash(PTD4); // Pin PTD4 will output 3.3
DigitalOut LED(PTB2);
DigitalOut CS(PTE0);

int main()
{
	COM.frequency(1000000);
	COM.format(8, 0);
	CS = on;
	LED = off;
	Flash = on;
	Sample_func.attach(Sample, 0.1);
        while(true)
        ;
}


float Temp = 0;
float ADC_value;

void Sample()
{
	ADC_value = ADC_PIN.read();
	volatile char AA = 'A';
	if (((int)(100 * ADC_value)) != ((int)(100 * Temp)))	
	{
		CS = off;
		LED = on;
		//COM.write((int)(1000*ADC_value));
		COM.write(AA);
		wait(0.005);   
		CS = on;
		LED = off;	
	}	
	PWM_Brightness.write(ADC_value);
	Temp = ADC_value;
}

2 Answers

8 years, 9 months ago.

Just like the previous question: Please use <<code>> and <</code>> tags to make your code readable.

Then it is always best to reduce the complexity as much as possible: If your SPI does not work, only have SPI in your code. (Also the SPISlave will need to have the CS toggled most likely between every byte, a limitations most microcontrollers share). Now it could also be that your ADC does not function properly. Does the code run? (So LEDs go on/off?)

At the end iof yor main function it is best to place a while(1);, you now dont know what the MCU does when it reaches the end of the main function.

Accepted Answer

Yes The code runs except for the SPI. The ADC samples a Potentiometer and uses the values to set the PWM connected to a light bulb. I am using an LED to give me a visual alert that Data has been sent by the SPI, The LED flashes On/Off and the PWM also works correctly when I turn the POT, yet there are no signals on the SPI Master pins. I am going remove all the other code and try SPI only as you said to see what happens

posted by Richard Mensah 10 Mar 2016
8 years, 9 months ago.

Print out the values of ADC_value to see if they indeed vary with your changes to the external Potentiometer. You may have to scale the ADC_value before they can be used by the PWM to allow for your visual effects.

https://developer.mbed.org/handbook/PwmOut

Consider to run a code loop (without the Potentiometer) to force the LED to change the PWM and connected LED for your effects. Once you can confirm the results with the external LED, then proceed to scale the ADC values to suit.

the ADC part of the code was from an earlier successful code so it works fine. when the pot is at maximum, it reads 0.99886 and when at minimum the ADC reads 0.00. So I think that part is fine. The range is from 0 to 1 so I can use it to set the duty cycle of the PWM.

posted by Richard Mensah 12 Mar 2016