ATMEGA168A - Using timer - c

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;
}

Related

Best way to handle multiple PCINT in AVR

I'm testing some things on a Attiny85 and thought about the best way to handle the interrupt rutine. I know it is bad to have a lot of code in the interrupt handler, but I'm uncertain of any other ways to do this. I want my main program to sleep and wake on PCINT, the PCINT comes from multiple pins (rotary encoder A, b & switch and a receiving UART) so I was thinking just having a lot of code in the handler.
The code to determining which pin caused the interrupt, would look like this
#include <avr/io.h>
#include <stdint.h> // has to be added to use uint8_t
#include <avr/interrupt.h> // Needed to use interrupts
volatile uint8_t portbhistory = 0xFF; // default is high because the pull-up
int main(void)
{
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // Clear the PB0, PB1, PB2 pin
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
// PB0, PB1 and PB2 are now inputs with pull-up enabled
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
/*main program loop here */
}
}
ISR (PCINT0_vect)
{
uint8_t changedbits;
changedbits = PINB ^ portbhistory;
portbhistory = PINB;
if(changedbits & (1 << PB0))
{
/* PCINT0 changed */
}
if(changedbits & (1 << PB1))
{
/* PCINT1 changed */
}
if(changedbits & (1 << PB2))
{
/* PCINT2 changed */
}
}
And then ofc inside each of the if-statements in the interrupt handler, there would be code handling something, like this code, turning on the Timer0
TCNT0 = 0; // Set counter to 0
OCR0A = SERIAL_BIT_TIME; // Call timer interrupt in middle of first bit
position = 0; // Reset position and data
TIMSK |= 1 << OCIE0A; // Enable interrupt for compare register A (timer interrupt)
TIFR |= 1 << OCF0A; // Clear timer interrupt flag to prevent it jumping directly there
PCMSK &= ~(1 << SERIAL_RECEIVE); // Disable pin change interrupt
or with the switch input, the code inside the if-statement would be
if (lightState)
{
dali.transmit(ADDRESS, OFF);
lightState = 0;
}
else
{
dali.transmit(ADDRESS, ON);
lightState = 1;
}
Would this be a dumb solution?
volatile uint8_t flag;
int main(void)
{
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // Clear the PB0, PB1, PB2 pin
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
// PB0, PB1 and PB2 are now inputs with pull-up enabled
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
gotosleep();
do
{
switch(flag)
{
case 1:
dosomething1();
break;
case 2:
dosomething2();
break;
case 3:
dosomething3();
break;
}
cli();
flag = 0;
sei();
}while(flag);
}
}
ISR (PCINT0_vect)
{
uint8_t changedbits;
changedbits = PINB ^ portbhistory;
portbhistory = PINB;
if(changedbits & (1 << PB0))
{
flag = 1;
}
if(changedbits & (1 << PB1))
{
flag = 2;
}
if(changedbits & (1 << PB2))
{
flag = 3;
}
}

Why is my Atmega644 TIMER0 running at half the speed of the expected clock speed?

I am trying to build a drum machine using an Atmega644. The drum machine will have 16 LED's which will show which 16th of the beat it is at. The user should be able to set the BPM and therefore the updating of the LED will be different for different BPM.
I have a 16 MHz Crystal set at XTAL1 and XTAL2. I used a FUSE calculator to write the FUSE bits to a "Full Swing Oscillator" and remove the internal clock divide 8. I then set up the TIMER0 to perform this task
#define F_CPU 16000000UL // 1MHz internal clock
unsigned int tempo = 250; // Default BPM of 120.
volatile unsigned int seq_timer = 0;
//set-up timer for 0.5 ms update
TIMSK0 = (1<<OCIE0A);
TCCR0A = (1 << WGM01) |(1<<WGM00);
TCCR0B = (0 << WGM02) | (0<< CS02) | (1 << CS01) | (1 << CS00); //divide by 64
OCR0A = 125;
ISR (TIMER0_COMPA_vect) {
if (seq_timer>0) {seq_timer--;}
}
//Update which 16th note we are at.
void step(void){
update_led();
step_number = step_number < 15 ? step_number +1 : 0;
}
//Shift the LED such that the LED corresponding to step_number is lit up.
void update_led(void) {
for (int j = 16; j>=0; j--)
{
if ((((1 << step_number))>>j) & 0x01) {
PORTD |= (1 << PD5); //Serial Input
}
else {
PORTD &= ~(1 << PD5);
}
PORTD |= (1 << PD4); //Read clock
PORTD &= ~(1<< PD4);
}
PORTD |= (1 << PD3); //Shift clock
_delay_ms(40);
PORTD &= ~(1 << PD3);
}
while (1)
{
if ((seq_timer == 0)) {
seq_timer = tempo;
step();
}
Step functions calls a "update_led()" function which light up the LED corresponding to the step_number. This should according to my calculations occur (16e6 / (64*250*125)) = 8 updates/sec. But instead it does 4 updates/sec.
Am I missing something crucial?

16-bit Timer PWM LED Dimmer

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.

Variable being reset upon completion of function C

I am trying to initiate a timer with CTC and external interrupts that will count to 60 seconds and use a function to display the output to LEDs on an ATmega128. The function, as follows, will count all the way to the first iteration of the tens place when debugging, but on the next run-through the tens variable is reset to zero and I cannot understand why it is resetting. Can anyone see where I might have made an error?
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>
volatile uint8_t passval = 0;
void display(uint8_t value);
int main(void)
{
DDRC = 0xFF; //PORTC output for two seven segment displays(tenths and ones place)
DDRD = 0x0F; //PORTD output for one seven segment display(tens place)
EICRA = ((1 << ISC01) | (1 << ISC00) | (1 << ISC11) | (1 << ISC10)); //Interrupt set to trigger on rising edge for PINA1...0
TCCR1B |= (1 << WGM12); //Sets CTC Mode
OCR1A = 24999; //Prescaler as 64 with a count of 25000
TCNT1 = 0; //Clears clock
TIMSK |= (1 << OCIE1A); //Enables interrupt
sei(); //Enable Global Interrupt
while (1)
{
TCCR1B |= ((1 << CS11) | (1 << CS10));
display(passval);
}
return 0;
}
void display(uint8_t value)
{
static uint8_t tenths, ones, tens;
tenths = value;
if (tenths > 0x09) //Rolls Over tenths place at 10
{
passval = 0x00;
tenths = 0x00;
ones++; //Advances ones place
if (ones > 0x09) //Rolls over ones places at 10
{
ones = 0x00;
tens++; //Advances tens place
if (tens > 0x06) //Rolls over tens place at 6
{
tens = 0x00;
}
}
}
PORTC = ((ones << 4) + tenths); //Display Output
PORTD = tens;
}
ISR (TIMER1_COMPA_vect) //Timer ISR
{
passval++; //Advance tenths place
}

How to read 32 bits variable from Software SPI in ATmega32?

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.

Resources