January 16, 2016
by lnino
|
Hey guys,
I have been using my own development board for the last years with my ATMega168. But in some Projects I Need more space for the code and therefore I am playing around with ATMega328.
When I use the the same code and Flash it to m168 the buzzer Sound is great and short. But when I burn the same code (with io_328p.h included) to my 328p the Sound is stretched and sounds like slow Motion.
Any ideas what can cause this behaviour?
Here is the code I am using:
#define F_CPU 14745600
#define D5 851
#define E5 758
#define Fsh5 675
#define G5 637
#define A5 568
#define B5 506
#define C6 477
#define D6 425
#define DUR 40
#define Csh6 450
#define Gsh5 601
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <inttypes.h>
#include <string.h>
#include <util/delay.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/io_328p.h"
#include "../mylibs/matrix_keypad_eval_brd.c"
// Global Variables
volatile uint8_t number;
volatile uint8_t okay_button;
volatile int32_t finish_count;
volatile int32_t level_count = 0;
volatile uint8_t lv1_var = 0;
// Prototypes
void play_tone(uint16_t, uint8_t);
void play_button_ton();
void play_level_succeed_ton();
void play_level_failed_ton();
void play_final_ton();
int main(void) {
// internal RC oscillator calibration for 8MHz.
OSCCAL = 176;
// Output
DDRC |= (1<<PC3); // Buzzer
init_matrix_keypad();
while(1)
{
// ##################################
// ##### Button Matrix Section ######
// ##################################
//COL1 auf Masse ziehen
SETBIT(COL1_PORT,COL1_BIT);
SETBIT(COL2_PORT,COL2_BIT);
SETBIT(COL3_PORT,COL3_BIT);
CLEARBIT(COL1_PORT,COL1_BIT);
asm( "nop" );
//prüfen, ob ROW 1,2,3,4 einen Low-Pegel aufweisen
if(!(CHECKBIT(ROW1_PIN,ROW1_BIT))) // When Button 1 pressed
{
play_button_ton();
_delay_ms(120);
number=1;
}
if(!(CHECKBIT(ROW2_PIN,ROW2_BIT))) // When Button 4 pressed
{
play_button_ton();
_delay_ms(120);
number=4;
}
if(!(CHECKBIT(ROW3_PIN,ROW3_BIT))) // When Button 7 pressed
{
play_button_ton();
_delay_ms(120);
number=7;
}
if(!(CHECKBIT(ROW4_PIN,ROW4_BIT))) // When Button * pressed
{
_delay_ms(120);
number=10;
}
//COL2 auf Masse ziehen
SETBIT(COL1_PORT,COL1_BIT);
SETBIT(COL2_PORT,COL2_BIT);
SETBIT(COL3_PORT,COL3_BIT);
CLEARBIT(COL2_PORT,COL2_BIT);
asm( "nop" );
//prüfen, ob PB 2,3,4 oder 5 einen Low-Pegel aufweisen
if(!(CHECKBIT(ROW1_PIN,ROW1_BIT))) // When Button 2 pressed
{
play_button_ton();
_delay_ms(120);
number=2;
}
if(!(CHECKBIT(ROW2_PIN,ROW2_BIT))) // When Button 5 pressed
{
play_button_ton();
_delay_ms(120);
number=5;
}
if(!(CHECKBIT(ROW3_PIN,ROW3_BIT))) // When Button 8 pressed
{
play_button_ton();
_delay_ms(120);
number=8;
}
if(!(CHECKBIT(ROW4_PIN,ROW4_BIT))) // When Button 0 pressed
{
play_button_ton();
_delay_ms(120);
number=11;
}
//COL3 auf Masse ziehen
SETBIT(COL1_PORT,COL1_BIT);
SETBIT(COL2_PORT,COL2_BIT);
SETBIT(COL3_PORT,COL3_BIT);
CLEARBIT(COL3_PORT,COL3_BIT);
asm( "nop" );
//prüfen, ob PB 2,3,4 oder 5 einen Low-Pegel aufweisen
if(!(CHECKBIT(ROW1_PIN,ROW1_BIT))) // When Button 3 pressed
{
play_button_ton();
_delay_ms(120);
number=3;
}
if(!(CHECKBIT(ROW2_PIN,ROW2_BIT))) // When Button 6 pressed
{
play_button_ton();
_delay_ms(120);
number=6;
}
if(!(CHECKBIT(ROW3_PIN,ROW3_BIT))) // When Button 9 pressed
{
play_button_ton();
_delay_ms(120);
number=9;
}
if(!(CHECKBIT(ROW4_PIN,ROW4_BIT))) // When Button # pressed
{
play_button_ton();
_delay_ms(120);
number=12;
}
return 0;
}
void play_tone(uint16_t delay, uint8_t duration) {
// delay is half-period in microseconds
// duration is in 10ms increments
// example: 440Hz --> delay=1136
// duration = 2*delay * cycles (all in same units)
// cycles = 10000 * duration / delay / 2
// cycles = 100 * duration / (delay/50)
uint16_t tmp = 100 * duration;
uint16_t delaysm = delay / 50;
uint16_t cycles = tmp / delaysm;
while(cycles > 0) {
PORTC |= (1<<PC3);
delay_us(delay);
PORTC &= ~(1<<PC3);
delay_us(delay);
cycles--;
}
}
void play_button_ton()
{
play_tone(C6, DUR/2);
}
void play_level_failed_ton()
{
play_tone(Fsh5, 3*DUR);
}
void play_level_succeed_ton()
{
play_tone(Csh6, DUR/2);
play_tone(B5, DUR/2);
play_tone(Csh6, DUR/2);
}
void play_final_ton()
{
play_tone(B5, DUR/2);
play_tone(A5, DUR/2);
play_tone(B5, DUR/2);
play_tone(A5, DUR/2);
play_tone(E5, DUR/2);
play_tone(B5, DUR/2);
play_tone(A5, DUR/2);
play_tone(B5, DUR/2);
play_tone(A5, DUR/2);
play_tone(E5, DUR/2);
}
And the make file 328p:
GCCFLAGS=-g -Os -Wall -mmcu=atmega328p
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m328p -b 115200 -P COM8
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o
all: lnino_eval_board-upload
lnino_eval_board.hex: lnino_eval_board.c
make -C ../libnerdkits
avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o lnino_eval_board.o lnino_eval_board.c ${LINKOBJECTS}
avr-objcopy -j .text -O ihex lnino_eval_board.o lnino_eval_board.hex
lnino_eval_board.ass: lnino_eval_board.hex
avr-objdump -S -d lnino_eval_board.o > lnino_eval_board.ass
lnino_eval_board-upload: lnino_eval_board.hex
avrdude ${AVRDUDEFLAGS} -U flash:w:lnino_eval_board.hex:a
And the make file for 168:
GCCFLAGS=-g -Os -Wall -mmcu=atmega168
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM8
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o
all: lnino_eval_board-upload
lnino_eval_board.hex: lnino_eval_board.c
make -C ../libnerdkits
avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o lnino_eval_board.o lnino_eval_board.c ${LINKOBJECTS}
avr-objcopy -j .text -O ihex lnino_eval_board.o lnino_eval_board.hex
lnino_eval_board.ass: lnino_eval_board.hex
avr-objdump -S -d lnino_eval_board.o > lnino_eval_board.ass
lnino_eval_board-upload: lnino_eval_board.hex
avrdude ${AVRDUDEFLAGS} -U flash:w:lnino_eval_board.hex:a
I am flashing the 168 bootloader like this:
avrdude -c usbasp -p m168 -e
cd\
cd bootloader168
make fuses
make install
I am flashing the 328 bootloader like that:
avrdude -c usbasp -p m328p -e
cd\
cd bootloader328P
make fuses
make install
|