<< return to Pixycam.com

Pixy communication between arduino leonardo

Hi guys,

i’m new here and i’m working on this project made by adafruit:

Now my problem:
I want to communicate between CMUcam5 and atmega32u4 on the Arduino Leonardo. I’m using the SPI-Bus on the arduino board.
At first i thought, that i’ve set the registers wrong, but i’m sure thats they’re allright.

Maybe you can help me to solve my problem.
i attached my source code at the bottom.
I’m using AVR Studio 6.2 and the Diamax AVR-all devices programmer.
And I’m writing in C language.

#define F_CPU 16000000UL

#define UARTaktiv //Auskommentieren, um UART zu deaktivieren

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "Header/Timer.h"
#include "Header/ADC.h"
#include "Header/USART.h"


void SPIinit(void)
{
	DDRB = (1<<PINB1)|(1<<PINB2)|(1<<PINB0);
	SPCR = 0b01110001;
	SPSR= 0;
}
unsigned char SPI_start(unsigned char cData)
{
	SPDR = cData;
	while(!(SPSR & (1<<SPIF)));
	return SPDR;
}

int main(void)
{
	
	TimerInit();
	USARTInit();
	ADCInit();
	PullupSet();
	timer_init_0();
	//sei();
	SPIinit();
	
	
    while(1)
    {

		_delay_ms(1);
		SPI_start(0x00);
		SPI_start(0xff);
		SPI_start(0xff);
		SPI_start(0xff);
		
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(SPI_start(0xff));
		USARTTransmit(13);

	}
}

Hello Jeremias,
I’m not familiar with AVR Studio. You have probably tried this already, but just to make sure, have you tried to communicate with your Pixy from your Leonardo using the hello_world example and the Arduino IDE?

If it’s not working, I wonder if the low level Arduino firmware sets any interrupt routines? I haven’t looked at the low level Arduino source. I assume that’s where you got the register settings in your code?

Edward

It didn’t fixed my problem and i’ve already tried. I’ve disabled any interrupt routines.

Hi Jeremias,
I’m not trying to fix your problem (yet). I’m trying to help you find the cause of the problem. Have you tried to communicate with your Pixy from your Leonardo use the hello_world example and the Arduino IDE?

What did you find?

Edward

Hi Jeremias,
I’m not trying to fix your problem (yet). I’m trying to help you find the cause of the problem. Have you tried to communicate with your Pixy from your Leonardo use the hello_world example and the Arduino IDE?

What did you find?

Edward

Sorry for that, but I’m a little bit out of ideas.

The problem is that I can’t test the hello_world example with the arduino IDE, because my Arduino IDE won’t take my AVR-All programmer.

If recently updated my code and I have a little success. I’m getting the 0x55AA word from Pixy over USART but the rest is senseless.

here the new code and i hope you can help me. My Problem is i’m getting no useful data on the object block, that pixy should send.

The checksum is always invaild.

@/*

  • PIXY-CAM.c
  • Created: 19.01.2015 10:41:07
  • Author: jesser
    */

#define F_CPU 16000000UL

#define UARTaktiv //Auskommentieren, um UART zu deaktivieren

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
// #include “Header/Timer.h”
// #include “Header/ADC.h”
// #include “Header/USART.h”

volatile uint8_t y=0;

void TimerInit ()
{
TCCR1A=0b10100001;
TCCR1B=0b00000011;
DDRB=(1<<PINB6) | (1<<PINB5);
}

void USART_init(uint8_t baud)
{
	
	/*Setzen der Baudrate (Übertragungsrate)*/
	UBRR1H = (unsigned char) (baud>>8);
	UBRR1L = (unsigned char) baud;
	
	/*Aktivieren von Empfänger(RX) und Sender(TX)*/
	UCSR1B = (1<<RXEN1) | (1<<TXEN1);
	
	/*Setzen des Formates: 8bit Groß, 2bits zum Stoppen*/
	UCSR1C = (1<<USBS1) | (3<<UCSZ10);
	
}

void USARTTransmit (unsigned char data)
{
	/*Auf leeren Übertragungspuffer warten*/
	while (!(UCSR1A & (1<<UDRE1)) );
	
	/*Packe die Daten in den Puffer und sende diese*/
	UDR1 = data;
}

void USART_Recieve()
{
	/*Warte auf Empfangen von Daten*/
	while (!(UCSR1A &(1<<RXC1)) );
	
	/*Bekomme die Daten vom Puffe und gebe sie zurück*/
	return UDR1;
}

