Piezo buzzer with PWM on arduino uno with atmega328p in C on registers

Hello,

I have a little project at the university where I have to use the timers to generate a pwm signal and get it into a piezo buzzer when something else turns over a certain value by using the registers, the thing is that we have worked on some other microcontrollers so I don’t really know the libraries for arduino uno with atmega328p and I would require some help on that. Also I have to use the registers instead of the function analogRead() that is reading from a temperature sensor.

Having the following code that works for my setup:

#include <LiquidCrystal.h>         

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Digital pins to which you connect the LCD
const int inPin = 0; 
const int buzzer=9;// A0 is the sensor
void setup()
{
  pinMode(buzzer,OUTPUT); // DDRA=0b00000001;
  lcd.begin(16,2);
}
void loop()
{
  int value = analogRead(inPin); // read the value from the sensor <= to be written on registers
  lcd.setCursor(0,1);
  float millivolts = (value / 1024.0) * 5; 
  float celsius = (millivolts-0.5)*100;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(celsius);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print((celsius * 9)/5 + 32); //turning the celsius into fahrehait
  lcd.print("F");
  delay(1000);
  if(celsius>30.0){tone(buzzer,1000);delay(1000);noTone(buzzer);delay(1000);} //<= on registers
}

Why can't you use built-in functions? That is why they are present. Maybe a class requirement? Anyway, you have the source code for analogRead() since the entire IDE is open source.

Look at C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c

an you will see how it is done and then do the same

blh64:
Why can't you use built-in functions? That is why they are present. Maybe a class requirement? Anyway, you have the source code for analogRead() since the entire IDE is open source.

Look at C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c

an you will see how it is done and then do the same

I am a newbie to arduino and the requirement is to write the code on registers, because that's the subject about at my university, working on registers.

MRNicolas:
I am a newbie to arduino and the requirement is to write the code on registers, because that's the subject about at my university, working on registers.

Then it's reasonable to tell you to seek help from your course's book and/or your instructor first.