View previous topic :: View next topic |
Author |
Message |
CrazyIgels
Joined: 26 Oct 2007 Posts: 79
|
Posted: Fri Oct 26, 2007 7:03 am Post subject: Two newcomer trouble |
|
|
Hello ALL.
First - i ask you - please sorry my bad english writing skils.
I have two trouble with BASCOM AVR and my Mega 64 board.
1. I need to generate a 5 kiloGertz square signal (_|``|_) on any pin of PORT D
How i can make this?
2. I need to read data from external device. Data is 4 - 20 mA (industrial automation current loop). How i can use my Mega64 oncrystal ADC? I need external resistor to measure voltage drop or something else?
Thanks ALL. Have a good day. |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
|
Back to top |
|
|
CrazyIgels
Joined: 26 Oct 2007 Posts: 79
|
Posted: Fri Oct 26, 2007 8:48 am Post subject: |
|
|
Used system clock? (MHz)? - I use 7.3728 external clock signal
Tolerance for the 5kHz signal? (+/- Hz)? - 150 - 200 Hz - good, but less diference - better |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Fri Oct 26, 2007 10:09 am Post subject: |
|
|
Hi,
Here you can see two solutions.
The first solution uses Timer2 and the square wave is generated 100% with hardware.
(No CPU time is used to generate the square wave).
This solution works only if the output pin is OC2. If you use the other timers of
the ATmega64, then the output will be on different pins. (See the datasheet of the ATmega64).
The second solution uses also Timer2 but the pin OC2 is not used.
The timer is used to trigger an interrupt and with this interrupt
we can toggle any pin we like. This solution will use CPU time.
If other interrupts are enabled, our interrupt service routine could be
delayed and therefore the 5kHz output frequency altered.
Best regards,
Luciano
The square wave you get:
(Click to enlarge)
First solution: 100% hardware
Code: | ' Example of 4980Hz square wave output on pin OC2. (PB7).
$regfile = "m64def.dat"
$crystal = 7372800
$hwstack = 32
$swstack = 16
$framesize = 40
' ======================================================
' Clear Timer on Compare (CTC mode), Top= OCR2
' OC2 PIN toggled at compare match
' Prescaling: System Clock / 8
TCCR2=&H1A ' See datasheet for details
OCR2=&H5C ' 4980Hz with system clock = 7.3728MHz
Config PinB.7 = Output ' OC2 PIN
' ======================================================
Do
!Nop
Loop
End |
Second solution: With Timer2 Compare match interrupt enabled
Code: | ' Example of 4980Hz square wave output on any pin you like.
$regfile = "m64def.dat"
$crystal = 7372800
$hwstack = 32
$swstack = 16
$framesize = 40
' ======================================================
' Clear Timer on Compare (CTC mode), Top= OCR2
' OC2 PIN: Disabled
' Prescaling: System Clock / 8
TCCR2=&H0A ' See datasheet for details
OCR2=&H5C ' 4980Hz with system clock = 7.3728MHz
' ======================================================
Config PinD.4 = Output ' Can be any pin
On OC2 Compare2_int
Enable OC2
Enable Interrupts
Do
!Nop
Loop
End
Compare2_int:
' This interrupt service routine is executed
' every 100.5 microseconds. If other interrupts are enabled
' this interrupt service routine could be delayed and
' therefore the 5kHz output frequency altered.
Toggle PortD.4 'Can be any pin
Return |
|
|
Back to top |
|
|
CrazyIgels
Joined: 26 Oct 2007 Posts: 79
|
Posted: Fri Oct 26, 2007 12:29 pm Post subject: |
|
|
Thanks.
Solution two working very good and stable. |
|
Back to top |
|
|
|