Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Table pointer
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Thu Oct 23, 2014 11:46 am    Post subject: Table pointer Reply with quote

I am trying to vary the amplitude of the sine wave using a table pointer actually i have implemented 20 tables containing values for different amplitude of sine wave but not success full so far. as when I place my ADC code in Loop it introduces jitter in the system.
'*****************************************************************************
' Program : PWM Sine Wave 50Hz inverter.bas
' Date : 23-10-14
' Processor : ATMEGA8
' Input: Internal sine wave value table
' Process : Pulse width modulation
'*****************************************************************************

$regfile = "m8def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 128 ' default use 128 for the hardware stack
$swstack = 64 ' default use 64 for the SW stack
$framesize = 64

' variable
Dim Half_cycle As Byte
Dim Sine_table(32) As Byte ' Sine_table
Dim Index As Byte
Dim W As Word
Dim Amplitude As Byte
Dim State As Byte ' Positive or Negativ Half_cycle

' Config Pin
Config Pind.5 = Output 'Pin OC1A - PWM1a
Config Pind.4 = Output 'Pin OC1B - PWM1b
Config Pinb.0 = Output 'Pin OC1A - PWM1a
Config Pind.7 = Output 'Pin OC1B - PWM1b

'******************************************************************
'* Halfperiod = 10 ms
'* There are 32 pwm values in a half period 10ms/32
'* We need to change the OCR value every 10000/32 =312,5 us
'*
'* Let Timer0 overflow after 312.5 us
'* Set Timer0 Prescaler to 64
'* Timer0 step = 64/8MHz = 8 us ; 312,5.0/8 = 39.0625 us
'* Preload value = 255 - 39
'******************************************************************

Config Timer0 = Timer , Prescale = 64 '64/8MHz = 8us
Config Adc = Single , Prescaler = Auto , Reference = Internal

Enable Ovf0
On Ovf0 Timer0_ovf

Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1
'********************************************************
'* PWM Freq 31.25 kHz for PWM = 8 bit
'* PWM Freg 15.625 kHz for PWM = 9 bit
'********************************************************

Enable Timer0 ' PWM Counter
Enable Interrupts

Start Timer1

For Index = 0 To 31


If Amplitude = 0 Then
Sine_table(index + 1) = Lookup(index , Sine_dat0) ' Read sine_dat
End If
If Amplitude = 50 Then
Sine_table(index + 1) = Lookup(index , Sine_dat19)
End If
Next Index




Index = 0
Half_cycle = 0 ' Positiv Sine Cycle

Do

Print "AMPLITUDE " ; Amplitude

Print "STABLE " ; Sine_table(32)

Loop ' Loop Forever

End ' End Program

'************************ Interrupt0 ****************************************



Timer0_ovf:

W = Getadc(0)
Amplitude = W / 4

Timer0 = 255 - 39

If Half_cycle = 0 Then ' Select Half Cycle Positiv
Pwm1a = Sine_table(index + 1) ' PWM Positive Half Cycle
Pwm1b = 0
Portb.0 = 1
Portd.7 = 0
Else
Pwm1b = Sine_table(index + 1) ' PWM Negativ Sine Cycle
Pwm1a = 0
Portd.7 = 1
Portb.0 = 0
End If

Incr Index ' Increments Index for "1"

If Index => 32 Then
Index = 0 ' Reset Index to "0"
If Half_cycle = 0 Then
Half_cycle = 1 ' Negativ Sine Cycle
Else
Half_cycle = 0 ' Positive Half Cycle

End If
End If

Return

'************ SINE WAVE DATA 32 samples base on PWM PERIOD 255 *************