void SPIinit(void)
{
DDRB = (1<<PINB1)|(1<<PINB2)|(1<<PINB0);
SPCR = 0b01110001;
SPSR= 0;
}
unsigned char SPI_start(unsigned char cData)
{
SPDR = cData;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}

uint8_t SensorArray()
{
uint8_t Sensor = 0b00000000;
if (PIND &(1<<4))
Sensor |=(0b00000001);
//if (ADCRead(4) <= 80)
if (PINF &(1<<4))
Sensor |=(0b00000010);
if (PINB &(1<<7))
Sensor |=(0b00000100);
//if (ADCRead(7) <= 80)
if (PINF &(1<<7))
Sensor |=(0b00001000);
//if (ADCRead(5) <= 80)
if (PINF &(1<<5))
Sensor |=(0b00010000);
if(PINC &(1<<6))
Sensor |=(0b00100000);
return Sensor;
}

void PullupSet()
{
PORTB = (1<<PINB7);
PORTC = (1<<PINC6);
PORTD = (1<<PIND4);
PORTF = (1<<PINF7) | (1<<PINF5) | (1<<PINF4);
}

int main(void)
{
uint16_t ObjectBlock;
uint16_t Checksum;
uint16_t signatureNr;
uint16_t xposition;
uint16_t yposition;
uint16_t width;
uint16_t height;
uint16_t count;
uint8_t buffer;

uint8_t sig1;
uint8_t sig2;

TimerInit();
USART_init(103);
PullupSet();
SPIinit();

while(1)
{
	count++;
	buffer=SPI_start(0xFF);
	
	if(buffer==0xAA )
	{
		count=0;
		sig1 =(buffer);
	}
	else if (count==1)
		sig2 =(buffer);	
	else if (count==2)
		Checksum=buffer;
	else if (count==3)
		Checksum=Checksum+buffer<<8;
	else if (count==4)
		signatureNr = buffer;
	else if (count==5)
		signatureNr+= buffer<<8;
	else if (count==6)
		xposition=buffer;
	else if (count==7)
		xposition+=buffer<<8;	
	else if (count==8)
		yposition=buffer;
	else if (count==9)
		yposition=yposition+buffer<<8;
	else if (count==10)
		width=buffer;
	else if (count==11)
		width=width+buffer<<8;	
	else if (count==12)
		height=buffer;
	else if (count==13)
		height=height+buffer<<8;		

	if ((sig1==0xAA) && (sig2==0x55))
	{
	USARTTransmit((uint8_t)((xposition & 0xff00)>>8));
	USARTTransmit((uint8_t)(xposition & 0x00ff));
	USARTTransmit(sig1);
	USARTTransmit(sig2);
	USARTTransmit((uint8_t)((Checksum & 0xff00)>>8));
	USARTTransmit((uint8_t)(Checksum & 0x00ff));
	USARTTransmit((uint8_t)((signatureNr & 0xff00)>>8));
	USARTTransmit((uint8_t)(signatureNr & 0x00ff));
	USARTTransmit((uint8_t)((xposition & 0xff00)>>8));
	USARTTransmit((uint8_t)(xposition & 0x00ff));
	USARTTransmit((uint8_t)((yposition & 0xff00)>>8));
	USARTTransmit((uint8_t)(yposition & 0x00ff));
	USARTTransmit((uint8_t)((width & 0xff00)>>8));
	USARTTransmit((uint8_t)(width & 0x00ff));
	USARTTransmit((uint8_t)((height & 0xff00)>>8));
	USARTTransmit((uint8_t)(height & 0x00ff));
	USARTTransmit(13);
	}
}

}@

Why are you using the AVR programmer? Can’t you just upload code from arduino IDE to leonardo over usb to test the basic functionality?

Thanks for this Information, but I’m not comfortable with Arduino IDE.

Hello Jeremias,
I think we are proposing that you use the Arduino IDE as a test, to make sure everything is working. You can then work backwards. All of the Arduino source code is available, so this should be doable.

Edward

so i’ve testet pixy cam with Arduino IDE and it still works, but i’m more confused, because why didn’t it function with avr?

Hello Jeremias,
It doesn’t work (most likely) because there is a register setting difference between the two pieces of code.

I’m not an AVR expert, so I can’t be much help there. But I do know that the SPI-related code for Arduino is available, so it should be a solvable problem.

Edward