timers and comparators

Hi, so quickly I have this task to do on Arduino and proteus:-

1 - Use an ultrasound Sensor to detect the distance between the vehicle and other objects

2 - Recognize the distance as a function of the time between the transmitter and receiver.

3 - Show the distance on LCD

4 - Scale the distance to a digital value between 0 (MAX) and 255 (MIN)

5 - Show this digital value on a PORT connected to LEDs

6 - Use this digital value as a comparator of a Timer 0, CTC mode to change the frequency of the output a wave on pin OCR0

7 - Use this digital value as a comparator of a Timer 2, Fast PWM mode to change the width of its output wave on pin OCR2

8 - Use an Oscilloscope to show the outputs

I finished the first 5 points but I really don't understand number 6 and 7 I can't find a code thx

(deleted)

spycatcher2k:
You don't 'find code', you write code. What does your teacher say they mean?

will sorry I am new to Arduino and the teacher isn't helping at all and don't want to answer

Marwan22:
will sorry I am new to Arduino and the teacher isn't helping at all and don't want to answer

So, either they are not doing their job, or you weren’t paying attention in lectures....

Edit: try chapter 15
Have a stab, post your code and we’ll help.

pcbbc:
So, either they are not doing their job, or you weren’t paying attention in lectures....

Edit: try chapter 15
Have a stab, post your code and we’ll help.

the subject is not even about Arduino he wants us to do this project so we can pass due to coronavirus but thx for your help

Marwan22:
the subject is not even about Arduino he wants us to do this project so we can pass due to coronavirus but thx for your help

So just presented you with the end of term assignment, no coursework or lecture notes at all?

ATmega328P is not about Arduino? Look at page 115, mode 2. What do you see?

pcbbc:
So just presented you with the end of term assignment, no coursework or lecture notes at all?

ATmega328P is not about Arduino? Look at page 115, mode 2. What do you see?

I know about registers and I know that atmega328P is the microcontroller in the Arduino but I didn't know that I can use AVR code in Arduino IDE I will try thx

WGM01 and WGM00 bits are located in TCCR0A. You can access TCCR0A in the IDE just like a variable.

TCCR0A = ......;

Maybe you need to set some of the other bits as well to meet your assignment objectives.
WGM02 is in TCCR0B. You can read and write to that as well.
Now at least you have the information necessary to set timer 0 CTC Mode (mode 2).

pcbbc:
WGM01 and WGM00 bits are located in TCCR0A. You can access TCCR0A in the IDE just like a variable.

TCCR0A = ......;

Maybe you need to set some of the other bits as well to meet your assignment objectives.
WGM02 is in TCCR0B. You can read and write to that as well.
Now at least you have the information necessary to set timer 0 CTC Mode (mode 2).

can I message u privately if u don't mind?

Marwan22:
can I message u privately if u don't mind?

I am not the person you addressed that question to

However may I request that you to continue with your questions in the Forum so that other people can learn along with you.

...R

Robin2:
I am not the person you addressed that question to

However may I request that you to continue with your questions in the Forum so that other people can learn along with you.

...R

ok so this is the code I wrote for CTC timer 0 and 256 Prescaler

#define myOutputPin 13
int x;
int toggle = 0;
void setup() {
// put your setup code here, to run once:
pinMode(myOutputPin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(myOutputPin,HIGH);
TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();
}
}
void TODELAY(){
TCCR0A=0;
TCCR0B=0;
TCNT0=0;
OCR0A=x;
TCCR0A |=(1<<COM0A0)|(1<<WGM01);
TCCR0B |=(1<<CS02);
}

I am taking x value from the ultrasonic after converting it to degital

Please post code that actually compiles without errors. Also please use ctrl-T to format your code, before posting it using code tags as requested in the sticky posts at the top of the forum, like this:

#define myOutputPin 13
int x;
int toggle = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(myOutputPin,OUTPUT);
}


