home | area personale         schemi | tutorial | robotica | pic micro | recensioni         forum | chat irc         faq | contatti         store | Ordina PCB
username
password
cerca

 
FORUM: Pic Micro
Tutto quanto riguarda questi microprocessori... progetti, suggerimenti, aiuti, discussioni...ecc


I2C - PIC16f877 - assembly
     
Autore Messaggio opzioni
picmicro675




una ogni 10 livelli


postato il:
10.09.2018, alle ore 19:58
Poi l' esempio chiaro lo si vede in DS00735A-page 23.
C'è solo da dire che per ogni operazione si tiene conto della posizione del byte da inviare e gli eventuali flag di errore trasmissione.



Anno nuovo, forum nuovo.
Mi sa che lascio.
angelole





postato il:
16.09.2018, alle ore 14:29
Ho fatto è rifatto come consigli tu .... ma niente. È più complicato di quanto pensassi. Ho peraltro seguito pedissequamente un documento ufficiale della microchip senza alcun valido risultato... Non so più cosa fare... peraltro in rete c'e' tanta roba sbagliata o incompleta ...
picmicro675




una ogni 10 livelli


postato il:
16.09.2018, alle ore 20:57
Intanto ti rimetto l' estratto redatto della AN735
http://www.grix.it//UserFiles/picmicro675/File/TransitFiles/…
Si potrebbe cominciare ad estrapolare le routines di gestione i2c master. Basta riferirsi alle chiamate
La conversione in macro, mi sembra elaborata, dato che si determina un lavoro tramite interrupt.
Qui di seguito hai il codice di Proton Basic
Device 16F877A
Declare Xtal = 4

Config CP_OFF, DEBUG_OFF, CPD_OFF, LVP_OFF, BODEN_OFF, WRT_OFF, _
       PWRTE_OFF, WDT_OFF, XT_OSC

Symbol ADD_DS1307_PICRX %11010001
Symbol ADD_DS1307_PICTX %11010000

ADCON0 = 0
ADCON1 = 7
TRISA = 0
TRISB = %00001111
TRISC = %00011000
TRISD = %00011000
TRISE = 0
Dim DS1307_Array[8] As Byte
Dim RTC_Second    As DS1307_Array#0     ' Second (0..59)
Dim RTC_Minute    As DS1307_Array#1     ' Minute (0..59)
Dim RTC_Hour      As DS1307_Array#2     ' Hour   (0..11 or 0..23)
Dim RTC_DayOfWeek As DS1307_Array#3     ' Day of the week (1..7)
Dim RTC_Day       As DS1307_Array#4     ' Date   (0..31)
Dim RTC_Month     As DS1307_Array#5     ' Month  (1..12)
Dim RTC_Year      As DS1307_Array#6     ' Year   (0..99) 

HBusIn  ADD_DS1307_PICRX, [Str DS1307_Array]

In pratica la dichiarazione dell' array permette di prendere tutti i dati del DS1307 e suddividerli.

Con un po di tempo vedo di estrapolare il sorgente dell' assembly. Ci sono delle chiamate a delle routine che ormai hanno perso i nomi delle variabili, quindi allunga l' elaborazione....



Anno nuovo, forum nuovo.
Mi sa che lascio.
picmicro675




una ogni 10 livelli


postato il:
17.09.2018, alle ore 02:15
Dunque... Redatto il risultato in assembler del sorgente basic e verificato che si compila.
        ;PROCESSOR      16F877A
        RADIX          DEC
        INCLUDE        "P16F877A.INC"
        ERRORLEVEL     -302
        __CONFIG       0x3D39

        #define     ADD_DS1307_PICRX b'11010001'
        #define     byte_len 8
        #define     _I2C_PORT PORTC
        #define     _I2C_SCL_PIN 3
        #define     _I2C_SDA_PIN 4
        #define     PP_BF  0
        #define     PP_ACKSTAT  6
        #define     PP_ACKDT  5
        #define     PP_ACKEN  4
        #define     PP_RCEN  3
        #define     PP_PEN  2
        #define     PP_RSEN  1
        #define     PP_SEN  0
        #define     PP_R_W  2

        CBLOCK 0x20
        BPF, GPR, PBP#VAR0, PP0, PP3, PP4, DS1307_Array:byte_len
        ENDC