Sine_dat0:
Data 0 , 25 , 49 , 73 , 96 , 118 , 139 , 159 , 177 , 193 , 208 , 220 , 231 , 239 , 245 , 249 , 250 , 249 , 245 , 239 , 231 , 220 , 208 , 193 , 177 , 159 , 139 , 118 , 96 , 73 , 49 , 25
Sine_dat1:
Data 0 , 24 , 47 , 70 , 92 , 113 , 133 , 152 , 170 , 186 , 200 , 212 , 222 , 230 , 235 , 239 , 240 , 239 , 235 , 230 , 222 , 212 , 200 , 186 , 170 , 152 , 133 , 113 , 92 , 70 , 47 , 24
Sine_dat2:
Data 0 , 23 , 45 , 67 , 88 , 108 , 128 , 146 , 163 , 178 , 191 , 203 , 212 , 220 , 226 , 229 , 230 , 229 , 226 , 220 , 212 , 203 , 191 , 178 , 163 , 146 , 128 , 108 , 88 , 67 , 45 , 23
Sine_dat3:
Data 0 , 22 , 43 , 64 , 84 , 104 , 122 , 140 , 156 , 170 , 183 , 194 , 203 , 211 , 216 , 219 , 220 , 219 , 216 , 211 , 203 , 194 , 183 , 170 , 156 , 140 , 122 , 104 , 84 , 64 , 43 , 22
Sine_dat4:
Data 0 , 21 , 41 , 61 , 80 , 99 , 117 , 133 , 148 , 162 , 175 , 185 , 194 , 201 , 206 , 209 , 210 , 209 , 206 , 201 , 194 , 185 , 175 , 162 , 148 , 133 , 117 , 99 , 80 , 61 , 41 , 21
Sine_dat5:
Data 0 , 20 , 39 , 58 , 77 , 94 , 111 , 127 , 141 , 155 , 166 , 176 , 185 , 191 , 196 , 199 , 200 , 199 , 196 , 191 , 185 , 176 , 166 , 155 , 141 , 127 , 111 , 94 , 77 , 58 , 39 , 20
Sine_dat6:
Data 0 , 19 , 37 , 55 , 73 , 90 , 106 , 121 , 134 , 147 , 158 , 168 , 176 , 182 , 186 , 189 , 190 , 189 , 186 , 182 , 176 , 168 , 158 , 147 , 134 , 121 , 106 , 90 , 73 , 55 , 37 , 19
Sine_dat7:
Data 0 , 18 , 35 , 52 , 69 , 85 , 100 , 114 , 127 , 139 , 150 , 159 , 166 , 172 , 177 , 179 , 180 , 179 , 177 , 172 , 166 , 159 , 150 , 139 , 127 , 114 , 100 , 85 , 69 , 52 , 35 , 18
Sine_dat8:
Data 0 , 17 , 33 , 49 , 65 , 80 , 94 , 108 , 120 , 131 , 141 , 150 , 157 , 163 , 167 , 169 , 170 , 169 , 167 , 163 , 157 , 150 , 141 , 131 , 120 , 108 , 94 , 80 , 65 , 49 , 33 , 17
Sine_dat9:
Data 0 , 16 , 31 , 46 , 61 , 75 , 89 , 102 , 113 , 124 , 133 , 141 , 148 , 153 , 157 , 159 , 160 , 159 , 157 , 153 , 148 , 141 , 133 , 124 , 113 , 102 , 89 , 75 , 61 , 46 , 31 , 16
Sine_dat10:
Data 0 , 15 , 29 , 44 , 57 , 71 , 83 , 95 , 106 , 116 , 125 , 132 , 139 , 144 , 147 , 149 , 150 , 149 , 147 , 144 , 139 , 132 , 125 , 116 , 106 , 95 , 83 , 71 , 57 , 44 , 29 , 15
Sine_dat11:
Data 0 , 14 , 27 , 41 , 54 , 66 , 78 , 89 , 99 , 108 , 116 , 123 , 129 , 134 , 137 , 139 , 140 , 139 , 137 , 134 , 129 , 123 , 116 , 108 , 99 , 89 , 78 , 66 , 54 , 41 , 27 , 14
Sine_dat12:
Data 0 , 13 , 25 , 38 , 50 , 61 , 72 , 82 , 92 , 100 , 108 , 115 , 120 , 124 , 128 , 129 , 130 , 129 , 128 , 124 , 120 , 115 , 108 , 100 , 92 , 82 , 72 , 61 , 50 , 38 , 25 , 13
Sine_dat13:
Data 0 , 12 , 23 , 35 , 46 , 57 , 67 , 76 , 85 , 93 , 100 , 106 , 111 , 115 , 118 , 119 , 120 , 119 , 118 , 115 , 111 , 106 , 100 , 93 , 85 , 76 , 67 , 57 , 46 , 35 , 23 , 12
Sine_dat14:
Data 0 , 11 , 21 , 32 , 42 , 52 , 61 , 70 , 78 , 85 , 91 , 97 , 102 , 105 , 108 , 109 , 110 , 109 , 108 , 105 , 102 , 97 , 91 , 85 , 78 , 70 , 61 , 52 , 42 , 32 , 21 , 11
Sine_dat15:
Data 0 , 10 , 20 , 29 , 38 , 47 , 56 , 63 , 71 , 77 , 83 , 88 , 92 , 96 , 98 , 100 , 100 , 100 , 98 , 96 , 92 , 88 , 83 , 77 , 71 , 63 , 56 , 47 , 38 , 29 , 20 , 10
Sine_dat16:
Data 0 , 9 , 18 , 26 , 34 , 42 , 50 , 57 , 64 , 70 , 75 , 79 , 83 , 86 , 88 , 90 , 90 , 90 , 88 , 86 , 83 , 79 , 75 , 70 , 64 , 57 , 50 , 42 , 34 , 26 , 18 , 9
Sine_dat17:
Data 0 , 8 , 16 , 23 , 31 , 38 , 44 , 51 , 57 , 62 , 67 , 71 , 74 , 77 , 78 , 80 , 80 , 80 , 78 , 77 , 74 , 71 , 67 , 62 , 57 , 51 , 44 , 38 , 31 , 23 , 16 , 8
Sine_dat18:
Data 0 , 7 , 14 , 20 , 27 , 33 , 39 , 44 , 49 , 54 , 58 , 62 , 65 , 67 , 69 , 70 , 70 , 70 , 69 , 67 , 65 , 62 , 58 , 54 , 49 , 44 , 39 , 33 , 27 , 20 , 14 , 7
Sine_dat19:
Data 0 , 6 , 12 , 17 , 23 , 28 , 33 , 38 , 42 , 46 , 50 , 53 , 55 , 57 , 59 , 60 , 60 , 60 , 59 , 57 , 55 , 53 , 50 , 46 , 42 , 38 , 33 , 28 , 23 , 17 , 12 , 6

