Новичок
Профиль
Группа: Участник
Сообщений: 12
Регистрация: 14.11.2006
Репутация: нет Всего: нет
|
Итак, жалко, что никто так и не сказал где-же есть пример, ну да ладно, нашел сам, кстати он есть на http://www.atmel.ru/, а вот на http://www.atmel.com/ его как ни странно нету. Переписал его под CodeVision и для ATMEGA8535 вот что получилось: Код | Chip type : ATmega8535 Program type : Application Clock frequency : 8,000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 128 *****************************************************/
#include <mega8535.h> #include <delay.h>
#define Xtal 8000000 // system clock frequency #define prescaler 1 // timer1 prescaler #define N_samples 128 // Number of samples in lookup table #define Fck Xtal/prescaler // Timer1 working frequency #define delaycyc 10 // port B setup delay cycles /* Timer/Counter 1 Control Register */ #define COM1A1 7 #define PWM10 0 /* Timer/Counter 1 Control and Status Register */ #define CS10 0 /* Data Register, Port D */ #define PD5 5 //************************** global variables **************************** unsigned char x_SWa = 0x00; // step width of high frequency unsigned char x_SWb = 0x00; // step width of low frequency unsigned char OCR1A_set; unsigned int X_LUTaExt = 0; // position freq. A in LUT (extended format) unsigned int X_LUTbExt = 0; // position freq. B in LUT (extended format) unsigned int X_LUTa; // position freq. A in LUT (actual position) unsigned int X_LUTb; // position freq. B in LUT (actual position)
//************************** SIN TABLE ************************************* // Samples table : one period sampled on 128 samples and // quantized on 7 bit //************************************************************************** flash unsigned char auc_SinParam [128] = { 64,67,70,73,76,79,82,85,88,91,94,96,99,102,104,106,109,111,113,115,117,118,120,121,123,124,125,126, 126,127,127,127,127,127,127,127,126,126,125,124,123,121,120,118,117,115,113,111,109,106,104,102, 99,96,94,91,88,85,82,79,76,73,70,67,64,60,57,54,51,48,45,42,39,36,33,31,28,25,23,21,18,16,14,12,10,9, 7,6,4,3,2,1,1,0,0,0,0,0,0,0,1,1,2,3,4,6,7,9,10,12,14,16,18,21,23,25,28,31,33,36,39,42,45,48,51,54,57,60};
//*************************** x_SW *************************************** //Table of x_SW (excess 8): x_SW = ROUND(8*N_samples*f*510/Fck) //**************************************************************************
//high frequency (coloun) //1209hz ---> x_SW = 79 //1336hz ---> x_SW = 87 //1477hz ---> x_SW = 96 //1633hz ---> x_SW = 107
const unsigned char auc_frequencyH [4] = { 107,96,87,79};
//low frequency (row) //697hz ---> x_SW = 46 //770hz ---> x_SW = 50 //852hz ---> x_SW = 56 //941hz ---> x_SW = 61
const unsigned char auc_frequencyL [4] = { 61,56,50,46};
//************************************************************************** // Timer 1 overflow interrupt service routine //************************************************************************** interrupt [TIM1_OVF] void timer1_ovf_isr(void) // Ïðåðûâàíèå ïî ïåðåïîëíåíèþ òàéìåðà 0 { // move Pointer about step width aheaed X_LUTaExt += x_SWa; X_LUTbExt += x_SWb; // normalize Temp-Pointer X_LUTa = (char)(((X_LUTaExt+4) >> 3)&(0x007F)); X_LUTb = (char)(((X_LUTbExt+4) >> 3)&(0x007F)); // calculate PWM value: high frequency value + 3/4 low frequency value OCR1A_set = (auc_SinParam[X_LUTa] + (auc_SinParam[X_LUTb]-(auc_SinParam[X_LUTb]>>2))); OCR1A = OCR1A_set; }
//************************************************************************** // Initialization //************************************************************************** void init (void) { // Timer/Counter 1 initialization TIMSK = 0x04; // Int T1 Overflow enabled TCCR1A = (1<<COM1A1)+(1<<PWM10); // non inverting / 8Bit PWM TCCR1B = (1<<CS10); // CLK/1 // DDRD = (1 << PD5); // PD5 (OC1A) as output #asm("sei"); // Interrupts enabled
}
//************************************************************************** // Time delay to ensure a correct setting of the pins of Port B //************************************************************************** void Delay (void) // { int i; for (i = 0; i < delaycyc; i++) #asm("nop"); }
//************************************************************************** // MAIN // Read from portB (eg: using evaluation board switch) which // tone to generate, extract mixing high frequency // (column) and low frequency (row), and then // fix x_SWa and x_SWb // row -> PINB high nibble // column -> PINB low nibble // row -> PINB high nibble // column -> PINB low nibble //************************************************************************** void main (void) { unsigned char uc_Input; unsigned char uc_Counter = 0; // Declare your local variables here PORTA=0x00; DDRA=0xFF; PORTB=0x00; DDRB=0x00; PORTC=0x00; DDRC=0x00; PORTD=0xFF; DDRD=0xFF;
init(); delay_ms(1000); for(;;){ // high nibble - rows DDRB = 0x0F; // high nibble input / low nibble output / PORTB = 0xF0; // high nibble pull up / low nibble low value uc_Counter = 0; Delay(); // wait for Port B lines to be set up correctly uc_Input = PINB; // read Port B do { if(!(uc_Input & 0x80)) // check if MSB is low { // if yes get step width and end loop x_SWb = auc_frequencyL[uc_Counter]; uc_Counter = 4; } else { x_SWb = 0; // no frequency modulation needed } uc_Counter++; uc_Input = uc_Input << 1; // shift Bits one left } while ((uc_Counter < 4)); // low nibble - columns DDRB = 0xF0; // high nibble output / low nibble input PORTB = 0x0F; // high nibble low value / low nibble pull up uc_Counter = 0; Delay(); // wait for Port B lines to be set up correctly uc_Input = PINB; uc_Input = uc_Input << 4; do { if(!(uc_Input & 0x80)) // check if MSB is low { // if yes get delay and end loop x_SWa = auc_frequencyH[uc_Counter]; uc_Counter = 4; } else { x_SWa = 0; } uc_Counter++; uc_Input = uc_Input << 1; } while (uc_Counter < 4); } }
|
Сообственно динамик подключаем к порту Д-5, на порт В вешаем клавиатуру, ну или просто замыкаем нужные ножки. DTMF генерит, но!!! Когда ничего не замкнуто на клавиатуре на порту D-5 присутствует какая-то паразитная генережка! Она-же присутствует и при самой генерации DTMF. Ктонибудь может объяснить откуда она берется? Замыканий на плате и других дефектов нет, т.к. генережка исчезает если заремарить строку OCR1A = OCR1A_set; Плиз, HELP
|