I am trying to get PWM output from PB7 pin with timer0 on Atmega2560 with no luck. It should generate tone for the connected repro.My PWM settings are:
DDRB = 0b11100000;
PORTB = 0b00000000;
OCR0A = 0x04;
TCCR0A = (0 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (0 << WGM00);
TCCR0B = (0 << WGM02) | (0 << CS02) | (0 << CS01) | (0 << CS00);
and then I have this function, which should generate the sound:
void SoundOutput(unsigned int uintTone)
{
if (uintTone != 0)
{
LED_2(1);
OCR0A = uintTone;
TCCR0B |= (1 << CS02) | (1 << CS01) | (1 << CS00);
}
else
{
TCCR0B &= ~((1 << CS02) | (1 << CS01) | (1 << CS00));
}
}
but nothing happens when i call it with tone parameter. Can you please help me?
Based on your comments, you are using ~12MHz clock as the input to your timer, and from your code, you are using 8 bit timer 0 in CTC mode with OCR0A as your top. You have OC0A set to toggle on a compare match.
According to the 2560 datasheet, the frequency of your timer is given by:
F_CLK/(2*(1 + OCR0A)) | F_CLK ~= 12MHz
This is an 8 bit timer, so that means that your minimum frequency that your PWM can generate is given by:
12e6/(2*(1 + 255)) ~= 20KHz
You simply aren't going to be able to generate an audible tone with that configuration unless you slow down the clock you are using for your timer or use a timer that can count higher.
1) Use a 16 bit counter (i.e. timer1). That will give you a min frequency of ~90Hz and a max frequency of ~6MHz, which should give you plenty of range to generate tones:
/* WGM BITS = 0100: CTC Mode */
/* COMA BITS = 01: Toggle OC1A on compare match */
/* CS BITS = 111: External clock source on rising edge */
TCCR1A = (0 << COM1A1) | (1 << COM1A0) | (0 << WGM01) | (0 << WGM00);
TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS02) | (1 << CS01) | (1 << CS00);
2) Use the internal clock source as the timer clock instead of an external source. Unless you changed fuse bits or you are changing it in the code somewhere, the clock will be 1MHz. Prescaling the clock by 8 gives you a frequency range of ~250Hz - ~60KHz.
/* WGM BITS = 010: CTC Mode */
/* COMA BITS = 01: Toggle OC1A on compare match */
/* CS BITS = 010: Prescale the internal clock by 8 */
TCCR0A = (0 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (0 << WGM00);
TCCR0B = (0 << WGM02) | (0 << CS02) | (1 << CS01) | (0 << CS00);
Related
I'm just starting out. Have a little patience for my questions.
I came across a listing of a program where the following statement appears:
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2));
My guess:
considering that at startup the microcontroller has by default the DDRB bits all set to 0, I get:
DDRB & = ~ ((1 << 0) | (1 << 0) | (1 << 0)); ===>> DDRB & = ~ ((1) | (1) | (1));
which is different from the author's cooment. Where am I wrong?
2 # question: recital
volatile uint8_t portbhistory = 0xFF;
changedbits = PINB ^ portbhistory;
the second statement is not equal to:
changedbits = ~ PINB;
what changes? thanks to those who want to answer me.
No it is DDRB & = ~ ((1 << 0) | (1 << 1) | (1 << 2));
DDB0, DDB1, DDB2 are bit numbers defined in header file.
#define DDB0 0
#define DDB1 1
/* ... actual values may be different */
Statement:
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2));
zeroes DDB0, DDB1 & DDB2 bits.
DDRB |= (1 << DDB0) | (1 << DDB1) | (1 << DDB2);
sets DDB0, DDB1 & DDB2 bits.
DDRB ^= (1 << DDB0) | (1 << DDB1) | (1 << DDB2);
toggles DDB0, DDB1 & DDB2 bits (sets their value to the opposite one).
I am trying to toggle an LED with a 1024 prescaler, CTC mode 4.
The LED comes on after a second and stays on.
What am I doing wrong?
#include "Arduino.h"
void setup() {
//PORTB ^= (0 << PB7);
DDRB = (1 << PB7);
TCNT1 = 0;
OCR1C = 100;
TCCR1A = 0; // thought maybe arduino was setting other bits?
TCCR1B = 0; //
TCCR1A = (0 << COM1C1) | (1 << COM1C0) | (0 << WGM11) | (0 << WGM10);
TCCR1B = (0 << WGM13) | (1 << WGM12) | (1 << CS12) | (0 << CS11) | (1 << CS10);
}
void loop() {}
UPDATE
#include "Arduino.h"
void setup() {
//PORTB ^= (0 << PB7);
DDRB = (1 << PB7);
TCNT1 = 0;
TCCR1A = 0; //
TCCR1B = 0; //
OCR1A = 100; // OCR1A is the CTC mode 4 top
TCCR1A = (0 << COM1C1) | (1 << COM1C0) | (0 << WGM11) | (0 << WGM10);
TCCR1B = (0 << WGM13) | (1 << WGM12) | (1 << CS12) | (0 << CS11) | (1 << CS10);
}
void loop() {}
Set OCR1A after clearing the TCCR1A and TCCR1B
As Mike said OCR1A is top for mode 4
I am new to AVR C Programming, I am testing a simple PWM using 16-bit timer on Atmega328p Counter/Timers which is suppose to act as a dimmer to an LED.
My Code:
#define F_CPU 16000000UL
void initTimer();
int x = 1;
int n = 1000;
int main(void)
{
initTimer();
DDRB |= (1 << PB1)| (1 << PB2);
while(1)
{
x++;
if(x > 65) {
x = 1;
}
}
}
void initTimer() {
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
// used for TOP, makes for 50 Hz PWM
ICR1 = 40000;
OCR1A = n * x;
}
ISR(TIMER1_OVF_vect)
{
OCR1A = n * x;
}
Problem is that it doesn't display the dimming effect, the brightness of the LED stays constant as to whatever value I set for OCR1A (PB1) output pin initially, its suppose to change value as the interrupt happen but its just not doing this, this is suppose to be simple test what am I doing wrong?
Update:
As advice I enable the interrupts using the TIMSK1 register and SEI(), however still the same issue the LED brightness stays constant as to whatever the original value of OCR1A that was specified in the initTimer().
int main(void)
{
initTimer();
DDRB |= (1 << PB1)| (1 << PB2);
while(1)
{
x++;
if(x > 65) {
x = 1;
}
}
}
void initTimer() {
ICR1 = 40000;
OCR1A = n * x;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TIMSK1 |= (1 << ICIE1);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
sei();
}
ISR (TIMER1_COMPA_vect)
{
OCR1A = n * x;
}
Although I tried an alternative approach and that gives me the dimming affect:
int main(void)
{
initTimer();
DDRB |= (1 << PB1)| (1 << PB2);
while(1)
{
_delay_ms(20);
OCR1A = n * 4;
_delay_ms(20);
OCR1A = n * 8;
_delay_ms(20);
OCR1A = n * 15;
_delay_ms(20);
OCR1A = n * 25;
_delay_ms(20);
OCR1A = n * 1;
}
}
void initTimer() {
ICR1 = 40000;
OCR1A = n * x;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
}
So it seems the problem is with the interrupts since PWM affect works but its just not working with the interrupt handler.
The first thing that jumps out at me is x and n should be volatile. You also should enable the interrupt in TIMSK0 register. Also enable interrupts by calling sei.
If I were you, I'd start with some know good sample code. The page I mentioned has a example that fires an interrupt every 4ms. Take that code and toggle the led on and off.
Another problem is you are changing x without regard to whether or not the isr was called. So in effect you will get a random x each time in the isr. This code is simple enough it might get stuck in a simple pattern. Instead move the setting of x to your isr.
Here is a good intro to avr timers: https://sites.google.com/site/qeewiki/books/avr-guide/timers-on-the-atmega328
ICR1 = 40000;
OCR1A = n * x;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
This is wrong, you need to configure TCCR1A and TCCR1B before initializing ICR1 and OCR1A. See this answer for more details.
it is my first try to set up SPI communication on ATmega32 and MAX31855. I've to read 32 bits from MAX31855 datasheet. I have writen my own function but it seems to read 0 all time (I have checked it on display). Here are my definitions from *.h file:
#ifndef SPI_SOFTWARE_H_
#define SPI_SOFTWARE_H_
#define ilosc_urzadzen 1
#define SoftSPI_MOSI PD0
#define SoftSPI_MISO PD1
#define SoftSPI_SCK PD2
#define SoftSPI_CS1 PD3
#define SoftSPI_CS2 PD4
#define SoftSPI_CS3 PD5
#define SoftSPI_CS4 PD6
#define SoftSPI_CS5 PD7
#define SoftSPI_DDR DDRD
#define SoftSPI_PIN PIND
#define SoftSPI_PORT PORTD
void init_SoftSPI();
uint32_t odczyt32bit(int CSx);
#endif
I have connected MAX31855: SO -> PD1, SCK -> PD2, CS -> PD3.
And here is my code from *.c:
#include <avr/io.h>
#include "SPI_software.h"
void init_SoftSPI(){
SoftSPI_DDR |= (1 << SoftSPI_MOSI) | (1 << SoftSPI_SCK);
if(ilosc_urzadzen == 1) SoftSPI_DDR |= (1 << SoftSPI_CS1);
else if(ilosc_urzadzen == 2) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2);
else if(ilosc_urzadzen == 3) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3);
else if(ilosc_urzadzen == 4) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3) | (1 << SoftSPI_CS4);
else if(ilosc_urzadzen == 5) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3) | (1 << SoftSPI_CS4) | (1 << SoftSPI_CS5);
}
uint32_t odczyt32bit(int CSx){
uint32_t liczba = 0;
if (CSx == 1){
SoftSPI_PORT &= ~SoftSPI_CS1;
for (int i = 0; i < 32; i++)
{
SoftSPI_PORT |= SoftSPI_SCK;
if (SoftSPI_PIN & (1 << SoftSPI_MISO)) liczba += 1;
liczba = liczba << 1;
SoftSPI_PORT &= ~SoftSPI_SCK;
}
SoftSPI_PORT |= SoftSPI_CS1;
}
return liczba;
}
After reading value from MAX31855 I shift it (20 places no 18 becouse I dont need fraction part):
temp = odczyt32bit(1);
temp = temp >> 20;
Where is my problem?
The clocking timing looks okay, but note that you only need to clock out 14 cycles (or just 12 for less precision) to get the result. You can stop reading the rest of the results whenever you want. (You don't need to read all 32 bits if you aren't using them.) If you do read all the bits, then you can detect any fault codes that indicate the thermocouple is not connected correctly.
Are you giving enough time for the conversion to occur? It needs 100 ms between readings.
I am trying to understand how to use timers with my ATMEGA168A, however the example I have (link) doesn't seem to work since it returns 0 all the time.
My idea is to make a HC-SR04 (link) ultra sound sensor work.
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
long measure(){
//Setting up the timer
TCCR1B |= (1 << CS12) | (1 << CS11) | (1 << CS10);
//Setting trigger as output
DDRD |= (1 << PD1);
//Setting echo as input
PORTD |= (1 << PD2);
//Triggering the hardware
PORTD ^= (1 << PD1);
_delay_us(10);
PORTD ^= (1 << PD1);
//Waiting until echo goes low
TCNT1 = 0;
while(bit_is_clear(PIND, PD2));
long timer_value = TCNT1;
//Calculating and returning the distance
long distance = timer_value / 58.82;
return distance;
}
How can I successfully measure the amount of time the PD2 was high?
To measure the amount of time the PD2 was high, write some code to do so, compile, write it to your microcontroller and turn on it.
Not tested, try this:
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
long measure(){
//Setting up the timer
TCCR1B |= (1 << CS12) | (1 << CS11) | (1 << CS10);
//Setting trigger as output
DDRD |= (1 << PD1);
//Setting echo as input
PORTD |= (1 << PD2);
//Triggering the hardware
PORTD ^= (1 << PD1);
_delay_us(10);
PORTD ^= (1 << PD1);
//Waiting until echo goes low (after Initiate)
while(!bit_is_clear(PIND, PD2));
//Waiting until echo goes high (Echo back starts)
while(bit_is_clear(PIND, PD2));
TCNT1 = 0;
//Waiting until echo goes low (Echo back ends)
while(!bit_is_clear(PIND, PD2));
long timer_value = TCNT1;
//Calculating and returning the distance
long distance = timer_value / 58.82;
return distance;
}