//timer setup for timer0, timer1, and timer2. //For arduino uno or any board wit
ID: 3601544 • Letter: #
Question
//timer setup for timer0, timer1, and timer2.
//For arduino uno or any board with ATMEL 328/168.. diecimila, duemilanove, lilypad, nano, mini...
//this code will enable all three arduino timer interrupts.
//timer0 will interrupt at 2kHz
const int potPin = 6; // potentiometer connected to pin A6
int potValue; // used to store the value returned from analogRead(potPin)
int pwmValue; // value of potValue mapped to the 0 - 255 range for PWM
//storage variables
boolean toggle0 = 0;
boolean toggle1 = 0;
boolean toggle2 = 0;
void setup(){
// put your setup code here, to run once:
Serial.begin(9600);
//set pins as outputs
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
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 = 124;// = (16*10^6) / (2000*64) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS01 and CS00 bits for 64 prescaler
TCCR0B |= (1 << CS01) | (1 << CS00);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
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 (toggle0){
digitalWrite(8,HIGH);
toggle0 = 0;
}
else{
digitalWrite(8,LOW);
toggle0 = 1;
}
}
void loop(){
// put your main code here, to run repeatedly:
potValue = analogRead(potPin);
pwmValue = map(potValue, 0, 1023, 0, 255);
Serial.print(potValue);
Serial.print(" ");
Serial.println(pwmValue);
delay(250);
//do other things here
Serial.println(toggle0);
}
-------------end of code------------------------------------------------------------------------
This is a project with an Arduino nano and a PWM....having issues adding this last part.
- if the absolute value of this new reading has changed more than 3 out of 1023 since the last time you read it:
- disable interrupts
- change match register with new value from 0 - 255 from latest potentiometer reading
(probably want to copy and re-use entire timer0 setup code)
- enable interrupts
Explanation / Answer
int afterReciveSerial = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPinPCM7, OUTPUT);
pinMode(ledPinPCM8, OUTPUT);
pinMode(ledPinPCM9, OUTPUT);
pinMode(ledPinPCM10, OUTPUT);
pinMode(ledPinPCM11, OUTPUT);
pinMode(ledPinPCM12, OUTPUT);
cli();//stop interrupts
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
OCR0A = 243;
TCCR0A |= (1 << WGM01);
TCCR0B |= (1 << CS11) | (1 << CS10);
TIMSK0 |= (1 << OCIE0A);
TCCR1A = 0;
TCCR1B = 0; TCNT1 = 0;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS11);
TIMSK1 |= (1 << OCIE1A);
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 249;
TCCR2A |= (1 << WGM21);
TCCR2B |= (1 << CS11);
TIMSK2 |= (1 << OCIE2A);
ISR(TIMER2_COMPA_vect){
if (toggle2){
digitalWrite(ledPin1,HIGH);
toggle2 = 0;
}
else{
digitalWrite(ledPin1,LOW);
toggle2 = 1;
}
}
if (Serial.available() > 0) {
int inChar = Serial.read();
afterReciveSerial = 1;
if ( inChar =='A' )
digitalWrite(ledPinPCM7, HIGH);
if ( inChar =='a' )
digitalWrite(ledPinPCM7, LOW);
if ( inChar =='B' )
digitalWrite(ledPinPCM8, HIGH);
if ( inChar =='b' )
digitalWrite(ledPinPCM8, LOW);
if ( inChar =='C' )
digitalWrite(ledPinPCM9, HIGH);
if ( inChar =='c' )
digitalWrite(ledPinPCM9, LOW);
if ( inChar =='D' )
digitalWrite(ledPinPCM10, HIGH);
if ( inChar =='d' )
digitalWrite(ledPinPCM10, LOW);
if ( inChar =='E' )
digitalWrite(ledPinPCM11, HIGH);
if ( inChar =='e' )
digitalWrite(ledPinPCM11, LOW);
if ( inChar =='F' )
digitalWrite(ledPinPCM12, HIGH);
if ( inChar =='f' )
digitalWrite(ledPinPCM12, LOW);
if ( inChar =='0' )
setSampleSpeed(120);
if ( inChar =='1' )
setSampleSpeed(240);
if ( inChar =='2' )
setSampleSpeed(480);
if ( inChar =='5' )
setSampleSpeed(128);
if ( inChar =='6' )
setSampleSpeed(256);
if ( inChar =='7' )
setSampleSpeed(512);
}
delay(10);
}
void send4Data() {
txtMsg = "";
sensorValue = analogRead(analogInPin);
outputValue = sensorValue;
sensorValue2 = analogRead(analogInPin2);
outputValue2 = sensorValue2;
sensorValue3 = analogRead(analogInPin3);
outputValue3 = sensorValue3;
sensorValue4 = analogRead(analogInPin4);
outputValue4 = sensorValue4;
channel1 = String( outputValue, HEX );
channel2 = String( outputValue2, HEX );
channel3 = String( outputValue3, HEX );
channel4 = String( outputValue4, HEX );
if (channel1.length() == 1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel1.length() == 2)
Serial.print(zeroChar);
Serial.print(channel1);
Serial.print(" ");
if (channel2.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel2.length() ==2)
Serial.print(zeroChar);
Serial.print(channel2);
Serial.print(" ");
if (channel3.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel3.length() ==2)
Serial.print(zeroChar);
Serial.print(channel3);
Serial.print(" ");
if (channel4.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel4.length() ==2)
Serial.print(zeroChar);
Serial.print(channel4);
Serial.print(" ");
}
void runLED() {
if ( (timeX>=0) && (timeX<100) )
digitalWrite(ledPinPCM7, HIGH);
else if ( (timeX>=100) && (timeX<200) )
digitalWrite(ledPinPCM7, LOW);
if ( (timeX>=100) && (timeX<200) )
digitalWrite(ledPinPCM8, HIGH);
else if ( (timeX>=200) && (timeX<300) )
digitalWrite(ledPinPCM8, LOW);
if ( (timeX>=200) && (timeX<300) )
digitalWrite(ledPinPCM9, HIGH);
else if ( (timeX>=300) && (timeX<400) )
digitalWrite(ledPinPCM9, LOW);
if ( (timeX>=300) && (timeX<400) )
digitalWrite(ledPinPCM10, HIGH);
else if ( (timeX>=400) && (timeX<500) )
digitalWrite(ledPinPCM10, LOW);
if ( (timeX>=400) && (timeX<500) )
digitalWrite(ledPinPCM11, HIGH);
else if ( (timeX>=500) && (timeX<600) )
digitalWrite(ledPinPCM11, LOW);
if ( (timeX>=500) && (timeX<600) )
digitalWrite(ledPinPCM12, HIGH);
else if ( (timeX>=0) && (timeX<100) )
digitalWrite(ledPinPCM12, LOW);
timeX = timeX+1;
if ( timeX > 600 ) timeX=0;
}
void setSampleSpeed(int sampleN) {
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
switch (sampleN) {
case 120:
OCR1A = 16665.7;
break;
case 240:
OCR1A = 8332.3;
break;
case 480:
OCR1A = 4165.7;
break;
case 128:
OCR1A = 15624; break;
case 256:
OCR1A = 7811.5;
break;
case 512:
OCR1A = 3905.25;
break;
default:
OCR1A = 16665.7; }
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS11);
TIMSK1 |= (1 << OCIE1A);
sei();}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.