Hi. I have a code that generates sine wave that shoul have 2600Hz frequency. When I run this code in Keil i can't see sine wave on logic analyzer so I dont know the frequency of sine wave. how can i solve this problem? thanks for all!
The Code
#include <stdio.h> #include <ADuC841.h>
sbit LED = 0x0B4;
void timer0(void) interrupt 1 //interrupt no. 1 for Timer 0 { TH0=0xFC; TL0=0xFB; }
void main (void) {
unsigned code values[50][2]={{0x0F, 0x7F},{0x08, 0x11},{0x01, 0x80},{0x07, 0xBF},{0x0D, 0x87}, {0x08, 0x91},{0x03, 0x68},{0x07, 0x02},{0x0B, 0xB7},{0x09, 0x88}, {0x05, 0x19},{0x05, 0xD4},{0x0A, 0x2C},{0x0A, 0xE7},{0x06, 0x78}, {0x04, 0x49},{0x08, 0xFE},{0x0C, 0x98},{0x07, 0x6F},{0x02, 0x79}, {0x08, 0x41},{0x0E, 0x80},{0x07, 0xEF},{0x00, 0x81},{0x08, 0x01}, {0x00, 0x81},{0x07, 0xEF},{0x0E, 0x80},{0x08, 0x41},{0x02, 0x79}, {0x07, 0x6F},{0x0C, 0x98},{0x08, 0xFE},{0x04, 0x49},{0x06, 0x78}, {0x0A, 0xE7},{0x0A, 0x2C},{0x05, 0xD4},{0x05, 0x19},{0x09, 0x88}, {0x0B, 0xB7},{0x07, 0x02},{0x03, 0x68},{0x08, 0x91},{0x0D, 0x87}, {0x07, 0xBF},{0x01, 0x80},{0x08, 0x11},{0x0F, 0x7F},{0x07, 0xFF}};
DACCON = 0x0D;
DAC0H = 0x08; DAC0L = 0x00;
TMOD = 0x01; // mode1 of Timer0 TH0 = 0xFC; // initial values loaded to timer TL0 = 0xFB; IE = 0x82; // enable interrupt TR0 = 1; //start timer while(1) { int i; for ( i = 0 ; i < 50; i++) { DAC0H = values[i][0]; DAC0L = values[i][1]; } LED ^= 1; } }
Wouldn't the best solution be to use real hardware and an oscilloscope?
yeah but i dont have the kit. its a homework and we need to generate a sine wave using look up table. my frequency is 2600Hz. but i dont know the frequency of the code. so any information except this?
can you run this code for me
#define BIT0 0 #define BIT1 2 #define BIT2 3 #define BIT3 5 #define BIT4 6 #define BIT5 7 #define BIT6 9 #define BIT7 1 #define BIT8 4
#define LED_OFF P0=12 #define LED_ON P0=7
int main( void ) { P1 = 0x19;
int count=0;
TMR0 = (1<<BIT3)|(BIT2); // divide by 1024 TMR1 = 1<<BIT7; // enable timer interrupt
sei();
for(;;) { // main loop // single press /* if( get_key_press( 1<<KEY0 )) { if(count<=3) count++; } */ // release after short press: task 1 // long press: task 2
if( get_key_short( 1<<KEY0 )) { if(count<=3) count++; }
if( get_key_long( 1<<KEY0 )) { LED1_OFF; LED2_OFF; LED3_OFF; count = 1; }
switch(count) { case 1: LED1_OFF; LED2_OFF; LED3_OFF; break; case 2: LED2_OFF; LED3_OFF; LED1_ON; break; case 3: LED3_OFF; LED1_OFF; LED2_ON; break; case 4: LED1_OFF; LED2_OFF; LED3_ON; break; }
// single press and repeat /* if( get_key_press( 1<<KEY0 ) || get_key_rpt( 1<<KEY0 )){ uint8_t i = LED_PORT;
i = (i & 0x07) | ((i << 1) & 0xF0); if( i < 0xF0 ) i |= 0x08; count=0; } */ } }
it gives errors but i did not understand what this code about sorry im new in this thing
hi what errors do you get
is anyone here good to fix them.
It would really help if you guys would read the posting instructions so your source isn't a jumbled mess of lines.
Don't post off topic noise to an existing thread, start a new one.
The sine table looks awful. How exactly did you create this, and how does blasting values to the DAC use the timer to pace the output?
your answer is not the answer to my nquestion. so if you want to insult people please do this somewhere else. as answer to your question this is a homework and i choose 2600Hz frequency. i need to generate a sine wave that has 2600 Hz frequency using timer interrupt. not anything else. Im new in this thing and i might wrote the code wrong. if this, please correct me KINDLY.
this is the code that gives the sine table value. i did run it in matlab and got the values.
clear; close all; N=1000; frekans=2600; fo=10000; for i=1:1:N x(i)=2047*cos(2*pi*frekans*i/fo); end figure; plot(x); B=input('\nCikis dosyasinin ismi *.txt = ','s'); fid=fopen(B,'w'); for i=1:1:N if (x(i) < 0) x(i)=x(i)+4096; end xx= round(x(i)); if (xx>=4096) xx=xx-4096; end %fprintf(fid,'%i \n',xx); fprintf(fid,'%4x \n',xx); end fclose(fid);
and this is the code i wrote
#include <stdio.h> #include <ADuC841.h> sbit LED = 0x0B4; void timer0(void) interrupt 1 //interrupt no. 1 for Timer 0 { TH0=0xFC; TL0=0xFB; } void main (void) { unsigned code values[50][2]={{0x0F, 0x7F},{0x08, 0x11},{0x01, 0x80},{0x07, 0xBF},{0x0D, 0x87}, {0x08, 0x91},{0x03, 0x68},{0x07, 0x02},{0x0B, 0xB7},{0x09, 0x88}, {0x05, 0x19},{0x05, 0xD4}, {0x0A, 0x2C},{0x0A, 0xE7},{0x06, 0x78}, {0x04, 0x49},{0x08, 0xFE},{0x0C, 0x98},{0x07, 0x6F}, {0x02, 0x79}, {0x08, 0x41},{0x0E, 0x80},{0x07, 0xEF},{0x00, 0x81},{0x08, 0x01}, {0x00, 0x81}, {0x07, 0xEF},{0x0E, 0x80},{0x08, 0x41},{0x02, 0x79}, {0x07, 0x6F},{0x0C, 0x98},{0x08, 0xFE}, {0x04, 0x49},{0x06, 0x78}, {0x0A, 0xE7},{0x0A, 0x2C},{0x05, 0xD4},{0x05, 0x19},{0x09, 0x88}, {0x0B, 0xB7},{0x07, 0x02},{0x03, 0x68},{0x08, 0x91},{0x0D, 0x87}, {0x07, 0xBF},{0x01, 0x80}, {0x08, 0x11},{0x0F, 0x7F},{0x07, 0xFF}}; DACCON = 0x0D; DAC0H = 0x08; DAC0L = 0x00; TMOD = 0x01; // mode1 of Timer0 TH0 = 0xFC; // initial values loaded to timer TL0 = 0xFB; IE = 0x82; // enable interrupt TR0 = 1; //start timer while(1) { int i; for ( i = 0 ; i < 50; i++) { DAC0H = values[i][0]; DAC0L = values[i][1]; } LED ^= 1; } }
You might want to consider rewriting that bit so it looks more artful
Maybe:
#define ZERO 0 #define ONE 2 #define TWO 3 #define THREE 5 #define FOUR 6 #define FIVE 7 #define SIX 9 #define SEVEN 1 #define EIGHT 4
the question is an oxymoron
how can I see an analog signal on a logic analyzer
Erik
So the table has 50 points, or 1000? Still it looks broken.
Clue: Put ONE cycle of a sine wave in your 50 points, sweeping from 0 to 2PI radians in 2PI/50 increments, and at your desired DC offset and amplitude. The output samples at 130 KHz (7.692ms intervals). Or reappraise your number of points to 32 or 16 or something.
Sorry not interested in doing your homework.
why do people answer all things except the asked one?! As I say I need to do this using only "timer interrupt". I dont want you to do my homework I have already done. If you dont understand the code then dont write anything. And im asking how to see analog wave on logic analyzer. not anything about code.
A logic analyzer is a tool intended to view logical levels - high or low. See any issues there? Digital high and low isn't exactly the same thing as analog curve shapes.
That's why this world got oscilloscopes - they are intended to present analog curves.
Right now, you want to use a hammer for your screws. Or zippers to build your house. Doesn't work too well.
Next thing - you want to use timers? But you aren't. The thing with timers is that you either make use of the timer interrupt. Or you poll a timer flag. Your code just ignores the timer and throws in values into the DAC as fast as your loop can manage.
When you think people are giving the wrong answers, it just might be the reverse. People give good answers, but your assumptions are wrong. Think twice about that since most people who answer you have spent 10 or 100 or 1000 times more time than you writing software.
Probably the same reason tools ask questions about their software and comprehension problems on a IDE vendors site, and haven't read the documentation for the IDE, or the chip they're using?
You don't understand the problem you're attempting to solve. Your sine table is useless, there is nothing here relating to 2600 Hz, or sine waves, you're not using a timer interrupt to pace the output, you're just jamming bogus values out the DAC and hoping that the "logic analyzer" or "oscilloscope" view is going to give you some magic clarity to the issues. It's not because you don't have the first clue about the signals and periodicity, basics that transcend the CPU or IDE used.
Try plugging your sine table into Excel and have that plot the data points.