HBUSIN2                                 ; input routine with start condition
        bsf         BPF,4
        btfss       BPF,4
HBUSIN
        bcf         BPF,4
        btfsc       BPF,5
        goto        $ + 5
        bsf         BPF,5
        call        HBUS@RST
        skpnc
        goto        HBUS@STP
        bcf         BPF,2
HBUS@INA
        bsf         STATUS,RP0
        bsf         SSPCON2,PP_RCEN
        btfsc       SSPCON2,PP_RCEN
        goto        $ - 1
        btfss       SSPSTAT,PP_BF
        goto        $ - 1
        bcf         STATUS,RP0
        movf        SSPBUF,W
        btfsc       BPF,2
        goto        HI2C@EX
        btfsc       BPF,4
        goto        $ + 6
HBUS@ACK                                ; send a ACK signal
        bsf         STATUS,RP0
        bcf         SSPCON2,PP_ACKEN
        bsf         SSPCON2,PP_ACKEN
        btfsc       SSPCON2,PP_ACKEN
        goto        $ - 1
        bcf         STATUS,RP0
        btfsc       BPF,4
        call        HBUS@STP
HI2C@EX                                 ; exit the transmission
        clrc
        movwf       PP0
        goto        HI2C@FIN
HBUSOUT2                                ; output routine with start condition
        bsf         BPF,4
        btfss       BPF,4
HBUSOUT
        bcf         BPF,4
        btfsc       BPF,6
        goto        $ + 6
        andlw       254
        movwf       GPR
        bsf         BPF,6
        clrc
        goto        HI2C@FIN
        btfsc       BPF,7
        goto        $ + 7
        movwf       PP3
        bsf         BPF,7
        call        HBUS@ST
        skpnc
        goto        HBUS@STP
        movf        PP3,W
        call        HBUS@WRC
        skpnc
        goto        HBUS@STP
        btfss       BPF,4
        goto        HI2C@FIN
HBUS@STP                                ; send a STOP to the bus
        bsf         STATUS,RP0
        bsf         SSPCON2,PP_PEN
        btfsc       SSPCON2,PP_PEN
        goto        $ - 1
        bcf         STATUS,RP0
        bcf         BPF,6
        bcf         BPF,7
        bcf         BPF,5
        bcf         BPF,2
HI2C@FIN                                ; finalize the transmission
        bsf         PP4,0
        skpc
        bcf         PP4,0
        goto        I@NT
HBUS@RST                                ; send RESTART
        call        HBUS@RESTRT
        goto        $ + 2
HBUS@ST                                 ; send a STOP
        call        HBUS@STRT
        movf        GPR,W
        btfsc       BPF,5
        iorlw       1
HBUS@WRC                                ; wait for write end
        movwf       SSPBUF
        bsf         STATUS,RP0
        btfsc       SSPSTAT,PP_R_W
        goto        $ - 1
        bcf         STATUS,RP0
        clrf        PP0
HBUS@LP1                                ; wait for ACK
        clrc
        bsf         STATUS,RP0
        btfss       SSPCON2,PP_ACKSTAT
        goto        I@NT
        bcf         STATUS,RP0
        goto        $ + 1
        decfsz      PP0,F
        goto        HBUS@LP1
        setc
        return
HBUS@STRT                               ; send a START condition, with pin
        bsf         STATUS,RP0          ; cleared
        bsf         _I2C_PORT,_I2C_SCL_PIN
        bsf         _I2C_PORT,_I2C_SDA_PIN
        bsf         SSPCON2,PP_SEN
        btfsc       SSPCON2,PP_SEN
        goto        $ - 1
        goto        I@NT
HBUS@RESTRT                             ; send a restart condition
        bsf         STATUS,RP0
        bsf         SSPCON2,PP_RSEN
        btfsc       SSPCON2,PP_RSEN
        goto        $ - 1
I@NT
        bcf         STATUS,IRP
        bcf         STATUS,RP0
        bcf         STATUS,RP1
        return