void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(myOutputPin,HIGH);
TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();
}
}
void TODELAY(){
 TCCR0A=0;
 TCCR0B=0;
 TCNT0=0;
 OCR0A=x;
 TCCR0A |=(1<<COM0A0)|(1<<WGM01);
 TCCR0B |=(1<<CS02);
}

aarg:
Please post code that actually compiles without errors. Also please use ctrl-T to format your code, before posting it using code tags as requested in the sticky posts at the top of the forum, like this:

#define myOutputPin 13

int x;
int toggle = 0;
void setup() {
 // put your setup code here, to run once:
pinMode(myOutputPin,OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
digitalWrite(myOutputPin,HIGH);
TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();
}
}
void TODELAY(){
TCCR0A=0;
TCCR0B=0;
TCNT0=0;
OCR0A=x;
TCCR0A |=(1<<COM0A0)|(1<<WGM01);
TCCR0B |=(1<<CS02);
}

ok so here is the full code

#include "LiquidCrystal.h"  //lcd libary    
#define myOutputPin 13                                
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);   //LCD object Parameters: (rs, enable, d4, d5, d6, d7)
int ledPins[] = {0,1,2,3,4,5,6,7};
int i;
const int trigPin = 12; //trig pin connection
const int echoPin = 11;  //echopin connection
long duration;
int distanceCm;
float liquid;
int x;
int toggle = 0;                                                                                                    
void setup() {      // setup perameter
pinMode(8, OUTPUT);
lcd.begin(16,2);                                                  
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(0,0);
lcd.print("  Distance    ");
lcd.setCursor(0,1);
lcd.print("  Measurement  ");
delay(2000);
lcd.clear();
for(int i = 0; i < 8; i++){        
pinMode(ledPins[i],OUTPUT);
}
pinMode(myOutputPin,OUTPUT);
}
void loop() {   // loop of flow program
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;                                                                                
lcd.setCursor(0,0);                                                
lcd.print("Distance Measur.");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distanceCm);
lcd.print(" Cm ");
delay(10);
x=map(distanceCm,3,422,0,255);
showBinNumber(x);
delay(10);

digitalWrite(myOutputPin,HIGH);
TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();
}

void showBinNumber(int num) {
for (int i=0; i<8; i++) {
  if (num%2)
    digitalWrite(ledPins[i], LOW);
  else
    digitalWrite(ledPins[i], HIGH);
  num/=2;
}
}
void TODELAY(){
TCCR0A=0;
TCCR0B=0;
TCNT0=0;
OCR0A=x;
TCCR0A |=(1<<COM0A0)|(1<<WGM01);
TCCR0B |=(1<<CS02);
}

Are you expecting TODELAY() to implement a delay when called? It won't do that - all it does is set a lot of registers and immediately returns.

aarg:
Are you expecting TODELAY() to implement a delay when called? It won't do that - all it does is set a lot of registers and immediately returns.

didn't know that I will try again thanx

if there is a video or something can help me I will appreciate it

Robin2:
I am not the person you addressed that question to

However may I request that you to continue with your questions in the Forum so that other people can learn along with you.

Robin, you’re a mindreader.

Conversations about problems on the forums only. Keeping things private helps only the individual and no benefit to the community. Plus I’m not available 24x7, but the forums are. So you’ll get a quicker reply.

Videos will not help you. They are probably the reason you are in trouble, because they are so often inept and misleading. Go to the original documentation for everything. This basic literacy is a fundamental requirement of microprocessor work. Spend an adequate length of time reading it. Nick Gammon's pages have great docs on timer code.

6 - Use this digital value as a comparator of a Timer 0, CTC mode to change the frequency of the output a wave on pin OCR0

There is no pin OCR0. There is OCR0A and OCR0B. If the professor gave you this there is a fundamental problem. The only CTC mode for Timer0 is CTC to OCR0A.

