161 lines
3.7 KiB
C
161 lines
3.7 KiB
C
/* ---------------------------------------------------------------------------
|
|
** This software is in the public domain, furnished "as is", without technical
|
|
** support, and with no warranty, express or implied, as to its usefulness for
|
|
** any purpose.
|
|
**
|
|
** ledmatrix.c
|
|
**
|
|
** Beschrijving: Simple HT16K33 Ledmatix demo.
|
|
** Target: AVR mcu
|
|
** Build: avr-gcc -std=c99 -Wall -O3 -mmcu=atmega128 -D F_CPU=8000000UL -c ledmatrix.c
|
|
** avr-gcc -g -mmcu=atmega128 -o ledmatrix.elf ledmatrix.o
|
|
** avr-objcopy -O ihex ledmatrix.elf ledmatrix.hex
|
|
** or type 'make'
|
|
** Author: dkroeske@gmail.com
|
|
** -------------------------------------------------------------------------*/
|
|
|
|
#define F_CPU 10e6
|
|
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
|
|
/******************************************************************/
|
|
void twi_init(void)
|
|
/*
|
|
short: Init AVR TWI interface and set bitrate
|
|
inputs:
|
|
outputs:
|
|
notes: TWI clock is set to 100 kHz
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
TWSR = 0;
|
|
TWBR = 32; // TWI clock set to 100kHz, prescaler = 0
|
|
}
|
|
|
|
/******************************************************************/
|
|
void twi_start(void)
|
|
/*
|
|
short: Generate TWI start condition
|
|
inputs:
|
|
outputs:
|
|
notes:
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
TWCR = (0x80 | 0x20 | 0x04);
|
|
while( 0x00 == (TWCR & 0x80) );
|
|
}
|
|
|
|
/******************************************************************/
|
|
void twi_stop(void)
|
|
/*
|
|
short: Generate TWI stop condition
|
|
inputs:
|
|
outputs:
|
|
notes:
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
TWCR = (0x80 | 0x10 | 0x04);
|
|
}
|
|
|
|
/******************************************************************/
|
|
void twi_tx(unsigned char data)
|
|
/*
|
|
short: transmit 8 bits data
|
|
inputs:
|
|
outputs:
|
|
notes:
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
TWDR = data;
|
|
TWCR = (0x80 | 0x04);
|
|
while( 0 == (TWCR & 0x80) );
|
|
}
|
|
|
|
/******************************************************************/
|
|
void wait( int ms )
|
|
/*
|
|
short: Busy wait number of millisecs
|
|
inputs: int ms (Number of millisecs to busy wait)
|
|
outputs:
|
|
notes: Busy wait, not very accurate. Make sure (external)
|
|
clock value is set. This is used by _delay_ms inside
|
|
util/delay.h
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
for (int i=0; i<ms; i++)
|
|
{
|
|
_delay_ms( 1 ); // library function (max 30 ms at 8MHz)
|
|
}
|
|
}
|
|
|
|
void matrix_clear()
|
|
{
|
|
twi_start();
|
|
twi_tx(0xE0);
|
|
twi_tx(0x00);
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
|
|
twi_tx(0x00);
|
|
}
|
|
twi_stop();
|
|
}
|
|
|
|
/******************************************************************/
|
|
int main( void )
|
|
/*
|
|
short: main() loop, entry point of executable
|
|
inputs:
|
|
outputs:
|
|
notes: Looping forever, trashing the HT16K33
|
|
Version : DMK, Initial code
|
|
*******************************************************************/
|
|
{
|
|
|
|
twi_init(); // Init TWI interface
|
|
|
|
// Init HT16K22. Page 32 datasheet
|
|
twi_start();
|
|
twi_tx(0xE0); // Display I2C addres + R/W bit
|
|
twi_tx(0x21); // Internal osc on (page 10 HT16K33)
|
|
twi_stop();
|
|
|
|
twi_start();
|
|
twi_tx(0xE0); // Display I2C address + R/W bit
|
|
twi_tx(0xA0); // HT16K33 pins all output
|
|
twi_stop();
|
|
|
|
twi_start();
|
|
twi_tx(0xE0); // Display I2C address + R/W bit
|
|
twi_tx(0xE3); // Display Dimming 4/16 duty cycle
|
|
twi_stop();
|
|
|
|
twi_start();
|
|
twi_tx(0xE0); // Display I2C address + R/W bit
|
|
twi_tx(0x81); // Display OFF - Blink On
|
|
twi_stop();
|
|
|
|
matrix_clear();
|
|
while (1)
|
|
{
|
|
for (int i = 0; i < 16; i += 2)
|
|
{
|
|
twi_start();
|
|
twi_tx(0xE0);
|
|
twi_tx((char)i);
|
|
twi_tx(0xFF);
|
|
twi_stop();
|
|
|
|
wait(200);
|
|
}
|
|
matrix_clear();
|
|
|
|
}
|
|
|
|
return 1;
|
|
} |