View previous topic :: View next topic |
Author |
Message |
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Fri Mar 25, 2016 8:16 pm Post subject: attiny13 adc to pwm |
|
|
Good evening to all,I am NEW in bascom and I will build adc to pwm for attiny13,I have built the following programs Unfortunately not working.can you help me PLEASE.
I want to Mazo if there are more grigoros METHOD ,to control the pulse duration pwm to use A POT.
'-------------------------------------------------------------
'
'
'
'-----------------------------------------------------------
'Computer directives
$crystal = 100000
$regfile = "attiny13.dat"
'------------------------------------------------------------
'Hardware setups
Config Portb.2 = Output
Config Pinb.3 = Input
Config Pinb.4 = Input
Config Adc = Single , Prescaler = Auto
Start Adc
'---------------------------------------------------------------
'Declare variables
Dim Lightlevel As Word
Dim I As Byte
'---------------------------------------------------------------
'Program starts here
Do
Lightlevel = Getadc(0)
Select Case Lightlevel
Case Is > 500 : Pinb.2 = 1
Delay 500us
Pinb.2 = 0
Delay 8.7ms
Case Is > 400 : Pinb.2 = 1
Delay 400us
Pinb.2 = 0
Delay 8.8ms
Case Is > 300 : Pinb.2 = 1
Delay 300us
Pinb.2 = 0
Delay 8.9ms
Case Is > 200 : Pinb.2 = 1
Delay 200us
Pinb.2 = 0
Delay 9ms
Case Is > 100 : Pinb.2 = 1
Delay 100us
Pinb.2 = 0
Delay 9.1ms
Case Is > 50 : Pinb.2 = 1
Delay 50us
Pinb.2 = 0
Delay 9.15ms
End Select
Loop
End
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Mar 25, 2016 9:59 pm Post subject: |
|
|
Try this and let me know how it work
You must be logged in to see schematic.
Code: | $regfile = "attiny13.dat"
$crystal = 9600000
$hwstack = 12
$swstack 6
$framesize = 16
' Full Range PWM from ADC by BARTek
' http://bart-projects.cba.pl/
' The device is able to calculate the full scale PWM from ADC incomplete scope
' For example, the values range from 23 to 1010 will be scaled to the value 0 - 255 PWM
' To calibrate, press button (short pin to ground) and enter the potentiometer lowest and highest value.
' After releasing the button, the value will be calculated and memorized
' Fuses for ATtiny13 LOW 7A HIGH FF
'$sim
Config TIMER0 = Pwm , Compare A Pwm = Clear Down , Prescale = 1
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc
Config PORTB = &B00001 : PORTB = &B10110
V_out Alias PORTB.0
Potenc Alias PINB.3
Switch Alias PINB.4
Dim Minimum As Word , Maximum As Word
Dim Pot As Word , Help As Word , Suma As Word , Wynik As Word
Dim Lock As Byte , Przelicznik As Dword , Wart As Byte , Mem As Word
Readeeprom Mem , 2
Readeeprom Minimum , 4
Do
Pot = Getadc(3)
Select Case Lock
Case 0
If Switch = 0 Then
Waitms 50
If Switch = 0 Then
Lock = 1
Minimum = 512
Maximum = 512
End If
Else
'digital filter for ADC values
Help = Suma
Shift Help , Right , 3
Suma = Suma - Help
Suma = Suma + Pot
Help = Suma
Shift Help , Right , 3
If Help < Minimum Then Help = Minimum
If Help <> Wynik Then
Wynik = Help
Help = Wynik - Minimum
Przelicznik = Help * Mem
Przelicznik = Przelicznik \ 4000
Wart = Przelicznik
Pwm0a = Wart
End If
End If
Case 1
If Switch = 1 Then
Lock = 0
Help = Maximum - Minimum
Przelicznik = 1024000 \ Help
Mem = Przelicznik
Writeeeprom Mem , 2
Writeeeprom Minimum , 4
Else
If Pot > Maximum Then Maximum = Pot
If Pot < Minimum Then Minimum = Pot
End If
End Select
Loop
End |
|
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Sat Mar 26, 2016 6:52 am Post subject: |
|
|
You can also try
Code: | ' Brightness of Led on PB0 is controled by potentiometer on PB3
$regfile = "attiny13.dat"
$crystal = 9600000
$hwstack = 16
$swstack = 16
$framesize = 8
Dim Adc_value As Word
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Timer0 = Pwm , Prescale = 64
Tccr0a = Bits(com0a1 , Wgm00 , Wgm01) 'mode 3, fast pwm, top=0FF
Config Portb.0 = Output 'Led
'---------------------------------------------------------
Do
Adc_value = Getadc(3)
Ocr0a = Adc_value / 4
Loop
End
|
|
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Thu Apr 07, 2016 10:09 pm Post subject: |
|
|
please the prices FROM their resistors and capacitors |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Thu Apr 07, 2016 10:18 pm Post subject: |
|
|
please the prices FROM their resistors and capacitors |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Fri Apr 08, 2016 4:50 pm Post subject: |
|
|
EDC Your program is fantastic is working swimmingly,I want to know how you can change the frequency the tetragonikon pulse 100hz,
ALSO I want to explain THE functions of each MANDATE.
thanks again |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Mon Apr 11, 2016 5:55 pm Post subject: |
|
|
GOOD EVENING Visovian Follow the example and add YOUR WAY I WANT TO DO (Case),BUT UNFORTUNATELY NOT WORK,CAN YOU HELP ME
\'brightness of led on PB0 is controled by potentiometer on PO3
$regfile = \"attiny13.dat\"
$crystal = 9600000
$hwstack = 16
$swstack = 16
$framesize = 8
Dim Adc_value As Word
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Timer0 = Pwm , Prescale = 64
Tccr0a = Bits(com0a1 , Wgm00 , Wgm01) \'mode 3,fast
\'pwm , top=off
Config Portb.3 = Input
Config Portb.2 = Output \'led
\'--------------------------------------------------------------------------
Do
Adc_value = Getadc(3)
Ocr0a = Adc_value / 4
Select Case Adc_value
Case Is > 500 : Pinb.2 = 1
Case Is > 400 : Pinb.2 = 0
Case Is > 300 : Pinb.2 = 1
Case Is > 200 : Pinb.2 = 0
Case Is > 100 : Pinb.2 = 1
Loop
End Select
End |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Mon Apr 11, 2016 7:36 pm Post subject: |
|
|
Hi,
If you want to set an Output you Need to write port, rather than pin. Pin is for reading the actual level on the Hardware pin (High/Low). So:
Code: | Case Is > 500 : Portb.2 = 1
Case Is > 400 : Portb.2 = 0
Case Is > 300 : Portb.2 = 1
Case Is > 200 : Portb.2 = 0
Case Is > 100 : Portb.2 = 1
|
And move the Loop outside the case Statement
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Tue Apr 12, 2016 5:08 pm Post subject: |
|
|
Good evening i.dobson your correction works perfectly,Thank you very much.
I am a beginner and made mistakes .
I want to ask the FOLLOWS(the attiny13 from what frequency larger or smaller a crystal may declare in bascom I want when declared delay Futures (waitus) to coincide with the olivescrystal. |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Fri Apr 15, 2016 6:25 pm Post subject: |
|
|
ok we did( CASE) Look at my example.
\'brightness of led on PB0 is controled by potentiometer on PO3
$regfile = \"attiny13.dat\"
$crystal = 1000000
$hwstack = 16
$swstack = 16
$framesize = 8
Dim Adc_value As Word
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Timer0 = Pwm , Prescale = 64
Tccr0a = Bits(com0a1 , Wgm00 , Wgm01) \'mode 3,fast
\'pwm , top=off
Config Pinb.3 = Input
Config Portb.2 = Output \'led
\'--------------------------------------------------------------------------
Do
Adc_value = Getadc(3)
Ocr0a = Adc_value / 4
Select Case Adc_value
Case Is > 1000 : Portb.2 = 1
Waitus 500
Portb.2 = 0
Waitus 9500
Case Is > 900 : Portb.2 = 1
Waitus 450
Portb.2 = 0
Waitus 9550
Case Is > 800 : Portb.2 = 1
Waitus 400
Portb.2 = 0
Waitus 9600
Case Is > 700 : Portb.2 = 1
Waitus 350
Portb.2 = 0
Waitus 9650
Case Is > 600 : Portb.2 = 1
Waitus 300
Portb.2 = 0
Waitus 9700
Case Is > 500 : Portb.2 = 1
Waitus 250
Portb.2 = 0
Waitus 9750
Case Is > 400 : Portb.2 = 1
Waitus 200
Portb.2 = 0
Waitus 9800
Case Is > 300 : Portb.2 = 1
Waitus 150
Portb.2 = 0
Waitus 9850
Case Is > 200 : Portb.2 = 1
Waitus 100
Portb.2 = 0
Waitus 9900
Case Is > 100 : Portb.2 = 1
Waitus 50
Portb.2 = 0
Waitus 9950
End Select
Loop
End
BUT how can I escape from all that hassle (case is)...?????
can you explain to me how to I make with command PWM FOR pulsing..
|
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Apr 15, 2016 9:51 pm Post subject: |
|
|
Attiny13 have hardware PWM channels on pins PB0 and PB1. On these pins generating PWM waves cost micro nothing because built-in timer working.
If You want PWM signal on other pins then you must generate this with software.
So it is not clear what you doing right now in the software.
You should consider to connect diode to pins PB0 or PB1...or ask/search how to generate PWM on other pins |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Mon Apr 18, 2016 8:29 pm Post subject: |
|
|
GOOD EVENING,THE CORRECTIONS made in the following example AND WORK famously.
$regfile = \"attiny13.dat\"
$crystal = 1000000
$hwstack = 16
$swstack = 16
$framesize = 8
Dim Adc_value As Word
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Config Timer0 = Pwm , Prescale = 64
Tccr0a = Bits(com0a1 , Wgm00 , Wgm01) \'mode 3,fast
\'pwm , top=off
Config Timer
Config Portb.4 = Input
Config Portb.0 = Output
\'led
\'--------------------------------------------------------------------------
Do
Adc_value = Getadc(2)
Ocr0a = Adc_value / 10
Loop
End
NOW I WANT MY explain HOW To activate the (OCOB to PB1) And running with the input (ADC3) so as to have two POTS INPUT and two output square wave PB0 TO PB1,To a time lag DELAY OF 50US PULSE OUTPUT..
THANKS AGAIN The AID IN THIS FORUM IS AMAZING... |
|
Back to top |
|
|
aphawk
Joined: 23 Jan 2010 Posts: 168 Location: Brazil
|
Posted: Wed Apr 20, 2016 3:24 am Post subject: |
|
|
1 - Change this :
Tccr0a = Bits(com0a1 , Wgm00 , Wgm01) \'mode 3,fast
to :
Tccr0a = Bits(com0a1 , com0b1, Wgm00 , Wgm01) \'mode 3,fast
2 - Change this :
Config Portb.4 = Input
Config Portb.0 = Output
to :
Config Portb.4 = Input
Config Portb.3 = Input
Config Portb.0 = Output
Config Portb.1=Output
3 - Change this :
Adc_value = Getadc(2)
Ocr0a = Adc_value / 10
To :
Adc_value = Getadc(2)
Ocr0a = Adc_value / 10
Adc_value = Getadc(3)
Ocr0b = Adc_value / 10
I think that this will work....
But please understand that the time base will be the same for the two outputs.
Paulo |
|
Back to top |
|
|
GIANNHS
Joined: 25 Mar 2016 Posts: 16 Location: Greece
|
Posted: Wed Apr 20, 2016 5:51 am Post subject: |
|
|
Yes APHAWK Your example will work and the result will be two identical pulses will simply regulate the time up with two DIFFERENT POT.
I will the SECOND PULSE (COM0B1) HAVE DELAYFROM THE FIRST pulse (com0a1) FROM 10us to 45us and its duration should be 500us stable not changing the pulse..
I want to set the time delay from 10us to 45us...
can distinguish the com0a1 ( Tccr0a = Bits(com0a1 , com0b1, Wgm00 , Wgm01) \\\'mode 3,fast
two pieces ,???? |
|
Back to top |
|
|
aphawk
Joined: 23 Jan 2010 Posts: 168 Location: Brazil
|
Posted: Wed Apr 20, 2016 1:26 pm Post subject: |
|
|
Sorry, I think that this can't be made because we have little program space. Maybe using ASM you can, but is better change the microcontroller.
As I told, the time duration for both PWM outputs will be the same. This happens because ATTINY13 has only one Timer/PWM , with two outputs.
To make what you want, you will need another microcontroller that has more program space, and use the new pot to adjust by software the time delay you need, reading the ADC and scaling the result to have the delay from 10 to 45 uSecs, and then make the total 500 uSecs pulse.
Or you can use another Timer, reading the output from the first timer and then make the delay you need reading the pot and scaling the result, and after using this other timer to make the 500 uSec pulse.
You see, there is many ways to make what you want, but we need change the Attiny13 to one that have more program space.
Paulo |
|
Back to top |
|
|
|
|
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
|
|