digitalWrite(myOutputPin,HIGH);
TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();
void TODELAY(){
TCCR0A=0;
TCCR0B=0;
TCNT0=0;
OCR0A=x;
TCCR0A |=(1<<COM0A0)|(1<<WGM01);
TCCR0B |=(1<<CS02);
}

You have posted code using delayMicroseconds() and pulseIn() which uses microsecond values from micros(). Timer0 controls those, and its setup uses FastPWM to 255 and a prescaler of 64. You can not expect to change the mode and prescaler and have the basic timing continue to work.

To fulfill your assignment you may need to convert millis() and micros() to use Timer1, and then you can modify Timer0 to the mode CTC to OCR0A.

If the assignment allows you to keep Timer0 in FastPWM to 255, you can use an output compare value on output A or B to generate an output which you can vary with the ultrasonic value.

cattledog:
There is no pin OCR0. There is OCR0A and OCR0B. If the professor gave you this there is a fundamental problem. The only CTC mode for Timer0 is CTC to OCR0A.

digitalWrite(myOutputPin,HIGH);

TODELAY();
digitalWrite(myOutputPin,LOW);
TODELAY();






void TODELAY(){
TCCR0A=0;
TCCR0B=0;
TCNT0=0;
OCR0A=x;
TCCR0A |=(1<<COM0A0)|(1<<WGM01);
TCCR0B |=(1<<CS02);
}




You have posted code using delayMicroseconds() and pulseIn() which uses microsecond values from micros(). Timer0 controls those, and its setup uses FastPWM to 255 and a prescaler of 64. You can not expect to change the mode and prescaler and have the basic timing continue to work.

To fulfill your assignment you may need to convert millis() and micros() to use Timer1, and then you can modify Timer0 to the mode CTC to OCR0A.

If the assignment allows you to keep Timer0 in FastPWM to 255, you can use an output compare value on output A or B to generate an output which you can vary with the ultrasonic value.

So here is my new code

#include "LiquidCrystal.h"  //lcd libary  
#define myOutputPin 8                                        
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);   //LCD object Parameters: (rs, enable, d4, d5, d6, d7)
int ledPins[] = {0,1,2,3,4,5,6,7};
int i;
const int trigPin = 12; //trig pin connection 
const int echoPin = 11;  //echopin connection 
long duration;
int distanceCm;
float liquid;
int x;
int toggle0 = 0;
                                                                                                          
void setup() {      // setup perameter
lcd.begin(16,2);                                                   
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(0,0);
lcd.print("  Distance    ");
lcd.setCursor(0,1);
lcd.print("  Measurement  ");
delay(2000);
lcd.clear();
for(int i = 0; i <= 7; i++){        
pinMode(ledPins[i],OUTPUT);
}
pinMode(myOutputPin,OUTPUT);
cli();//stop interrupts
 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 = x;// = (16*10^6) / (2000*64) - 1 (must be <256)
  // turn on CTC mode
 TCCR0A |= (1 << WGM01);
  // Set CS01 and CS00 bits for 256 prescaler
 TCCR0B |= (1 << CS02) ;   
  // enable timer compare interrupt
 TIMSK0 |= (1 << OCIE0A);
 sei();//allow interrupts
}
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(myOutputPin,HIGH);
    toggle0 = 0;
  }
  else{
    digitalWrite(myOutputPin,LOW);
    toggle0 = 1;
  }
}

void loop() {   // loop of flow program
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;                                                                                 
delay(10);
lcd.setCursor(0,0);
lcd.print("Distance:");
lcd.print(distanceCm);
lcd.print(" Cm ");
delay(10);
int x=map(distanceCm,3,422,0,255);

    showBinNumber(x);
    delay(10);
  }

void showBinNumber(int num) {
  for (int i=0; i<8; i++) {
    if (num%2)
      digitalWrite(ledPins[i], LOW);
    else
      digitalWrite(ledPins[i], HIGH);
    num/=2;
  } 
}

Does the ultrasonic sensor give your correct distances with the timer set up like that?