Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I think your getting confused as we changed both the crystal frequency and the output frequency.3dluvr said:Right, I double the prescaler to fit the 20MHz frequency, but then with 10MHz crystal the values are still wrong though, why is that?
It worked fine with 8MHz, and it seems 20MHz is dead on, yet with 10MHz it's all off?
And what about the LED flashing part, is that still correct for the 64 value sine table?
#include <16F88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=10000000)
int timeCount;
long TempA, TempB, hundreths;
long long step, step2, position, position2;
CONST unsigned int SineTab[64] = {
7,8,8,9,10,10,11,11
12,12,13,13,13,14,14,14
14,14,14,14,13,13,13,12
12,11,11,10,10,9,8,8
7,6,6,5,4,4,3,3
2,2,1,1,1,0,0,0
0,0,0,0,1,1,1,2
2,3,3,4,4,5,6,6};
#INT_TIMER2
void interrupt(){
position+=step;
TempB=SineTab[(position>>16)&63];
output_b(TempB);
position2+=step2;
TempA=SineTab[(position2>>16)&63];
output_a(TempA);
if((TempA&8)!=(TempB&8)){
output_bit(PIN_A4, 1);
}
else{
output_bit(PIN_A4, 0);
}
if(timeCount++ == 50){
timeCount=0;
hundreths++;
}
}
void PlayTone(long long Freq1, long long Freq2, long Duration) {
signed long long work;
hundreths=0;
While(hundreths<Duration) {
if (Freq2 >= Freq1) { // ramp up
work = Freq2 - Freq1;
work *= hundreths;
work /= Duration;
step = work + Freq1;
} else { // ramp down
work = Freq1 - Freq2;
work *= hundreths;
work /= Duration;
step = Freq1 - work;
}
}
}
void main(void) {
setup_comparator(NC_NC_NC_NC);
set_timer2(0);
setup_timer_2(T2_DIV_BY_4, 124, 1); // 200us = (1/10000000 * 4 * 4 * 124+1)
step = 88542; // dynamic frequency PortB 211.1Hz
step2 = 83886; //167772; // static frequency PortA 200Hz
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE) {
//PlayTone(88542,88542,500);
//PlayTone(88542,86403,500);
//PlayTone(86403,86403,500);
//PlayTone(86403,88542,500);
delay_us(1);
}
disable_interrupts(INT_TIMER2);
}