(BASCOM-AVR version : 2.0.7.5 , Latest : 2.0.7.7 )
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Oct 23, 2014 12:38 pm    Post subject: 30 hertz sinus generator.... Reply with quote

Perhaps not what you are looking for, but here you can find my 30 hertz sinus generator....

http://members.home.nl/bzijlstra/vor-tester.html#phase-shifting-low-budget

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Thu Oct 23, 2014 5:26 pm    Post subject: Reply with quote

But I want to tie the ADC to select the different tables according to the required amplitude.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1939

blank.gif
PostPosted: Thu Oct 23, 2014 7:55 pm    Post subject: Reply with quote

Forget about using GetADC() within the ISR, it works blocking, till the conversion has finished.
Simply read ADCL/ADCH at start of the ISR, and immediately after, start a conversion by setting ADCSR.ADSC = 1.
This way the ADC has all the time till the next ISR-call, to finish its conversion.
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2484
Location: Queensland

australia.gif
PostPosted: Thu Oct 23, 2014 11:59 pm    Post subject: Reply with quote

To get amplitude control, just use one table with range 0-255, and multiply each table value by a variable in range 0-255 'on the fly'. This gives you a number in range 0-65534. Use the high byte of this to get a scaled value in range 0-255 for the output.
Code:

Dim Scalefactor as byte 'the amplitude you want to set
Dim OutputValue as byte 'the value you send for each sample
Dim TableValue as byte 'the value read from the table for each sample
Dim wTemp1 as word
Dim wTemp2 as word

wTemp1 = TableValue 'assign current table value to word
wTemp2 = Scalefactor 'assign amplitude to word

wTemp1 = wTemp1 * wTemp2 'do multiply to scale current value
OutputValue = high(wTemp1) 'take hi byte as result
 

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Fri Oct 24, 2014 3:40 pm    Post subject: Reply with quote

Thanks Adrian you are always there to help, but would please place these routines in the appropriate places with ADC routines by copy pasting the code from my first post.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1939

blank.gif
PostPosted: Fri Oct 24, 2014 10:32 pm    Post subject: Reply with quote

Hi Adrian, why could you even dare to post a standalone sample, which does not smoothly fit into above code mess? LOL
I knew somehow, I shouldn't have answered... But never mind, I'll remember now the TO.
Back to top
View user's profile
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Sun Oct 26, 2014 9:46 am    Post subject: Reply with quote

@MWS I really don,t know weather your comment is for my in-ability or to the Adrian but I know Adrian is a nice gentleman with his help I started programming using Bascom, but this time his sample is going over my head.
Thanks and regards.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1939

blank.gif
PostPosted: Sun Oct 26, 2014 11:03 am    Post subject: Reply with quote

naseerak wrote:
is for my in-ability or to the Adrian

