This small project was a quick solution for someone that had an interest in generating a slow sinewave with a frequency of 7.96Hz similar in frequency with the Earth's magnetism and resonances.
Basically, this circuit runs on a crystal at 4MHz for timing, and from this speed, the program produces a 0 to 255 sinewave value which is output to a Digital-to-Analog resistor ladder on PORTB that can be used by another circuit.
You can download Sinewave Generator, or you can copy and paste the code shown below into your ASM code editor.
title '7.96 Hertz Sinewave Program, Copyright Jose Da Silva, Nov95' list p=16C54,f=INHX8M,C=120,T=on,ST=off #include "P16C5X.INC" __fuses _CP_OFF&_WDT_ON&_XT_OSC __idlocs h'9511' ;year/month org 08h ;scratch variables acca equ .8 ;res 1 accb equ .9 ;res 1 accc equ .10 ;res 1 ;best times for a 4MHz xtal: 243(wavetable)*517(wait increment)=7.9598Hz+%error org 0 ;****************************************************************************** ;Wave table values for PORTB Digital-to-Analog resistor ladder, from 0..255..0 ;enter :w=table.position ;exit :w=value ;time :4cycles wav_tbl addwf PCL,f ;get next sinewave increment dt 000,000,000,000,001,001,002,002,003,004,004,005,006,007,008,009 dt 00B,00C,00E,00F,011,012,014,016,018,01A,01C,01E,020,022,024,027 dt 029,02C,02E,031,033,036,039,03C,03E,041,044,047,04A,04D,050,053 dt 056,059,05D,060,063,066,069,06D,070,073,076,07A,07D,080,084,087 dt 08A,08D,091,094,097,09A,09E,0A1,0A4,0A7,0AA,0AD,0B0,0B4,0B7,0B9 dt 0BC,0BF,0C2,0C5,0C8,0CA,0CD,0D0,0D2,0D5,0D7,0D9,0DC,0DE,0E0,0E2 dt 0E4,0E6,0E8,0EA,0EC,0EE,0EF,0F1,0F2,0F4,0F5,0F6,0F7,0F8,0F9,0FA dt 0FB,0FC,0FD,0FD,0FE,0FE,0FE,0FF,0FF,0FF,0FF,0FF,0FE,0FE,0FE,0FE dt 0FD,0FD,0FC,0FB,0FA,0F9,0F8,0F7,0F6,0F5,0F4,0F2,0F1,0EF,0EE,0EC dt 0EA,0E8,0E6,0E4,0E2,0E0,0DE,0DC,0D9,0D7,0D5,0D2,0D0,0CD,0CA,0C8 dt 0C5,0C2,0BF,0BC,0B9,0B7,0B4,0B0,0AD,0AA,0A7,0A4,0A1,09E,09A,097 dt 094,091,08D,08A,087,084,080,07D,07A,076,073,070,06D,069,066,063 dt 060,05D,059,056,053,050,04D,04A,047,044,041,03E,03C,039,036,033 dt 031,02E,02C,029,027,024,022,020,01E,01C,01A,018,016,014,012,011 dt 00F,00E,00C,00B,009,008,007,006,005,004,003,003,002,002,001,001 dt 000,000,000 wav_len equ $-wav_tbl-1 org 0FFh ;****************************************************************************** ;main pause for 4Mhz xtal ;enter : ;exit :w=0, f=:acca=0,accb=0 ;time :(proper time delay)=517-13-4 m_pause movlw 0A4+1 ;init pause delay movwf acca m_pausl decfsz acca,f ;wait a moment goto m_pausl clrwdt ;(make sure watch dog still okay) nop ;(time adjustment) retlw 0 ;****************************************************************************** main clrwdt ;initialize watch dog timer movlw b'00001111' ;set options register option movlw 0 ;init ports as outputs only (no loose inputs) movwf PORTA movwf PORTB tris PORTA tris PORTB #ifdef __16C55 movwf PORTC tris PORTC #endif #ifdef __16C57 movwf PORTC tris PORTC #endif clrf acca ;init scratch variables clrf accb ;do the 'wave' mainLp call m_pause ;wait a bit incf accb,f ;update wave table pointer movlw wav_len ;(reset to zero if end of table) subwf accb,w skpnz movwf accb movf accb,w ;get next sinewave increment call wav_tbl movwf PORTB ;output sinewave increment goto mainLp ;****************************************************************************** #ifdef __16C54 org 1FFh #endif #ifdef __16C55 org 1FFh #endif #ifdef __16C57 org 3FFh #endif #ifdef __16C58 org 3FFh #endif start goto main ;program starts here on reset end |
If you find this program useful for yourself, or a project, I will appreciate a little credit for providing it here for your use/project (for example, in your source code comments, or even a web link back to this web page is great if you have a project that uses it that you want to show/demonstrate).
Thanks.
Other PIC Related Pages On This Web Site | ||||
---|---|---|---|---|
Home Page | A/B Long Term Timer | ASCII To MorseCode | Clear RAM Macro | Cylon Eye |
Delay Macro | Random Number Generator (RNG) | Software RS232 | Table Array FSR Macro | PicDis Disassembler |