Arduino and avr4studio

I do not have a scope, this is the working example

#define F_CPU 20000000UL	// Baby Orangutan frequency (20MHz)
#include <avr/io.h>

#define CE_LOW	PORTB &= ~( 1 << PB5 )	// Define display port status
#define CE_HIGH	PORTB |=  ( 1 << PB5 ) 
#define DATA_LOW	PORTD &= ~( 1 << PD1 )
#define DATA_HIGH	PORTD |=  ( 1 << PD1 ) 
#define RS_LOW	PORTD &= ~( 1 << PD2 )
#define RS_HIGH	PORTD |=  ( 1 << PD2 ) 
#define CLK_LOW	PORTD &= ~( 1 << PD4 )
#define CLK_HIGH	PORTD |=  ( 1 << PD4 ) 

int main (void)
{
	DDRB |= 1 << PB0; PORTB |=  ( 1 << PB0 );
	DDRB |= 1 << PB5;
	DDRD |= 1 << PD1;
	DDRD |= 1 << PD2;
	DDRD |= 1 << PD4;

	RS_LOW;		// Start value
	CE_HIGH;		// Start value

	//*** setup display

	RS_HIGH;		// First RS is brought to logic high
	CE_LOW;		// and then CE is brought to logic low

	// ******************************* Load control word0 01001111

	CLK_LOW;		// Low again
	DATA_LOW;		// 0 Controlword 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 normal operation, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0 brightness, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0 brightness, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 pwm, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 pwm, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge
	
	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 pwm, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge
	
	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 pwm, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CE_HIGH;		// when 8 bits have been loaded the CE line is brought to high
	CLK_LOW;		// when CLK goes to logic low, new dat is copied

	// Load control word 1 10000001

	RS_HIGH;		// First RS is brought to logic high
	CE_LOW;		// and then CE is brought to logic low

	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 Controlword 1, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_LOW;		// 0, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CLK_LOW;		// Low again
	DATA_HIGH;		// 1 Serial, next each successive rising CLK edge will shift the data at the DIN pin
	CLK_HIGH;		// Next rising CLK edge

	CE_HIGH;		// when 8 bits have been loaded the CE line is brought to high
	CLK_LOW;		// when CLK goes to logic low, new dat is copied

	//*** end setup displsy

	//*** begin zebra
	RS_LOW;		// First RS is brought low
	CE_LOW;		// then CE is brought low

	unsigned int i;
  	for (i=1;i<=80;i++)	// data entry, for test write 80 times 00, 01, 10 or 11 total bits 160
	{
		CLK_LOW;	// Low again
		DATA_LOW;	// Next each successive rising CLK edge will shift the data at the DIN pin
		CLK_HIGH;	// Next rising CLK edge

		CLK_LOW;	// Low ready to rise
		DATA_HIGH;	// Next each successive rising CLK edge will shift the data at the DIN pin
		CLK_HIGH;	// Next CLK rising edge

	}

	CE_HIGH;	// When all 160 bits have been loaded CE is brought to logic high

	CLK_LOW; 	// When CLK is next brought low, new data is latched into the dot drivers   

	//*** end zebra, all uneven dots should be off
	return 0;
}

One of your problems is that you are using the Baby Orangutan’s SPI module, but you are not using the pins that correspond with that module. The SPI module uses pins PB3 (MOSI), PB4 (MISO), and PB5 (SCK). I think you are still using the pins that would correspond to USART SPI pins.

- Ben

That is probably true, according the Atmega168 datasheet I need to set the UMSEL for Synchronous USART so SCK will move to PD4, I assume TXEN takes care of PD1, when I try this it won’t build.

UCSR0B = (1<<RXEN0)|(1<<TXEN0); 		// Enable receiver and transmitter.
UCSR0C = (1<<UMSEL0); 			// Synchronous USART
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPHA)|(0<<CPOL);
// Enable SPI, Master, set clock rate fck/16, rising edge

error: ‘UMSEL0’ undeclared (first use in this function)

I think you’re still not understanding that the AVR’s hardware SPI module is not the same as the hardware USART. Using the USART for SPI does not involve using the SPI registers, such as SPCR. If you read through section 20 of the datasheet, you’ll see that it makes no reference to the SPI registers. Have you looked at the sample C code in section 20 for initializing the USART and sending data?

As far as your compilation issues go, just because a term is in the datasheet does not mean it is defined by WinAVR. If you find that a definition is missing, you can look through the appropriate included WinAVR header files to see if it is defined under a slightly different name, or you can replace it with the actual value. In your case, the problem is that there are two UMSEL bits:

Bit 7:6 – UMSELn1:0: USART Mode Select

The “n” here is the USART number, which is 0, so I expect there to be two defines you can use: UMSEL01 and UMSEL00. If these don’t work, you can see from the register description that these are bits 7 and 6 of the register, so you can just directly set those bits:

UCSR0C = (1<<6);

However, this brings up another problem. We’re talking here about using the USART in Master SPI mode, which requires both UMSEL bits to be set. You seem to be trying to use the USART in synchronous serial mode, which is different. In synchronous serial mode, you will not get the same behavior as described in section 20 of the datasheet.

By the way, I still think your best bet is to just bitbang the protocol like you already have (though I would generalize it so that you can just call functions to send an arbitrary eight bits).

- Ben

I started this thread with the following question:
I need to send a name or a number to the display, is there a smart way to migrate this to AVR4Studio as I do not want to rewrite my software in Arduino.

The answer is that there are no shortcuts. Writing a smart solution from scratch requires a higher level of background knowledge than I can deliver.

So your advice to focus on 160BlinkLeds is probably the best way to operate. As I am running out of time I will stick to the 7 segment solution I am using at the moment.

This thread ends here.

An amazing 20000 readers visited this post, one of them must have been from Atmel, with Atmel Studio 7 you can import any Arduino sketch and continue to program in Atmel Studio.

I’ve only tried for a few minutes, but my experience is ‘import Arduino’ gets lost for anything more complicated than blink. This is a problem of using things like Arduino (ruins your ability to use professional approaches later). Arduino was made for artist, by artist, it should stay in that field.