Data Structures | Defines | Enumerations | Functions | Variables

main.c File Reference

Firmware for the i2c-dimmer. More...

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "usiTwiSlave.h"

Go to the source code of this file.

Data Structures

struct  Command
 Holds one command that is received via i2c. More...

Defines

#define TWI_SLA   0x10
 i2c slave address
#define CHANNEL_COUNT   13
 number of 'fadeable' channels
#define PORT_COUNT   2
 the channels are distributed over two ports
#define OUTPORT0   PORTB
 output port 0
#define OUTDDR0   DDRB
 set port 0 to be output
#define OUTMASK0   0x5F
 see channel_pin, channel_port
#define OUTPORT1   PORTD
 output port 0
#define OUTDDR1   DDRD
 set port 0 to be output
#define OUTMASK1   0x7F
 see channel_pin, channel_port
#define STATE_COUNT   14
 number of states for pwm
#define STATE_START_COUNT   2
 number of state groups to be treated individually

Enumerations

enum  ReadCommandState { WAIT_FOR_ADDRESS, WAIT_FOR_VALUE_LOW, WAIT_FOR_VALUE_HIGH }
 

Three bytes have to be received for a full command.

More...

Functions

void timer_start ()
 initialize timer
void set_brightness (uint8_t channel, uint16_t brightness)
 Set brightness on one channel.
void init_ports (void)
 initialize hardware
void set_port (int port, uint8_t state)
 set output
void evaluate_i2c_input (void)
 Check if anything has been received via i2c and evaluate the received data.
int main (void)
 Main-function.

Variables

const uint8_t channel_port[CHANNEL_COUNT] PROGMEM
 We want to drive as many channels as possible.
const uint8_t switch_timer_index [STATE_START_COUNT] = { 13, 0 }
 start interval of the state groups
uint8_t switch_state [STATE_COUNT][PORT_COUNT]
 contains the port assignments for each interval
uint8_t switch_state_new [STATE_COUNT][PORT_COUNT]
Command command = {0, 0, WAIT_FOR_ADDRESS}
 the next command is built in this variable

Detailed Description

Firmware for the i2c-dimmer.

Author:
Ronald Schaten <ronald@schatenseite.de> & Thomas Stegemann
Version:
Id:
main.c,v 1.1 2007/07/29 17:19:50 rschaten Exp

Permission to use, copy, modify, and distribute this software and its documentation under the terms of the GNU General Public License is hereby granted. No representations are made about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. See the GNU General Public License for more details.

Definition in file main.c.


Define Documentation

#define CHANNEL_COUNT   13

number of 'fadeable' channels

Definition at line 309 of file main.c.

Referenced by main().

#define OUTDDR0   DDRB

set port 0 to be output

Definition at line 313 of file main.c.

Referenced by init_ports().

#define OUTDDR1   DDRD

set port 0 to be output

Definition at line 317 of file main.c.

Referenced by init_ports().

#define OUTMASK0   0x5F

see channel_pin, channel_port

Definition at line 314 of file main.c.

Referenced by init_ports(), and set_port().

#define OUTMASK1   0x7F

see channel_pin, channel_port

Definition at line 318 of file main.c.

Referenced by init_ports(), and set_port().

#define OUTPORT0   PORTB

output port 0

Definition at line 312 of file main.c.

Referenced by init_ports(), and set_port().

#define OUTPORT1   PORTD

output port 0

Definition at line 316 of file main.c.

Referenced by init_ports(), and set_port().

#define PORT_COUNT   2

the channels are distributed over two ports

Definition at line 310 of file main.c.

Referenced by main().

#define STATE_COUNT   14

number of states for pwm

Definition at line 340 of file main.c.

Referenced by main(), and set_brightness().

#define STATE_START_COUNT   2

number of state groups to be treated individually

Definition at line 341 of file main.c.

Referenced by main().

#define TWI_SLA   0x10

i2c slave address

Definition at line 307 of file main.c.

Referenced by main().


Enumeration Type Documentation

Three bytes have to be received for a full command.

This enum is used to indicate what part of the command we are waiting for.

Enumerator:
WAIT_FOR_ADDRESS 

first byte is the address

WAIT_FOR_VALUE_LOW 

second byte is the lower part of the value

WAIT_FOR_VALUE_HIGH 

third byte is the higher part of the value

Definition at line 356 of file main.c.


Function Documentation

void evaluate_i2c_input ( void   )

Check if anything has been received via i2c and evaluate the received data.

The received data is set into the command variable according to the state of the command we are waiting for.

Definition at line 449 of file main.c.

References Command::address, set_brightness(), Command::state, usiTwiDataInReceiveBuffer(), usiTwiReceiveByte(), Command::value, WAIT_FOR_ADDRESS, WAIT_FOR_VALUE_HIGH, and WAIT_FOR_VALUE_LOW.

Referenced by main().

void init_ports ( void   )

initialize hardware

Definition at line 418 of file main.c.

References OUTDDR0, OUTDDR1, OUTMASK0, OUTMASK1, OUTPORT0, and OUTPORT1.

Referenced by main().

int main ( void   )

Main-function.

Initializes everything and contains the main loop which controls the actual PWM output.

Returns:
An integer. Whatever... :-)

Definition at line 496 of file main.c.

References CHANNEL_COUNT, evaluate_i2c_input(), init_ports(), PORT_COUNT, set_brightness(), set_port(), STATE_COUNT, STATE_START_COUNT, switch_state, switch_state_new, switch_timer_index, timer_start(), TWI_SLA, and usiTwiSlaveInit().

void set_brightness ( uint8_t  channel,
uint16_t  brightness 
)

Set brightness on one channel.

Parameters:
channelthe channel to address (0 .. CHANNEL_COUNT)
brightnessthe value to set (0 .. 16383)

Definition at line 395 of file main.c.

References STATE_COUNT, and switch_state_new.

Referenced by evaluate_i2c_input(), and main().

void set_port ( int  port,
uint8_t  state 
)

set output

Parameters:
portport to set
statevalue to be sent to the port

Definition at line 431 of file main.c.

References OUTMASK0, OUTMASK1, OUTPORT0, and OUTPORT1.

Referenced by main().

void timer_start (  )

initialize timer

Definition at line 381 of file main.c.

Referenced by main().


Variable Documentation

Command command = {0, 0, WAIT_FOR_ADDRESS}

the next command is built in this variable

Definition at line 376 of file main.c.

const uint16_t switch_timer [STATE_COUNT] PROGMEM
Initial value:
 {
       0,    0,    0,    0,    0,    0,
       1,    1,    1,    1,    1,    1,    1 }

We want to drive as many channels as possible.

interval length of the states

this is used to determine the pin that is used for output

Unfortunately the usable pins aren't 'in a row', so we have to determine which channel ends up on which port and pin. this is used to determine the port that is used for output

Definition at line 326 of file main.c.

uint8_t switch_state[STATE_COUNT][PORT_COUNT]

contains the port assignments for each interval

Definition at line 349 of file main.c.

Referenced by main().

uint8_t switch_state_new[STATE_COUNT][PORT_COUNT]

Definition at line 351 of file main.c.

Referenced by main(), and set_brightness().

const uint8_t switch_timer_index[STATE_START_COUNT] = { 13, 0 }

start interval of the state groups

Definition at line 346 of file main.c.

Referenced by main().