mercredi 8 janvier 2014

Little system made with a Petrol heater sensor and an this AVR.
When the A/D converter voltage is over threshold, the Mic sends 3 pulses on the call cell phone button (this wakes him up, and calls the last number called stored in the calling list).
After 15 sec, it hooks down.
It waits then a call in to stop the routine. If no answer, the routine is repeated 4 times.
If still no answer, the Mic wait a while (10mn) to redo the operation if the temperature is still up.
Temperature threshold to be set.

/*
 * Tempcellularalarm.asm
 *
 *  Created: 25/07/2013 14:33:48
 *   Author: fg
 */


 ; ********************************************
; * [Add Project title here] *
; * [Add more info on software version here] *
; * (C)20xx by [Add Copyright Info here] *
; ********************************************
;
; Included header file for target AVR type
.NOLIST
.INCLUDE "tn13Adef.inc" ; Header for ATTINY13
.LIST
;
; ============================================
; H A R D W A R E I N F O R M A T I O N
; ============================================
;
; [Add all hardware information here]
;
; ============================================
; P O R T S A N D P I N S
; ============================================
;
; [Add names for hardware ports and pins here]
; Format: .EQU Controlportout = PORTA
; .EQU Controlportin = PINA
; .EQU LedOutputPin = PORTA2
;
; ============================================
; C O N S T A N T S T O C H A N G E
; ============================================
;
; [Add all constants here that can be subject
; to change by the user]
; Format: .EQU const = $ABCD
;
; ============================================
; F I X + D E R I V E D C O N S T A N T S
; ============================================
;
; [Add all constants here that are not subject
; to change or calculated from constants]
; Format: .EQU const = $ABCD
;
; ============================================
; R E G I S T E R D E F I N I T I O N S
; ============================================
;
; [Add all register names here, include info on
; all used registers without specific names]
; Format: .DEF rmp = R16
.DEF loop1r = R9
.DEF loop2r = R10
.DEF loop3r = R11                ; Loops registers for 0,5 second timer
.DEF Tmaxl = R12
.DEF Tmaxh = R13                ; Low and high bytes for the threshold comparing
.DEF ENDMS = R15                ; 0 register to compare with a counter register (CPI and Skip or branch if equal)
.DEF rmp = R16                    ; Multipurpose register
.DEF Ring = R17                    ; Ring flag
.DEF Flags = R19                ; End conversion flag      
.DEF COUNTER = R21              
.DEF COUNTERVAL = R23          
.DEF Timerloopcounter = R24        ; multipurpose counters



;
; ============================================
; S R A M D E F I N I T I O N S
; ============================================
;
.DSEG
.ORG 0X0060
; Format: Label: .BYTE N ; reserve N Bytes from Label:

; ============================================
; R E S E T A N D I N T V E C T O R S
; ============================================
;
.CSEG
.ORG $0000

rjmp Main ; Reset vector
reti ; Int vector 1
rjmp PIN0INTERRUPT                        ; Triggered when a ring come in
reti ; Int vecTOR 3
reti ; Int vector 4
reti ; Int vector 5
rjmp Timerreloadinterrupt                ; Large prescaler timer loop interrupt
reti ; Int vector 7
reti ; Int vector 8
rjmp endconversion                        ; A/N conversion end interrupt
;
; ============================================
; I N T E R R U P T S E R V I C E S
; ============================================


PIN0INTERRUPT :
    clr ring
    reti                        ; This routine clears the ring flag so the program stop calling or hang up.

Timerreloadinterrupt :
    dec    Timerloopcounter
    sbi Pinb, 0                    ; Toggle led
    reti                  

endconversion :
    inc Flags
    reti                        ; Set the flag to follow down when conversion is done

; ============================================
; M A I N P R O G R A M I N I T
; ============================================
;.Avr-Asm-Tutorial 15 http://www.avr-asm-tutorial.net

Main:

; Init stack************************************************************

  

    LDI rmp, LOW(RAMEND) ; Lower byte
    OUT SPL,rmp ; to stack pointer

; Init Port B Directions************************************************

    ldi rmp, 0b00001000 ; Direction of Port B

                    ; 0 = Led/Ring in
                    ; 1 = Phone pick-up
                    ; 2 = Hook down
                    ; 3 = none
                    ; 4 = Sensor
                    ; 5 = Reset
    out DDRB,rmp



; Init Port B levels******************************************************

     ldi rmp, 0b0000000
    out PortB, rmp

; Set watchdog timer *****************************************************

    ldi rmp, 1<    out WDTCR, rmp                    ; Watchdog enable
    ldi rmp, (1<    out WDTCR, rmp                    ; Set WDT to 8 s

; ============================================
; P R O G R A M
; ============================================
  
  

    ldi Timerloopcounter, 6
loopV :
    rcall secondtimer05
    dec Timerloopcounter
    brne loopV
  

start :
    WDR
    sbi DDRB,0
    sbi PORTB,0            ; Switch on led (DDRB to out and PortB to + 5 V)

;measureloop :

    ldi Flags, 0        ; reset done conversion flag

;Check :

    ldi rmp, (1<    out DIDR0, rmp                                ; Enable buffer on pin 4 only
    ldi rmp,  (0<    out ADMUX, rmp                                ; Switch off the other multiplexers          
                      
    ldi rmp, 0b11001000
    out ADCSRA, rmp                                ; Enable conversion complete interruption  
                                                ; Enable analog converter
                                                ; Start conversion  
                  
    sei                                            ; Enable interrupts

Loopwaitconv :
    cpi Flags,1                                    ; Compare flags register with 1    (Flag register-1)
    brne Loopwaitconv                            ; If not 0 loop back
  
    cli                                            ; If flag set, disable interrupts
  
    ldi rmp, 0
    out ADCSRA, rmp        ; Disable conversion complete interruption  
                        ; Disable analog converter
                        ; No conversion  


    ;ldi rmp, 0x43
    ldi rmp, -6
    mov Tmaxl, rmp
    ;ldi rmp, 0x01
    ldi rmp, -6
    mov Tmaxh, rmp                    ; Load threshold

    in rmp, adcl
    sub rmp, Tmaxl                    ; Compare
    in rmp, adch
    sbc rmp, Tmaxh                    ; Compare

    brpl Phonecall                    ; Over? => Call
                                    ; otherwise, follow down

; LARGEPRESCALERTIMER 10 min Settings****************************************************************************

LARGEPRESCALERTIMER :

    LDI Timerloopcounter, 100
    LDI COUNTERVAL, 220
    RCALL LARGEPRESCALERTIMER        ; Set to ~10 min wait
    rjmp start



;LARGEPRESCALERTIMER*************************************************************************

        ; Settings to count with 0,027 second per cycle
                        ; Time = ([312,52 to 392,56 ) + [COUNTERVAL*27322 µs+ 10*26,6 µs ]*Timerloopcounter
                        ; If Timerloopcounter = 0 => Time =  339,12 µs +COUNTERVAL*27307 µs

    ldi rmp, (1<    out    TCCR0A, rmp                        ;CTC MODE
    ldi rmp, (1<    out TIMSK0, rmp                        ;OCA INTERRUPT ENABLE  
    out    OCR0A, COUNTERVAL                ;OcA value  

    ldi    rmp, (1<    out GTCCR , rmp                ;Set synchro bit GTCCR 7 which synchro the TIMER prescaler    
    ldi rmp, 1<    out    CLKPR ,rmp                ;Set ClkPRES enable        ;                              
    ldi rmp, (1<    out    CLKPR ,rmp                ;Set CLkpres to /256    ;0,83 µs * (4+12) = 13,28 µs

    ldi r16, 5                                                              
    out TCCR0B, r16                ;Set timer prescaler to /1024              
    ldi    rmp, 0<    out GTCCR , rmp                ;Clear GTCCR 7                              
    sei                                                ;5*26,6µs = 133µs    Nb : The timer starts counting from here
                                                        ; Init = 146,28 µs

WaittimerTEN :
    wdr                            ; Watchdog reset.
    cpse Timerloopcounter, ENDMS            ;[OCR0A*27307 µs+(4+2+4)*26,6]*Timerloopcounter
    rjmp WaittimerTEN
    sbi PINB,0                    ; TOGGLE PIN SO THE LED BLINKS WHILE WAITING

EXIT:
    cli
    ldi rmp, 1<    out    CLKPR ,rmp                ;Set ClkPRES enable                                ;2
    ldi rmp, (0<    out    CLKPR ,rmp                ;Set CLkpres to /8                                ;4 = (6 to 9)*26,6 µs = 159,6 to 239,4 µs
    ldi rmp, (0<    out TIMSK0, rmp                ;OCA INTERRUPT DISABLE                            ;2
    nop                                                                            ;3
    ret                                                                            ;4+4)*0,83µs = 6.64 µs
                                                                            ; Exit = (159,6 to 239,4 µs)+ 6.64 µs = 166,24 to 246,04 µs

              

;Pheaune kall*************************************************************************

Phonecall :
  
    ldi counter, 5                            ; Set 4 times calling
    ldi ring, 1                                ; Set ring flag

Phonecallloop :

    dec counter  
    breq lp                                    ; If 4 Times call, go to end

    sbi DDRb, 1
    rcall secondtimer05
    cbi DDRb, 1
    rcall secondtimer05
    sbi DDRb, 1
    rcall secondtimer05
    cbi DDRb, 1
    rcall secondtimer05
    sbi DDRb, 1
    rcall secondtimer05
    cbi    DDRb, 1
    rcall secondtimer05                        ; Hang up (This model needs 3 times Hang up pushing,
                                            ; Wake-up from sleep, last number dialled display, and pick-up)


  
    ldi Timerloopcounter, 20
loopIV :
    WDR
    rcall secondtimer05
    dec Timerloopcounter
    brne loopIV    ;                        ; Wait (4 tones or the messagebox)
  

    sbI DDRB, 2
    rcall secondtimer05
    cbi DDRB, 2                            ; Hook down

;Wait & Answer check********************************************************
  
    cbi portb, 0                      
    cbi DDRB, 0                    ; Pin0 to in on ring receive mode
    ldi rmp, 1<    out GIMSK, rmp                ; Enable interrupts
    ldi rmp, 1<    out PCMSK, rmp                ; Set Interrupt on pin 0  
    SEI



        ldi COUNTERVAL, 2
loopVI :
    ldi Timerloopcounter, 230
    LDI Timerloopcounter, 10
loopIII :
    WDR
    rcall secondtimer05
    dec Timerloopcounter
    brne loopIII
    dec COUNTERVAL
    brne loopVI
    cli                                        ; Wait ~ 5 min an answer

    ldi rmp, 0<    out GIMSK, rmp
    ldi rmp, 0<    out PCMSK, rmp                            ; Off Interrupt on pin 0

    cpi Ring, 0                                ; Compare Ring with 0 (Ring-0)
    breq lp                                    ; If answer (Ring register decreased) GOto end
    rjmp Phonecallloop                        ; If not finished, phone again
  

; secondtimer05***************************************************

    secondtimer05 :

    ldi rmp, 4
    mov loop3r, rmp
    ldi rmp, 77
    mov loop2r,rmp
    mov    loop1r,rmp

loop3 :
    dec loop3r
    BRNE loop2                ; 0,5 second timer
    ret

loop2 :
    dec loop2r
    breq loop3
loop1 :
    dec loop1r
    breq loop2
    rjmp loop1


; End of source code
                                ;