CONST unsigned int SINE_WAVE[32] = {
7,8,10,11,12,13,13,14,14,14,13,13,12,11,10,8,
7,6,4,3,2,1,1,0,0,0,1,1,2,3,4,6};
void interrupt(){
position+=step;
PORTB=SineTab[(position>>8)&31];
position2+=step2;
PORTA=SineTab[(position2>>8)&31];
if(timeCount++==20){
timeCount=0;
hundreths++;
}
}
StartValue = (int)11.1*4.096; // start frequncy
EndValue = (int)6.4*4.096; // end frequency
timeSteps = 8000; // number of 100th of a second
hundreths=0; // clear 100th counter
While(hundreths<timeSteps){
//the following needs to use signed values and maybe floats
step=(EndValue-StartValue)*hundreths/timeSteps+StartValue;
}
void interrupt(){
position+=step;
TempB=SineTab[(position>>8)&31];
PORTB=TempB;
position2+=step2;
TempA=SineTab[(position2>>8)&31];
PORTA=TempA;
if((TempA-7)+(TempB-7)>0){
LED=1;
}
else{
LED=0;
}
if(timeCount++==20){
timeCount=0;
hundreths++;
}
}
void PlayTone(long Freq1,long Freq2,long Duration){
signed int32 work;
hundreths=0;
While(hundreths<Duration){
work=Freq2-Freq1;
work*=hundreths;
work/=Duration;
step=work+Freq1;
}
void main(){
PlayTone(45,45,200); //play 11.1Hz for 2 seconds
PlayTone(45,868,500); //increase to 211.9 over 5 seconds
PlayTone(868,819,1000); //play 211.9Hz and change to 200Hz over 10 seconds
}
if((TempA&8)!=(TempB&8)){
LED=1;
}
else{
LED=0;
}
position+=step;
TempB=SineTab[(position>>8)&31];
output_b(TempB);
position2+=step2;
TempA=SineTab[(position2>>8)&31];
output_a(TempA);
#include <16F88.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=8000000)
int timeCount; //int = int8
long TempA, TempB, step, step2, position, position2; //long = int16
CONST unsigned int SineTab[32] = {
7,8,10,11,12,13,13,14,14,14,13,13,12,11,10,8,
7,6,4,3,2,1,1,0,0,0,1,1,2,3,4,6};
#INT_TIMER2
void interrupt(){
position+=step;
TempB=SineTab[(position>>8)&31];
output_b(TempB);
position2+=step2;
TempA=SineTab[(position2>>8)&31];
output_a(TempA);
}
void main(void) {
setup_oscillator(OSC_8MHZ | OSC_INTRC); // Use internal 8MHz oscillator
set_timer2(0);
setup_timer_2(T2_DIV_BY_4, 249, 1); // 500us = (1/8000000 * 4 * 4 * 249+1)
step = 819; // changing frequency
step2 = 868; // base frequency
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE)
{
delay_us(1);
}
disable_interrupts(INT_TIMER2);
}
LIST p=16F88
#INCLUDE <P16F88.INC>
errorlevel -302
;------------------------------------------------------------------------------
__CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
;------------------------------------------------------------------------------
CBLOCK 0x20 ; Sample GPR variable registers allocated contiguously
Position:2
Step:2
TempB
ENDC
W_TEMP EQU 0x7D ; w register for context saving (ACCESS)
STATUS_TEMP EQU 0x7E ; status used for context saving (ACCESS)
PCLATH_TEMP EQU 0x7F ; variable used for context saving
;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------
RESET ORG 0x0000 ; processor reset vector9
PAGESEL START
GOTO START ; go to beginning of program
;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------
ISR ORG 0x0004 ; interrupt vector location
; Context saving for ISR
MOVWF W_TEMP ; save off current W register contents
MOVF STATUS,W ; move status register into W register
MOVWF STATUS_TEMP ; save off contents of STATUS register
MOVF PCLATH,W ; move pclath register into W register
MOVWF PCLATH_TEMP ; save off contents of PCLATH register
;------------------------------------------------------------------------------
; USER INTERRUPT SERVICE ROUTINE GOES HERE
;------------------------------------------------------------------------------
movfw Step ;Position+=Step
addwf Position,F
movfw Step+1
btfsc STATUS,C
addlw 1
addwf Position+1,F
movlw high GetSine ;Prepare to get sine value
movwf PCLATH
movfw Position+1
andlw .31 ;TempB=(Position>>8)&31
call GetSine
movwf TempB
movwf PORTB
bcf PIR1,TMR2IF
; Restore context before returning from interrupt
MOVF PCLATH_TEMP,W ; retrieve copy of PCLATH register
MOVWF PCLATH ; restore pre-isr PCLATH register contents
MOVF STATUS_TEMP,W ; retrieve copy of STATUS register
MOVWF STATUS ; restore pre-isr STATUS register contents
SWAPF W_TEMP,F
SWAPF W_TEMP,W ; restore pre-isr W register contents
RETFIE ; return from interrupt
;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------
START
bsf STATUS,RP0
movlw 0xf0
movwf TRISA
movwf TRISB
movlw b'00000111' ; disable comparator
movwf CMCON
movlw B'01110000' ;select 8MHz clock
movwf OSCCON
clrf ANSEL ;no ADCs
bcf STATUS,RP0
movlw 1<<TMR2ON|b'01'<<T2CKPS0
movwf T2CON
bsf STATUS,RP0
movlw .249
movwf PR2
bsf PIE1,TMR2IE
bcf STATUS,RP0
movlw low(.819)
movwf Step
movlw high(.819)
movwf Step+1
movlw (1<<GIE|1<<PEIE|0<<TMR0IE|0<<INTE|0<<RBIE|0<<TMR0IF|0<<INTF|0<<RBIF)
movwf INTCON ; enable Peripheral interupts
;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE
;------------------------------------------------------------------------------
GOTO $
org 0x0400
GetSine addwf PCL,F
dt .7,.8,.10,.11,.12,.13,.13,.14,.14,.14,.13,.13,.12,.11,.10,.8
dt .7,.6,.4,.3,.2,.1,.1,.0,.0,.0,.1,.1,.2,.3,.4,.6
END
Pommie said:I can't understand how 819 can be a problem yet 868 isn't. The only thing I can think is that there is interference between the 119.9 frequency and the 2000Hz frequency.
What happens with 818 and 820?
Are you able to compile assembler as I could knock up a quick asm version to check it isn't a quirk of the compiler.
Mike.
Edit, a possible solution may be to switch to 250uS interrupts and hope the filter sorts out any problems.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?