;----------------------------------------------------------- ; titre : driver can 4 voies + envoi seriel ; version : 1.1 ; cible : pic16f877-04p ; quartz : 4mhz ; auteur : yann leidwanger ; email : tech@jls-info.com ;----------------------------------------------------------- ;----------------------------------------------------------- ; options de compilation ;----------------------------------------------------------- LIST P=16F877 INCLUDE ERRORLEVEL -302 ;----------------------------------------------------------- ; variables & equivalences ;----------------------------------------------------------- cblock 20h ;ram banque 0 temp ;mcu cpt1 ;compteur 1 cpt2 ;compteur 2 cpt_can ;compteur dclk can data_can_i ;data serielle lue can data_can_o ;data serielle ecrite can datah ;msb can datal ;lsb can endc ledv equ 01h ;led v rb1 ledr equ 02h ;led r rb2 p_can equ porte ;pilotage can din equ 00h ;re0 : data in dcs equ 01h ;re1 : chip select dclk equ 02h ;re2 : data clock dout equ 05h ;ra5 : data out ; controle can : soc, chx, 12 bits, single ended, no power down ch0 equ 097h ch1 equ 0d7h ch2 equ 0a7h ch3 equ 0e7h ;----------------------------------------------------------- ; code segment ;----------------------------------------------------------- ORG 0003h goto start ;----------------------------------------------------------- ; emet w sur uart ;----------------------------------------------------------- uart_emit clrwdt btfss pir1,txif goto uart_emit movwf txreg return ;----------------------------------------------------------- ; emet w sur uart en hexa ;----------------------------------------------------------- uart_emit_hex movwf temp swapf temp,w ;qh call writedig movf temp,w ;qb writedig andlw 0fh ;isole qb addlw 0f6h ;conversion hexa btfsc status,c ; addlw 07h ;lettre addlw 3ah ;chiffre goto uart_emit ;----------------------------------------------------------- ; lecture can, channel dans w ;----------------------------------------------------------- convert bcf p_can,dcs ;cs = 0 movwf data_can_o ;controle can movlw 08h movwf cpt_can ;8 pulses dclk can conv rlf data_can_o,f ;conversion // -> serie btfsc status,c goto write_1 write_0 bcf p_can,din goto pulse1 write_1 bsf p_can,din pulse1 bsf p_can,dclk bcf p_can,dclk ;pulse dclk decfsz cpt_can,f goto conv ;8 bits ecrits ? bcf p_can,din ;din = 0 call lect8bits movwf datah ;sauve data h can call lect8bits movwf datal ;sauve data l can mise_en_forme rlf datal,f rlf datah,f andlw 0f0h ;ajustement data h / l movwf datal bsf p_can,dcs ;cs = 1 return lect8bits movlw 08h movwf cpt_can ;8 pulses dclk lect bsf p_can,dclk ;front montant dclk btfsc porta,dout goto lect_h lect_l bcf status,c goto pulse2 lect_h bsf status,c pulse2 bcf p_can,dclk ;front descendant dclk rlf data_can_i,f ;conversion serie -> // decfsz cpt_can,f goto lect ;8 bits lus ? movf data_can_i,w ;sauve data dans w return ;----------------------------------------------------------- ; programme principal ;----------------------------------------------------------- start clrwdt bsf status,rp0 bcf status,rp1 ;banque 1 clrf adcon0 movlw 06h movwf adcon1 ;can interne off, port a std movlw b'11111111' movwf trisa ;port a en entree movlw b'00000000' movwf trise ;re0, re1, re2 en sortie movlw b'11111001' movwf trisb ;rb1, rb2 en sortie bsf txsta,brgh ;high speed bsf txsta,txen movlw 19h movwf spbrg ;9600 Bauds bcf status,rp0 bcf status,rp1 ;banque 0 bsf rcsta,cren bsf rcsta,spen ;serial port enabled bsf portb,ledv ;led v on bsf p_can,dcs ;cs = 1 bcf p_can,dclk ;dclk = 0 bcf p_can,din ;din = 0 loop clrwdt movlw ch0 call mes_can ;acq + envoi uart ch0 movlw ch1 call mes_can ;acq + envoi uart ch1 movlw ch2 call mes_can ;acq + envoi uart ch2 movlw ch3 call mes_can ;acq + envoi uart ch3 goto loop mes_can call convert ;conversion can movf datah,w call uart_emit_hex ;envoi data h can movf datal,w call uart_emit_hex ;envoi data l can movlw ' ' call uart_emit ;separateur return END ;----------------------------------------------------------- ; that's all folks !!! ;-----------------------------------------------------------