Date: 31 Oct 1996 To: PIC mailing list by Parallax, Inc. From: Jaap van Ganswijk Subject: Re: [PICS] how sine wave in PIC At 00:25 96-11-01 +1100, you wrote: >how do you generate a SINE WAVE in programming. > >i wish to use a PIC16C84 and output a > 7 bit number that follows a sine wave. > >i can do this in basic on a PC but i use the SIN function. > >how do you do it in a PIC? Use a table generated by a PC... Here is the C-function I'm generally using: //isin.c //calculate integer sinus and cosinus {0..0x100-1} -> {-127..127} //19881122/wjvg static char Asin[0x100]={ 0x00,0x03,0x06,0x09,0x0c,0x10,0x13,0x16, 0x19,0x1c,0x1f,0x22,0x25,0x28,0x2b,0x2e, 0x31,0x33,0x36,0x39,0x3c,0x3f,0x41,0x44, 0x47,0x49,0x4c,0x4e,0x51,0x53,0x55,0x58, 0x5a,0x5c,0x5e,0x60,0x62,0x64,0x66,0x68, 0x6a,0x6b,0x6d,0x6f,0x70,0x71,0x73,0x74, 0x75,0x76,0x78,0x79,0x7a,0x7a,0x7b,0x7c, 0x7d,0x7d,0x7e,0x7e,0x7e,0x7f,0x7f,0x7f, 0x7f,0x7f,0x7f,0x7f,0x7e,0x7e,0x7e,0x7d, 0x7d,0x7c,0x7b,0x7a,0x7a,0x79,0x78,0x76, 0x75,0x74,0x73,0x71,0x70,0x6f,0x6d,0x6b, 0x6a,0x68,0x66,0x64,0x62,0x60,0x5e,0x5c, 0x5a,0x58,0x55,0x53,0x51,0x4e,0x4c,0x49, 0x47,0x44,0x41,0x3f,0x3c,0x39,0x36,0x33, 0x31,0x2e,0x2b,0x28,0x25,0x22,0x1f,0x1c, 0x19,0x16,0x13,0x10,0x0c,0x09,0x06,0x03, 0x00,0xfd,0xfa,0xf7,0xf4,0xf0,0xed,0xea, 0xe7,0xe4,0xe1,0xde,0xdb,0xd8,0xd5,0xd2, 0xcf,0xcd,0xca,0xc7,0xc4,0xc1,0xbf,0xbc, 0xb9,0xb7,0xb4,0xb2,0xaf,0xad,0xab,0xa8, 0xa6,0xa4,0xa2,0xa0,0x9e,0x9c,0x9a,0x98, 0x96,0x95,0x93,0x91,0x90,0x8f,0x8d,0x8c, 0x8b,0x8a,0x88,0x87,0x86,0x86,0x85,0x84, 0x83,0x83,0x82,0x82,0x82,0x81,0x81,0x81, 0x81,0x81,0x81,0x81,0x82,0x82,0x82,0x83, 0x83,0x84,0x85,0x86,0x86,0x87,0x88,0x8a, 0x8b,0x8c,0x8d,0x8f,0x90,0x91,0x93,0x95, 0x96,0x98,0x9a,0x9c,0x9e,0xa0,0xa2,0xa4, 0xa6,0xa8,0xab,0xad,0xaf,0xb2,0xb4,0xb7, 0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xca,0xcd, 0xcf,0xd2,0xd5,0xd8,0xdb,0xde,0xe1,0xe4, 0xe7,0xea,0xed,0xf0,0xf4,0xf7,0xfa,0xfd, }; int isin(x) unsigned x; { return Asin[x&0xff]; } int icos(x) unsigned x; { return Asin[(x+0x40)&0xff]; } //end Greetings, Jaap