Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Help add feedback to Sine Wave Generation using ATmega16

AnwarB

Newbie level 4
Joined
Aug 7, 2020
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
115
Hi all
On the subject of Tahmid
tahmidmc.blogspot.com/2012/10/generation-of-sine-wave-using-spwm-in_10.html
Talk about generating a sine wave with SPWM in the PIC16F684
Feedback added here
tahmidmc.blogspot.com/2012/11/feedback-in-sine-wave-inverter-pic16f.html
To be like this

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const unsigned char sin_table[416]=
{
0,16,33,49,64,79,93,106,118,129,138,146,153,158,161,163,163,161,158,153,146,138,129,118,106,93,79,64,49,33,16,0, //65%
0,17,34,51,67,82,97,110,122,134,143,152,158,164,167,169,169,167,164,158,152,143,134,122,110,97,82,67,51,34,17,0, //67.5%
0,18,35,52,69,85,100,114,127,138,149,157,164,169,173,175,175,173,169,164,157,149,138,127,114,100,85,69,52,35,18,0, //70%
0,18,36,54,71,88,103,118,131,143,154,163,170,175,179,181,181,179,175,170,163,154,143,131,118,103,88,71,54,36,18,0, //72.5%
0,19,38,56,74,91,107,122,136,149,160,169,176,182,186,188,188,186,182,176,169,160,149,136,122,107,91,74,56,38,19,0, //75%
0,20,39,58,77,94,111,126,141,153,165,174,182,188,192,194,194,192,188,182,174,165,153,141,126,111,94,77,58,39,20,0, //77.5%
0,20,40,60,79,97,114,130,145,158,170,180,188,194,198,200,200,198,194,188,180,170,158,145,130,114,97,79,60,40,20,0, //80%
0,21,41,62,81,100,118,134,149,163,175,185,193,199,204,206,206,204,199,193,185,175,163,149,134,118,100,81,62,41,21,0, //82.5%
0,22,43,64,84,103,122,139,154,168,181,191,200,206,211,213,213,211,206,200,191,181,168,154,139,122,103,84,64,43,22,0, //85
0,22,44,66,86,106,125,143,159,173,186,197,205,212,216,219,219,216,212,205,197,186,173,159,143,125,106,86,66,44,22,0, //87.5%
0,23,45,67,89,109,129,147,163,178,191,202,211,218,222,225,225,222,218,211,202,191,178,163,147,129,109,89,67,45,23,0, //90%
0,23,46,69,91,112,132,150,167,183,196,207,217,224,228,231,231,228,224,217,207,196,183,167,150,132,112,91,69,46,23,0, //92.5%
0,24,48,71,94,116,136,155,173,188,202,214,223,230,235,238,238,235,230,223,214,202,188,173,155,136,116,94,71,48,24,0, //95%
};
 
unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD,
 
TBL_POINTER_SHIFT, SET_FREQ;
unsigned int DUTY_CYCLE, FBV, FB_Step, adder;
 
void interrupt()
{
if (TMR2IF_bit == 1){TBL_POINTER_NEW =
 
TBL_POINTER_OLD + SET_FREQ;
if (TBL_POINTER_NEW < TBL_POINTER_OLD){P1M1_bit =
 
~P1M1_bit;}
TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
DUTY_CYCLE = TBL_POINTER_SHIFT + adder;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
}}
 
