• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sine table code in c

be80be

Well-Known Member
Thread starter #1
I was working on a sine table I have the sine table.
so if I bit bang this no pwm or timer
I would basically run a loop 256 times
using the table as the on time and off time am a right or not even close?


Code:
uint8_t  sine_wave[256] = {
  0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
  0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
  0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
  0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
  0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
  0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
  0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC,
  0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD,
  0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6,
  0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB,
  0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC,
  0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9,
  0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3,
  0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C,
  0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83,
  0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x6D, 0x6A,
  0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52,
  0x4F, 0x4D, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x3C,
  0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28,
  0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18,
  0x16, 0x15, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C,
  0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
  0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
  0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A,
  0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x13, 0x15,
  0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24,
  0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37,
  0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
  0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
  0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D
};
 

dknguyen

Well-Known Member
Most Helpful Member
#2
I don't think that's right. Isn't that just a list of amplitudes? Shouldn't you be pre-calculating the on and off-times? So that If the list is [On1, Off1, On2, Off2, ....] where On_n+Off_n = your period, for all n.

Or I guess you could just have them be just a list of all on (on1, on2, on3, ...) times or off times (off1, off2, off3...)and subtract it from the period on the fly to calculate the other half (Off1 = Period - On1) or (On1 = Period - Off1).
 

be80be

Well-Known Member
Thread starter #3
I found lots about doing this with pwm the chip I was playing with don't have pwm I was just trying to get a sine wave I got part of one the down side slope looks good on a scope the upside looks like a clift LOL that was with a pwm and low pass filter.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#4
How many spare pins have you got?

I used three pins... A DAC and a filter and my sine was pretty good
32 entries in the sine table for one cycle
 

Attachments

be80be

Well-Known Member
Thread starter #5
I got lots of pins lol could you share your code I'm just playing with my scope and how to make sine wave.

I just tried with the chip I had hooked up it can't hold the table I'm think about a 24f it has a dac I think 10 bits
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#6
You wont need the code.

As it was three bit dac ( LOL three bits ) table was simple...

char sinetable[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};

Oh yeah!! That was about 1khz... a delay of 100us between values so the filter was 1k and 0.1uf
 

Nigel Goodwin

Super Moderator
Most Helpful Member
#9
I just tried with the chip I had hooked up it can't hold the table I'm think about a 24f it has a dac I think 10 bits
I don't think the 24F generally has DAC (at least not of any decent resolution), but it's simple to use PWM as a DAC, and most PIC's have at least one PWM channel - what PIC are you using that doesn't have PWM?.

I've recently used PWM for the contrast setting on an LCD module, just added a 1K resistor and 10uF as a lowpass filter.
 

be80be

Well-Known Member
Thread starter #11
Changed chips im not getting a nice sine wave don't think the cap is right I can't read them LOL
I tried 3 two looked better these caps writing is so small hard to see whats what.
 
Last edited:

be80be

Well-Known Member
Thread starter #13
I was going to try that Mike but I gave up I'm still learning xc8 how would i put
char sine[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};
In program memory the chip has lots of that but just 72 bytes of data tho.
 

be80be

Well-Known Member
Thread starter #15
Well I tried that not too good lol
sine.jpg

I cleaned it up a bit Ian looks nice now Thanks
now to work on the next part LOL
2018-03-13-014741.jpg
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
#17
I assume you've already worked out that to put your data in flash you just proceed it with const.

const char sine[] = {7,7,7,7,6,6,6,5,5,4,3,2,2,1,1,1,0,0,0,0,1,1,1,2,2,3,4,5,5,6,6,6};

Mike.
 

be80be

Well-Known Member
Thread starter #18
I need some kind of Language Reference sheet I can do good if I can bring up and look at it.
I can't think like i did 8 year's ago my doctor is slowly killing my brain with meds.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#19
With C compilers, its best to write you own stuff or derive it from working code... Libraries shipped with compilers are too generic and bloat code... I think it makes it harder to understand IFDEF this and IFDEF that... Gets on my left testicle...

As for age.. Well I can't see my bench to well, let alone things on it!! I have now 3 different pairs of specs.. All with different focal lengths... All three are on my head so I can swap them out.. I look like a right nutjob..

My shortest focal glasses have little magnifying lenses built into the already magnified lenses... Time to give up or what!!
 

be80be

Well-Known Member
Thread starter #20
Na i never give up hell i wear 3 pairs of glasses one over the next lol.
My big peoblem is out of habit im good it nothing gets moved hell i can wire a house and cant even see the wire copper but i do look it over with glasses on
 

Latest threads

EE World Online Articles

Loading

 
Top