;******************************************************************************
;               TEST to send a string to DS1307
;******************************************************************************
        bcf         STATUS,RP0
        ; HBUSIN  ADD_DS1307_PICRX,[STR DS1307_ARRAY]
        movlw       ADD_DS1307_PICRX        ; send address on the bus to DS1307
        call        HBUSOUT
        clrf        PBP#VAR0                ; reset lenght counter
PB@LB6
        call        HBUSIN
        movf        PBP#VAR0,W              ; adjust to point to the array
        addlw       DS1307_Array            ; unoptimized
        movwf       FSR                     ; with inidrect addressing
        movf        PP0,W                   ; reload i2c saved value
        movwf       INDF                    ; save it into the array
        incf        PBP#VAR0,F              ; increase the counter
        movf        PBP#VAR0,W              ; and put in W
        sublw       byte_len-1              ; is it maximum ?
        bnz         PB@LB6                  ; no, loop again
        call        HBUSIN2                 ; set to terminate the connection
        movf        PBP#VAR0,W              ; including the stop signal
        addlw       DS1307_Array
        movwf       FSR                     ; and write last byte
        movf        PP0,W
        movwf       INDF                    ; last location

        goto        $
        end
;        movlw       DS1307_Array           ; optimized
;        movwf       FSR                    ; optimized
;        movlw       byte_len               ; optimized
;        movwf       PBP#VAR0               ; optimized
;        call        HBUSIN                 ; optimized
;        incf        FSR,F                  ; optimized
;        movf        PP0,W
;        movwf       INDF                   ; optimized
;        decfsz      PBP#VAR0               ; optimized
;        goto        PB@LB6                 ; optimized
;        call        HBUSIN2                ; optimized
;        incf        FSR,F                  ; optimized
;        movf        PP0,W
;        movwf       INDF                   ; optimized

Dovrebbe caricare il risultato della lettura in DS1307_Array e successive 7 locazioni. Attenzione ai salti di pagina.
Per la scrittura, basta poco, facendo all' incirca quello che fa in fondo, ma con il busout. Ma in un certo frangente, lo farei fare al basic e quindi redarre il risultato.
Comunque le routine fanno il suo lavoro e ho provato col simulatore, qualcosa di più elaborato. Questa però non l' ho ancora provata. Ma mi intendo che le funzioni sono quelle normalmente impiegate nel proton basic e ben collaudate.

Da notare l' ultima parte (commentata) che penso sia meno convulsa di far i calcoli per incrementare il puntatore.



Anno nuovo, forum nuovo.
Mi sa che lascio.
picmicro675




una ogni 10 livelli


postato il:
17.09.2018, alle ore 02:19
Ah! dimenticavo che quella routine dovrebbe riciclare al punto di chiamata HBUSIN.


Anno nuovo, forum nuovo.
Mi sa che lascio.
picmicro675




una ogni 10 livelli


postato il:
17.09.2018, alle ore 02:22
Ah! dimenticavo (ancora) che la dimostrazione legge dal DS1307, sebbene ho messo
TEST to send a string to DS1307



Anno nuovo, forum nuovo.
Mi sa che lascio.
angelole





postato il:
19.09.2018, alle ore 17:17
Non vedo il settaggio dei registri nell'assembler del sorgente basic ... perchè?
angelole





postato il:
19.09.2018, alle ore 17:24
Questo è l'ultimo codice... ma niente, oltre al bit start non vedo nulla sull'analizzatore...


               #define        LC01CTRLIN    H'A0'
              #define        LC01CTRLOUT   H'A1'
              #define        LC01ADDR      H'12'
              #define        LC01DATA      H'34'

              #define        BAUD          D'100'
              #define        FOSC          D'4000'


              PROCESSOR      16F877A
          RADIX          DEC
              INCLUDE        "P16F877A.INC" 
              ERRORLEVEL     -302
              __CONFIG       0x3D39     ;_CP_OFF   _DEBUG_OFF   _WRT_ENABLE_OFF
              ; _CPD_OFF   _LVP_OFF   _BODEN_OFF   _PWRTE_OFF   _WDT_OFF   _XT_OSC
      