I've addressed Adrian, and in case you did not understand my words, it was a sarcastic comment about your brazen request.
Quote:
I know Adrian is a nice gentleman with his help I started programming using Bascom, but this time his sample is going over my head.

Well, I can't talk for Adrian, but my idea is to help people, who take the efforts to try and help themselves.

Your request to Adrian, to actually write your code, i.e. do your very work, because you're lazy or unwilling, I felt inadequate.
Look below your name: you've joined the forum in 2008, in case you do not understand Adrian's easy and bite-sized sample after these 6 years, you do something seriously wrong.
And even if you did not understand, you can learn it, by simulating his code.

Even more, you asked him to integrate the ADC-functions in your code.
I found that highly blatant and decided for myself not to respond to one of your problems again, till at last I see you do your work and take your own efforts, to solve YOUR problems.
Back to top
View user's profile
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Sun Oct 26, 2014 1:35 pm    Post subject: Reply with quote

@MWS IF you see I have implemented the ADC code but apparently in the wrong sequence, This code in working but with a jitter in the PWM, as for your suggestion for reading the ADC register I have,nt done it before and knows very little about this method. As for full code is concerned only stupid will have a quest for that.. I just need the proper placement of the code not the full code please don,t take my request wrong and trying for a few days with different experiments but to no vail.
Sorry for any appropriate behavior.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1939

blank.gif
PostPosted: Sun Oct 26, 2014 2:50 pm    Post subject: Reply with quote

Programming is to have an idea about order of execution and understanding about other "mechanisms" in the program and of the hardware.

In case you're stuck, you can ask for suggestions, that's what Adrian an I have given. It's not just "proper placement" of some commands, it's understanding of the whole.

Your code, as shown in the top post, is simply unfunctional in regards of control via ADC. It's not just adding some clever code to a few places and all is good.

For example: The For/Next at the start very likely fills the SRam array with an incomplete sine table, as soon the ADC is enabled. For dynamically controlling the output via the ADC input, the approach is completely unsuitable. So I assume the "jitter" is not just that, instead it's a complete messy signal, as the sine table is only filled one time at startup, and then it's filled only with values <> 0, if Amplitude is either 0 or 50. So, as soon you put GetADC() in, this mess is created, otherwise Amplitude stays 0, and then you have at least a complete sine table. This gives you the wrong Impression, that GetADC() actually creates, what you believe as being jitter.

Adrian's suggestions are the way to go, but they have to be implemented into the ISR, you could use my suggestion then, to free processor time. But you have to do it, you have to implement a clever logic, and that's called "programming" then.
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2484
Location: Queensland

australia.gif
PostPosted: Sun Oct 26, 2014 11:36 pm    Post subject: Reply with quote

My intention was not to clean up your code, but to show you a way to implement amplitude control of an analog signal without having multiple tables of repeated data.

I really have no intention to debug someone elses code - I find it hard enough to debug my own ! But where I can show a way to use a different approach, or just point out some obvious error, based on how I may have done something in the past, then I am happy to show that.

One of the really good things that comes out of this forum is to show some code, as you have done, and then see how others may have approached the same problem. I know this sounds a bit academic, epecially when you are in the thick of a tangle of code and possibly a deadline, but sometimes it really helps to step back a bit and look at the overall program design.

I notice more and more that 'code' is thought of by some programmers as a magic bullet, where a couple of lines will invoke some really complex process which will solve all problems. Some modern languages are certainly like that, but thankfully not (yet) Basic. In this language you still need to understand what you want, and design a program to implement that, rather than just throw down a few lines of code and see if it fixes your problem.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Mon Oct 27, 2014 6:37 pm    Post subject: Reply with quote

@Adrian you hit the nail on the middle. Basic is nt good for serious programming, still it is worth to have a try in BASCOM I will continue my struggle with this and come up with a result here.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1939

blank.gif
PostPosted: Mon Oct 27, 2014 7:07 pm    Post subject: Reply with quote

naseerak wrote:
Basic is nt good for serious programming

Hm, it's getting worse. Someone who has not the slightest clue about simply implementing a code sample, tells that Basic, thus Bascom is not good for serious programming. LOL
I think you do not know, what serious programming actually is, so please don't talk like the blind about the colors of the rainbow.
Back to top
View user's profile
naseerak

Bascom Member



Joined: 08 Nov 2008
Posts: 138

pakistan.gif
PostPosted: Fri Oct 31, 2014 5:33 pm    Post subject: Reply with quote

Can any body tell me how much time GETADC() will take to complete.
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum