//storage variables
boolean toggle0 = 0;
boolean toggle1 = 0;
boolean toggle2 = 0;
#include <SoftwareSerial.h>
#define rxPin 255 // Not used, so set to invalid pin #
#define txPin 14 // Connect BPI/BPK's SER input to this pin.
#define inverted 1 // In setup, 1=inverted, 0=noninverted
boolean dir=1;
int temp_c=1;
int count_to_8=1;
int countTmr0=0;
int countTmr1=0;
int countTmr2=0;
void setup() {
Serial.begin(9600); // Set the data rate
DDRB=255;
cli();//stop interrupts
//set timer0 interrupt at 2kHz
TCCR0A = 0; // set entire TCCR2A register to 0
TCCR0B = 0; // same for TCCR2B
TCNT0 = 0; //initialize counter value to 0
// set compare match register for 2khz increments
OCR0A = 31; // = (16*10^6) / (2000*64) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS11 and CS10 bits for 64 prescaler
TCCR0B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A =1500;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
//set timer2 interrupt at 8kHz
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR2A = 49;// = (16*10^6) / (8000*8) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS11 bit for 8 prescaler
TCCR2B |= (1 << CS11);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();//allow interrupts
}//end setup
ISR(TIMER0_COMPA_vect) { //timer0 interrupt 2kHz toggles pin 8
//generates pulse wave of frequency 2kHz/2 = 1kHz (takes two cycles for full wave- toggle high then toggle low)
if(countTmr0 >= 100){
if (toggle0){
toggle0 = 0;
//PORTB=PORTB ^00000001;
PORTB=PORTB^1<<0;
}
else{
toggle0 = 1;
//PORTB=PORTB ^00000001;
PORTB=PORTB^1<<0;
}
countTmr0=0;
}
countTmr0=countTmr0+1;
}
ISR(TIMER1_COMPA_vect) { //timer1 interrupt 1Hz creates a toggles pin 36 bit 2 of port c
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
//Serial.println(temp_c, BIN);
if (dir==1){/*if moving right*/
if (temp_c==1){ //* hex 1 = to 000000001 binary* all the way to the right.
dir =0; //change direction to going left
}
}
else
if (temp_c==0x80) {//
dir = 1;
}
if(dir==0){
temp_c=temp_c<<1;}
else
temp_c=temp_c>>1;
PORTD= temp_c;
Serial.println (PORTD, BIN);
}
ISR(TIMER2_COMPA_vect) { //generates pulse wave of frequency 8kHz/2 = 4kHz (takes two cycles for full wave- toggle high then toggle low)
if(countTmr2 >= 10000){
if (toggle2) {
toggle2 = 0;
PORTB=PORTB^1<<1;
// Serial.println(PORTB, BIN);
}
else {
toggle2 = 1;
PORTB=PORTB^1<<1;
//Serial.print(PORTB, BIN);
}
countTmr2=0;
}
countTmr2=countTmr2+1;
}
void loop() {
}
// notes N = N | 1 << K OR N |= 1 << K
//where K is the bit that is to be set
//PORTB=PORTB^1<<1; in this case we are toggling (xoring) pin 1 toggles pin 1
//PORTB=PORTB ^00000001; toggles pin 0