Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 8 months ago.
USB distortion?
Hello,
I've made a 8x8 LED matrix driven by a MAX7219. I can control the IC with SPI. But then i tried to program it over USB. I installed the driver correctly en made a simple C# application to send characters to the mbed. This works fine. So I've made a program that lights up every single LED as a "test" and after this is done the mbed waits to receive a character via USB. But when the mbed is sending data to the MAX7219 to light up every single LED, some lines are getting disturbed and other LED's are lighting up then supposed. This only happens when the mbed is not waiting for data.
Any who can help?
Regards,
Jason
#include "mbed.h" #include "USBSerial.h" #include <math.h> // p5: DIN, p7: CLK, p8: LOAD/CS SPI max72_spi(p5, NC, p7); DigitalOut load(p8); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); //Virtual serial port over USB USBSerial serial; Serial pc(USBTX, USBRX); int maxInUse = 1; //change this variable to set how many MAX7219's you'll use //char ontvangen = 'A'; // define max7219 registers #define max7219_reg_noop 0x00 #define max7219_reg_digit0 0x01 #define max7219_reg_digit1 0x02 #define max7219_reg_digit2 0x03 #define max7219_reg_digit3 0x04 #define max7219_reg_digit4 0x05 #define max7219_reg_digit5 0x06 #define max7219_reg_digit6 0x07 #define max7219_reg_digit7 0x08 #define max7219_reg_decodeMode 0x09 #define max7219_reg_intensity 0x0A #define max7219_reg_scanLimit 0x0B #define max7219_reg_shutdown 0x0C #define max7219_reg_displayTest 0x0F #define LOW 0 #define HIGH 1 #define MHZ 1000000 void maxSingle( int reg, int col) { //maxSingle is the "easy" function to use for a //single max7219 load = LOW; max72_spi.write(reg); max72_spi.write(col); load = HIGH; } void maxAll (int reg, int col) // initialize all MAX7219's in the system { load = LOW; // begin for ( int c=1; c<= maxInUse; c++) { max72_spi.write(reg); // specify register max72_spi.write(col); // put data } load = HIGH; } void maxOne(int maxNr, int reg, int col) { //maxOne is for adressing different MAX7219's, //while having a couple of them cascaded int c = 0; load = LOW; for ( c = maxInUse; c > maxNr; c--) { max72_spi.write(0); // no-op max72_spi.write(0); // no-op } max72_spi.write(reg); // specify register max72_spi.write(col); // put data for ( c=maxNr-1; c >= 1; c--) { max72_spi.write(0); // no-op max72_spi.write(0); // no-op } load = HIGH; } void setup () { // Start of the setup // initiation of the max 7219 // SPI setup: 8 bits, mode 0 wait_ms(2000); led1 = HIGH; max72_spi.format(8, 0); max72_spi.frequency(10*MHZ); maxAll(max7219_reg_scanLimit, 0x07); maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode maxAll(max7219_reg_displayTest, 0x00); // no display test for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off maxAll(e,0); } maxAll(max7219_reg_intensity, 0x0f); // the first 0x0f is the value you can set wait_ms(2000); led1 = LOW; // range: 0x00 to 0x0f // End of the setup } void reset() { for(int i = 1; i <=8; i++) { maxSingle(i,0); } } int main() { setup(); bool lightUp = false; reset(); // Just a simple test // Here I get some problems if the USB is connected, I don't know why. // Without the USB everything works fine for(int i = 1; i <= 8; i++) { for(int j = 0; j < 8; j++) { led2 = lightUp; maxSingle(i,pow((float)2,(float)j)); wait_ms(30); if((pow((float)2,(float)j)) == 128) { maxSingle(i,0); } lightUp =! lightUp; } } for(int i = 8; i >= 1; i--) { for(int j = 8; j >= 0; j--) { led2 = lightUp; maxSingle(i,pow((float)2,(float)j)); wait_ms(30); if((pow((float)2,(float)j)) == 1) { maxSingle(i,0); } lightUp =! lightUp; } } for(int i = 1; i <= 8; i++) { led2 = lightUp; maxSingle(i,255); wait_ms(50); maxSingle(i,0); lightUp =! lightUp; } for(int i = 7; i >= 1; i--) { led2 = lightUp; maxSingle(i,255); wait_ms(50); maxSingle(i,0); lightUp =! lightUp; } // End of test // Receiving characters from C# // This works fine while(1) { //int ontvangen = serial.getc(); maxSingle(1,serial.getc()); } }
Hi,
without code or at least schematic, you won't get much assistance.
posted by Martin Kojtal 12 Jun 2014So with USB disconnected your start-up test works fine but with it connected it doesn't?
Sounds a lot like a hardware issue, probably on the power supply side of things. I'm not sure exactly what it could be, it looks like you don't connect the USB power pin just the ground and data but if the only difference is whether an idle USB cable is plugged in or not then there isn't a lot left for it to be. If you are turning all the LEDs on at the same time are you sure you've got enough power to do that? Good low impedance ground connections between the boards? A large enough bypass cap on the power input to the LED driver?
posted by Andy A 12 Jun 2014Exactly, everything works fine without USB cable plugged in. With the USB cable plugged it works right now and then. But the data I receive from my application is okay, I have no problems with that. D+ and D- are connected from the USB B connector to the mbed and the GROUND from the connector to the GROUND from the mbed. The +5V is unconnected from the USB connector because it's not needed.
posted by Jason Pitteman 13 Jun 2014