CBLOCK 30H      
              Dly0
              Dly1
              Timer1                  ; contatore    per DelayP
              Timer2                  ;    "              "
              Timer3                  ;    "              "
              DispTemp                   ; contenitore temporaneo per calcoli routine display
              DispCnt           
              TabTemp           
              Varie                      ; subroutine bip
              F20    
              Temp
              ADD_DS1307_PICRX           ; indirizzo nel caso in cui il up legga i dati dall'orologio
              ADD_DS1307_PICTX           ; indirizzo nel caso in cui il up scriva i dati sull'orologio
ENDC   

;***************************************************************
;      PROGRAMMA PRINCIPALE
;***************************************************************
Start
          org       0x00
;***************************************************************
;        Inizializzazione sistema e variabili   
;***************************************************************  
Inizializzazione     

          clrf       PORTA          ; port A = 0
          clrf       PORTB         ; port B = 0
          clrf       PORTC        ; port C = 0
          clrf       PORTD        ; port D = 0
              ;-------------------
              BANKSEL    ADCON0
              movlw      00000000b
              movwf      ADCON0
              BANKSEL    ADCON1
              movlw      00000111b
              movwf      ADCON1
;--------------------------------------------------------------------------------------------------
;             0=uscita  1=ingresso
;--------------------------------------------------------------------------------------------------
           
              BANKSEL    TRISB          
              movlw      00001111b         
              movwf      TRISB
          ;------------------
              BANKSEL    TRISC          
              movlw      00011000b         
              movwf      TRISC
          ;------------------
              BANKSEL    TRISD          
              movlw      00000000b         
              movwf      TRISD
              ;------------------
              BANKSEL    SSPCON
          movlw      B'00101000'   ; xxxxxxxxxxx
              MOVWF      SSPCON
              BANKSEL    SSPSTAT
              MOVLW      B'10000000'
              MOVWF      SSPSTAT
              BANKSEL    SSPADD
              MOVLW      (FOSC / (4 * BAUD)) - 1
              MOVWF      SSPADD
;--------------------------------------------------------------------------------------------------
              BANKSEL    PORTB
              Btfsc      PORTB,3
              GOTO       $-1
              CALL       RITARDO_30ms
              BTFSS      PORTB,3
              GOTO       $-1
              CALL       RITARDO_30ms  

              BANKSEL    PORTD
              MOVLW      00H
              MOVWF      PORTD
              CALL       ritardo_secondo
              MOVLW      0XFF
              MOVWF      PORTD
              ;CALL       ritardo_secondo
              ;GOTO       $-6
I2CWrite
              BANKSEL    SSPCON2
              BSF        SSPCON2,SEN
              CALL       WaitMSSP
              ;---------------------
              MOVLW      LC01CTRLIN
              CALL       Send_I2C_Byte
              CALL       WaitMSSP

              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CFail

              MOVLW      LC01ADDR
              CALL       Send_I2C_Byte
              CALL       WaitMSSP

              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CFail

              MOVLW      LC01DATA
              CALL       Send_I2C_Byte
              CALL       WaitMSSP 
      
              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CFail  

              BANKSEL    SSPCON2
              BSF        SSPCON2,PEN
              CALL       WaitMSSP                                          
         
I2CRead
              BANKSEL    SSPCON2
              BSF        SSPCON2,RSEN
              CALL       WaitMSSP   

              MOVLW      LC01CTRLIN
              CALL       Send_I2C_Byte
              CALL       WaitMSSP    
                         
              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CRead

              MOVLW      LC01ADDR
              CALL       Send_I2C_Byte
              CALL       WaitMSSP 

              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CFail

              BSF        SSPCON2,RSEN
              CALL       WaitMSSP

              MOVLW      LC01CTRLOUT
              CALL       Send_I2C_Byte
              CALL       WaitMSSP

              BANKSEL    SSPCON2
              BTFSC      SSPCON2,ACKSTAT
              GOTO       I2CFail

              BSF        SSPCON2,RCEN

              CALL       WaitMSSP

              BANKSEL    SSPCON2
              BSF        SSPCON2,ACKDT
              BSF        SSPCON2,ACKEN

              BSF        SSPCON2,PEN           
              CALL       WaitMSSP

              BANKSEL    SSPBUF
              MOVF       SSPBUF,W
              MOVF       PORTD

