Hi, i have a 328 and using the 0018 ide.
im trying to get conways game of life working using mstimer2 as i get the impression that frequencytimer2 doesnt work. i cant get it to work anyway as i know zilch about this stuff. i just need this to work for a degree project. Heres the code for game of life, mstimer2.cpp, and mstimer2.h
#include <MsTimer2.h>
/*
- Conway's "Life"
- Adapted from the Life example
- on the Processing.org site
*/
#include <MsTimer2.h>
byte analogPin = 5;
byte col = 0;
byte leds[8][8];
// pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
int pins[17]= {-1, 5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10, 9, 8, 7, 6};
// col[xx] of leds = pin yy on led matrix
int cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06], pins[11], pins[15], pins[16]};
// row[xx] of leds = pin yy on led matrix
int rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1], pins[7], pins[2], pins[5]};
#define DELAY 0
#define SIZE 8
extern byte leds;
byte world[2];
long density = 50;
void setup() {
setupLeds();
randomSeed(analogRead(5));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (random(100) < density) {
world*[j][0] = 1;*
- }*
- else {*
_ world*[j][0] = 0;_
_ }_
_ world[j][1] = 0;
}
}
}
void loop() {
// Display current generation*
* for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
leds[j] = world[j][0];
}
}
delay(DELAY);
// Birth and death cycle*
* for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
// Default is for cell to stay the same*
* world[x][y][1] = world[x][y][0];
int count = neighbours(x, y);
if (count == 3 && world[x][y][0] == 0) {
// A new cell is born*
* world[x][y][1] = 1;
}
if ((count < 2 || count > 3) && world[x][y][0] == 1) {
// Cell dies*
* world[x][y][1] = 0;
}
}
}*_
* // Copy next generation into place*
* for (int x = 0; x < SIZE; x++) {*
* for (int y = 0; y < SIZE; y++) {*
* world[x][y][0] = world[x][y][1];*
* }*
* }*
}
int neighbours(int x, int y) {
return world[(x + 1) % SIZE][y][0] +
* world[x][(y + 1) % SIZE][0] +*
* world[(x + SIZE - 1) % SIZE][y][0] +*
* world[x][(y + SIZE - 1) % SIZE][0] +*
* world[(x + 1) % SIZE][(y + 1) % SIZE][0] +*
* world[(x + SIZE - 1) % SIZE][(y + 1) % SIZE][0] +*
* world[(x + SIZE - 1) % SIZE][(y + SIZE - 1) % SIZE][0] +*
* world[(x + 1) % SIZE][(y + SIZE - 1) % SIZE][0];*
}
void setupLeds() {
* // sets the pins as output*
* for (int i = 1; i <= 16; i++) {*
_ pinMode(pins*, OUTPUT);
}
// set up cols and rows*
* for (int i = 1; i <= 8; i++) {
digitalWrite(cols[i - 1], LOW);
}
for (int i = 1; i <= 8; i++) {
digitalWrite(rows[i - 1], LOW);
}*_
* clearLeds();*
* // Turn off toggling of pin 11*
* // Set refresh rate (interrupt timeout period)*
* // Set interrupt routine to be called*
}
void clearLeds() {
* // Clear display array*
* for (int i = 0; i < 8; i++) {*
* for (int j = 0; j < 8; j++) {*
_ leds*[j] = 0;
}
}
}
// Interrupt routine*
void display() {
* digitalWrite(cols[col], LOW); // Turn whole previous column off*
* col++;
if (col == 8) {
col = 0;
}
for (int row = 0; row < 8; row++) {
if (leds[col][7 - row] == 1) {
digitalWrite(rows[row], LOW); // Turn on this led*
* }
else {
digitalWrite(rows[row], HIGH); // Turn off this led*
* }
}
digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
delayMicroseconds(900); // Delay so that on times are longer than off time = brighter leds*
}
now mstimer2.cpp
/
MsTimer2.h - Using timer2 with 1ms resolution*
* Javier Valencia javiervalencia80@gmail.com*_
* History:*
* 29/May/09 - V0.5 added support for Atmega1280 (thanks to Manuel Negri)*
* 19/Mar/09 - V0.4 added support for ATmega328P (thanks to Jerome Despatis)*
* 11/Jun/08 - V0.3*
* changes to allow working with different CPU frequencies*
* added support for ATMega128 (using timer2)*
* compatible with ATMega48/88/168/8*
* 10/May/08 - V0.2 added some security tests and volatile keywords*
* 9/May/08 - V0.1 released working on ATMEGA168 only*
* This library is free software; you can redistribute it and/or*
* modify it under the terms of the GNU Lesser General Public*
* License as published by the Free Software Foundation; either*
* version 2.1 of the License, or (at your option) any later version.*
* This library is distributed in the hope that it will be useful,*
* but WITHOUT ANY WARRANTY; without even the implied warranty of*
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU*
* Lesser General Public License for more details.*
* You should have received a copy of the GNU Lesser General Public*
* License along with this library; if not, write to the Free Software*
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*
*/
#include <MsTimer2.h>
unsigned long MsTimer2::msecs;
void (*MsTimer2::func)();
volatile unsigned long MsTimer2::count;
volatile char MsTimer2::overflowing;
volatile unsigned int MsTimer2::tcnt2;
void MsTimer2::set(unsigned long ms, void (*f)()) {
* float prescaler = 0.0;*
#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
* TIMSK2 &= ~(1<<TOIE2);*
* TCCR2A &= ~((1<<WGM21) | (1<<WGM20));*
* TCCR2B &= ~(1<<WGM22);*
* ASSR &= ~(1<<AS2);*
* TIMSK2 &= ~(1<<OCIE2A);*
* if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64*
* TCCR2B |= (1<<CS22);*
* TCCR2B &= ~((1<<CS21) | (1<<CS20));*
* prescaler = 64.0;*
* } else if (F_CPU < 1000000UL) { // prescaler set to 8*
* TCCR2B |= (1<<CS21);*
* TCCR2B &= ~((1<<CS22) | (1<<CS20));*
* prescaler = 8.0;*
* } else { // F_CPU > 16Mhz, prescaler set to 128*
* TCCR2B |= ((1<<CS22) | (1<<CS20));*
* TCCR2B &= ~(1<<CS21);*
* prescaler = 128.0;*
* }*
#elif defined (AVR_ATmega8)
* TIMSK &= ~(1<<TOIE2);*
* TCCR2 &= ~((1<<WGM21) | (1<<WGM20));*
* TIMSK &= ~(1<<OCIE2);*
* ASSR &= ~(1<<AS2);*
* if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64*
* TCCR2 |= (1<<CS22);*
* TCCR2 &= ~((1<<CS21) | (1<<CS20));*
* prescaler = 64.0;*
* } else if (F_CPU < 1000000UL) { // prescaler set to 8*
* TCCR2 |= (1<<CS21);*
* TCCR2 &= ~((1<<CS22) | (1<<CS20));*
* prescaler = 8.0;*
* } else { // F_CPU > 16Mhz, prescaler set to 128*
* TCCR2 |= ((1<<CS22) && (1<<CS20));*
* TCCR2 &= ~(1<<CS21);*
* prescaler = 128.0;*
* }*
#elif defined (AVR_ATmega128)
* TIMSK &= ~(1<<TOIE2);*
* TCCR2 &= ~((1<<WGM21) | (1<<WGM20));*
* TIMSK &= ~(1<<OCIE2);*
* if ((F_CPU >= 1000000UL) && (F_CPU <= 16000000UL)) { // prescaler set to 64*
* TCCR2 |= ((1<<CS21) | (1<<CS20));*
* TCCR2 &= ~(1<<CS22);*
* prescaler = 64.0;*
* } else if (F_CPU < 1000000UL) { // prescaler set to 8*
* TCCR2 |= (1<<CS21);*
* TCCR2 &= ~((1<<CS22) | (1<<CS20));*
* prescaler = 8.0;*
* } else { // F_CPU > 16Mhz, prescaler set to 256*
* TCCR2 |= (1<<CS22);*
* TCCR2 &= ~((1<<CS21) | (1<<CS20));*
* prescaler = 256.0;*
* }*
#endif
tcnt2 = 256 - (int)((float)F_CPU * 0.001 / prescaler);
* if (ms == 0)*
* msecs = 1;*
* else*
* msecs = ms;*
* func = f;*
}
void MsTimer2::start() {
* count = 0;*
* overflowing = 0;*
#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
* TCNT2 = tcnt2;*
* TIMSK2 |= (1<<TOIE2);*
#elif defined (AVR_ATmega128)
* TCNT2 = tcnt2;*
* TIMSK |= (1<<TOIE2);*
#elif defined (AVR_ATmega8)
* TCNT2 = tcnt2;*
* TIMSK |= (1<<TOIE2);*
#endif
}
void MsTimer2::stop() {
#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
* TIMSK2 &= ~(1<<TOIE2);*
#elif defined (AVR_ATmega128)
* TIMSK &= ~(1<<TOIE2);*
#elif defined (AVR_ATmega8)
* TIMSK &= ~(1<<TOIE2);*
#endif
}
void MsTimer2::_overflow() {
* count += 1;*
* if (count >= msecs && !overflowing) {*
* overflowing = 1;*
* count = 0;*
_ (func)();
overflowing = 0;
}
}_
ISR(TIMER2_OVF_vect) {
#if defined (AVR_ATmega168) || defined (AVR_ATmega48) || defined (AVR_ATmega88) || defined (AVR_ATmega328P) || (AVR_ATmega1280)
_ TCNT2 = MsTimer2::tcnt2;_
#elif defined (AVR_ATmega128)
_ TCNT2 = MsTimer2::tcnt2;_
#elif defined (AVR_ATmega8)
_ TCNT2 = MsTimer2::tcnt2;
#endif*
* MsTimer2::overflow();*
}
now mstimer2.h
#ifndef MsTimer2_h
#define MsTimer2_h
#include <avr/interrupt.h>
namespace MsTimer2 {
* extern unsigned long msecs;*
_ extern void (func)();
extern volatile unsigned long count;
extern volatile char overflowing;
extern volatile unsigned int tcnt2;*_
_ void set(unsigned long ms, void (f)());
void start();
void stop();_
void overflow();*
}
#endif
i hope someone can help me with this, im going insane with forum merry go round. please bare in mind that i know nothing of how to program. im only an artist trying to add a little pizzaz to a light. thanks. jay_