Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
johnwarfin
PostPosted: Jan 08, 2010 - 10:12 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

js wrote:
More like a HORROR movie. I hope none of the stuff he has "designed" is used to save or preserve people's lives. Shocked


FYI interrupts are not the path to truly deterministic code. And let's not talk about debugging, code verification, or portability.

You have my permission to sprinkle them EVERWHERE yourself. INTs r 4 U!
 
 View user's profile Send private message  
Reply with quote Back to top
zbaird
PostPosted: Jan 08, 2010 - 10:44 PM
Raving lunatic


Joined: Aug 13, 2006
Posts: 6761
Location: Bellingham, WA - USA

Out of curiosity, why is "truly deterministic" code necessarily an appropriate response of an interactive system responding to external stimuli?

_________________
Chuck Baird
"It's better to catch the trapeze than test the safety net" -- RPi book
http://www.cbaird.org
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
geoelec
PostPosted: Jan 08, 2010 - 11:32 PM
Resident


Joined: Jan 26, 2009
Posts: 535
Location: Thessaloniki Greece

First check the interrupt vector table between two micros,the stack pointer,peripheral registers and port registers.And everything can be tested and compiled in Avrstudio.
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 01:23 AM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

Just in case somebody is interested in a 8-bit (256 steps) multi-channel software PWM implementation, I'm pleased to share my code written several years ago.

It takes slightly more than 2 CPU clocks per channel and is usable in a wide range of applications. With 100% 16 MIPS CPU load it generates over 2 KHz 8-bit 8-channel PWM or 500 Hz 8-bit 8-channel PWM with only 25% CPU load:

Code:

;-----------------------------------------------------------------------------------------
;
; 8 channel interrupt driven 8-bit (256 steps) software PWM implementation example.
; r3..r10 hold channel duty cycle values
; PORTB pins are PWM channels' outputs (port can be redefined via PWMPORT alias)

.include        "tn2313def.inc"

.def    STSAV   = r2            ; SREG holding register for all non-nested interrupts
.def    pwm     = r15           ; PWM digital sawtooth generator used in  Timer 1 ISR
.def    tmp     = r16           ; General purpose temporary register
.def    inttmp  = r25           ; Common temporary register for all non-nested interrupts

.equ    pwmport = PORTB         ; PWM output port

.equ    fosc    = 16000000              ; CPU clock frequency, Hz
.equ    fpwm    = 2000                  ; Desired PWM frequency, Hz (2050 Hz max @16 MIPS)

.set    t1val   = fosc/fpwm/256-1       ; Timer 1 TOP reload value for given FPWM and FOSC

.if (t1val < 30)                ; Timer 1 ISR takes up to 30 clocks to execute
.warning "Selected PWM frequency is too high, forcing highest available (full CPU usage!)"
.set    t1val   = 30
.endif

.cseg
;-----------------------------------------------------------------------------------------
; Interrupt vectors:
        rjmp    reset

.org    OC1Aaddr
        rjmp    OC1Aisr
;-----------------------------------------------------------------------------------------

;-----------------------------------------------------------------------------------------
reset:
        ldi     tmp,low(RAMEND)
        out     SPL,tmp        ; Stack pointer initialization
.ifdef  SPH
        ldi     tmp,high(RAMEND)
        out     SPH,tmp
.endif

        ldi     tmp,0xFF        ; Set up PB0..7 as outputs
        out     DDRB,tmp

        ldi     tmp,32*1        ; Initialize r3..r10 (PWM channel values)
        mov     r3,tmp          ;
        ldi     tmp,32*2        ;
        mov     r4,tmp          ;
        ldi     tmp,32*3        ;
        mov     r5,tmp          ;
        ldi     tmp,32*4        ;
        mov     r6,tmp          ;
        ldi     tmp,32*5        ;
        mov     r7,tmp          ;
        ldi     tmp,32*6        ;
        mov     r8,tmp          ;
        ldi     tmp,32*7        ;
        mov     r9,tmp          ;
        ldi     tmp,32*8-2      ;
        mov     r10,tmp         ;

        rcall   init_timer1     ; Set up Timer 1

        sei                     ; Enable interrupts

        rjmp    PC              ; Loop forever
;-----------------------------------------------------------------------------------------

;-----------------------------------------------------------------------------------------
; Timer 1 initialization (Clear Timer on Compare match - CTC mode).
; Timer 1 runs at Fosc in CTC mode with overlow rate derived from a desired PWM frequency.