LOOP
              GOTO       LOOP

I2CFail
              BANKSEL    SSPCON2
              BSF        SSPCON2,PEN
              CALL       WaitMSSP

              BANKSEL    PORTD
              MOVLW      00H
              MOVWF      PORTD
              CALL       ritardo_secondo   
              MOVLW      0XFF
              MOVWF      PORTD        
              CALL       ritardo_secondo
              GOTO       $-6
Send_I2C_Byte
              BANKSEL    SSPBUF
              MOVWF      SSPBUF
              RETLW      0

WaitMSSP
              BANKSEL    PIR1
              BTFSC      PIR1,SSPIF
              GOTO       $-1
              BCF        PIR1,SSPIF
              RETLW      0

             
;-------------------- SUBROUTINE RITARDO (30 millisecondi)--------------
RITARDO_30ms                               
              movlw     1
              movwf     Timer1
loopp1        movlw     39
              movwf     Timer2
loopp2        movlw     255
              movwf     Timer3
loopp3        
              decfsz    Timer3
              goto      loopp3     
              decfsz    Timer2
              goto      loopp2
              decfsz    Timer1
              goto      loopp1
              return
;-------------------- SUBROUTINE UN SECONDO --------------------------
ritardo_secondo
              movlw     5
              movwf     Timer1
laap1         movlw     199
              movwf     Timer2
laap2         movlw     143
              movwf     Timer3
laap3         nop
              nop
              nop
              nop
              decfsz    Timer3
              goto      laap3     
              decfsz    Timer2
              goto      laap2
              decfsz    Timer1
              goto      laap1
              return    
;---------------------------------------------------------------------             
              end 
angelole





postato il:
19.09.2018, alle ore 17:27
Ho seguito pedissequamente le info di questo documento microchip:
ww1.microchip.com/downloads/en/DeviceDoc/i2c.pdf

anche se riferito ad una eeprom... pur tuttavia i concetti base sono sempre gli stessi.

Riesco a copilare ma, all'atto pratico, dopo lo start non invia alcun dato!
picmicro675




una ogni 10 livelli


postato il:
19.09.2018, alle ore 19:21
Ci sono le possibilità di usare il debug. Quello hardware (se hai il pickit3 o ICD3) oppure tramite simulatore. Quello compreso in MPLAB (alquanto rustico da usare) oppure quello che uso io.
Ora provo a vedere i sorgente e se si trova il problema.

Comunque mi pare di capire che vuoi fare un orologio con interfaccia LCD, che si trovano a bizzeffe in rete, basta prenderne uno e caricare il codice. Per il tuo micro comunque ho visto 2 o 3 progetti, ma difficile che siano scritti in assembly.

Non ho capito quale possa essere il problema delle routines che ho postato nell' articolo sopra. Se lo scopo è di ottenere un tale orologio, potrebbe essere anche una soluzione usare il proton basic, che per quel micro è in versione FREE (niente da pagare). Del quale poi si arriva anche a scrivere per l' orologio che intenti fare. Se però è uno studio di scuola allora devi capire bene come gira il complessivo.

Ti faccio sapere cosa mi risulta.
Comunque ho già tagliato quelle chiamate CALL Send_I2C_Byte CALL WaitMSSP,
perché si toglie il RETL 0 e si va direttamente a WaitMSSP. . Senza fare una andata e ritorno a gratis.



Anno nuovo, forum nuovo.
Mi sa che lascio.
segui questo thread con grixFC, per questa funzione devi aver installato il software grixFC

torna su
     

Come utente anonimo puoi leggere il contenuto di questo forum ma per aprire una discussione
o per partecipare ad una discussione esistente devi essere registrato ed accedere al sito




 







 
 
indietro | homepage | torna su copyright © 2004/2024 GRIX.IT - La community dell'elettronica Amatoriale