MetkuMods



OLED-teknologiaa
4D Systemsiltä


 
Kirjoittaja: Aki Korhonen
Julkaistu: 25.06.2009
Valmistaja: 4D Systems
In English In English
Suomeksi Suomeksi

Mikrokontrolleriesimerkki käyttäen Atmelin AVR-piiriä

µOLED-96-G1 -moduuli on erittäin helppo yhdistää mikrokontrolleriin, koska tarvitset ainoastaan yhden vastuksen, mikäli kytkennän käyttöjännitteenä on 5V (3.3V systeemin kanssa et edes tarvitse vastusta). Esimerkissä käytetään Atmelin ATmega8 -piiriä, mutta on mahdollista käyttää mitä tahansa mikrokontrolleria, josta saat sarjadataa ulos. Mikäli näyttö kytketään mikrokontrolleriin, täytyy sarjaliikenneohjelmiston olla moduulille ladattuna ensin.

 

Introcution to 4D Systems OLED Technology
Näyttö ja mikrokontrolleri

 

Introcution to 4D Systems OLED Technology
Näyttö ja mikrokontrolleri koekytkentäalustalla

Ohjelmointi on erittäin helppoa, koska moduulille lähetetään ainoastaan yksinkertaisia komentoja sarjaliikenneväylän yli. Näyttömoduulin ohjekirja kertoo kaiken tarvittavan tiedon käyttöön ja listaa kaikki mahdolliset komennot, joita voidaan käyttää ohjaamiseen. Esimerkkikoodissa on muutama ohjekirjan selostuksen mukaan tehty funktio, mm. tyhjennä näyttö, aseta taustaväri, piirrä ympyrä...

 

Introcution to 4D Systems OLED Technology
Mikrokontrolleri ohjaamassa näyttöä

Esimerkkikoodi alustaa näytön ensin ja alkaa piirtää siihen pomppivaa palloa, mutta ilman näytön tyhjennystä askelten välissä, joten se jättää jälkeensä värikkään vanan. Väriä vaihdetaan jokaisen askeleen välissä siten, että väri vaihtuu tasaisesti seuraavaan, joten jonkun ajan päästä näyttö on täynnä värikästä kuviota.

!!HUOMIO!! Älä välitä näytössä näkyvistä päivitysviivoista, ne johtuvat ainoastaan kamerasta, et näe niitä paljaalla silmällä.

Get Flash to see this player.


Koodi:

/**
* MetkuMods - /
* OLED displays from 4D Systems
* AVR + µOLED-96-G1 example
*
* Author: Aki Korhonen
* Date: 2009-06-08
*/

// If clock speed isn't set in the project settings then it is set here
#ifndef F_CPU
	#define F_CPU 4000000UL // 4 MHz
#endif

#define BAUD 9600

#include <avr/io.h>
#include <avr/interrupt.h>

#include <inttypes.h>
#include <util/delay.h>
#include <util/setbaud.h>
#include <math.h>

// ------------------------------------- //

// General stuff
#define OLED_RES 4
uint16_t counter = 0;

// ------------------------------------- //

// Screen stuff
#define DISP_HEIGHT 64
#define DISP_WIDTH 96
uint16_t color_16b = 0x0000;

// Bouncing Ball
uint8_t bb_x_step = 1;
uint8_t bb_y_step = 1;
uint8_t bb_size = 4;
uint8_t bb_x = 5;
uint8_t bb_y = 10;
uint8_t bb_color_r = 16;
uint8_t bb_color_g = 0;
uint8_t bb_color_b = 16;
int8_t bb_color_r_step = 1;
int8_t bb_color_g_step = 1;
int8_t bb_color_b_step = 2;

// ------------------------------------- //

// Function Prototypes
void BouncingBall();

// ------------------------------------- //

// Serial functions

// UART initialization
void Init_USART(void)
{
	UBRRH = UBRRH_VALUE;
	UBRRL = UBRRL_VALUE;
	
	UCSRA = 0x00;
	UCSRB |= (1 << RXEN); // RXEN, Enable RX
	UCSRB |= (1 << RXCIE); // RXCIE, Enable RX interrupt
	UCSRB |= (1 << TXEN); // TXEN, Enable TX
	UCSRC = 0x86; // 8 Data, 1 Stop, No Parity
}

// SendByte sends the wanted byte to the TX line
void USART_SendByte(uint8_t data)
{
	// Wait if a byte is being transmitted
	loop_until_bit_is_set(UCSRA, UDRE);

	// Send the given data
	UDR = data;
}

// ReceiveByte reads a byte from RX line
uint8_t USART_ReceiveByte(void)
{
	// Wait until a byte has been received
	loop_until_bit_is_set(UCSRA, RXC);

	// Return received data
	return UDR;
}

// ------------------------------------- //

// OLED functions

uint16_t OLED_makeColor(uint8_t red, uint8_t green, uint8_t blue)
{
	uint8_t msb, lsb;
	uint16_t color;
	
	if(0x1F < red) red = 0x1F;
	if(0x3F < green) green = 0x3F;
	if(0x1F < blue) blue = 0x1F;
	
	msb = ((red << 3) & ~0b00000111) | ((green >> 3) & ~0b11111000);
	lsb = ((green << 5) & ~0b00011111) | (blue & ~0b11100000);

	color = (((uint16_t)msb) << 8) | lsb;
	
	return color;
}

void OLED_setBackgroundColor(uint16_t color)
{
	USART_SendByte(0x42);
	USART_SendByte((color >> 8));
	USART_SendByte((uint8_t)color);
}

void OLED_drawCircle(uint8_t x, uint8_t y, uint8_t rad, uint16_t color)
{
	USART_SendByte(0x43);
	
	if(0x5F < x) x = 0x5F;
	if(0x3F < y) y = 0x3F;
	if(0x3F < rad) rad = 0x3F;
	
	USART_SendByte(x);
	USART_SendByte(y);
	USART_SendByte(rad);
	USART_SendByte((color >> 8));
	USART_SendByte((uint8_t)color);
}

void OLED_eraseScreen()
{
	USART_SendByte(0x45);
}

void OLED_putPixel(uint8_t x, uint8_t y, uint16_t color)
{
	USART_SendByte(0x50);
	USART_SendByte(x);
	USART_SendByte(y);
	USART_SendByte((color >> 8));
	USART_SendByte((uint8_t)color);
}

void OLED_putChar(char c, uint8_t col, uint8_t row, uint16_t color)
{
	USART_SendByte(0x54);
	USART_SendByte(c);
	USART_SendByte(col);
	USART_SendByte(row);
	USART_SendByte((color >> 8));
	USART_SendByte((uint8_t)color);
}

void OLED_Init(void)
{
	USART_SendByte(0x55);
}

// ------------------------------------- //

// Interrupts

// Timer0 interrupt
ISR(TIMER0_OVF_vect)
{
	// Speed of the ball, smaller value makes faster
	if(counter == 20)
	{
		BouncingBall();
		
		counter = 0;
	}
	else
	{
		counter++;
	}
}

// RX interrupt, this gets executed when data is coming from the RX line
ISR(USART_RXC_vect)
{
	uint8_t c;
	
	// Get the byte that is coming
	c = USART_ReceiveByte();
}

// ------------------------------------- //

// Main program
int main(void)
{
	DDRD = _BV(OLED_RES);
	PORTD = 0xFF;
	
	// Flip the reset line of the display
	PORTD &= ~(_BV(OLED_RES));
	_delay_ms(2);
	PORTD |= _BV(OLED_RES);

	// Disable global interrupts
	cli();

	// Delays, wait for the display to be ready
	_delay_ms(700);
	_delay_ms(700);
	_delay_ms(700);
	_delay_ms(700);

	// Initialize UART
	Init_USART();

	// Initialize OLED
	OLED_Init();

	// Initialize timers
	TCCR0 = 0x00;
	TCNT0 = 0x00;
	TCCR0 = 0b00000010;
	TIMSK |= _BV(TOIE0);

	// OLED tests
	OLED_eraseScreen();

	// Enable global interrupts
	sei();
	
	while(1)
	{
		// Nothing is done here
	}

	return 0;
}

// ------------------------------------------------ //

void BouncingBall()
{
	bb_x += bb_x_step;
	bb_y += bb_y_step;

	if(bb_x <= bb_size/2) // Left
	{
		bb_y_step *= -1;
		bb_x_step += 1;
	}
	else if(DISP_WIDTH-1-bb_size/2 <= bb_x && bb_x_step > 0) // Right
	{
		bb_x_step *= -1;
	}

	if(bb_y <= bb_size/2) // Up
	{
		bb_y_step *= -1;
	}
	else if(bb_y > DISP_HEIGHT-1-bb_size/2 && bb_y_step > 0) // Down
	{
		bb_y_step *= -1;
	}

	color_16b = OLED_makeColor(bb_color_r, bb_color_g, bb_color_b);
	
	OLED_drawCircle(bb_x, bb_y, bb_size/2, color_16b);

	bb_color_r += bb_color_r_step;
	bb_color_g += bb_color_g_step;
	bb_color_b += bb_color_b_step;

	if(bb_color_r >= 0x1F || bb_color_r <= 0)
		bb_color_r_step *= -1;
	
	if(bb_color_g >= 0x3F || bb_color_g <= 0)
		bb_color_g_step *= -1;
	
	if(bb_color_b >= 0x1F || bb_color_b <= 0)
		bb_color_b_step *= -1;
}

// ------------------------------------------------ //






Sivut:   1 2 3 4 5 6 7 8 9 10 11 12 



 
  Content in english!
  Sisältö suomeksi!


 Google





 .:Back to top Bandwidth by Mpoli

Copyright © Metku.net, All Rights Reserved.
All content and graphics in MetkuMods are sole property of Jani Pönkkö and may not be reproduced or copied in any manner without written permission from him.
All brand names, trademarks and copyrights are the property of their respective owners.Privacy Policy