init_timer1:
        ldi     tmp,high(t1val)         ; Initialize TOP register (OCR1A) and Timer 1:
        out     OCR1AH,tmp              ; High byte first
        out     TCNT1H,tmp              ; Reset Timer 1 to get correct 1st result
        ldi     tmp,low(t1val)          ;
        out     OCR1AL,tmp              ; Low byte last
        out     TCNT1L,tmp              ;

        ldi     tmp,(1<<WGM12)|(1<<CS10) ; CTC mode, T1CLK = Fosc
        out     TCCR1B,tmp              ;

        in      tmp,TIMSK               ; Enable OC1A interrupts
        sbr     tmp,1<<OCIE1A           ;
        out     TIMSK,tmp               ;

        ret
;-----------------------------------------------------------------------------------------

;-----------------------------------------------------------------------------------------
; Timer1 (CTC mode) compare match ISR - single 8-bit software PWM step.
; 24 clocks, 21 program words

OC1Aisr:
        in      stsav,SREG      ; Save SREG to STSAV reserved register
 
        inc     pwm             ; Advance PWM sawtooth generator
 
        cp      r3,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r4,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r5,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r6,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r7,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r8,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r9,pwm          ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
        cp      r10,pwm         ; CARRY = 1 if Rx <= PWM - 2 clocks per PWM channel
        rol     inttmp          ; Shift in the CARRY to an INTTMP
 
        out     pwmport,inttmp  ; Set PWM 1..8 pins to new states

        out     SREG,stsav      ; Restore SREG from STSAV reserved register
        reti
;-----------------------------------------------------------------------------------------


Last edited by MBedder on Jan 09, 2010 - 08:41 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
zbaird
PostPosted: Jan 09, 2010 - 01:36 AM
Raving lunatic


Joined: Aug 13, 2006
Posts: 6761
Location: Bellingham, WA - USA

No stack initialization?

_________________
Chuck Baird
"It's better to catch the trapeze than test the safety net" -- RPi book
http://www.cbaird.org
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 01:53 AM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

Exactly. Guess why Very Happy

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Jan 09, 2010 - 03:32 AM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13957
Location: Vancouver, BC

Quote:
Exactly. Guess why

Because you want the program to fail at the first RCALL?

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 08:44 AM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

No, just due to a copy/paste mistake. BTW, for this particular code and device this was not necessary - guess why Very Happy Very Happy

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jan 09, 2010 - 02:57 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26113
Location: Wisconsin USA

Without looking it up, Tiny2313 probably has SP at RAMEND at reset anyway.

Again without looking it up, this sounds a lot like the Atmel app note on "multiple PWMs" or something like that.

With only one interrupt source enabled, an app like that would have little jitter and it wouldn't be accumulative as noted in the Atmel app note.

I guess if jitter was a problem in, say, a multi-channel hobby servo the "secondary" data channels such as UART or trigger I/Os or heartbeat timers could be polled.

Lee
 
 View user's profile Send private message  
Reply with quote Back to top
johnwarfin
PostPosted: Jan 09, 2010 - 03:03 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

zbaird wrote:
Out of curiosity, why is "truly deterministic" code necessarily an appropriate response of an interactive system responding to external stimuli?


Mostly for runtime validation and testing. It get's particularly bad when multiple ints are involved. You can get high reliabilty figures but never quite reach 100% unlike deterministic code which can. And then there are the development issues (debugging) which become orders of magnitude more difficult with interrupts.

They are somtimes necessary but are hugely over-used, specially by inexperienced designers who like to show off instead of get the job done. Kinda like with processor specific peripherals. You know, the guys that use a timer to blink an LED. :rolleyes:
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 04:08 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

theusch wrote:
Without looking it up, Tiny2313 probably has SP at RAMEND at reset anyway.

Nope - its at 0x00 at reset.
The answer is simple - since my code is less than 256 words long and doesn't use nested calls/pushes, a single level 8-bit stack location is enough. This location (0x00) is mapped to an R0 register which is not used anywhere else in my code.

When a call or interrupt occurs the caller's PCL is saved to R0, then SP gets underflown to 0xFF and the caller's PCH is attempted to be saved to an unimplemented RAM location which simply does not perform any action. At RET/RETI the caller's PCH gets read from the said unimplemented location which yields to a 0x00 value. Then SP gets incremented pointing to R0 so the caller's PCL as well the entire PC gets properly restored.

