View previous topic :: View next topic |
Author |
Message |
wiktor
Joined: 26 Feb 2008 Posts: 1
|
Posted: Wed Feb 27, 2008 12:39 am Post subject: shiftin / out problem? |
|
|
I am building a device which consist of 3 attiny2313, 2 ADC MX7821 and 1 atmega644. I use crystal 18.432MHz (fuses set). Two attiny communicate with MX7821s, change the data format from parallel do serial and send it via shifout to atmega (data channels ch1 and ch2). Third attiny measures temperature from DS18S20 (data channel ch3). Sampling frequencies of ch1 is 2000Hz, ch2 – 4000Hz and ch3 – 1Hz.
Atmega sends bytes in order: (1) ch2 ch2 ch1 …. (6144) ch2 ch2 ch1 ch3 (6147) via hardware UART. The problem is after few seconds data becomes mixed up.
My suggestions are:
1. Badly implemented sampling technique (samples come in not constant time periods)
2. Shiftin / out without delay works improperly for my crystal
Sampling is done by timer0 in attiny. Crystal is prescaled by 64 and counts to 12 (timer0 = 244). Twelve following samples are meaned to get 2000Hz. In simulation it looks fine.
Shiftin / out seems to be loosing bits or sending false bits. I.e. when I try to send &B10000001 after few seconds I get &B11000000 (probably MSB is the LSB of a previous byte). I used timer0 in atmega to see if shiftin time is shrinking or something and indeed it shrinks and then looses byte.
My questions:
1. Is my crystal too fast to use it without delay? If so, how can I do it in different way
2. Is that possible that shifting takes different time periods?
3. Is my sampling technique correct?
I am rather a newbie but spend some time playing bascom. Please help.
Main part of attiny program – shifts out data with 2000Hz:
Dim Sr as word, Bajt2 as byte, Bajt as byte
Config Timer0 = Timer , Prescale = 64
On Timer0 Getdata
Enable Interrupts
Enable Timer0
Timer0 = 200 ‘for the first time I am loading more to prevent overlapping of ch1 and ch2
Do
!NOP
Loop
End
Getdata:
Timer0 = 244
‘stand alone mode
Reset Portd.2 '~CS = low
Reset Portd.4 '~RD = low
Reset Portd.5 '~wr = low
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
Portd.5 = 1 '~wr = high
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
!NOP
'constantly sends 129 for testing, in normal mode shoud be pinb
Bajt = 129 'Pinb
Sr = Sr + Bajt
Z = Z + 1
If Z > 12 Then
Sr = Sr / 12 'mean
Bajt2 = Sr
Shiftout Dane_out , Clk , Bajt2 , 0
Z = 1
Sr = 0
End If
Return
Main part of atmega program (this one implements only data chain of ch2 ch2 ch1….):
Dim bajt1 as byte, bajt2 as byte, bajt3 as byte
Do
Shiftin Dane_ch2 , Clk_ch2 , Bajt3 , 5
If Bajt2 <> 100 Then
Toggle Diode6
End If
Shiftin Dane_ch2 , Clk_ch2 , Bajt2 , 5
If Bajt2 <> 100 Then
Toggle Diode5
End If
Shiftin Dane_ch1 , Clk_ch1 , Bajt1 , 5
Printbin Bajt3 ; Bajt2 ; Bajt1
If Bajt1 <> 129 Then
Toggle Diode4
End If
Loop
End |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Feb 29, 2008 8:44 am Post subject: |
|
|
I never used the shiftin/shiftout commands, but they should be immune to clock speed or timing issues, since they rely on synchronised clock and data signals.
However you must be aware that they use software to generate all those bit streams. Personally I would avoid software serial stuff unless you *really* need it.
If you can, use the hardware SPI systems in each processor instead. You will have to designate one device, probably the AtMega, as master, and send a data request and chip select to each of the tiny avrs in turn. Then they can put the data they have on the spi bus, and you read it from the master with the spiin command. That way you still get synchronised transfers, but avoid tying up the processors with software bit-banging. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
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
|
|