void main()
{
SET_FREQ = 410;
TBL_POINTER_SHIFT = 0;
TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;
DUTY_CYCLE = 0;
FB_Step = 0;
adder = 0;
T1CON = 0b00001001;//1:1 Prescale Value, Enables Timer1
ANSEL = 0b00000001;//RA0/AN0 as analogue input.
TRISA = 0b00000001;//PortA as output except RA0 as input
ADCON0 = 0b00000011;//Analog Channel (AN0)
ADCON1 = 0b01100000;//FOSC/64
CMCON0 = 7;
PR2 = 249;
TRISC = 0x3F;
CCP1CON = 0x4C;
TMR2IF_bit = 0;
T2CON = 4;
while (TMR2IF_bit == 0);
TMR2IF_bit = 0;
TRISC = 0;
TMR2IE_bit = 1;
GIE_bit = 1;
PEIE_bit = 1;
ADC_Init();
 
while(1)
{
FBV = ADC_Get_Sample(0);
if (FBV < 512){FB_Step++;
if (FB_Step > 12) FB_Step = 12;
}
else
{
if (FB_Step > 0){FB_Step--;}}
adder = FB_Step << 5;
TMR1L = 0;
TMR1H = 0;
T1IF_bit = 0;
}
}


And on the topic of Thamed
Talk about Sine Wave Generation with "Fast PWM Mode" of AVR - using ATmega16
I tried adding a feedback to it and it stopped at ADC pins
Please help with that as my experience is not enough
This is what I modified, is it correct?
I am doing an experiment on the ATmega8
Thank
 

Attachments

Last edited by a moderator:

danadakk

Full Member level 6
Joined
Mar 26, 2018
Messages
335
Helped
62
Reputation
123
Reaction score
63
Trophy points
28
Activity points
1,661
Just an observation but un-commented code makes it doubly difficult to
understand what code is doing.

Also helps when posting code to use code tags so indentation preserved hence
logic block flow more visually apparent.


Regards, Dana.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,174
Helped
1,140
Reputation
2,299
Reaction score
1,114
Trophy points
1,403
Location
Brazil
Activity points
53,281
At a glance, once you did not eable peripherals interrupt yet, seems weird this part of the code:

Code:
TMR2IF_bit = 0;
...
while (TMR2IF_bit == 0);
 

AnwarB

Newbie level 4
Joined
Aug 7, 2020
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
115
Is there a problem with this?
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,285
Helped
4,706
Reputation
9,427
Reaction score
4,471
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,942
while (TMR2IF_bit == 0);
Stops the program progressing until the TMR2 interrupt occurs. Usually, interrupts are designed to 'interrupt' the program flow rather than suspend it!
Brian.
 

AnwarB

Newbie level 4
Joined
Aug 7, 2020
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
115
Sorry, I'm inexperienced with this
This is from the topic of Tahamid
Please see here
Someone on this date October 2, 2013 at 1:28 AM
Put this
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,285
Helped
4,706
Reputation
9,427
Reaction score
4,471
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,942
I haven't gone back to see Tahamid's original postings but the software doesn't look correct to me and some of it seems to be missing. I assume the intention of having 13 sine tables is to give it voltage regulation capability but exactly how it does it isn't obvious. I'm not sure why TMR1 is used at all, it doesn't seem to do anything.

Brian.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,174
Helped
1,140
Reputation
2,299
Reaction score
1,114
Trophy points
1,403
Location
Brazil
Activity points
53,281
In addition, the code could be optimized in memory usage, since the entire table is composed of values symmetrically distributed starting from the middle of each array. Frankly, considering that this code was not wrote by the author of the Blog, but rather from someone in the comment section, I would throw it away and take another from a reliable source, or read datasheet to do it yourself.
 

AnwarB

Newbie level 4
Joined
Aug 7, 2020
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
115
I mentioned that I have little experience in this area
Just want to add feedback to avr-atmega8
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,285
Helped
4,706
Reputation
9,427
Reaction score
4,471
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
121,942
We can't help with partial code, especially porting it from on device to another.
My guess is the way it is supposed to work is you step through ONE line of the table at a rate of (number of entries on one line multiplied by the frequency you want to produce) and use that value to feed the PWM generator. To regulate the output voltage you periodically read the ADC and use it's output to select which line in the table you use. As Andre pointed out, you can halve the size of the table by using each value twice, utilizing its symmetry. If I'm right about how the regulation works, personally I would use a different array for each set of values, it would make the code far simpler.

Brian.
 

Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top