Quote:
Again without looking it up, this sounds a lot like the Atmel app note on "multiple PWMs" or something like that.
I've never read that appnote. My code was written to demonstrate the AVR advantages over PIC18 in one of the numerous AVR vs PIC holy wars on one of the Russian technical forums.

The opponents tried to prove that the PIC18 code takes 2 instruction per PWM channel (taking advantage of PIC's DECFSZ instruction missing in AVR) whereas AVR code takes 3. I (aka AVR that times Smile) realized how to do it in two AVR instruction per channel and proposed this 24-channel PWM code which accumulates the channel/sawtooth comparison results in a register and then outputs the whole 8-bit value to a port. The opponents had fled from the battlefield in shame and fear, and the AVR was completely avenged and acquitted Very Happy

P.S. Sorry, the URLs above are in Russian.
 
 View user's profile Send private message  
Reply with quote Back to top
johnwarfin
PostPosted: Jan 09, 2010 - 05:16 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

MBedder wrote:
theusch wrote:
Without looking it up, Tiny2313 probably has SP at RAMEND at reset anyway.

Nope - its at 0x00 at reset.


Quote:

SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 SPL
7 6 5 4 3 2 1 0
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 1 0 0 1 1 1 1 1


Theusch apparently "bedder" at AVR than PICboy.

PS Generating legal servo siganls IS one of the few areas PIC has it over AVR. That and external access to op amp.
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 05:37 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

17th hat to eat - shouldn't be that easy, cowboy Very Happy Very Happy Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
johnwarfin
PostPosted: Jan 09, 2010 - 06:39 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

MBedder wrote:
17th hat to eat - shouldn't be that easy, cowboy Very Happy Very Happy Very Happy


Sorry. I had tiny13.

As far as hats you seem unable to differentiate between changing a port and changing a bit but also appear to think you can turn on an LED with 0 lines of code. To reiterate, you were suckered from the start when I challenged you to write a smaller program than my one liner.

Keep ignoring these simple realities as it's very entertaining. Again and again. I certainly never get tired of pointing this out. Smile
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 06:51 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

18th Very Happy Very Happy Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
johnwarfin
PostPosted: Jan 09, 2010 - 06:53 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

LOL! You sound like you're upset. Are you upset? Smile Smile Smile Smile Smile
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 09, 2010 - 07:00 PM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

Huh? Me?? How could I get upset with a boyscout hat eater who does not wear glasses and thinks that everything in this world was done with a Photoshop??? C'mon, cowboy - I already got tired to LOL Very Happy Very Happy Very Happy

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
johnwarfin
PostPosted: Jan 09, 2010 - 07:16 PM
Hangaround


Joined: Dec 29, 2009
Posts: 160
Location: ny,ny

Uh oh... I think he's upset. He's definitely upset.

And apparently read my post before discoveing the error. Welp... at least SOME of us are able to admit we made a mistake.
BTW I only said I'd eat ONE hat if you could turn on an LED with a PIC using less than one line of code. And never mentioned hats on the SPL deal. Not too good at math, are we? 0, 1, 16, 17, 18?

LOL!
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jan 09, 2010 - 09:12 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 26113
Location: Wisconsin USA

Quote:

The opponents had fled from the battlefield in shame and fear, and the AVR was completely avenged and acquitted

Wink

In all the Holy Wars, it must be remembered that a specific "trick" only matters in the edge-condition situations. In general, micros aren't totally full and are loafing.

E.g., your stack trick above--who cares if the micro is 2% full or 3% full, or if it takes a microsecond longer to complete startup?

Quote:

I've never read that appnote.

AVR136: Low-jitter Multi-channel Software PWM
http://www.atmel.com/dyn/resources/prod ... oc8020.pdf
 
 View user's profile Send private message  
Reply with quote Back to top
MBedder
PostPosted: Jan 10, 2010 - 09:24 AM
Raving lunatic


Joined: Nov 02, 2009
Posts: 3239
Location: Zelenograd, Russia

As I stated above, it was not a trick but just a copy/paste error. But since I tested that the code is working before posting it here, I wondered why, so I analyzed the details and posted my explanation above.

Thanks for the link. BTW according to its code it works 8 times slower than my code.

_________________
Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits